Skip to content

Commit

Permalink
fix: Always add default order as last order param in tracker exporter…
Browse files Browse the repository at this point in the history
…s [DHIS2-18659] (#19551)

* fix: Always add default order as last order param for events [DHIS2-18659]

* fix: Always add default order as last order param for enrollments [DHIS2-18659]

* fix: Always add default order as last order param for relationships [DHIS2-18659]

* fix: Always add default order as last order param for tracked entities [DHIS2-18659]
  • Loading branch information
enricocolasante authored Dec 20, 2024
1 parent 900b01c commit 6cb0d1f
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private static String orderBy(List<Order> orders) {
orderJoiner.add(
order.getField() + " " + (order.getDirection().isAscending() ? "asc" : "desc"));
}
return " order by " + orderJoiner;
return " order by " + orderJoiner + ", " + DEFAULT_ORDER;
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1612,7 +1612,7 @@ private String getOrderQuery(EventQueryParams params) {
}

if (!orderFields.isEmpty()) {
return "order by " + StringUtils.join(orderFields, ',') + " ";
return "order by " + StringUtils.join(orderFields, ',') + ", " + DEFAULT_ORDER + " ";
} else {
return "order by " + DEFAULT_ORDER + " ";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.Set;
import java.util.function.Function;
import java.util.function.LongSupplier;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import org.apache.commons.collections4.CollectionUtils;
import org.hisp.dhis.common.IdentifiableObject;
Expand Down Expand Up @@ -264,10 +265,13 @@ private <T extends IdentifiableObject> String getRelationshipEntityType(T entity

private List<Order> orderBy(
RelationshipQueryParams queryParams, CriteriaBuilder builder, Root<Relationship> root) {
List<Order> defaultOrder = orderBy(List.of(DEFAULT_ORDER), builder, root);
if (!queryParams.getOrder().isEmpty()) {
return orderBy(queryParams.getOrder(), builder, root);
return Stream.concat(
orderBy(queryParams.getOrder(), builder, root).stream(), defaultOrder.stream())
.toList();
} else {
return orderBy(List.of(DEFAULT_ORDER), builder, root);
return defaultOrder;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1007,10 +1007,10 @@ private String getQueryOrderBy(TrackedEntityQueryParams params, boolean innerOrd
}

if (!orderFields.isEmpty()) {
return "ORDER BY " + StringUtils.join(orderFields, ',') + SPACE;
return "ORDER BY " + StringUtils.join(orderFields, ',') + ", " + DEFAULT_ORDER + SPACE;
}

return "ORDER BY " + DEFAULT_ORDER + " ";
return "ORDER BY " + DEFAULT_ORDER + SPACE;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,32 @@ void shouldReturnPaginatedTrackedEntitiesGivenNonDefaultPageSizeAndTotalPages()
trackedEntityService.getTrackedEntities(params, new PageParams(3, 3, true)).getItems());
}

@Test
void shouldOrderTrackedEntitiesByInactiveAndByDefaultOrder()
throws ForbiddenException, BadRequestException, NotFoundException {
List<String> expected =
Stream.of(
get(TrackedEntity.class, "QesgJkTyTCk"),
get(TrackedEntity.class, "dUE514NMOlo"),
get(TrackedEntity.class, "mHWCacsGYYn"))
.sorted(Comparator.comparing(TrackedEntity::getId).reversed()) // reversed = desc
.map(TrackedEntity::getUid)
.toList();

TrackedEntityOperationParams params =
TrackedEntityOperationParams.builder()
.organisationUnits(orgUnit)
.orgUnitMode(SELECTED)
.trackedEntities(UID.of("mHWCacsGYYn", "QesgJkTyTCk", "dUE514NMOlo"))
.trackedEntityType(trackedEntityType)
.orderBy("inactive", SortDirection.ASC)
.build();

List<String> trackedEntities = getTrackedEntities(params);

assertEquals(expected, trackedEntities);
}

@Test
void shouldOrderTrackedEntitiesByPrimaryKeyDescByDefault()
throws ForbiddenException, BadRequestException, NotFoundException {
Expand Down Expand Up @@ -566,6 +592,28 @@ void shouldOrderTrackedEntitiesByInactiveAsc()
assertEquals(List.of("dUE514NMOlo", "QS6w44flWAf"), trackedEntities);
}

@Test
void shouldOrderEnrollmentsByStatusAndByDefaultOrder()
throws ForbiddenException, BadRequestException {
List<String> expected =
Stream.of(get(Enrollment.class, "HDWTYSYkICe"), get(Enrollment.class, "GYWSSZunTLk"))
.sorted(Comparator.comparing(Enrollment::getId).reversed()) // reversed = desc
.map(Enrollment::getUid)
.toList();

EnrollmentOperationParams operationParams =
EnrollmentOperationParams.builder()
.orgUnits(get(OrganisationUnit.class, "DiszpKrYNg8"))
.orgUnitMode(SELECTED)
.enrollments(UID.of("HDWTYSYkICe", "GYWSSZunTLk"))
.orderBy("status", SortDirection.DESC)
.build();

List<String> actual = getEnrollments(operationParams);

assertEquals(expected, actual);
}

@Test
void shouldReturnPaginatedEnrollmentsGivenNonDefaultPageSize()
throws ForbiddenException, BadRequestException {
Expand Down Expand Up @@ -677,6 +725,29 @@ void shouldOrderEnrollmentsByEnrolledAtDesc() throws ForbiddenException, BadRequ
assertEquals(List.of("TvctPPhpD8z", "nxP7UnKhomJ"), enrollments);
}

@Test
void shouldOrderEventsByStatusAndByDefaultOrder() throws ForbiddenException, BadRequestException {
List<String> expected =
Stream.of(
get(Event.class, "ck7DzdxqLqA"),
get(Event.class, "kWjSezkXHVp"),
get(Event.class, "OTmjvJDn0Fu"))
.sorted(Comparator.comparing(Event::getId).reversed()) // reversed = desc
.map(Event::getUid)
.toList();

EventOperationParams operationParams =
eventParamsBuilder
.orgUnit(get(OrganisationUnit.class, "DiszpKrYNg8"))
.events(UID.of("ck7DzdxqLqA", "kWjSezkXHVp", "OTmjvJDn0Fu"))
.orderBy("status", SortDirection.DESC)
.build();

List<String> actual = getEvents(operationParams);

assertEquals(expected, actual);
}

@Test
void shouldReturnPaginatedEventsWithNotesGivenNonDefaultPageSize()
throws ForbiddenException, BadRequestException {
Expand Down Expand Up @@ -1274,6 +1345,29 @@ void shouldOrderByFieldInAscendingOrderWhenModeDescendants(
assertEquals(List.of(firstEvent, secondEvent), events);
}

@Test
void shouldOrderRelationshipsByCreatedAtClientAndByDefaultOrder()
throws ForbiddenException, BadRequestException, NotFoundException {
Relationship oLT07jKRu9e = get(Relationship.class, "fHn74P5T3r1");
Relationship yZxjxJli9mO = get(Relationship.class, "yZxjxJli9mO");
List<String> expected =
Stream.of(oLT07jKRu9e, yZxjxJli9mO)
.sorted(Comparator.comparing(Relationship::getId).reversed()) // reversed = desc
.map(Relationship::getUid)
.toList();

RelationshipOperationParams params =
RelationshipOperationParams.builder()
.type(TrackerType.TRACKED_ENTITY)
.identifier(UID.of("dUE514NMOlo"))
.orderBy("createdAtClient", SortDirection.DESC)
.build();

List<String> relationships = getRelationships(params);

assertEquals(expected, relationships);
}

@Test
void shouldOrderRelationshipsByPrimaryKeyDescByDefault()
throws ForbiddenException, BadRequestException, NotFoundException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,22 @@
"to": {
"event": "pTzf9KYMk72"
}
},
{
"relationship": "fHn74P5T3r1",
"relationshipType": {
"idScheme": "UID",
"identifier": "TV9oB9LT3sh"
},
"createdAtClient": "2018-11-01T13:24:37.118",
"bidirectional": false,
"deleted": false,
"from": {
"trackedEntity": "dUE514NMOlo"
},
"to": {
"event": "D9PbzJY8bJM"
}
}
],
"username": "system-process"
Expand Down

0 comments on commit 6cb0d1f

Please sign in to comment.