|
9 | 9 | */ |
10 | 10 | package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8; |
11 | 11 |
|
| 12 | +import java.util.ArrayList; |
12 | 13 | import java.util.Arrays; |
13 | 14 | import java.util.List; |
14 | 15 |
|
|
18 | 19 | import io.swagger.models.properties.RefProperty; |
19 | 20 | import io.swagger.models.properties.StringProperty; |
20 | 21 | import org.apache.commons.collections.CollectionUtils; |
| 22 | +import org.apache.commons.lang3.StringUtils; |
21 | 23 | import org.openmrs.Location; |
22 | 24 | import org.openmrs.LocationTag; |
23 | | -import org.openmrs.api.LocationService; |
24 | 25 | import org.openmrs.api.context.Context; |
25 | 26 | import org.openmrs.module.webservices.rest.web.RequestContext; |
26 | 27 | import org.openmrs.module.webservices.rest.web.RestConstants; |
|
35 | 36 | import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging; |
36 | 37 | import org.openmrs.module.webservices.rest.web.response.ResponseException; |
37 | 38 |
|
| 39 | +import static org.openmrs.util.PrivilegeConstants.VIEW_LOCATIONS; |
| 40 | + |
38 | 41 | /** |
39 | 42 | * {@link Resource} for {@link Location}, supporting standard CRUD operations |
40 | 43 | */ |
@@ -249,39 +252,53 @@ public void purge(Location location, RequestContext context) throws ResponseExce |
249 | 252 | protected NeedsPaging<Location> doGetAll(RequestContext context) { |
250 | 253 | return new NeedsPaging<Location>(Context.getLocationService().getAllLocations(context.getIncludeAll()), context); |
251 | 254 | } |
252 | | - |
| 255 | + |
253 | 256 | /** |
254 | 257 | * @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 |
256 | 259 | * intersection of the results; excludes retired locations |
257 | 260 | */ |
258 | 261 | @Override |
259 | 262 | 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"); |
264 | 265 | String query = context.getParameter("q"); |
265 | | - |
| 266 | + |
266 | 267 | List<Location> locationsByTag = null; |
267 | 268 | 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 | + } |
272 | 289 | } |
273 | | - |
274 | | - if (query != null) { |
275 | | - locationsByQuery = locationService.getLocations(query); |
| 290 | + |
| 291 | + if (StringUtils.isNotBlank(query)) { |
| 292 | + locationsByQuery = Context.getLocationService().getLocations(query); |
276 | 293 | } |
277 | | - |
| 294 | + |
278 | 295 | if (locationsByTag == null) { |
279 | 296 | return new NeedsPaging<Location>(locationsByQuery, context); |
280 | 297 | } else if (locationsByQuery == null) { |
281 | 298 | return new NeedsPaging<Location>(locationsByTag, context); |
282 | 299 | } else { |
283 | 300 | return new NeedsPaging<Location>( |
284 | | - (List<Location>) CollectionUtils.intersection(locationsByQuery, locationsByTag), context); |
| 301 | + (List<Location>) CollectionUtils.intersection(locationsByQuery, locationsByTag), context); |
285 | 302 | } |
286 | 303 | } |
287 | 304 | } |
0 commit comments