-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EA-198: Add support for configuring Mother Child relationships within… (
#237)
- Loading branch information
1 parent
348d202
commit 131ee91
Showing
13 changed files
with
1,059 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
api/src/main/java/org/openmrs/module/emrapi/maternal/Child.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.openmrs.module.emrapi.maternal; | ||
|
||
import lombok.Data; | ||
import org.openmrs.Patient; | ||
import org.openmrs.module.emrapi.adt.InpatientAdmission; | ||
|
||
@Data | ||
public class Child { | ||
private Patient child; | ||
private Patient mother; | ||
private InpatientAdmission childAdmission; | ||
} |
20 changes: 20 additions & 0 deletions
20
api/src/main/java/org/openmrs/module/emrapi/maternal/ChildrenByMothersSearchCriteria.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.openmrs.module.emrapi.maternal; | ||
|
||
import java.util.List; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.experimental.Accessors; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class ChildrenByMothersSearchCriteria { | ||
private List<String> motherUuids; // restrict to children of these mothers | ||
@Accessors(fluent = true) private boolean requireMotherHasActiveVisit = false; // restrict to children of mothers who have an active visit | ||
@Accessors(fluent = true) private boolean requireChildHasActiveVisit = false; // restrict to children who have an active visit | ||
@Accessors(fluent = true) private boolean requireChildBornDuringMothersActiveVisit = false; // restrict to children who were born during their mother's active visit | ||
} | ||
|
||
|
22 changes: 22 additions & 0 deletions
22
api/src/main/java/org/openmrs/module/emrapi/maternal/MaternalService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.openmrs.module.emrapi.maternal; | ||
|
||
import java.util.List; | ||
|
||
import org.openmrs.api.OpenmrsService; | ||
|
||
public interface MaternalService extends OpenmrsService { | ||
|
||
/** | ||
* Fetches patients who are "children" (personB) of a Mother-Child relationship | ||
* @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) | ||
*/ | ||
List<Child> getChildrenByMothers(ChildrenByMothersSearchCriteria 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); | ||
} |
124 changes: 124 additions & 0 deletions
124
api/src/main/java/org/openmrs/module/emrapi/maternal/MaternalServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package org.openmrs.module.emrapi.maternal; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import org.openmrs.Patient; | ||
import org.openmrs.RelationshipType; | ||
import org.openmrs.api.APIException; | ||
import org.openmrs.api.impl.BaseOpenmrsService; | ||
import org.openmrs.module.emrapi.EmrApiProperties; | ||
import org.openmrs.module.emrapi.adt.AdtService; | ||
import org.openmrs.module.emrapi.adt.InpatientAdmission; | ||
import org.openmrs.module.emrapi.adt.InpatientAdmissionSearchCriteria; | ||
import org.openmrs.module.emrapi.db.EmrApiDAO; | ||
|
||
public class MaternalServiceImpl extends BaseOpenmrsService implements MaternalService { | ||
|
||
private EmrApiProperties emrApiProperties; | ||
|
||
private AdtService adtService; | ||
|
||
private EmrApiDAO emrApiDAO; | ||
|
||
public void setEmrApiProperties(EmrApiProperties emrApiProperties) { | ||
this.emrApiProperties = emrApiProperties; | ||
} | ||
|
||
public void setEmrApiDAO(EmrApiDAO emrApiDAO) { | ||
this.emrApiDAO = emrApiDAO; | ||
} | ||
|
||
public void setAdtService(AdtService adtService) { | ||
this.adtService = adtService; | ||
} | ||
|
||
public List<Child> getChildrenByMothers(ChildrenByMothersSearchCriteria 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", criteria.getMotherUuids()); | ||
parameters.put("childUuids", null); | ||
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<Child> 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); | ||
} | ||
|
||
// 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()))); | ||
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())); | ||
} | ||
|
||
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; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
api/src/main/java/org/openmrs/module/emrapi/maternal/Mother.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.openmrs.module.emrapi.maternal; | ||
|
||
import lombok.Data; | ||
import org.openmrs.Patient; | ||
import org.openmrs.module.emrapi.adt.InpatientAdmission; | ||
|
||
|
||
@Data | ||
public class Mother { | ||
private Patient mother; | ||
private Patient child; | ||
private InpatientAdmission motherAdmission; | ||
} |
19 changes: 19 additions & 0 deletions
19
api/src/main/java/org/openmrs/module/emrapi/maternal/MothersByChildrenSearchCriteria.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
import lombok.experimental.Accessors; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class MothersByChildrenSearchCriteria { | ||
private List<String> childUuids; // restrict to mothers of these children | ||
@Accessors(fluent = true) private Boolean requireMotherHasActiveVisit = false; // restrict to mothers who have an active visit | ||
@Accessors(fluent = true) private Boolean requireChildHasActiveVisit = false; // restrict to mothers of children who have an active visit | ||
@Accessors(fluent = true) private boolean requireChildBornDuringMothersActiveVisit = false; // restrict to mothers who had a child born during their active visit | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
select | ||
mother, | ||
child | ||
from | ||
Relationship as motherChildRelationship | ||
inner join motherChildRelationship.personA as mother | ||
inner join motherChildRelationship.personB as child | ||
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 year(child.birthdate) >= year(motherVisit.startDatetime) | ||
and month(child.birthdate) >= month(motherVisit.startDatetime) | ||
and day(child.birthdate) >= day(motherVisit.startDatetime)) > 0) | ||
and mother.voided = false and child.voided = false and motherChildRelationship.voided = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.