Skip to content

Commit

Permalink
EA-198-2: Add support for configuring (and fetching) Mother Child rel… (
Browse files Browse the repository at this point in the history
  • Loading branch information
mogoodrich authored Aug 9, 2024
1 parent 131ee91 commit 81bf287
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 212 deletions.
12 changes: 0 additions & 12 deletions api/src/main/java/org/openmrs/module/emrapi/maternal/Child.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@
public interface MaternalService extends OpenmrsService {

/**
* Fetches patients who are "children" (personB) of a Mother-Child relationship
* Fetches patients in a "Mother-to-Child" relationship, based on the given search criteria.
*
* @param criteria search criteria (see class for details)
* @return a list of children, with their linked mothers (note this returns a "Child" per mother-child pair, so a child with multiple mothers will appear multiple times)
* @return a list of mothers and children that match the search criteria
*/
List<Child> getChildrenByMothers(ChildrenByMothersSearchCriteria criteria);
List<MotherAndChild> getMothersAndChildren(MothersAndChildrenSearchCriteria criteria);

/**
* Fetches patients who are "mothers" (personA) of a Mother-Child relationship
* @param criteria search criteria (see class for details)
* @return a list of mothers, with their linked children (note this returns a "Mother" per mother-child pair, so a mother with multiple children will appear multiple times)
*/
List<Mother> getMothersByChildren(MothersByChildrenSearchCriteria criteria);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.openmrs.Patient;
Expand Down Expand Up @@ -36,7 +37,7 @@ public void setAdtService(AdtService adtService) {
this.adtService = adtService;
}

public List<Child> getChildrenByMothers(ChildrenByMothersSearchCriteria criteria) {
public List<MotherAndChild> getMothersAndChildren(MothersAndChildrenSearchCriteria criteria) {

RelationshipType motherChildRelationshipType = emrApiProperties.getMotherChildRelationshipType();

Expand All @@ -46,79 +47,40 @@ public List<Child> getChildrenByMothers(ChildrenByMothersSearchCriteria criteria

Map<String, Object> parameters = new HashMap<>();
parameters.put("motherUuids", criteria.getMotherUuids());
parameters.put("childUuids", null);
parameters.put("childUuids", criteria.getChildUuids());
parameters.put("motherChildRelationshipType", motherChildRelationshipType);
parameters.put("requireMotherHasActiveVisit", criteria.requireMotherHasActiveVisit());
parameters.put("requireChildHasActiveVisit", criteria.requireChildHasActiveVisit());
parameters.put("requireChildBornDuringMothersActiveVisit", criteria.requireChildBornDuringMothersActiveVisit());
parameters.put("motherRequiredToHaveActiveVisit", criteria.isMotherRequiredToHaveActiveVisit());
parameters.put("childRequiredToHaveActiveVisit", criteria.isChildRequiredToHaveActiveVisit());
parameters.put("childRequiredToBeBornDuringMothersActiveVisit", criteria.isChildRequiredToBeBornDuringMothersActiveVisit());

List<?> l = emrApiDAO.executeHqlFromResource("hql/mother_child.hql", parameters, List.class);

List<Child> ret = new ArrayList<>();
List<MotherAndChild> ret = new ArrayList<>();

for (Object req : l) {
Object[] row = (Object[]) req;
Child child = new Child();
child.setMother((Patient) row[0]);
child.setChild((Patient) row[1]);
ret.add(child);
MotherAndChild mothreAndChild = new MotherAndChild();
mothreAndChild.setMother((Patient) row[0]);
mothreAndChild.setChild((Patient) row[1]);
ret.add(mothreAndChild);
}

// now fetch all the admissions for children in the result set
InpatientAdmissionSearchCriteria inpatientAdmissionSearchCriteria = new InpatientAdmissionSearchCriteria();
inpatientAdmissionSearchCriteria.setPatientIds(new ArrayList<>(ret.stream().map(Child::getChild).map(Patient::getId).collect(Collectors.toSet())));
Set<Integer> patientIds = ret.stream().map(MotherAndChild::getChild).map(Patient::getId).collect(Collectors.toSet());
patientIds.addAll(ret.stream().map(MotherAndChild::getMother).map(Patient::getId).collect(Collectors.toSet()));
inpatientAdmissionSearchCriteria.setPatientIds(new ArrayList<>(patientIds));
List<InpatientAdmission> admissions = adtService.getInpatientAdmissions(inpatientAdmissionSearchCriteria);
Map<Patient, InpatientAdmission> admissionsByPatient = new HashMap<>();
for (InpatientAdmission admission : admissions) {
admissionsByPatient.put(admission.getVisit().getPatient(), admission);
}
for (Child child : ret) {
child.setChildAdmission(admissionsByPatient.get(child.getChild()));
for (MotherAndChild motherAndChild : ret) {
motherAndChild.setMotherAdmission(admissionsByPatient.get(motherAndChild.getMother()));
motherAndChild.setChildAdmission(admissionsByPatient.get(motherAndChild.getChild()));
}

return ret;
}

public List<Mother> getMothersByChildren(MothersByChildrenSearchCriteria criteria) {
RelationshipType motherChildRelationshipType = emrApiProperties.getMotherChildRelationshipType();

if (motherChildRelationshipType == null) {
throw new APIException("Mother-Child relationship type has not been configured");
}

Map<String, Object> parameters = new HashMap<>();
parameters.put("motherUuids", null);
parameters.put("childUuids", criteria.getChildUuids());
parameters.put("motherChildRelationshipType", motherChildRelationshipType);
parameters.put("requireMotherHasActiveVisit", criteria.requireMotherHasActiveVisit());
parameters.put("requireChildHasActiveVisit", criteria.requireChildHasActiveVisit());
parameters.put("requireChildBornDuringMothersActiveVisit", criteria.requireChildBornDuringMothersActiveVisit());

List<?> l = emrApiDAO.executeHqlFromResource("hql/mother_child.hql", parameters, List.class);

List<Mother> ret = new ArrayList<>();

for (Object req : l) {
Object[] row = (Object[]) req;
Mother mother = new Mother();
mother.setMother((Patient) row[0]);
mother.setChild((Patient) row[1]);
ret.add(mother);
}

// now fetch all the admissions for mothers in the result set
InpatientAdmissionSearchCriteria inpatientAdmissionSearchCriteria = new InpatientAdmissionSearchCriteria();
inpatientAdmissionSearchCriteria.setPatientIds(new ArrayList<>(ret.stream().map(Mother::getMother).map(Patient::getId).collect(Collectors.toSet())));
List<InpatientAdmission> admissions = adtService.getInpatientAdmissions(inpatientAdmissionSearchCriteria);
Map<Patient, InpatientAdmission> admissionsByPatient = new HashMap<>();
for (InpatientAdmission admission : admissions) {
admissionsByPatient.put(admission.getVisit().getPatient(), admission);
}
for (Mother mother : ret) {
mother.setMotherAdmission(admissionsByPatient.get(mother.getMother()));
}


return ret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@


@Data
public class Mother {
public class MotherAndChild {
private Patient mother;
private Patient child;
private InpatientAdmission motherAdmission;
private InpatientAdmission childAdmission;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.openmrs.module.emrapi.maternal;

import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class MothersAndChildrenSearchCriteria {
private List<String> motherUuids; // restrict to children of these mothers
private List<String> childUuids; // restrict to mothers of these children
private boolean motherRequiredToHaveActiveVisit = false; // restrict to mothers who have an active visit
private boolean childRequiredToHaveActiveVisit = false; // restrict to mothers of children who have an active visit
private boolean childRequiredToBeBornDuringMothersActiveVisit = false; // restrict to mothers who had a child born during their active visit

}

This file was deleted.

6 changes: 3 additions & 3 deletions api/src/main/resources/hql/mother_child.hql
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ where
((:motherUuids) is null or mother.uuid in (:motherUuids))
and ((:childUuids) is null or child.uuid in (:childUuids))
and motherChildRelationship.relationshipType = :motherChildRelationshipType
and (:requireMotherHasActiveVisit = false or (select count(motherVisit) from Visit as motherVisit where motherVisit.patient = mother and motherVisit.stopDatetime is null and motherVisit.voided = false) > 0)
and (:requireChildHasActiveVisit = false or (select count(childVisit) from Visit as childVisit where childVisit.patient = child and childVisit.stopDatetime is null and childVisit.voided = false) > 0)
and (:requireChildBornDuringMothersActiveVisit = false or (select count(motherVisit) from Visit as motherVisit where motherVisit.patient = mother and motherVisit.stopDatetime is null and motherVisit.voided = false
and (:motherRequiredToHaveActiveVisit = false or (select count(motherVisit) from Visit as motherVisit where motherVisit.patient = mother and motherVisit.stopDatetime is null and motherVisit.voided = false) > 0)
and (:childRequiredToHaveActiveVisit = false or (select count(childVisit) from Visit as childVisit where childVisit.patient = child and childVisit.stopDatetime is null and childVisit.voided = false) > 0)
and (:childRequiredToBeBornDuringMothersActiveVisit = false or (select count(motherVisit) from Visit as motherVisit where motherVisit.patient = mother and motherVisit.stopDatetime is null and motherVisit.voided = false
and year(child.birthdate) >= year(motherVisit.startDatetime)
and month(child.birthdate) >= month(motherVisit.startDatetime)
and day(child.birthdate) >= day(motherVisit.startDatetime)) > 0)
Expand Down
Loading

0 comments on commit 81bf287

Please sign in to comment.