Skip to content
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.
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 @@ -10,6 +10,7 @@
package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8;

import java.util.List;
import java.util.Comparator;

import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
Expand Down Expand Up @@ -313,6 +314,15 @@ protected PageableResult doSearch(RequestContext context) {
if (context.getType() != null) {
filterByType(orders, context.getType());
}

//Sort orders by date in descending order (newest first)
orders.sort(new Comparator<Order>() {
@Override
public int compare(Order o1, Order o2) {
return o2.getDateCreated().compareTo(o1.getDateCreated());
Copy link

Choose a reason for hiding this comment

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

Should we consider dateActivated or dateCreated?

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 now that you mention it I think it is better as well, but in the time I interacted with the issue I didn't see the difference, can you give me examples why is it better?

Copy link

Choose a reason for hiding this comment

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

Yes now that you mention it I think it is better as well, but in the time I interacted with the issue I didn't see the difference, can you give me examples why is it better?

There will be differences especially where we do RDE for orders

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay I will change the code to sort by dateActivated

}
});

return new NeedsPaging<Order>(orders, context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
*/
package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Arrays;

import org.junit.Assert;
import org.junit.Test;
Expand All @@ -18,6 +21,8 @@
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.RestTestConstants1_8;
import org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResourceTest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class OrderResource1_8Test extends BaseDelegatingResourceTest<OrderResource1_8, Order> {

Expand Down Expand Up @@ -68,4 +73,46 @@ private void voidOneOrder() {
Context.getOrderService().saveOrder(order);
}

@Test
public void testSortingLogic() {
Date earlier = new Date(1000);
Date later = new Date(2000);

Order o1 = new Order();
o1.setDateCreated(earlier);

Order o2 = new Order();
o2.setDateCreated(later);

// Test the actual comparison logic
int comparisonResult = o2.getDateCreated().compareTo(o1.getDateCreated());
Assert.assertTrue("Newer date should come first", comparisonResult > 0);
}

@Test
public void shouldSortOrdersByDateInDescendingOrder() {
// Create test orders with different dates
Order order1 = new Order();
order1.setDateCreated(new Date(1000000));

Order order2 = new Order();
order2.setDateCreated(new Date(2000000));

Order order3 = new Order();
order3.setDateCreated(new Date(3000000));

List<Order> orders = Arrays.asList(order1, order2, order3);

// Sort the orders
orders.sort(new Comparator<Order>() {
@Override
public int compare(Order o1, Order o2) {
return o2.getDateCreated().compareTo(o1.getDateCreated());
}
});

// Verify the order by comparing dates
assertTrue(orders.get(0).getDateCreated().after(orders.get(1).getDateCreated()));
assertTrue(orders.get(1).getDateCreated().after(orders.get(2).getDateCreated()));
}
}
Loading