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..d2c26eda --- /dev/null +++ b/omod/src/main/java/org/openmrs/module/emrapi/web/controller/MothersAndChildrenController.java @@ -0,0 +1,52 @@ +package org.openmrs.module.emrapi.web.controller; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import java.util.List; + +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 = "mother") List motherUuids, + @RequestParam(required = false, value = "child") List 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(motherUuids); + criteria.setChildUuids(childUuids); + criteria.setMotherRequiredToHaveActiveVisit(requireMotherHasActiveVisit); + criteria.setChildRequiredToHaveActiveVisit(requireChildHasActiveVisit); + criteria.setChildRequiredToBeBornDuringMothersActiveVisit(requireChildBornDuringMothersActiveVisit); + List motherAndChildList = maternalService.getMothersAndChildren(criteria); + return new NeedsPaging<>(motherAndChildList, context).toSimpleObject(new SimpleBeanConverter()); + } + +}