Skip to content

Commit 294ff7f

Browse files
RESTWS-907: Cannot search for inactive drug orders in RefApp 3.x
1 parent 7ee4c56 commit 294ff7f

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ public static List<Order> getOrders(Patient patient, CareSetting careSetting, Or
4343
Date asOfDate, boolean includeVoided) {
4444

4545
OrderService os = Context.getOrderService();
46-
if (!INACTIVE.equals(status) && !ANY.equals(status)) {
46+
if (!INACTIVE.equalsIgnoreCase(status) && !ANY.equalsIgnoreCase(status)) {
4747
return os.getActiveOrders(patient, orderType, careSetting, asOfDate);
4848
}
4949

50-
if (INACTIVE.equals(status)) {
50+
if (INACTIVE.equalsIgnoreCase(status)) {
5151
includeVoided = false;
5252
}
5353

5454
List<Order> orders = os.getOrders(patient, careSetting, orderType, includeVoided);
55-
if (INACTIVE.equals(status)) {
55+
if (INACTIVE.equalsIgnoreCase(status)) {
5656
removeActiveOrders(orders, asOfDate);
5757
}
5858

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@
3535
import java.util.ArrayList;
3636
import java.util.Arrays;
3737
import java.util.Date;
38+
import java.util.LinkedList;
3839
import java.util.List;
40+
import java.util.ListIterator;
41+
import java.util.Map;
42+
import java.util.stream.Collectors;
3943

4044
import static org.hamcrest.Matchers.hasItems;
4145
import static org.hamcrest.Matchers.contains;
@@ -486,6 +490,31 @@ public void shouldGetAllInActiveOrdersForAPatient() throws Exception {
486490
PropertyUtils.getProperty(resultList.get(1), "uuid").toString() });
487491
assertThat(uuids, hasItems(expectedOrderUuids));
488492
}
493+
494+
@Test
495+
public void doSearch_shouldGetAllInActiveOrdersForAPatient() throws Exception {
496+
SimpleObject results = deserialize(handle(newGetRequest(getURI(),
497+
new Parameter("patient", "da7f524f-27ce-4bb2-86d6-6d1d05312bd5"), new Parameter("status", "INACTIVE"),
498+
new Parameter("careSetting", RestTestConstants1_10.CARE_SETTING_UUID))));
499+
assertEquals(4, Util.getResultsSize(results));
500+
501+
List<Object> resultsList = Util.getResultsList(results);
502+
List<Map<String, Object>> resultMap = new LinkedList<Map<String, Object>>();
503+
for (Object o : resultsList) {
504+
resultMap.add((Map<String, Object>) o);
505+
}
506+
507+
String[] expectedStatuses = new String[] {"inactive", "inactive", "inactive", "inactive"};
508+
List<String> statusList = new LinkedList<String>();
509+
for (Map<String, Object> m : resultMap) {
510+
for (String key : m.keySet()) {
511+
if (key.equals("status")) {
512+
statusList.add((String) m.get(key));
513+
}
514+
}
515+
}
516+
assertEquals(Arrays.asList(expectedStatuses), statusList);
517+
}
489518

490519
@Test
491520
public void shouldGetAllOrdersForAPatientInTheSpecifiedCareSetting() throws Exception {

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.ArrayList;
1515
import java.util.Collection;
1616
import java.util.Collections;
17+
import java.util.Date;
1718
import java.util.HashMap;
1819
import java.util.HashSet;
1920
import java.util.Iterator;
@@ -25,11 +26,14 @@
2526

2627
import org.apache.commons.beanutils.PropertyUtils;
2728
import org.apache.commons.collections.CollectionUtils;
29+
import org.apache.commons.lang.ObjectUtils;
2830
import org.apache.commons.lang.StringUtils;
31+
import org.apache.commons.lang.time.DateUtils;
2932
import org.apache.commons.logging.Log;
3033
import org.apache.commons.logging.LogFactory;
3134
import org.hibernate.proxy.HibernateProxy;
3235
import org.openmrs.OpenmrsObject;
36+
import org.openmrs.Order;
3337
import org.openmrs.api.context.Context;
3438
import org.openmrs.module.ModuleUtil;
3539
import org.openmrs.module.webservices.rest.SimpleObject;
@@ -397,7 +401,8 @@ public SimpleObject asRepresentation(T delegate, Representation representation)
397401
simple = (SimpleObject) meth.invoke(handler, delegate);
398402
else
399403
simple = (SimpleObject) meth.invoke(handler, delegate, representation);
400-
404+
405+
maybeDecorateWithStatus(simple, delegate);
401406
maybeDecorateWithType(simple, delegate);
402407
decorateWithResourceVersion(simple, representation);
403408

@@ -441,6 +446,46 @@ private void maybeDecorateWithType(SimpleObject simple, T delegate) {
441446
if (hasTypesDefined())
442447
simple.add(RestConstants.PROPERTY_FOR_TYPE, getTypeName(delegate));
443448
}
449+
450+
/**
451+
* If this resource is an Order, add a STATUS field to the REST response which indicates whether the order is ACTIVE or INACTIVE.
452+
*
453+
* @param simple simplified representation which will be decorated with the status
454+
* @param delegate the object that simple represents
455+
*/
456+
457+
private void maybeDecorateWithStatus(SimpleObject simple, T delegate) {
458+
if (delegate instanceof Order) {
459+
Date asOfDate = new Date();
460+
Order order = (Order) delegate;
461+
if (order.isDiscontinued(asOfDate) || isExpired(order, asOfDate)) {
462+
simple.add("status", "inactive");
463+
} else {
464+
simple.add("status", "active");
465+
}
466+
}
467+
}
468+
469+
/**
470+
* @param order to check the autoExpireDate
471+
* @param checkDate to check against the order.autoExpireDate
472+
* @return boolean true or false if the order.autoExpireDate is passed or not
473+
*/
474+
private boolean isExpired(Order order, Date checkDate) {
475+
if (order.isVoided()) {
476+
return false;
477+
} else {
478+
if (checkDate == null) {
479+
checkDate = new Date();
480+
}
481+
482+
if (checkDate.after(order.getDateCreated())) {
483+
return order.getAutoExpireDate() != null && checkDate.after(order.getAutoExpireDate());
484+
} else {
485+
return false;
486+
}
487+
}
488+
}
444489

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

0 commit comments

Comments
 (0)