Skip to content

Commit

Permalink
Merge pull request #1679 from njorocs/KHP3-4147
Browse files Browse the repository at this point in the history
Validated linking of child to only one mother in relationships
  • Loading branch information
patryllus authored Oct 19, 2023
2 parents 40f56cc + 94e8447 commit a10f2b1
Showing 1 changed file with 91 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.openmrs.ui.framework.page.PageModel;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestParam;
import sun.awt.X11.XSystemTrayPeer;

import java.util.ArrayList;
import java.util.Date;
Expand All @@ -50,7 +51,7 @@ public void controller(@FragmentParam(value = "relationship", required = false)

public Object saveRelationship(@MethodParam("getEditRelationshipForm") @BindParams EditRelationshipForm form,
UiUtils ui) {

form.createRelationship();
ui.validate(form, form, null);
form.save();

Expand All @@ -76,6 +77,16 @@ public class EditRelationshipForm extends ValidatingCommandObject {
private Date startDate;

private Date endDate;
Date today = new Date();

boolean motherExists;
boolean fatherExists;

Relationship rel;

public void setMotherExists(boolean motherExists) {
this.motherExists = motherExists;
}

public EditRelationshipForm(Relationship existing, Patient patient) {
this.existing = existing;
Expand All @@ -96,11 +107,14 @@ public EditRelationshipForm(Relationship existing, Patient patient) {
this.isToPatient = existing.getRelationshipType().getId() + ":" + personSide;
}
}
this.motherExists = isMotherExists();System.out.println("+++++++this.motherExists+++"+this.motherExists);
this.fatherExists = isFatherExists();System.out.println("+++++++this.isFatherExists+++"+this.fatherExists);
}

/**
* @see ValidatingCommandObject#validate(Object, org.springframework.validation.Errors)
*/

@Override
public void validate(Object o, Errors errors) {
require(errors, "person");
Expand All @@ -109,7 +123,6 @@ public void validate(Object o, Errors errors) {
if (patient.equals(person)) {
errors.rejectValue("person", "Can't be the patient");
}
Date today = new Date();
if(startDate != null && startDate.after(today)) {
errors.rejectValue("startDate", "Relationship start date can't be in the future");
}
Expand All @@ -127,13 +140,55 @@ public void validate(Object o, Errors errors) {
if(endDate != null && endDate.after(today)) {
errors.rejectValue("endDate", "Relationship end date can't be in the future");
}

if (motherExists) {
errors.rejectValue(null, "Child already linked to a mother");
}
if (fatherExists) {
errors.rejectValue(null, "Child already linked to a father");
}
}
public boolean motherExistsForChild(Person person){
boolean exists = false;
List<Relationship> relationships;
relationships = Context.getPersonService().getRelationshipsByPerson(person);
boolean isChildToExistingMother;
if (!relationships.isEmpty()) {
for (Relationship r : relationships) {
isChildToExistingMother = r.getRelationshipType().getaIsToB().equalsIgnoreCase("Parent") && r.getPersonA().getGender().equalsIgnoreCase("F") && !r.getVoided();

if (isChildToExistingMother && (r.getEndDate() == null || r.getEndDate().after(today))) {
exists = true;
break;
}
}
}
return exists;
}
public boolean fatherExistsForChild(Person person){
boolean exists = false;
List<Relationship> relationships;
relationships = Context.getPersonService().getRelationshipsByPerson(person);
boolean isChildToExistingFather;
if (!relationships.isEmpty()) {
for (Relationship r : relationships) {
isChildToExistingFather = r.getRelationshipType().getaIsToB().equalsIgnoreCase("Parent") && r.getPersonA().getGender().equalsIgnoreCase("M") && !r.getVoided();

if (isChildToExistingFather && (r.getEndDate() == null || r.getEndDate().after(today))) {
exists = true;
break;
}
}
}
return exists;
}

/**
* Saves the form
* Creates a relationship
* @return
*/
public void save() {
Relationship rel = (existing != null) ? existing : new Relationship();
public Relationship createRelationship() {
rel = (existing != null) ? existing : new Relationship();
RelationshipType type;
Person personA, personB;

Expand All @@ -149,19 +204,30 @@ public void save() {
personA = patient;
personB = person;
}
}
else { // Doesn't matter who is A or B, e.g. Sibling
} else { // Doesn't matter who is A or B, e.g. Sibling
type = Context.getPersonService().getRelationshipType(Integer.valueOf(isToPatient));
personA = patient;
personB = person;
}

rel.setRelationshipType(type);
rel.setPersonA(personA);
rel.setPersonB(personB);
rel.setStartDate(startDate);
rel.setEndDate(endDate);

if (motherExistsForChild(rel.getPersonB()) && (rel.getRelationshipType().getaIsToB().equalsIgnoreCase("Parent") && rel.getPersonA().getGender().equalsIgnoreCase("F"))) {
this.setMotherExists(true);
}

if (fatherExistsForChild(rel.getPersonB()) && rel.getRelationshipType().getaIsToB().equalsIgnoreCase("Parent") && rel.getPersonA().getGender().equalsIgnoreCase("M")) {
this.setFatherExists(true);
}
return rel;
}
/**
* Saves the form
*/
public void save() {
Context.getPersonService().saveRelationship(rel);
}

Expand Down Expand Up @@ -204,6 +270,23 @@ public Date getEndDate() {
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
public Relationship getRel() {
return rel;
}

public void setRel(Relationship rel) {
this.rel = rel;
}
public boolean isMotherExists() {
return motherExists;
}
public boolean isFatherExists() {
return fatherExists;
}

public void setFatherExists(boolean fatherExists) {
this.fatherExists = fatherExists;
}
}

/**
Expand Down

0 comments on commit a10f2b1

Please sign in to comment.