class DefaultCertificateAuthorityService implements CertificateAuthorityService {

  @Override
  public CertificateCredentialValue findTransitionalVersion(final String caName) {
    final List<CredentialVersion> credentialVersions = certificateVersionDataService.findBothActiveCertAndTransitionalCert(caName);

    if (credentialVersions == null) {
      throw new EntryNotFoundException(ErrorMessages.Credential.INVALID_ACCESS);
    }

    final CertificateCredentialVersion transitionalVersion = (CertificateCredentialVersion) credentialVersions.stream().filter(version -> {
      if (!(version instanceof CertificateCredentialVersion)) {
        throw new ParameterizedValidationException(ErrorMessages.NOT_A_CA_NAME);
      }
      return ((CertificateCredentialVersion) version).isVersionTransitional();
    }).findFirst().orElse(null);

    if (transitionalVersion == null) {
      return null;
    } else if (!transitionalVersion.getParsedCertificate().isCa()) {
      throw new ParameterizedValidationException(ErrorMessages.CERT_NOT_CA);
    }

    return new CertificateCredentialValue(
            transitionalVersion.getCertificate(),
            transitionalVersion.getPrivateKey(),
            transitionalVersion.isCertificateAuthority(),
            transitionalVersion.isSelfSigned(),
            transitionalVersion.getGenerated(),
            transitionalVersion.isVersionTransitional(),
            transitionalVersion.getVersionCreatedAt());
  }

  @Autowired public  DefaultCertificateAuthorityService(final DefaultCertificateVersionDataService certificateVersionDataService);

}