/**
  * Gets the document as input stream.
  *
  * @param docEntry the doc entry
  * @return the document as input stream
  */
 private InputStream getDocumentAsInputStream(DocumentEntryType docEntry) {
   DocumentRequest documentRequest =
       new DocumentRequest(
           docEntry.getRepositoryUniqueId(),
           affinityDomain.getRepositoryDestination().getUri(),
           docEntry.getEntryUUID());
   ConvenienceCommunicationCh convComm = new ConvenienceCommunicationCh(affinityDomain);
   XDSRetrieveResponseType rrt = convComm.retrieveDocument(documentRequest);
   try {
     if ((rrt.getErrorList() != null)) {
       logger.error(
           "error retriveing doc "
               + docEntry.getEntryUUID()
               + " - "
               + rrt.getErrorList().getHighestSeverity().getName());
     }
     if ((rrt.getAttachments() == null) || rrt.getAttachments().size() != 1) {
       logger.error("document not downloaded or more than one");
       return null;
     }
     return rrt.getAttachments().get(0).getStream();
   } finally {
     convComm.clearDocuments();
   }
 }
  private void updateAffinityDomain(
      String truststorePath, String truststorePass, String keystorePath, String keystorePass)
      throws URISyntaxException, SecurityDomainException {
    // set secure destinations
    Destination pdqDestination =
        new Destination(ORGANIZATIONAL_ID, new URI(PDQ_REQUEST_URL), keystorePath, keystorePass);
    pdqDestination.setSenderApplicationOid(ORGANIZATIONAL_ID);
    pdqDestination.setReceiverApplicationOid(PDQ_REQUEST_PATID_OID);
    pdqDestination.setReceiverFacilityOid(PDQ_REQUEST_PATID_OID);

    Destination xdsRegistryDestination =
        new Destination(ORGANIZATIONAL_ID, new URI(XDS_REGISTRY_URL), keystorePath, keystorePass);
    Destination xdsRepositoryDestination =
        new Destination(ORGANIZATIONAL_ID, new URI(XDS_REPOSITORY_URL), keystorePath, keystorePass);
    xdsRegistryDestination.setReceiverApplicationOid(XDS_REPOSITORY_OID);
    xdsRegistryDestination.setReceiverFacilityOid(XDS_REPOSITORY_OID);

    affinityDomain = new AffinityDomain();
    affinityDomain.setPdqDestination(pdqDestination);
    affinityDomain.setRegistryDestination(xdsRegistryDestination);
    affinityDomain.addRepository(xdsRepositoryDestination);
    affinityDomain.setPixDestination(pdqDestination);

    final AtnaSecurityConfiguration atnaSecConfig = new AtnaSecurityConfiguration();
    atnaSecConfig.setKeyStoreFile(new File(keystorePath));
    atnaSecConfig.setKeyStorePassword(keystorePass);
    atnaSecConfig.setKeyStoreType("PKCS12");
    atnaSecConfig.setTrustStoreFile(new File(truststorePath));
    atnaSecConfig.setTrustStorePassword(truststorePass);
    atnaSecConfig.setTrustStoreType("JKS");
    atnaSecConfig.setUri(new URI(ATNA_URL));

    SecurityDomainManager.generateSecurityDomain("suisse-open-exchange.healthcare", atnaSecConfig);
    SecurityDomainManager.addUriToSecurityDomain(
        "suisse-open-exchange.healthcare", atnaSecConfig.getUri());
  }
  @Override
  public List<org.ehealth_connector.common.Patient> getPatients(Patient elexisPatient) {
    MasterPatientIndexQuery mpiQuery =
        new MasterPatientIndexQuery(affinityDomain.getPdqDestination());

    Name name = new Name(elexisPatient.getVorname(), elexisPatient.getName());
    mpiQuery.addPatientName(true, name);

    String birthDate = elexisPatient.getGeburtsdatum();
    if (birthDate != null && !birthDate.isEmpty()) {
      mpiQuery.setPatientDateOfBirth(DateUtil.date(birthDate));
    }

    MasterPatientIndexQueryResponse ret =
        ConvenienceMasterPatientIndexV3.queryPatientDemographics(mpiQuery, affinityDomain);

    return ret.getPatients();
  }
 @Override
 public boolean isVaild() {
   return affinityDomain != null && affinityDomain.getPdqDestination() != null;
 }