public static ch.elexis.data.Patient getElexisPatient(Patient ehcPatient) {
    // try to look up via ids
    List<Identificator> ids = ehcPatient.getIds();
    for (Identificator identificator : ids) {
      String idRoot = identificator.getRoot();
      if (idRoot.equals(CodeSystems.SwissSSNDeprecated.getCodeSystemId())
          || idRoot.equals(CodeSystems.SwissSSN.getCodeSystemId())) {
        IPersistentObject ret = Xid.findObject(Xid.DOMAIN_AHV, identificator.getExtension());
        if (ret instanceof Kontakt) {
          if (((Kontakt) ret).istPatient()) {
            return ch.elexis.data.Patient.load(ret.getId());
          }
        }
        System.out.println("foud ret " + ret);
        if (ret instanceof ch.elexis.data.Patient) {
          return (ch.elexis.data.Patient) ret;
        }
      }
    }

    Query<ch.elexis.data.Patient> qpa =
        new Query<ch.elexis.data.Patient>(ch.elexis.data.Patient.class);
    // initialize data
    Name ehcName = ehcPatient.getName();
    Date ehcBirthdate = ehcPatient.getBirthday();
    String gender =
        ehcPatient.getAdministrativeGenderCode() == AdministrativeGender.FEMALE
            ? Person.FEMALE
            : Person.MALE;
    TimeTool ttBirthdate = new TimeTool();
    // add data to query
    if (ehcName.getFamilyName() != null && !ehcName.getFamilyName().isEmpty()) {
      qpa.add(ch.elexis.data.Patient.FLD_NAME, Query.EQUALS, ehcName.getFamilyName());
    }
    if (ehcName.getGivenNames() != null && !ehcName.getGivenNames().isEmpty()) {
      qpa.add(ch.elexis.data.Patient.FLD_FIRSTNAME, Query.EQUALS, ehcName.getGivenNames());
    }
    if (ehcBirthdate != null) {
      ttBirthdate.setTime(ehcBirthdate);
      qpa.add(Person.BIRTHDATE, Query.EQUALS, ttBirthdate.toString(TimeTool.DATE_COMPACT));
    }
    List<ch.elexis.data.Patient> existing = qpa.execute();
    // create or overwrite Patient
    ch.elexis.data.Patient ret = null;
    if (existing.isEmpty()) {
      ret =
          new ch.elexis.data.Patient(
              ehcName.getFamilyName(),
              ehcName.getGivenNames(),
              ttBirthdate.toString(TimeTool.DATE_COMPACT),
              gender);
    } else {
      ret = existing.get(0);
    }

    return ret;
  }
 /**
  * Gets the document entry types.
  *
  * @param aMyPatientId the a my patient id
  * @return the document entry types
  */
 private List<DocumentEntryType> getAllPatientDocumentEntryTypes(
     org.ehealth_connector.common.Patient ehcPatient) {
   List<DocumentEntryType> ret = new ArrayList<DocumentEntryType>();
   List<Identificator> ids = ehcPatient.getIds();
   if (ids != null && !ids.isEmpty()) {
     FindDocumentsQuery fdq =
         new FindDocumentsQuery(
             ids.get(0), null, null, null, null, null, null, null, AvailabilityStatus.APPROVED);
     logger.debug("getDocumentEntryTypes");
     final ConvenienceCommunicationCh convComm = new ConvenienceCommunicationCh(affinityDomain);
     logger.debug("queryDocuments");
     final XDSQueryResponseType regDocQuery = convComm.queryDocuments(fdq);
     if (regDocQuery != null) {
       final List<DocumentEntryResponseType> docEntrieResponses =
           regDocQuery.getDocumentEntryResponses();
       logger.info("Document Entries found: " + docEntrieResponses.size());
       for (final DocumentEntryResponseType docEntryResponse : docEntrieResponses) {
         final DocumentEntryType docEntry = docEntryResponse.getDocumentEntry();
         ret.add(docEntry);
       }
     }
     convComm.clearDocuments();
   }
   return ret;
 }
 private Optional<Identificator> getMeineImpfungenPatientId(
     org.ehealth_connector.common.Patient patient) {
   List<Identificator> ids = patient.getIds();
   if (ids != null && !ids.isEmpty()) {
     for (Identificator identificator : ids) {
       if (MeineImpfungenService.PDQ_REQUEST_PATID_OID.equals(identificator.getRoot())) {
         return Optional.of(identificator);
       }
     }
   }
   return Optional.empty();
 }
  public static Patient getEhcPatient(ch.elexis.data.Patient elexisPatient) {
    Patient ret =
        new Patient(
            getEhcPersonName(elexisPatient),
            getEhcGenderCode(elexisPatient),
            getDate(elexisPatient.getGeburtsdatum()));

    // PHONE
    Telecoms telecoms = new Telecoms();
    String value = elexisPatient.get(Kontakt.FLD_PHONE1);
    if (value != null && !value.isEmpty() && !value.equalsIgnoreCase("0")) {
      telecoms.addPhone(value, AddressUse.PRIVATE);
    }
    value = elexisPatient.get(Kontakt.FLD_MOBILEPHONE);
    if (value != null && !value.isEmpty() && !value.equalsIgnoreCase("0")) {
      telecoms.addPhone(value, AddressUse.MOBILE);
    }
    ret.setTelecoms(telecoms);
    // ADDRESS
    Anschrift elexisAddress = elexisPatient.getAnschrift();
    if (elexisAddress != null) {
      ret.addAddress(getEhcAddress(elexisAddress));
    }

    String socialSecurityNumber = elexisPatient.getXid(Xid.DOMAIN_AHV);
    if (socialSecurityNumber != null) {
      socialSecurityNumber = socialSecurityNumber.trim();
      socialSecurityNumber = socialSecurityNumber.replaceAll("\\.", "");
      if (socialSecurityNumber.length() == 11) {
        ret.addId(
            new Identificator(
                CodeSystems.SwissSSNDeprecated.getCodeSystemId(), socialSecurityNumber));
      } else if (socialSecurityNumber.length() == 13) {
        ret.addId(new Identificator(CodeSystems.SwissSSN.getCodeSystemId(), socialSecurityNumber));
      }
    }

    return ret;
  }