Skip to content

Commit

Permalink
RESTWS-907: Cannot search for inactive drug orders in RefApp 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
IamMujuziMoses committed Oct 5, 2023
1 parent 294ff7f commit d1091f5
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -508,7 +506,7 @@ public void doSearch_shouldGetAllInActiveOrdersForAPatient() throws Exception {
List<String> statusList = new LinkedList<String>();
for (Map<String, Object> m : resultMap) {
for (String key : m.keySet()) {
if (key.equals("status")) {
if (key.equals(RestConstants.PROPERTY_FOR_STATUS)) {
statusList.add((String) m.get(key));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit d1091f5

Please sign in to comment.