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();
    }
  }
 private String getParentId(Person mother, List<Patient> patients) {
   for (Patient patient : patients) {
     if (patient.getMrsPatient().getPerson().getId().equals(mother.getId().toString())) {
       return patient.getMrsPatient().getMotechId();
     }
   }
   return null;
 }
 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());
 }
 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;
 }
 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 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;
  }
 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));
     }
   }
 }
 public PatientForm(Patient patient) {
   this.motechId = patient.getMrsPatient().getMotechId();
   this.firstName = patient.getMrsPatient().getPerson().getFirstName();
   this.middleName = patient.getMrsPatient().getPerson().getMiddleName();
   this.lastName = patient.getMrsPatient().getPerson().getLastName();
   this.dateOfBirth = patient.getMrsPatient().getPerson().getDateOfBirth();
   this.sex = patient.getMrsPatient().getPerson().getGender();
 }