Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RESTWS-907: Cannot search for inactive drug orders in RefApp 3.x #589

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ public static List<Order> 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<Order> orders = os.getOrders(patient, careSetting, orderType, includeVoided);
if (INACTIVE.equals(status)) {
if (INACTIVE.equalsIgnoreCase(status)) {
removeActiveOrders(orders, asOfDate);
}

Expand Down
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 @@ -35,7 +35,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.contains;
Expand Down Expand Up @@ -486,6 +488,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<Object> resultsList = Util.getResultsList(results);
List<Map<String, Object>> resultMap = new LinkedList<Map<String, Object>>();
for (Object o : resultsList) {
resultMap.add((Map<String, Object>) o);
}

String[] expectedStatuses = new String[] {"inactive", "inactive", "inactive", "inactive"};
List<String> statusList = new LinkedList<String>();
for (Map<String, Object> m : resultMap) {
for (String key : m.keySet()) {
if (key.equals(RestConstants.PROPERTY_FOR_STATUS)) {
statusList.add((String) m.get(key));
}
}
}
assertEquals(Arrays.asList(expectedStatuses), statusList);
}

@Test
public void shouldGetAllOrdersForAPatientInTheSpecifiedCareSetting() throws Exception {
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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ibacher, how about something like this?!

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 @@ -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