Search in sources :

Example 16 with Order

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());
}
Also used : Order(org.motechproject.mds.util.Order) QueryParams(org.motechproject.mds.query.QueryParams) Record(org.motechproject.mds.testutil.records.Record) Test(org.junit.Test)

Example 17 with Order

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));
}
Also used : Order(org.motechproject.mds.util.Order) Record(org.motechproject.mds.testutil.records.Record) Test(org.junit.Test)

Example 18 with Order

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));
}
Also used : Order(org.motechproject.mds.util.Order) Record(org.motechproject.mds.testutil.records.Record) Test(org.junit.Test)

Example 19 with Order

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;
}
Also used : Order(org.motechproject.mds.util.Order) ArrayList(java.util.ArrayList)

Example 20 with Order

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;
}
Also used : Order(org.motechproject.mds.util.Order) NullComparator(org.apache.commons.collections.comparators.NullComparator) ArrayList(java.util.ArrayList) BeanComparator(org.apache.commons.beanutils.BeanComparator) ReverseComparator(org.apache.commons.collections.comparators.ReverseComparator) CompoundComparator(org.springframework.util.comparator.CompoundComparator) NullComparator(org.apache.commons.collections.comparators.NullComparator) ReverseComparator(org.apache.commons.collections.comparators.ReverseComparator) CompoundComparator(org.springframework.util.comparator.CompoundComparator) BeanComparator(org.apache.commons.beanutils.BeanComparator) Comparator(java.util.Comparator)

Aggregations

Order (org.motechproject.mds.util.Order)23 QueryParams (org.motechproject.mds.query.QueryParams)14 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)6 Record (org.motechproject.mds.testutil.records.Record)6 DateTime (org.joda.time.DateTime)4 Arrays.asList (java.util.Arrays.asList)3 List (java.util.List)3 EmailRecordSearchCriteria (org.motechproject.email.builder.EmailRecordSearchCriteria)2 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 LocalDateTime (java.time.LocalDateTime)1 Collections.singletonList (java.util.Collections.singletonList)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Query (javax.jdo.Query)1