From 2e23aea0a6744485b3d223cca5caaeb58c63df10 Mon Sep 17 00:00:00 2001 From: Mark Goodrich Date: Fri, 9 Aug 2024 17:33:57 -0400 Subject: [PATCH] EA-198: Add support for configuring (and fetching) Mother Child relationships within the EMR API module --- .../module/emrapi/EmrApiConstants.java | 2 +- .../emrapi/maternal/MaternalServiceImpl.java | 2 + api/src/main/resources/hql/mother_child.hql | 4 +- api/src/test/resources/baseTestDataset.xml | 2 +- .../MothersAndChildrenController.java | 54 +++++++++++++++++++ 5 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 omod/src/main/java/org/openmrs/module/emrapi/web/controller/MothersAndChildrenController.java diff --git a/api/src/main/java/org/openmrs/module/emrapi/EmrApiConstants.java b/api/src/main/java/org/openmrs/module/emrapi/EmrApiConstants.java index 3f6807d9..31635469 100644 --- a/api/src/main/java/org/openmrs/module/emrapi/EmrApiConstants.java +++ b/api/src/main/java/org/openmrs/module/emrapi/EmrApiConstants.java @@ -216,5 +216,5 @@ public class EmrApiConstants { public static final String GP_USE_LEGACY_DIAGNOSIS_SERVICE = "emrapi.useLegacyDiagnosisService"; - public static final String METADATA_MAPPING_MOTHER_CHILD_RELATIONSHIP_TYPE = "emrapi.motherChildRelationshipType"; + public static final String METADATA_MAPPING_MOTHER_CHILD_RELATIONSHIP_TYPE = "emr.motherChildRelationshipType"; } diff --git a/api/src/main/java/org/openmrs/module/emrapi/maternal/MaternalServiceImpl.java b/api/src/main/java/org/openmrs/module/emrapi/maternal/MaternalServiceImpl.java index 9d01c649..26721bbb 100644 --- a/api/src/main/java/org/openmrs/module/emrapi/maternal/MaternalServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/emrapi/maternal/MaternalServiceImpl.java @@ -47,7 +47,9 @@ public List getMothersAndChildren(MothersAndChildrenSearchCriter Map parameters = new HashMap<>(); parameters.put("motherUuids", criteria.getMotherUuids()); + parameters.put("restrictByMothers", criteria.getMotherUuids() != null); parameters.put("childUuids", criteria.getChildUuids()); + parameters.put("restrictByChildren", criteria.getChildUuids() != null); parameters.put("motherChildRelationshipType", motherChildRelationshipType); parameters.put("motherRequiredToHaveActiveVisit", criteria.isMotherRequiredToHaveActiveVisit()); parameters.put("childRequiredToHaveActiveVisit", criteria.isChildRequiredToHaveActiveVisit()); diff --git a/api/src/main/resources/hql/mother_child.hql b/api/src/main/resources/hql/mother_child.hql index 2d53226d..2223a536 100644 --- a/api/src/main/resources/hql/mother_child.hql +++ b/api/src/main/resources/hql/mother_child.hql @@ -6,8 +6,8 @@ from 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)) + (:restrictByMothers = false or mother.uuid in (:motherUuids)) + and (:restrictByChildren = false or child.uuid in (:childUuids)) and motherChildRelationship.relationshipType = :motherChildRelationshipType 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) diff --git a/api/src/test/resources/baseTestDataset.xml b/api/src/test/resources/baseTestDataset.xml index a4ea690b..61eca09d 100644 --- a/api/src/test/resources/baseTestDataset.xml +++ b/api/src/test/resources/baseTestDataset.xml @@ -141,7 +141,7 @@ diff --git a/omod/src/main/java/org/openmrs/module/emrapi/web/controller/MothersAndChildrenController.java b/omod/src/main/java/org/openmrs/module/emrapi/web/controller/MothersAndChildrenController.java new file mode 100644 index 00000000..420bca78 --- /dev/null +++ b/omod/src/main/java/org/openmrs/module/emrapi/web/controller/MothersAndChildrenController.java @@ -0,0 +1,54 @@ +package org.openmrs.module.emrapi.web.controller; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import java.util.Arrays; +import java.util.List; + +import org.apache.commons.lang.StringUtils; +import org.openmrs.module.emrapi.maternal.MaternalService; +import org.openmrs.module.emrapi.maternal.MotherAndChild; +import org.openmrs.module.emrapi.maternal.MothersAndChildrenSearchCriteria; +import org.openmrs.module.emrapi.rest.converter.SimpleBeanConverter; +import org.openmrs.module.webservices.rest.SimpleObject; +import org.openmrs.module.webservices.rest.web.RequestContext; +import org.openmrs.module.webservices.rest.web.RestUtil; +import org.openmrs.module.webservices.rest.web.representation.Representation; +import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class MothersAndChildrenController { + + @Autowired + private MaternalService maternalService; + + @RequestMapping(method = RequestMethod.GET, value = "/rest/**/emrapi/maternal/mothersAndChildren") + @ResponseBody + public SimpleObject getMothersAndChildren( + HttpServletRequest request, + HttpServletResponse response, + @RequestParam(required = false, value = "motherUuids") String motherUuids, + @RequestParam(required = false, value = "childUuids") String childUuids, + @RequestParam(required = false, value = "requireMotherHasActiveVisit") boolean requireMotherHasActiveVisit, + @RequestParam(required = false, value = "requireChildHasActiveVisit") boolean requireChildHasActiveVisit, + @RequestParam(required = false, value = "requireChildBornDuringMothersActiveVisit") boolean requireChildBornDuringMothersActiveVisit + ) { + RequestContext context = RestUtil.getRequestContext(request, response, Representation.DEFAULT); + MothersAndChildrenSearchCriteria criteria = new MothersAndChildrenSearchCriteria(); + criteria.setMotherUuids(StringUtils.isNotBlank(motherUuids) ? Arrays.asList(motherUuids.split(",")) : null); + criteria.setChildUuids(StringUtils.isNotBlank(childUuids) ? Arrays.asList(childUuids.split(",")) : null); + criteria.setMotherRequiredToHaveActiveVisit(requireMotherHasActiveVisit); + criteria.setChildRequiredToHaveActiveVisit(requireChildHasActiveVisit); + criteria.setChildRequiredToBeBornDuringMothersActiveVisit(requireChildBornDuringMothersActiveVisit); + List motherAndChildList = maternalService.getMothersAndChildren(criteria); + return new NeedsPaging<>(motherAndChildList, context).toSimpleObject(new SimpleBeanConverter()); + } + +}