use of org.motechproject.mds.util.Order in project motech by motech.
the class MdsRestFacadeTest method shouldAppendMetadata.
@Test
public void shouldAppendMetadata() {
setUpCrudAccess(false, true, false, false);
when(dataService.count()).thenReturn(81l);
QueryParams queryParams = new QueryParams(5, 20, new Order("value", Order.Direction.DESC));
RestResponse result = mdsRestFacade.get(queryParams, false);
assertNotNull(result.getMetadata());
assertEquals(ENTITY_NAME, result.getMetadata().getEntity());
assertEquals(TEST_MODULE, result.getMetadata().getModule());
assertEquals(Record.class.getName(), result.getMetadata().getClassName());
assertEquals(NAMESPACE, result.getMetadata().getNamespace());
assertEquals(5, result.getMetadata().getPage());
assertEquals(20, result.getMetadata().getPageSize());
assertEquals(81l, result.getMetadata().getTotalCount());
}
use of org.motechproject.mds.util.Order in project motech by motech.
the class InMemoryQueryFilterTest method shouldApplyQueryParams.
@Test
public void shouldApplyQueryParams() {
QueryParams queryParams = new QueryParams(2, 3, new Order("value", Order.Direction.DESC));
List<Record> result = InMemoryQueryFilter.filter(testCollection, queryParams);
assertListByValues(result, asList("nullRecord", "hmm", "hmm"));
assertListByIds(result, asList(null, 5L, 6L));
}
use of org.motechproject.mds.util.Order in project motech by motech.
the class InMemoryQueryFilterTest method shouldPaginate.
@Test
public void shouldPaginate() {
Order order = new Order("value", Order.Direction.ASC);
QueryParams queryParams = new QueryParams(1, 3, order);
List<Record> result = InMemoryQueryFilter.filter(testCollection, queryParams);
assertListByValues(result, asList("aaa", "hmm", "hmm"));
assertListByIds(result, asList(4L, 5L, 6L));
queryParams = new QueryParams(2, 3, order);
result = InMemoryQueryFilter.filter(testCollection, queryParams);
assertListByValues(result, asList("nullRecord", "something", "test"));
assertListByIds(result, asList(null, 3L, 2L));
}
use of org.motechproject.mds.util.Order in project motech by motech.
the class InMemoryQueryFilter method filter.
/**
* Filters the provided collection based on the provided query params.
* @param objects the collection to filter
* @param queryParams the query params with the ordering and paging information to apply
* @param <T> the type of the filtered collection
* @return the filtered, ordered list of objects from the original collection
*/
public static <T> List<T> filter(Collection<T> objects, QueryParams queryParams) {
// if no ordering in the params the use ID, since we must always order here somehow
// given that collection does not have any order, paging it would not make sense
List<Order> orderList = new ArrayList<>(queryParams.getOrderList());
// we always add the order on the ID field as the final one, to keep the results consistent
if (!queryParams.containsOrderOnField(Constants.Util.ID_FIELD_NAME)) {
orderList.add(new Order(Constants.Util.ID_FIELD_NAME, Order.Direction.ASC));
}
List<T> result = order(objects, orderList);
// paginate if required
if (queryParams.isPagingSet()) {
result = paginate(result, queryParams.getPage(), queryParams.getPageSize());
}
return result;
}
use of org.motechproject.mds.util.Order in project motech by motech.
the class InMemoryQueryFilter method order.
/**
* Orders the provided collection using the provided ordering information.
* @param collection the collection to order
* @param orderList list of orders that should be applied to the collection
* @param <T> the type of the collection to order
* @return a new list with ordered objects from the provided collection
*/
private static <T> List<T> order(Collection<T> collection, List<Order> orderList) {
List<Comparator<T>> comparatorList = new ArrayList<>();
for (Order order : orderList) {
Comparator<T> comparator = new BeanComparator<>(order.getField(), new NullComparator());
// reverse it if order is descending
if (order.getDirection() == Order.Direction.DESC) {
comparator = new ReverseComparator(comparator);
}
comparatorList.add(comparator);
}
// we use a compound comparator to chain comparators for each provided order
CompoundComparator<T> compoundComparator = new CompoundComparator<>(comparatorList.toArray(new Comparator[comparatorList.size()]));
// convert to a list and sort it
List<T> result = new ArrayList<>(collection);
Collections.sort(result, compoundComparator);
return result;
}
Aggregations