From db0e97f268353bde396567f0768061042ec86fec Mon Sep 17 00:00:00 2001 From: Mark Goodrich Date: Tue, 12 Mar 2024 16:45:54 -0400 Subject: [PATCH] RESTWS-931: Visit Search: add support to include parent locations (#599) --- .../resource/openmrs1_9/VisitResource1_9.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/omod-1.9/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_9/VisitResource1_9.java b/omod-1.9/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_9/VisitResource1_9.java index 21cc3278b..de53a35c2 100644 --- a/omod-1.9/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_9/VisitResource1_9.java +++ b/omod-1.9/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_9/VisitResource1_9.java @@ -9,9 +9,11 @@ */ package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_9; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Date; +import java.util.List; import java.util.Set; import java.util.Iterator; import org.openmrs.Location; @@ -324,20 +326,24 @@ public SimpleObject search(RequestContext context) throws ResponseException { String includeInactiveParameter = context.getRequest().getParameter("includeInactive"); String fromStartDate = context.getRequest().getParameter("fromStartDate"); String visitTypeParameter = context.getRequest().getParameter("visitType"); + String includeParentLocations = context.getRequest().getParameter("includeParentLocations"); if (patientParameter != null || includeInactiveParameter != null || locationParameter != null || visitTypeParameter != null) { Date minStartDate = fromStartDate != null ? (Date) ConversionUtil.convert(fromStartDate, Date.class) : null; return getVisits(context, patientParameter, includeInactiveParameter, minStartDate, locationParameter, - visitTypeParameter); + visitTypeParameter, includeParentLocations); } else { return super.search(context); } } private SimpleObject getVisits(RequestContext context, String patientParameter, String includeInactiveParameter, - Date minStartDate, String locationParameter, String visitTypeParameter) { + Date minStartDate, String locationParameter, String visitTypeParameter, String includeParentLocations) { Collection patients = patientParameter == null ? null : Arrays.asList(getPatient(patientParameter)); - Collection locations = locationParameter == null ? null : Arrays.asList(getLocation(locationParameter)); + Collection locations = locationParameter == null ? null : + Boolean.parseBoolean(includeParentLocations) ? + getLocationAndParents(getLocation(locationParameter), null) : + Arrays.asList(getLocation(locationParameter)); Collection visitTypes = visitTypeParameter == null ? null : Arrays .asList(getVisitType(visitTypeParameter)); boolean includeInactive = includeInactiveParameter == null ? true : Boolean.parseBoolean(includeInactiveParameter); @@ -345,7 +351,21 @@ private SimpleObject getVisits(RequestContext context, String patientParameter, minStartDate, null, null, null, null, includeInactive, context.getIncludeAll()), context).toSimpleObject(this); } - + + /** + * Recursively builds a list that includes the passed-in location and all it's ancestors + */ + private List getLocationAndParents(Location location, List locations) { + if (locations == null) { + locations = new ArrayList(); + } + locations.add(location); + if (location.getParentLocation() != null) { + locations = getLocationAndParents(location.getParentLocation(), locations); + } + return locations; + } + /** * Get all the visits *