diff --git a/omod-1.10/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_10/OrderUtil.java b/omod-1.10/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_10/OrderUtil.java index 005171757..624b112ba 100644 --- a/omod-1.10/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_10/OrderUtil.java +++ b/omod-1.10/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_10/OrderUtil.java @@ -43,16 +43,16 @@ public static List getOrders(Patient patient, CareSetting careSetting, Or Date asOfDate, boolean includeVoided) { OrderService os = Context.getOrderService(); - if (!INACTIVE.equals(status) && !ANY.equals(status)) { + if (!INACTIVE.equalsIgnoreCase(status) && !ANY.equalsIgnoreCase(status)) { return os.getActiveOrders(patient, orderType, careSetting, asOfDate); } - if (INACTIVE.equals(status)) { + if (INACTIVE.equalsIgnoreCase(status)) { includeVoided = false; } List orders = os.getOrders(patient, careSetting, orderType, includeVoided); - if (INACTIVE.equals(status)) { + if (INACTIVE.equalsIgnoreCase(status)) { removeActiveOrders(orders, asOfDate); } diff --git a/omod-1.10/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_10/OrderController1_10Test.java b/omod-1.10/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_10/OrderController1_10Test.java index 323a4265c..039a45e75 100644 --- a/omod-1.10/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_10/OrderController1_10Test.java +++ b/omod-1.10/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_10/OrderController1_10Test.java @@ -35,7 +35,11 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Date; +import java.util.LinkedList; import java.util.List; +import java.util.ListIterator; +import java.util.Map; +import java.util.stream.Collectors; import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.contains; @@ -486,6 +490,31 @@ public void shouldGetAllInActiveOrdersForAPatient() throws Exception { PropertyUtils.getProperty(resultList.get(1), "uuid").toString() }); assertThat(uuids, hasItems(expectedOrderUuids)); } + + @Test + public void doSearch_shouldGetAllInActiveOrdersForAPatient() throws Exception { + SimpleObject results = deserialize(handle(newGetRequest(getURI(), + new Parameter("patient", "da7f524f-27ce-4bb2-86d6-6d1d05312bd5"), new Parameter("status", "INACTIVE"), + new Parameter("careSetting", RestTestConstants1_10.CARE_SETTING_UUID)))); + assertEquals(4, Util.getResultsSize(results)); + + List resultsList = Util.getResultsList(results); + List> resultMap = new LinkedList>(); + for (Object o : resultsList) { + resultMap.add((Map) o); + } + + String[] expectedStatuses = new String[] {"inactive", "inactive", "inactive", "inactive"}; + List statusList = new LinkedList(); + for (Map m : resultMap) { + for (String key : m.keySet()) { + if (key.equals("status")) { + statusList.add((String) m.get(key)); + } + } + } + assertEquals(Arrays.asList(expectedStatuses), statusList); + } @Test public void shouldGetAllOrdersForAPatientInTheSpecifiedCareSetting() throws Exception { diff --git a/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/resource/impl/BaseDelegatingResource.java b/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/resource/impl/BaseDelegatingResource.java index 179b0327a..2da1ec86f 100644 --- a/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/resource/impl/BaseDelegatingResource.java +++ b/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/resource/impl/BaseDelegatingResource.java @@ -14,6 +14,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -25,11 +26,14 @@ import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.time.DateUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.proxy.HibernateProxy; import org.openmrs.OpenmrsObject; +import org.openmrs.Order; import org.openmrs.api.context.Context; import org.openmrs.module.ModuleUtil; import org.openmrs.module.webservices.rest.SimpleObject; @@ -397,7 +401,8 @@ public SimpleObject asRepresentation(T delegate, Representation representation) simple = (SimpleObject) meth.invoke(handler, delegate); else simple = (SimpleObject) meth.invoke(handler, delegate, representation); - + + maybeDecorateWithStatus(simple, delegate); maybeDecorateWithType(simple, delegate); decorateWithResourceVersion(simple, representation); @@ -441,6 +446,46 @@ private void maybeDecorateWithType(SimpleObject simple, T delegate) { if (hasTypesDefined()) simple.add(RestConstants.PROPERTY_FOR_TYPE, getTypeName(delegate)); } + + /** + * If this resource is an Order, add a STATUS field to the REST response which indicates whether the order is ACTIVE or INACTIVE. + * + * @param simple simplified representation which will be decorated with the status + * @param delegate the object that simple represents + */ + + private void maybeDecorateWithStatus(SimpleObject simple, T delegate) { + if (delegate instanceof Order) { + Date asOfDate = new Date(); + Order order = (Order) delegate; + if (order.isDiscontinued(asOfDate) || isExpired(order, asOfDate)) { + simple.add("status", "inactive"); + } else { + simple.add("status", "active"); + } + } + } + + /** + * @param order to check the autoExpireDate + * @param checkDate to check against the order.autoExpireDate + * @return boolean true or false if the order.autoExpireDate is passed or not + */ + private boolean isExpired(Order order, Date checkDate) { + if (order.isVoided()) { + return false; + } else { + if (checkDate == null) { + checkDate = new Date(); + } + + if (checkDate.after(order.getDateCreated())) { + return order.getAutoExpireDate() != null && checkDate.after(order.getAutoExpireDate()); + } else { + return false; + } + } + } /** * If this resources supports subclasses, this method gets the user-friendly type name for the