Skip to content

Commit c677942

Browse files
RESTWS-760: Order resource does not include drug strength
1 parent 9e3e879 commit c677942

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

omod-1.10/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_10/OrderResource1_10.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,27 @@
1616
import io.swagger.models.properties.StringProperty;
1717
import org.apache.commons.lang.StringUtils;
1818
import org.openmrs.CareSetting;
19+
import org.openmrs.Drug;
20+
import org.openmrs.DrugOrder;
1921
import org.openmrs.Order;
2022
import org.openmrs.OrderType;
2123
import org.openmrs.Patient;
2224
import org.openmrs.api.context.Context;
2325
import org.openmrs.module.webservices.docs.swagger.core.property.EnumProperty;
26+
import org.openmrs.module.webservices.rest.SimpleObject;
2427
import org.openmrs.module.webservices.rest.web.ConversionUtil;
2528
import org.openmrs.module.webservices.rest.web.RequestContext;
2629
import org.openmrs.module.webservices.rest.web.RestConstants;
2730
import org.openmrs.module.webservices.rest.web.annotation.Resource;
2831
import org.openmrs.module.webservices.rest.web.api.RestService;
32+
import org.openmrs.module.webservices.rest.web.representation.CustomRepresentation;
2933
import org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation;
3034
import org.openmrs.module.webservices.rest.web.representation.FullRepresentation;
3135
import org.openmrs.module.webservices.rest.web.representation.Representation;
3236
import org.openmrs.module.webservices.rest.web.resource.api.PageableResult;
3337
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
34-
import org.openmrs.module.webservices.rest.web.resource.impl.EmptySearchResult;
3538
import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging;
39+
import org.openmrs.module.webservices.rest.web.response.ConversionException;
3640
import org.openmrs.module.webservices.rest.web.response.InvalidSearchException;
3741
import org.openmrs.module.webservices.rest.web.response.ObjectNotFoundException;
3842
import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException;
@@ -311,7 +315,42 @@ public int compare(Order o1, Order o2) {
311315
});
312316
return sortedList;
313317
}
314-
318+
319+
/**
320+
* If this resource is a DrugOrder, then we add a strength property to the input, and return it
321+
*
322+
* @param simple simplified representation which will be decorated with the strength property
323+
*
324+
* @param delegate the DrugOrder object that simple represents
325+
*/
326+
private void decorateWithStrengthProperty(SimpleObject simple, Order delegate) {
327+
OrderType drugOrderType = Context.getOrderService().getOrderTypeByName("Drug order");
328+
if (delegate.getOrderType().equals(drugOrderType)) {
329+
Drug drug = ((DrugOrder) delegate).getDrug();
330+
if (drug != null) {
331+
simple.add(RestConstants.PROPERTY_FOR_STRENGTH, drug.getStrength() != null ? drug.getStrength() : "[no strength]");
332+
} else {
333+
simple.add(RestConstants.PROPERTY_FOR_STRENGTH,"[no strength]");
334+
}
335+
}
336+
}
337+
338+
/**
339+
* @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#asRepresentation(Object, Representation)
340+
*/
341+
@Override
342+
public SimpleObject asRepresentation(Order delegate, Representation representation) throws ConversionException {
343+
DelegatingResourceDescription repDescription;
344+
if (representation instanceof CustomRepresentation) {
345+
repDescription = ConversionUtil.getCustomRepresentationDescription((CustomRepresentation) representation);
346+
return convertDelegateToRepresentation(delegate, repDescription);
347+
}
348+
349+
SimpleObject simple = super.asRepresentation(delegate, representation);
350+
decorateWithStrengthProperty(simple, delegate);
351+
return simple;
352+
}
353+
315354
/**
316355
* @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getResourceVersion()
317356
*/

omod-1.10/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_10/OrderController1_10Test.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,4 +598,16 @@ public void doSearch_shouldReturnExceptionIfNoPatientUuidIsSpecified() throws Ex
598598
);
599599
handle(req);
600600
}
601+
602+
@Test
603+
public void doSearch_shouldReturnWithDrugStrengthProperty() throws Exception {
604+
MockHttpServletRequest req = newGetRequest(getURI(),
605+
new Parameter("orderTypes", RestTestConstants1_10.DRUG_ORDER_TYPE_UUID),
606+
new Parameter("patient", PATIENT_UUID)
607+
);
608+
609+
SimpleObject orders = deserialize(handle(req));
610+
List<Object> results = orders.get("results");
611+
assertEquals(PropertyUtils.getProperty(results.get(0), "strength"), "325.0mg");
612+
}
601613
}

omod-2.3/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/search/openmrs2_3/OrderSearchHandler2_3Test.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,5 +220,4 @@ public void getSearchConfig_shouldReturnOrdersByAccessionNumber() throws Excepti
220220
Assert.assertEquals(1, orders.size());
221221
Assert.assertEquals(PropertyUtils.getProperty(orders.get(0), "uuid"), "e1f95924-697a-11e3-bd76-0800271c1b75");
222222
}
223-
224223
}

omod-common/src/main/java/org/openmrs/module/webservices/rest/web/RestConstants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ public class RestConstants {
155155
public static String REPRESENTATION_FULL = "full";
156156

157157
public static String REPRESENTATION_CUSTOM_PREFIX = "custom:";
158+
159+
/**
160+
* Used in DrugOrder object representations to indicate the specific strength an instance has
161+
*/
162+
public static String PROPERTY_FOR_STRENGTH = "strength";
158163

159164
// The URI prefix through which clients consuming web services will connect
160165
// to the web application

0 commit comments

Comments
 (0)