public Patient getPatientByMotechId(String patientId) {
   Patient patient = allPatients.patientByMotechId(patientId);
   if (patient == null) {
     return null;
   }
   Relationship motherRelationship =
       allPatients.getMotherRelationship(patient.getMrsPatient().getPerson());
   if (motherRelationship != null) {
     setParentId(patient, motherRelationship);
   }
   return patient;
 }
  public String registerPatient(Patient patient)
      throws ParentNotFoundException, PatientIdNotUniqueException,
          PatientIdIncorrectFormatException {
    try {

      if (StringUtils.isEmpty(patient.getMrsPatient().getMotechId())) {
        MRSPatient mrsPatient = patient.getMrsPatient();
        String motechId = identifierGenerationService.newPatientId();
        patient =
            new Patient(
                new MRSPatient(
                    mrsPatient.getId(), motechId, mrsPatient.getPerson(), mrsPatient.getFacility()),
                patient.getParentId());
      }
      String savedPatientId = allPatients.save(patient);
      if (StringUtils.isNotEmpty(patient.getParentId())) {
        createRelationship(patient.getParentId(), savedPatientId);
      }
      return savedPatientId;
    } catch (IdentifierNotUniqueException e) {
      throw new PatientIdNotUniqueException();
    } catch (InvalidCheckDigitException e) {
      throw new PatientIdIncorrectFormatException();
    }
  }
  public String updatePatient(Patient patient, String parentId) throws ParentNotFoundException {
    String savedPatientId = allPatients.update(patient);
    Patient savedPatient = getPatientByMotechId(savedPatientId);
    Relationship relationship =
        allPatients.getMotherRelationship(savedPatient.getMrsPatient().getPerson());

    if (relationship != null && StringUtils.isEmpty(parentId)) {
      allPatients.voidMotherChildRelationship(savedPatient.getMrsPatient().getPerson());
    }
    if (relationship == null && StringUtils.isNotEmpty(parentId)) {
      createRelationship(parentId, savedPatientId);
    }
    if (relationship != null && StringUtils.isNotEmpty(parentId)) {
      updateRelationship(parentId, savedPatient, relationship);
    }
    return savedPatientId;
  }
 void createRelationship(String parentId, String savedPatientId) throws ParentNotFoundException {
   Patient mother = getPatientByMotechId(parentId);
   if (mother == null) {
     throw new ParentNotFoundException();
   }
   Patient child = getPatientByMotechId(savedPatientId);
   allPatients.createMotherChildRelationship(
       mother.getMrsPatient().getPerson(), child.getMrsPatient().getPerson());
 }
 private void setParentId(Patient patient, Relationship motherRelationship) {
   Person mother = motherRelationship.getPersonA();
   if (mother != null && !mother.getNames().isEmpty()) {
     List<Patient> patients =
         allPatients.search(mother.getNames().iterator().next().getFullName(), null);
     if (patients != null && !patients.isEmpty()) {
       patient.parentId(getParentId(mother, patients));
     }
   }
 }
 private void updateRelationship(String parentId, Patient savedPatient, Relationship relationship)
     throws ParentNotFoundException {
   Patient updatedMother = getPatientByMotechId(parentId);
   if (updatedMother == null) {
     throw new ParentNotFoundException();
   }
   Person personA = relationship.getPersonA();
   if (personA != null
       && !personA.getId().toString().equals(updatedMother.getMrsPatient().getPerson().getId())) {
     allPatients.updateMotherChildRelationship(
         updatedMother.getMrsPatient().getPerson(), savedPatient.getMrsPatient().getPerson());
   }
 }
 public Integer getAgeOfPatientByMotechId(String motechId) {
   return allPatients.getAgeOfPersonByMotechId(motechId);
 }
 public List<Patient> search(String name, String motechId) {
   return allPatients.search(emptyToNull(name), emptyToNull(motechId));
 }