From d1091f5b4b3428edf5a861c3d0dfe03a2fcaa82a Mon Sep 17 00:00:00 2001 From: IamMujuziMoses Date: Sun, 24 Sep 2023 23:07:11 +0300 Subject: [PATCH] RESTWS-907: Cannot search for inactive drug orders in RefApp 3.x --- .../DrugOrderSubclassHandler1_10.java | 11 +++++ .../TestOrderSubclassHandler1_10.java | 11 +++++ .../openmrs1_10/OrderController1_10Test.java | 4 +- .../DrugOrderSubclassHandler1_8.java | 14 ++++++ .../resource/openmrs1_8/OrderResource1_8.java | 38 +++++++++++++++ .../webservices/rest/web/RestConstants.java | 7 ++- .../resource/impl/BaseDelegatingResource.java | 47 +------------------ .../impl/BaseDelegatingSubclassHandler.java | 1 + 8 files changed, 83 insertions(+), 50 deletions(-) diff --git a/omod-1.10/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_10/DrugOrderSubclassHandler1_10.java b/omod-1.10/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_10/DrugOrderSubclassHandler1_10.java index a0a188698..9187a606b 100644 --- a/omod-1.10/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_10/DrugOrderSubclassHandler1_10.java +++ b/omod-1.10/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_10/DrugOrderSubclassHandler1_10.java @@ -32,6 +32,7 @@ import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging; import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException; import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.DrugOrderSubclassHandler1_8; +import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.OrderResource1_8; /** * Exposes the {@link org.openmrs.DrugOrder} subclass as a type in @@ -202,4 +203,14 @@ public static String getDisplay(DrugOrder delegate) { return ret.toString(); } + + /** + * @see OrderResource1_8#getStatus(org.openmrs.Order) + */ + @PropertyGetter("status") + public String getStatus(DrugOrder delegate) { + OrderResource1_8 orderResource = (OrderResource1_8) Context.getService(RestService.class) + .getResourceBySupportedClass(Order.class); + return orderResource.getStatus(delegate); + } } diff --git a/omod-1.10/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_10/TestOrderSubclassHandler1_10.java b/omod-1.10/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_10/TestOrderSubclassHandler1_10.java index a42d475fb..974a1f728 100644 --- a/omod-1.10/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_10/TestOrderSubclassHandler1_10.java +++ b/omod-1.10/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_10/TestOrderSubclassHandler1_10.java @@ -40,6 +40,7 @@ import io.swagger.models.properties.IntegerProperty; import io.swagger.models.properties.RefProperty; import io.swagger.models.properties.StringProperty; +import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.OrderResource1_8; /** * Exposes the {@link org.openmrs.TestOrder} subclass as a type in @@ -215,4 +216,14 @@ public static String getDisplay(TestOrder delegate) { .getResourceBySupportedClass(Order.class); return orderResource.getDisplayString(delegate); } + + /** + * @see OrderResource1_8#getStatus(org.openmrs.Order) + */ + @PropertyGetter("status") + public String getStatus(TestOrder delegate) { + OrderResource1_8 orderResource = (OrderResource1_8) Context.getService(RestService.class) + .getResourceBySupportedClass(Order.class); + return orderResource.getStatus(delegate); + } } 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 039a45e75..1ac4ac72f 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 @@ -37,9 +37,7 @@ 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; @@ -508,7 +506,7 @@ public void doSearch_shouldGetAllInActiveOrdersForAPatient() throws Exception { List statusList = new LinkedList(); for (Map m : resultMap) { for (String key : m.keySet()) { - if (key.equals("status")) { + if (key.equals(RestConstants.PROPERTY_FOR_STATUS)) { statusList.add((String) m.get(key)); } } diff --git a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/DrugOrderSubclassHandler1_8.java b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/DrugOrderSubclassHandler1_8.java index f1d82939d..0242139da 100644 --- a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/DrugOrderSubclassHandler1_8.java +++ b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/DrugOrderSubclassHandler1_8.java @@ -206,4 +206,18 @@ public static String getDisplay(DrugOrder delegate) { // TODO dates, etc return ret.toString(); } + /** + * @see OrderResource1_8#getStatus(org.openmrs.Order) + * + * Gets the status of the DrugOrder + * + * @param delegate DrugOrder to check if ACTIVE or INACTIVE + * @return status String + */ + @PropertyGetter("status") + public String getStatus(DrugOrder delegate) { + OrderResource1_8 orderResource = (OrderResource1_8) Context.getService(RestService.class) + .getResourceBySupportedClass(Order.class); + return orderResource.getStatus(delegate); + } } diff --git a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/OrderResource1_8.java b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/OrderResource1_8.java index e087cb560..4a9ae7705 100644 --- a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/OrderResource1_8.java +++ b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/OrderResource1_8.java @@ -9,6 +9,7 @@ */ package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8; +import java.util.Date; import java.util.List; import io.swagger.models.Model; @@ -134,6 +135,43 @@ public String getDisplayString(Order order) { return "[No Concept]"; return order.getConcept().getName().getName(); } + + /** + * Status field for {@link Order} + * + * @param order to check if ACTIVE or INACTIVE. + * @return Status + */ + @PropertyGetter("status") + public String getStatus(Order order) { + Date asOfDate = new Date(); + if (order.isDiscontinued(asOfDate) || isExpired(order, asOfDate)) { + return "inactive"; + } else { + return "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; + } + } + } /** * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getRepresentationDescription(org.openmrs.module.webservices.rest.web.representation.Representation) diff --git a/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/RestConstants.java b/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/RestConstants.java index e2a204ae7..435bcb878 100644 --- a/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/RestConstants.java +++ b/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/RestConstants.java @@ -139,7 +139,12 @@ public class RestConstants { * resource that represents a full class hierarchy */ public static final String PROPERTY_FOR_TYPE = "type"; - + + /** + * Used in object representations to indicate the status of the order + */ + public static final String PROPERTY_FOR_STATUS = "status"; + // a ref is just a uuid/uri/display value public static String REPRESENTATION_REF = "ref"; 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 2da1ec86f..179b0327a 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,7 +14,6 @@ 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; @@ -26,14 +25,11 @@ 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; @@ -401,8 +397,7 @@ 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); @@ -446,46 +441,6 @@ 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 diff --git a/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/resource/impl/BaseDelegatingSubclassHandler.java b/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/resource/impl/BaseDelegatingSubclassHandler.java index c7796267a..c39fccb48 100644 --- a/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/resource/impl/BaseDelegatingSubclassHandler.java +++ b/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/resource/impl/BaseDelegatingSubclassHandler.java @@ -131,6 +131,7 @@ public SimpleObject convertToRef(Subclass delegate) throws ConversionException { DelegatingResourceDescription rep = new DelegatingResourceDescription(); rep.addProperty("uuid"); rep.addProperty("display"); + rep.addProperty("status"); if (delegate instanceof OpenmrsData) { if (((OpenmrsData) delegate).isVoided()) rep.addProperty("voided");