Skip to content

Commit 6206bfc

Browse files
RESTWS-760: Order resource does not include drug strength
1 parent d0647d2 commit 6206bfc

File tree

5 files changed

+53
-58
lines changed

5 files changed

+53
-58
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-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/OrderController1_9Test.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.apache.commons.beanutils.PropertyUtils;
1515
import org.junit.Assert;
1616
import org.junit.Before;
17-
import org.junit.Ignore;
1817
import org.junit.Test;
1918
import org.openmrs.DrugOrder;
2019
import org.openmrs.Order;
@@ -263,7 +262,6 @@ public void shouldUpdateDrugOrder() throws Exception {
263262
* been updated
264263
*/
265264
@Test
266-
@Ignore("RESTWS-760: Order resource does not include drug strength")
267265
public void shouldAllowYouToPostANonUpdatablePropertyWithAnUnchangedValue() throws Exception {
268266
MockHttpServletRequest get = request(RequestMethod.GET, getURI() + "/" + DRUG_ORDER_UUID);
269267
SimpleObject drugOrder = deserialize(handle(get));

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,12 @@
99
*/
1010
package org.openmrs.module.webservices.rest.web.v1_0.search.openmrs2_3;
1111

12-
import static org.junit.Assert.assertNotNull;
13-
1412
import org.apache.commons.beanutils.PropertyUtils;
1513
import org.junit.Assert;
1614
import org.junit.Test;
1715
import org.openmrs.Order;
1816
import org.openmrs.module.webservices.rest.SimpleObject;
1917
import org.openmrs.module.webservices.rest.web.RequestContext;
20-
import org.openmrs.module.webservices.rest.web.RestTestConstants1_10;
21-
import org.openmrs.module.webservices.rest.web.RestTestConstants1_8;
2218
import org.openmrs.module.webservices.rest.web.v1_0.controller.RestControllerTestUtils;
2319
import org.springframework.mock.web.MockHttpServletRequest;
2420
import org.springframework.web.bind.annotation.RequestMethod;
@@ -224,15 +220,4 @@ public void getSearchConfig_shouldReturnOrdersByAccessionNumber() throws Excepti
224220
Assert.assertEquals(1, orders.size());
225221
Assert.assertEquals(PropertyUtils.getProperty(orders.get(0), "uuid"), "e1f95924-697a-11e3-bd76-0800271c1b75");
226222
}
227-
228-
@Test
229-
public void search_shouldReturnWithDrugStrengthProperty() throws Exception {
230-
MockHttpServletRequest req = request(RequestMethod.GET, getURI());
231-
req.setParameter("orderTypes", RestTestConstants1_10.DRUG_ORDER_TYPE_UUID);
232-
req.addParameter("patient", RestTestConstants1_8.PATIENT_UUID);
233-
234-
SimpleObject result = deserialize(handle(req));
235-
List<Object> orders = result.get("results");
236-
Assert.assertEquals(PropertyUtils.getProperty(orders.get(1), "strength"), "325.0mg");
237-
}
238223
}

omod-common/src/main/java/org/openmrs/module/webservices/rest/web/resource/impl/BaseDelegatingResource.java

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@
2929
import org.apache.commons.logging.Log;
3030
import org.apache.commons.logging.LogFactory;
3131
import org.hibernate.proxy.HibernateProxy;
32-
import org.openmrs.Drug;
33-
import org.openmrs.DrugOrder;
3432
import org.openmrs.OpenmrsObject;
35-
import org.openmrs.Order;
36-
import org.openmrs.OrderType;
3733
import org.openmrs.api.context.Context;
3834
import org.openmrs.module.ModuleUtil;
3935
import org.openmrs.module.webservices.rest.SimpleObject;
@@ -387,7 +383,6 @@ public SimpleObject asRepresentation(T delegate, Representation representation)
387383

388384
maybeDecorateWithType(simple, delegate);
389385
decorateWithResourceVersion(simple, representation);
390-
decorateWithStrengthProperty(simple, delegate);
391386

392387
return simple;
393388
}
@@ -405,7 +400,6 @@ public SimpleObject asRepresentation(T delegate, Representation representation)
405400

406401
maybeDecorateWithType(simple, delegate);
407402
decorateWithResourceVersion(simple, representation);
408-
decorateWithStrengthProperty(simple, delegate);
409403

410404
return simple;
411405
}
@@ -447,39 +441,6 @@ private void maybeDecorateWithType(SimpleObject simple, T delegate) {
447441
if (hasTypesDefined())
448442
simple.add(RestConstants.PROPERTY_FOR_TYPE, getTypeName(delegate));
449443
}
450-
451-
/**
452-
* If this resource is a DrugOrder, then we add a strength property to the input, and return it
453-
*
454-
* @param simple simplified representation which will be decorated with the strength property
455-
*
456-
* @param delegate the DrugOrder object that simple represents
457-
*/
458-
private void decorateWithStrengthProperty(SimpleObject simple, T delegate) {
459-
if (delegate instanceof Order) {
460-
Order order = (Order) delegate;
461-
OrderType drugOrderType;
462-
try {
463-
drugOrderType = Context.getOrderService().getOrderTypeByName("Drug order");
464-
} catch (NoSuchMethodError nsme) {
465-
drugOrderType = Context.getOrderService().getOrderType(2); // DrugOrder for backward compatibility
466-
}
467-
468-
if (order.getOrderType().equals(drugOrderType)) {
469-
470-
Drug drug = ((DrugOrder) order).getDrug();
471-
if (drug != null) {
472-
try {
473-
simple.add(RestConstants.PROPERTY_FOR_STRENGTH, drug.getStrength() != null ? drug.getStrength() : "[no strength]");
474-
} catch (NoSuchMethodError nsme) {
475-
simple.add(RestConstants.PROPERTY_FOR_STRENGTH, drug.getDoseStrength() != null ? drug.getDoseStrength() + drug.getUnits() : "[no strength]");
476-
}
477-
} else {
478-
simple.add(RestConstants.PROPERTY_FOR_STRENGTH,"[no strength]");
479-
}
480-
}
481-
}
482-
}
483444

484445
/**
485446
* If this resources supports subclasses, this method gets the user-friendly type name for the

0 commit comments

Comments
 (0)