Search in sources :

Example 1 with Pagination

use of org.hisp.dhis.query.Pagination in project dhis2-core by dhis2.

the class MessageConversationController method getEntityList.

@Override
@SuppressWarnings("unchecked")
protected List<org.hisp.dhis.message.MessageConversation> getEntityList(WebMetadata metadata, WebOptions options, List<String> filters, List<Order> orders) throws QueryParserException {
    List<org.hisp.dhis.message.MessageConversation> messageConversations;
    if (options.getOptions().containsKey("query")) {
        messageConversations = Lists.newArrayList(manager.filter(getEntityClass(), options.getOptions().get("query")));
    } else {
        messageConversations = new ArrayList<>(messageService.getMessageConversations());
    }
    Query query = queryService.getQueryFromUrl(getEntityClass(), filters, orders, new Pagination(), options.getRootJunction());
    query.setDefaultOrder();
    query.setDefaults(Defaults.valueOf(options.get("defaults", DEFAULTS)));
    query.setObjects(messageConversations);
    messageConversations = (List<org.hisp.dhis.message.MessageConversation>) queryService.query(query);
    if (options.get("queryString") != null) {
        String queryOperator = "token";
        if (options.get("queryOperator") != null) {
            queryOperator = options.get("queryOperator");
        }
        List<String> queryFilter = Arrays.asList("subject:" + queryOperator + ":" + options.get("queryString"), "messages.text:" + queryOperator + ":" + options.get("queryString"), "messages.sender.displayName:" + queryOperator + ":" + options.get("queryString"));
        Query subQuery = queryService.getQueryFromUrl(getEntityClass(), queryFilter, Collections.emptyList(), new Pagination(), Junction.Type.OR);
        subQuery.setObjects(messageConversations);
        messageConversations = (List<org.hisp.dhis.message.MessageConversation>) queryService.query(subQuery);
    }
    int count = messageConversations.size();
    Query paginatedQuery = queryService.getQueryFromUrl(getEntityClass(), Collections.emptyList(), Collections.emptyList(), getPaginationData(options), options.getRootJunction());
    paginatedQuery.setObjects(messageConversations);
    messageConversations = (List<org.hisp.dhis.message.MessageConversation>) queryService.query(paginatedQuery);
    if (options.hasPaging()) {
        Pager pager = new Pager(options.getPage(), count, options.getPageSize());
        metadata.setPager(pager);
    }
    return messageConversations;
}
Also used : Pagination(org.hisp.dhis.query.Pagination) Query(org.hisp.dhis.query.Query) MessageConversation(org.hisp.dhis.webapi.webdomain.MessageConversation) Pager(org.hisp.dhis.common.Pager)

Example 2 with Pagination

use of org.hisp.dhis.query.Pagination in project dhis2-core by dhis2.

the class PaginationUtilsTest method verifyIgnoreNegativePage.

@Test
void verifyIgnoreNegativePage() {
    Map<String, String> options = new HashMap<>();
    options.put(WebOptions.PAGING, "true");
    options.put(WebOptions.PAGE, "-2");
    options.put(WebOptions.PAGE_SIZE, "200");
    WebOptions webOptions = new WebOptions(options);
    Pagination paginationData = PaginationUtils.getPaginationData(webOptions);
    assertThat(paginationData.getFirstResult(), is(0));
    assertThat(paginationData.getSize(), is(200));
}
Also used : Pagination(org.hisp.dhis.query.Pagination) HashMap(java.util.HashMap) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) Test(org.junit.jupiter.api.Test)

Example 3 with Pagination

use of org.hisp.dhis.query.Pagination in project dhis2-core by dhis2.

the class PaginationUtilsTest method verifyPaginationIsDisabled.

@Test
void verifyPaginationIsDisabled() {
    Map<String, String> options = new HashMap<>();
    options.put(WebOptions.PAGING, "false");
    WebOptions webOptions = new WebOptions(options);
    Pagination paginationData = PaginationUtils.getPaginationData(webOptions);
    assertThat(paginationData.getFirstResult(), is(0));
    assertThat(paginationData.getSize(), is(0));
    assertThat(paginationData.hasPagination(), is(false));
}
Also used : Pagination(org.hisp.dhis.query.Pagination) HashMap(java.util.HashMap) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) Test(org.junit.jupiter.api.Test)

Example 4 with Pagination

use of org.hisp.dhis.query.Pagination in project dhis2-core by dhis2.

the class UserController method makeQuery.

private Query makeQuery(WebOptions options, List<String> filters, List<Order> orders, UserQueryParams params) {
    Pagination pagination = CollectionUtils.isEmpty(filters) ? new Pagination() : getPaginationData(options);
    List<String> ordersAsString = (orders == null) ? null : orders.stream().map(Order::toOrderString).collect(Collectors.toList());
    /*
         * Keep the memory query on the result
         */
    Query query = queryService.getQueryFromUrl(getEntityClass(), filters, orders, pagination, options.getRootJunction());
    // Fetches all users if there are no query, i.e only filters...
    List<User> users = userService.getUsers(params, ordersAsString);
    query.setObjects(users);
    query.setDefaults(Defaults.valueOf(options.get("defaults", DEFAULTS)));
    query.setDefaultOrder();
    return query;
}
Also used : Order(org.hisp.dhis.query.Order) Pagination(org.hisp.dhis.query.Pagination) CurrentUser(org.hisp.dhis.user.CurrentUser) User(org.hisp.dhis.user.User) Query(org.hisp.dhis.query.Query)

Example 5 with Pagination

use of org.hisp.dhis.query.Pagination in project dhis2-core by dhis2.

the class PaginationUtilsTest method verifyPaginationStartsAtZero.

@Test
void verifyPaginationStartsAtZero() {
    Map<String, String> options = new HashMap<>();
    options.put(WebOptions.PAGING, "true");
    options.put(WebOptions.PAGE, "1");
    options.put(WebOptions.PAGE_SIZE, "20");
    WebOptions webOptions = new WebOptions(options);
    Pagination paginationData = PaginationUtils.getPaginationData(webOptions);
    assertThat(paginationData.getFirstResult(), is(0));
    assertThat(paginationData.getSize(), is(20));
}
Also used : Pagination(org.hisp.dhis.query.Pagination) HashMap(java.util.HashMap) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) Test(org.junit.jupiter.api.Test)

Aggregations

Pagination (org.hisp.dhis.query.Pagination)6 HashMap (java.util.HashMap)4 WebOptions (org.hisp.dhis.webapi.webdomain.WebOptions)4 Test (org.junit.jupiter.api.Test)4 Query (org.hisp.dhis.query.Query)2 Pager (org.hisp.dhis.common.Pager)1 Order (org.hisp.dhis.query.Order)1 CurrentUser (org.hisp.dhis.user.CurrentUser)1 User (org.hisp.dhis.user.User)1 MessageConversation (org.hisp.dhis.webapi.webdomain.MessageConversation)1