Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EA-198-2: Add support for configuring (and fetching) Mother Child rel… #238

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("requireMotherHasActiveVisit", criteria.isRequireMotherHasActiveVisit());
parameters.put("requireChildHasActiveVisit", criteria.isRequireChildHasActiveVisit());
parameters.put("requireChildBornDuringMothersActiveVisit", criteria.isRequireChildBornDuringMothersActiveVisit());

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();
MotherAndChild child = new MotherAndChild();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should rename this local variable to motherAndChild, though you could also just have a constructor that takes in both and not need a local variable at all...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, yep... missed this one.

child.setMother((Patient) row[0]);
child.setChild((Patient) row[1]);
ret.add(child);
}

// 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 requireMotherHasActiveVisit = false; // restrict to mothers who have an active visit
private boolean requireChildHasActiveVisit = false; // restrict to mothers of children who have an active visit
private boolean requireChildBornDuringMothersActiveVisit = false; // restrict to mothers who had a child born during their active visit
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you did want these to read better, you could go with something like:

motherRequiredToHaveActiveVisit -> isMotherRequiredToHaveActiveVisit()
childRequiredToHaveActiveVisit -> isChildRequiredToHaveActiveVisit()
childRequiredToBeBornDuringMothersActiveVisit -> isChildRequiredToBeBornDuringMothersActiveVisit()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that.


}

This file was deleted.

Loading
Loading