class OrphanRemovalService {

    public void deleteOrphans(ClientId clientId) throws OrphansNotFoundException,
            ActionNotPossibleException, GlobalConfOutdatedException {

        auditDataHelper.put(clientId);

        if (isAlive(clientId) || hasAliveSiblings(clientId)) {
            throw new OrphansNotFoundException();
        }

        Orphans orphans = findOrphans(clientId);
        if (orphans.isEmpty()) {
            throw new OrphansNotFoundException();
        }
        try {
            // delete the orphans
            for (KeyInfo keyInfo : orphans.getKeys()) {
                keyService.deleteKeyAndIgnoreWarnings(keyInfo.getId());
            }
            tokenCertificateService.deleteCertificates(orphans.getCerts());
            for (CertRequestInfo certRequestInfo : orphans.getCsrs()) {
                tokenCertificateService.deleteCsr(certRequestInfo.getId());
            }
        } catch (KeyNotFoundException | CsrNotFoundException | CertificateNotFoundException e) {
            // we just internally looked up these items, so them not being found is an internal error
            throw new RuntimeException(e);
        }
    }

    @Autowired public  OrphanRemovalService(TokenService tokenService,
            TokenCertificateService tokenCertificateService,
            ClientService clientService,
            KeyService keyService,
            AuditDataHelper auditDataHelper);

}