Skip to content

Commit

Permalink
RESTWS-931: Visit Search: add support to include parent locations (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
mogoodrich authored Mar 12, 2024
1 parent 2a77608 commit db0e97f
Showing 1 changed file with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -324,28 +326,46 @@ 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<Patient> patients = patientParameter == null ? null : Arrays.asList(getPatient(patientParameter));
Collection<Location> locations = locationParameter == null ? null : Arrays.asList(getLocation(locationParameter));
Collection<Location> locations = locationParameter == null ? null :
Boolean.parseBoolean(includeParentLocations) ?
getLocationAndParents(getLocation(locationParameter), null) :
Arrays.asList(getLocation(locationParameter));
Collection<VisitType> visitTypes = visitTypeParameter == null ? null : Arrays
.asList(getVisitType(visitTypeParameter));
boolean includeInactive = includeInactiveParameter == null ? true : Boolean.parseBoolean(includeInactiveParameter);
return new NeedsPaging<Visit>(Context.getVisitService().getVisits(visitTypes, patients, locations, null,
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<Location> getLocationAndParents(Location location, List<Location> locations) {
if (locations == null) {
locations = new ArrayList<Location>();
}
locations.add(location);
if (location.getParentLocation() != null) {
locations = getLocationAndParents(location.getParentLocation(), locations);
}
return locations;
}

/**
* Get all the visits
*
Expand Down

0 comments on commit db0e97f

Please sign in to comment.