Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,15 @@ protected PageableResult doSearch(RequestContext context) throws ResponseExcepti
if (context.getType() != null) {
filterByType(orders, context.getType());
}


// if the user indicated a specific sort parameter, apply sort here
if (StringUtils.isNotBlank(sortParam)) {
List<Order> sortedOrder = sortOrdersBasedOnDateActivatedOrDateStopped(orders, sortParam, status);
return new NeedsPaging<Order>(sortedOrder, context);
}
else {
return new NeedsPaging<Order>(orders, context);
List<Order> descSortedOrder = sortOrdersInDescendingOrderByDate(orders);
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this still call sortOrdersBasedOnDateActivatedOrDateStopped() albeit with DESC as the sortParam?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this makes sense, but that means it'll require the status parameter all the time, else it will throw a Null Pointer Exception

return new NeedsPaging<Order>(descSortedOrder, context);
}
} else {
throw new InvalidSearchException("Please provide patientUuid in the patient parameter");
Expand All @@ -290,6 +292,21 @@ private static Date getUsableDate(Order order) {
private static Date getActiveOrderSortDate(Order order) {
return order.getDateActivated() != null ? order.getDateActivated() : order.getDateCreated();
}

private List<Order> sortOrdersInDescendingOrderByDate(List<Order> orders) {
List<Order> sortedList = new ArrayList<Order>(orders);
Collections.sort(sortedList, new Comparator<Order>() {
@Override
public int compare(Order o1, Order o2) {
return getOrderSortDate(o2).compareTo(getOrderSortDate(o1));
}
});
return sortedList;
}

private static Date getOrderSortDate(Order order) {
return order.getDateActivated() != null ? order.getDateActivated() : order.getDateCreated();
}

public List<Order> sortOrdersBasedOnDateActivatedOrDateStopped(List<Order> orders, final String sortOrder,
final String status) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,4 +598,23 @@ public void doSearch_shouldReturnExceptionIfNoPatientUuidIsSpecified() throws Ex
);
handle(req);
}

@Test
public void doSearch_shouldReturnOrdersSortedInDescendingOrderByDate() throws Exception {
SimpleObject orders = deserialize(handle(newGetRequest(getURI(),
new Parameter("patient", "da7f524f-27ce-4bb2-86d6-6d1d05312bd5")
)));

List<Object> resultList = Util.getResultsList(orders);
assertTrue(resultList.size() >= 2);

String uuid1 = PropertyUtils.getProperty(resultList.get(0), "uuid").toString();
String uuid2 = PropertyUtils.getProperty(resultList.get(1), "uuid").toString();

Order order1 = orderService.getOrderByUuid(uuid1);
Order order2 = orderService.getOrderByUuid(uuid2);

// orders sorted by date in descending order (newest first)
assertTrue(order1.getDateActivated().after(order2.getDateActivated()));
}
}
Loading