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-17671] (#19551) (#19569)
  • Loading branch information
enricocolasante authored Dec 30, 2024
1 parent 923878a commit e841f7d
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,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 @@ -1532,7 +1532,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 @@ -32,6 +32,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 javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
Expand Down Expand Up @@ -245,10 +246,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 @@ -987,10 +987,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 @@ -211,6 +211,33 @@ 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(Set.of(orgUnit.getUid()))
.orgUnitMode(SELECTED)
.trackedEntityUids(Set.of("mHWCacsGYYn", "QesgJkTyTCk", "dUE514NMOlo"))
.trackedEntityTypeUid(trackedEntityType.getUid())
.orderBy("inactive", SortDirection.ASC)
.user(importUser)
.build();

List<String> trackedEntities = getTrackedEntities(params);

assertEquals(expected, trackedEntities);
}

@Test
void shouldOrderTrackedEntitiesByPrimaryKeyDescByDefault()
throws ForbiddenException, BadRequestException, NotFoundException {
Expand Down Expand Up @@ -586,6 +613,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()
.orgUnitUids(Set.of("DiszpKrYNg8"))
.orgUnitMode(SELECTED)
.enrollmentUids(Set.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 @@ -700,6 +749,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
.orgUnitUid("DiszpKrYNg8")
.events(Set.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 @@ -1308,6 +1380,29 @@ void shouldOrderByFieldInAscendingOrderWhenModeDescendants(
assertEquals(List.of(firstEvent, secondEvent), events);
}

@Test
void shouldOrderRelationshipsByCreatedAtClientAndByDefaultOrder()
throws ForbiddenException, 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("dUE514NMOlo")
.orderBy("createdAtClient", SortDirection.DESC)
.build();

List<String> relationships = getRelationships(params);

assertEquals(expected, relationships);
}

@Test
void shouldOrderRelationshipsByPrimaryKeyDescByDefault()
throws ForbiddenException, NotFoundException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,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 e841f7d

Please sign in to comment.