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 "";
}
}
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());
}
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 ";
}
}
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())));
}
Aggregations