Skip to content

Commit 5dc594d

Browse files
authored
RESTWS-960: Consolidate Location Search Handling (#634)
1 parent 0933bb6 commit 5dc594d

File tree

5 files changed

+49
-196
lines changed

5 files changed

+49
-196
lines changed

omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/LocationResource1_8.java

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8;
1111

12+
import java.util.ArrayList;
1213
import java.util.Arrays;
1314
import java.util.List;
1415

@@ -18,9 +19,9 @@
1819
import io.swagger.models.properties.RefProperty;
1920
import io.swagger.models.properties.StringProperty;
2021
import org.apache.commons.collections.CollectionUtils;
22+
import org.apache.commons.lang3.StringUtils;
2123
import org.openmrs.Location;
2224
import org.openmrs.LocationTag;
23-
import org.openmrs.api.LocationService;
2425
import org.openmrs.api.context.Context;
2526
import org.openmrs.module.webservices.rest.web.RequestContext;
2627
import org.openmrs.module.webservices.rest.web.RestConstants;
@@ -35,6 +36,8 @@
3536
import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging;
3637
import org.openmrs.module.webservices.rest.web.response.ResponseException;
3738

39+
import static org.openmrs.util.PrivilegeConstants.VIEW_LOCATIONS;
40+
3841
/**
3942
* {@link Resource} for {@link Location}, supporting standard CRUD operations
4043
*/
@@ -249,39 +252,53 @@ public void purge(Location location, RequestContext context) throws ResponseExce
249252
protected NeedsPaging<Location> doGetAll(RequestContext context) {
250253
return new NeedsPaging<Location>(Context.getLocationService().getAllLocations(context.getIncludeAll()), context);
251254
}
252-
255+
253256
/**
254257
* @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#doSearch(org.openmrs.module.webservices.rest.web.RequestContext)
255-
* A query string and/or a tag uuid can be passed in; if both are passed in, returns an
258+
* A query string and/or a tag (referenced by name or uuid) can be passed in; if both are passed in, returns an
256259
* intersection of the results; excludes retired locations
257260
*/
258261
@Override
259262
protected PageableResult doSearch(RequestContext context) {
260-
261-
LocationService locationService = Context.getLocationService();
262-
263-
String tagUuid = context.getParameter("tag");
263+
264+
String tag = context.getParameter("tag");
264265
String query = context.getParameter("q");
265-
266+
266267
List<Location> locationsByTag = null;
267268
List<Location> locationsByQuery = null;
268-
269-
if (tagUuid != null) {
270-
LocationTag locationTag = locationService.getLocationTagByUuid(tagUuid);
271-
locationsByTag = locationService.getLocationsHavingAllTags(Arrays.asList(locationTag));
269+
270+
if (StringUtils.isNotBlank(tag)) {
271+
locationsByTag = new ArrayList<Location>();
272+
273+
try {
274+
Context.addProxyPrivilege(VIEW_LOCATIONS); //Not using PrivilegeConstants.VIEW_LOCATIONS which was removed in platform 1.11+
275+
Context.addProxyPrivilege("Get Locations"); //1.11+
276+
277+
LocationTag locationTag = Context.getLocationService().getLocationTagByUuid(tag);
278+
if (locationTag == null) {
279+
locationTag = Context.getLocationService().getLocationTagByName(tag);
280+
}
281+
282+
if (locationTag != null) {
283+
locationsByTag = Context.getLocationService().getLocationsHavingAllTags(Arrays.asList(locationTag));
284+
}
285+
} finally {
286+
Context.removeProxyPrivilege(VIEW_LOCATIONS); //Not using PrivilegeConstants.VIEW_LOCATIONS which was removed in platform 1.11+
287+
Context.removeProxyPrivilege("Get Locations"); //1.11+
288+
}
272289
}
273-
274-
if (query != null) {
275-
locationsByQuery = locationService.getLocations(query);
290+
291+
if (StringUtils.isNotBlank(query)) {
292+
locationsByQuery = Context.getLocationService().getLocations(query);
276293
}
277-
294+
278295
if (locationsByTag == null) {
279296
return new NeedsPaging<Location>(locationsByQuery, context);
280297
} else if (locationsByQuery == null) {
281298
return new NeedsPaging<Location>(locationsByTag, context);
282299
} else {
283300
return new NeedsPaging<Location>(
284-
(List<Location>) CollectionUtils.intersection(locationsByQuery, locationsByTag), context);
301+
(List<Location>) CollectionUtils.intersection(locationsByQuery, locationsByTag), context);
285302
}
286303
}
287304
}

omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/search/openmrs1_8/LocationSearchHandler.java

Lines changed: 0 additions & 78 deletions
This file was deleted.

omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/LocationController1_9Test.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,20 @@ public void shouldSearchAndReturnListOfLocationsWithSpecifiedTag() throws Except
287287
Assert.assertEquals(1, hits.size()); // should ignore retired location?
288288
Assert.assertEquals(service.getLocation(1).getUuid(), PropertyUtils.getProperty(hits.get(0), "uuid"));
289289
}
290+
291+
@Test
292+
public void shouldSearchAndReturnListOfLocationsWithSpecifiedReferencedByName() throws Exception {
293+
294+
executeDataSet(LOCATION_TAG_INITIAL_XML);
295+
296+
MockHttpServletRequest req = request(RequestMethod.GET, getURI());
297+
req.addParameter("tag", "General Hospital");
298+
299+
SimpleObject result = deserialize(handle(req));
300+
List<Object> hits = (List<Object>) result.get("results");
301+
Assert.assertEquals(1, hits.size()); // should ignore retired location?
302+
Assert.assertEquals(service.getLocation(1).getUuid(), PropertyUtils.getProperty(hits.get(0), "uuid"));
303+
}
290304

291305
@Test
292306
public void shouldSearchAndReturnListOfLocationsWithSpecifiedTagAndQueryString() throws Exception {

omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/search/openmrs1_9/LocationSearchHandlerTest.java

Lines changed: 0 additions & 95 deletions
This file was deleted.

omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/search/openmrs1_9/PatientByIdentifierSearchHandlerTest1_9.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import org.openmrs.module.webservices.rest.SimpleObject;
1717
import org.openmrs.module.webservices.rest.web.RestTestConstants1_8;
1818
import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest;
19-
import org.openmrs.module.webservices.rest.web.v1_0.search.openmrs1_8.LocationSearchHandler;
2019
import org.springframework.mock.web.MockHttpServletRequest;
2120
import org.springframework.web.bind.annotation.RequestMethod;
2221

@@ -47,11 +46,7 @@ public long getAllCount() {
4746
public String getUuid() {
4847
return RestTestConstants1_8.PATIENT_UUID;
4948
}
50-
51-
/**
52-
* @verifies return location by tag uuid
53-
* @see LocationSearchHandler#getSearchConfig()
54-
*/
49+
5550
@Test
5651
public void getSearchConfig_shouldReturnPatientByIdentifier() throws Exception {
5752
MockHttpServletRequest req = request(RequestMethod.GET, getURI());

0 commit comments

Comments
 (0)