Search in sources :

Example 1 with OrderParam

use of org.hisp.dhis.webapi.controller.event.mapper.OrderParam in project dhis2-core by dhis2.

the class HibernateTrackedEntityInstanceStore method getQueryOrderBy.

/**
 * Generates the ORDER BY clause. This clause is used both in the subquery
 * and main query. When using it in the subquery, we want to make sure we
 * get the right teis. When we order in the main query, it's to make sure we
 * return the results in the correct order, since order might be mixed after
 * GROUP BY.
 *
 * @param innerOrder indicates whether this is the subquery order by or main
 *        query order by
 * @param params
 * @param isGridQuery indicates whether this is used for grid query or not.
 * @return a SQL ORDER BY clause.
 */
private String getQueryOrderBy(boolean innerOrder, TrackedEntityInstanceQueryParams params, boolean isGridQuery) {
    if (params.getOrders() != null && (!isGridQuery || (params.getAttributes() != null && !params.getAttributes().isEmpty()))) {
        ArrayList<String> orderFields = new ArrayList<>();
        for (OrderParam order : params.getOrders()) {
            if (isStaticColumn(order.getField())) {
                String columnName = TrackedEntityInstanceQueryParams.OrderColumn.getColumn(order.getField());
                orderFields.add(columnName + " " + order.getDirection());
            } else {
                extractDynamicOrderField(innerOrder, params, orderFields, order);
            }
        }
        if (!orderFields.isEmpty()) {
            return "ORDER BY " + StringUtils.join(orderFields, ',') + SPACE;
        }
    }
    if (params.getAttributesAndFilters().stream().noneMatch(qi -> qi.hasFilter() && qi.isUnique())) {
        return "ORDER BY TEI.trackedentityinstanceid ASC ";
    } else {
        return "";
    }
}
Also used : OrderParam(org.hisp.dhis.webapi.controller.event.mapper.OrderParam) ArrayList(java.util.ArrayList) TextUtils.getQuotedCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString) TextUtils.getCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getCommaDelimitedString) DateUtils.getLongGmtDateString(org.hisp.dhis.util.DateUtils.getLongGmtDateString) DateUtils.getMediumDateString(org.hisp.dhis.util.DateUtils.getMediumDateString)

Example 2 with OrderParam

use of org.hisp.dhis.webapi.controller.event.mapper.OrderParam in project dhis2-core by dhis2.

the class TrackedEntityInstanceStoreTest method testQueryOrderByIdInsteadOfCreatedDate.

@Test
void testQueryOrderByIdInsteadOfCreatedDate() {
    LocalDate now = LocalDate.now();
    Date today = Date.from(now.atStartOfDay().toInstant(ZoneOffset.UTC));
    Date tomorrow = Date.from(now.plusDays(1).atStartOfDay().toInstant(ZoneOffset.UTC));
    teiA.setCreated(tomorrow);
    teiB.setCreated(today);
    teiStore.save(teiA);
    teiStore.save(teiB);
    programInstanceService.enrollTrackedEntityInstance(teiB, prA, new Date(), new Date(), ouB);
    // Get all
    TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
    OrderParam orderParam = OrderParam.builder().field(TrackedEntityInstanceQueryParams.CREATED_ID).direction(OrderParam.SortDirection.ASC).build();
    params.setOrders(Lists.newArrayList(orderParam));
    List<TrackedEntityInstance> teis = teiStore.getTrackedEntityInstances(params);
    assertEquals(2, teis.size());
    assertEquals(teiA.getUid(), teis.get(0).getUid());
    assertEquals(teiB.getUid(), teis.get(1).getUid());
}
Also used : OrderParam(org.hisp.dhis.webapi.controller.event.mapper.OrderParam) LocalDate(java.time.LocalDate) Date(java.util.Date) LocalDate(java.time.LocalDate) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 3 with OrderParam

use of org.hisp.dhis.webapi.controller.event.mapper.OrderParam in project dhis2-core by dhis2.

the class JdbcEventStore method getOrderQuery.

private String getOrderQuery(EventSearchParams params) {
    ArrayList<String> orderFields = new ArrayList<>();
    if (params.getGridOrders() != null) {
        for (OrderParam order : params.getGridOrders()) {
            Set<QueryItem> items = params.getDataElements();
            for (QueryItem item : items) {
                if (order.getField().equals(item.getItemId())) {
                    orderFields.add(order.getField() + " " + order.getDirection());
                    break;
                }
            }
        }
    }
    if (params.getOrders() != null) {
        for (OrderParam order : params.getOrders()) {
            if (QUERY_PARAM_COL_MAP.containsKey(order.getField())) {
                String orderText = QUERY_PARAM_COL_MAP.get(order.getField());
                orderText += " " + (order.getDirection().isAscending() ? "asc" : "desc");
                orderFields.add(orderText);
            }
        }
    }
    if (!orderFields.isEmpty()) {
        return "order by " + StringUtils.join(orderFields, ',') + " ";
    } else {
        return "order by psi_lastupdated desc ";
    }
}
Also used : QueryItem(org.hisp.dhis.common.QueryItem) OrderParam(org.hisp.dhis.webapi.controller.event.mapper.OrderParam) ArrayList(java.util.ArrayList) DateUtils.getMediumDateString(org.hisp.dhis.util.DateUtils.getMediumDateString) TextUtils.getQuotedCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString) DateUtils.getLongGmtDateString(org.hisp.dhis.util.DateUtils.getLongGmtDateString)

Example 4 with OrderParam

use of org.hisp.dhis.webapi.controller.event.mapper.OrderParam in project dhis2-core by dhis2.

the class TrackedEntityCriteriaMapperTest method verifyCriteriaMapping.

@Test
void verifyCriteriaMapping() {
    TrackedEntityInstanceCriteria criteria = new TrackedEntityInstanceCriteria();
    criteria.setQuery("query-test");
    criteria.setAttribute(newHashSet(attrD.getUid(), attrE.getUid()));
    criteria.setFilter(newHashSet(filtF.getUid(), filtG.getUid()));
    criteria.setOu(organisationUnit.getUid());
    criteria.setOuMode(OrganisationUnitSelectionMode.DESCENDANTS);
    criteria.setProgram(programA.getUid());
    criteria.setProgramStatus(ProgramStatus.ACTIVE);
    criteria.setFollowUp(true);
    criteria.setLastUpdatedStartDate(getDate(2019, 1, 1));
    criteria.setLastUpdatedEndDate(getDate(2020, 1, 1));
    criteria.setLastUpdatedDuration("20");
    criteria.setProgramEnrollmentStartDate(getDate(2019, 8, 5));
    criteria.setProgramEnrollmentEndDate(getDate(2020, 8, 5));
    criteria.setProgramIncidentStartDate(getDate(2019, 5, 5));
    criteria.setProgramIncidentEndDate(getDate(2020, 5, 5));
    criteria.setTrackedEntityType(trackedEntityTypeA.getUid());
    criteria.setEventStatus(EventStatus.COMPLETED);
    criteria.setEventStartDate(getDate(2019, 7, 7));
    criteria.setEventEndDate(getDate(2020, 7, 7));
    criteria.setAssignedUserMode(AssignedUserSelectionMode.PROVIDED);
    criteria.setAssignedUser(userIds);
    criteria.setSkipMeta(true);
    criteria.setPage(1);
    criteria.setPageSize(50);
    criteria.setTotalPages(false);
    criteria.setSkipPaging(false);
    criteria.setIncludeDeleted(true);
    criteria.setIncludeAllAttributes(true);
    criteria.setOrder(Collections.singletonList(OrderCriteria.of("created", OrderParam.SortDirection.ASC)));
    final TrackedEntityInstanceQueryParams queryParams = trackedEntityCriteriaMapper.map(criteria);
    assertThat(queryParams.getQuery().getFilter(), is("query-test"));
    assertThat(queryParams.getQuery().getOperator(), is(QueryOperator.EQ));
    assertThat(queryParams.getProgram(), is(programA));
    assertThat(queryParams.getTrackedEntityType(), is(trackedEntityTypeA));
    assertThat(queryParams.getOrganisationUnits(), hasSize(1));
    assertThat(queryParams.getOrganisationUnits().iterator().next(), is(organisationUnit));
    assertThat(queryParams.getAttributes(), hasSize(2));
    assertTrue(queryParams.getAttributes().stream().anyMatch(a -> a.getItem().getUid().equals(attrD.getUid())));
    assertTrue(queryParams.getAttributes().stream().anyMatch(a -> a.getItem().getUid().equals(attrE.getUid())));
    assertThat(queryParams.getFilters(), hasSize(2));
    assertTrue(queryParams.getFilters().stream().anyMatch(a -> a.getItem().getUid().equals(filtF.getUid())));
    assertTrue(queryParams.getFilters().stream().anyMatch(a -> a.getItem().getUid().equals(filtG.getUid())));
    assertThat(queryParams.getPageSizeWithDefault(), is(50));
    assertThat(queryParams.getPageSize(), is(50));
    assertThat(queryParams.getPage(), is(1));
    assertThat(queryParams.isTotalPages(), is(false));
    assertThat(queryParams.getProgramStatus(), is(ProgramStatus.ACTIVE));
    assertThat(queryParams.getFollowUp(), is(true));
    assertThat(queryParams.getLastUpdatedStartDate(), is(criteria.getLastUpdatedStartDate()));
    assertThat(queryParams.getLastUpdatedEndDate(), is(criteria.getLastUpdatedEndDate()));
    assertThat(queryParams.getProgramEnrollmentStartDate(), is(criteria.getProgramEnrollmentStartDate()));
    assertThat(queryParams.getProgramEnrollmentEndDate(), is(DateUtils.addDays(criteria.getProgramEnrollmentEndDate(), 1)));
    assertThat(queryParams.getProgramIncidentStartDate(), is(criteria.getProgramIncidentStartDate()));
    assertThat(queryParams.getProgramIncidentEndDate(), is(DateUtils.addDays(criteria.getProgramIncidentEndDate(), 1)));
    assertThat(queryParams.getEventStatus(), is(EventStatus.COMPLETED));
    assertThat(queryParams.getEventStartDate(), is(criteria.getEventStartDate()));
    assertThat(queryParams.getEventEndDate(), is(criteria.getEventEndDate()));
    assertThat(queryParams.getAssignedUserSelectionMode(), is(AssignedUserSelectionMode.PROVIDED));
    assertTrue(queryParams.getAssignedUsers().stream().anyMatch(u -> u.equals(userId1)));
    assertTrue(queryParams.getAssignedUsers().stream().anyMatch(u -> u.equals(userId2)));
    assertThat(queryParams.getAssignedUsers().stream().anyMatch(u -> u.equals(userId3)), is(false));
    assertThat(queryParams.isIncludeDeleted(), is(true));
    assertThat(queryParams.isIncludeAllAttributes(), is(true));
    assertTrue(queryParams.getOrders().stream().anyMatch(orderParam -> orderParam.equals(OrderParam.builder().field("created").direction(OrderParam.SortDirection.ASC).build())));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) TrackedEntityTypeService(org.hisp.dhis.trackedentity.TrackedEntityTypeService) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) Autowired(org.springframework.beans.factory.annotation.Autowired) OrganisationUnitService(org.hisp.dhis.organisationunit.OrganisationUnitService) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) Program(org.hisp.dhis.program.Program) TrackedEntityAttributeService(org.hisp.dhis.trackedentity.TrackedEntityAttributeService) HashSet(java.util.HashSet) OrderCriteria(org.hisp.dhis.webapi.controller.event.webrequest.OrderCriteria) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) Matchers.hasSize(org.hamcrest.Matchers.hasSize) User(org.hisp.dhis.user.User) DhisWebSpringTest(org.hisp.dhis.webapi.DhisWebSpringTest) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TrackedEntityCriteriaMapper(org.hisp.dhis.webapi.controller.event.mapper.TrackedEntityCriteriaMapper) UserService(org.hisp.dhis.user.UserService) OrderParam(org.hisp.dhis.webapi.controller.event.mapper.OrderParam) AccessStringHelper(org.hisp.dhis.security.acl.AccessStringHelper) OrganisationUnitSelectionMode(org.hisp.dhis.common.OrganisationUnitSelectionMode) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) QueryOperator(org.hisp.dhis.common.QueryOperator) TrackedEntityInstanceCriteria(org.hisp.dhis.webapi.controller.event.webrequest.TrackedEntityInstanceCriteria) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) EventStatus(org.hisp.dhis.event.EventStatus) Sets(com.google.common.collect.Sets) DateUtils(org.apache.commons.lang3.time.DateUtils) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Test(org.junit.jupiter.api.Test) ProgramStatus(org.hisp.dhis.program.ProgramStatus) CurrentUserService(org.hisp.dhis.user.CurrentUserService) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AssignedUserSelectionMode(org.hisp.dhis.common.AssignedUserSelectionMode) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) CodeGenerator(org.hisp.dhis.common.CodeGenerator) ProgramService(org.hisp.dhis.program.ProgramService) Collections(java.util.Collections) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityInstanceCriteria(org.hisp.dhis.webapi.controller.event.webrequest.TrackedEntityInstanceCriteria) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) DhisWebSpringTest(org.hisp.dhis.webapi.DhisWebSpringTest) Test(org.junit.jupiter.api.Test)

Aggregations

OrderParam (org.hisp.dhis.webapi.controller.event.mapper.OrderParam)4 ArrayList (java.util.ArrayList)2 TextUtils.getQuotedCommaDelimitedString (org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString)2 DateUtils.getLongGmtDateString (org.hisp.dhis.util.DateUtils.getLongGmtDateString)2 DateUtils.getMediumDateString (org.hisp.dhis.util.DateUtils.getMediumDateString)2 Test (org.junit.jupiter.api.Test)2 Sets (com.google.common.collect.Sets)1 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)1 LocalDate (java.time.LocalDate)1 Collections (java.util.Collections)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 DateUtils (org.apache.commons.lang3.time.DateUtils)1 CoreMatchers.is (org.hamcrest.CoreMatchers.is)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers.hasSize (org.hamcrest.Matchers.hasSize)1 DhisSpringTest (org.hisp.dhis.DhisSpringTest)1 AssignedUserSelectionMode (org.hisp.dhis.common.AssignedUserSelectionMode)1 CodeGenerator (org.hisp.dhis.common.CodeGenerator)1 IllegalQueryException (org.hisp.dhis.common.IllegalQueryException)1