Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Sort data elements and event fields as single unit [DHIS2-18012] #19512

Merged
merged 9 commits into from
Dec 19, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,21 @@ public class HibernateEventChangeLogStore {
private static final String COLUMN_CHANGELOG_USER = "ecl.createdByUsername";
private static final String COLUMN_CHANGELOG_DATA_ELEMENT = "d.uid";
private static final String COLUMN_CHANGELOG_FIELD = "ecl.eventField";

private static final String ORDER_CHANGE_EXPRESSION =
"CONCAT(COALESCE(d.formName, ''), COALESCE(" + COLUMN_CHANGELOG_FIELD + ", ''))";
private static final String DEFAULT_ORDER =
COLUMN_CHANGELOG_CREATED + " " + SortDirection.DESC.getValue();

/**
* Event change logs can be ordered by given fields which correspond to fields on {@link
* EventChangeLog}. Maps fields to DB columns. The order implementation for change logs is
* different from other tracker exporters {@link EventChangeLog} is the view which is already
* returned from the service/store. Tracker exporter services return a representation we have to
* map to a view model. This mapping is not necessary for change logs.
* EventChangeLog}. Maps fields to DB columns, except when sorting by 'change'. In that case we
* need to sort by concatenation, to treat the dataElement and eventField as a single entity.
*/
private static final Map<String, String> ORDERABLE_FIELDS =
Map.ofEntries(
entry("createdAt", COLUMN_CHANGELOG_CREATED),
entry("username", COLUMN_CHANGELOG_USER),
entry("dataElement", COLUMN_CHANGELOG_DATA_ELEMENT),
entry("field", COLUMN_CHANGELOG_FIELD));
entry("change", ORDER_CHANGE_EXPRESSION));

private static final Map<Pair<String, Class<?>>, String> FILTERABLE_FIELDS =
Map.ofEntries(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@
"lastUpdated": "2015-03-31T11:22:51.642",
"name": "Height in cm",
"shortName": "Height in cm",
"formName": "Height in cm",
"url": "",
"valueType": "NUMBER"
},
Expand All @@ -549,6 +550,7 @@
"lastUpdated": "2015-03-31T11:22:51.642",
"name": "Height in mm",
"shortName": "Height in mm",
"formName": "Height in mm",
"url": "",
"valueType": "NUMBER"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,55 +153,58 @@ void shouldSortChangeLogsWhenOrderingByCreatedAtAsc()
void shouldSortChangeLogsWhenOrderingByDataElementAsc()
throws ForbiddenException, NotFoundException {
EventChangeLogOperationParams params =
EventChangeLogOperationParams.builder().orderBy("dataElement", SortDirection.ASC).build();
EventChangeLogOperationParams.builder().orderBy("change", SortDirection.ASC).build();
Event event = getEvent("kWjSezkXHVp");

updateDataValues(event, "GieVkTxp4HH", "20", "25");
updateDataValues(event, "GieVkTxp4HG", "20");

List<EventChangeLog> changeLogs =
getDataElementChangeLogs(
eventChangeLogService.getEventChangeLog(
UID.of("kWjSezkXHVp"), params, defaultPageParams));
eventChangeLogService
.getEventChangeLog(UID.of("kWjSezkXHVp"), params, defaultPageParams)
.getItems();

assertNumberOfChanges(5, changeLogs);
assertNumberOfChanges(7, changeLogs);
assertAll(
() -> assertDataElementUpdate("GieVkTxp4HG", "10", "20", changeLogs.get(0)),
() -> assertDataElementCreate("GieVkTxp4HG", "10", changeLogs.get(1)),
() -> assertDataElementUpdate("GieVkTxp4HH", "20", "25", changeLogs.get(2)),
() -> assertDataElementUpdate("GieVkTxp4HH", "15", "20", changeLogs.get(3)),
() -> assertDataElementCreate("GieVkTxp4HH", "15", changeLogs.get(4)));
() -> assertDataElementUpdate("GieVkTxp4HH", "20", "25", changeLogs.get(0)),
() -> assertDataElementUpdate("GieVkTxp4HH", "15", "20", changeLogs.get(1)),
() -> assertDataElementCreate("GieVkTxp4HH", "15", changeLogs.get(2)),
() -> assertDataElementUpdate("GieVkTxp4HG", "10", "20", changeLogs.get(3)),
() -> assertDataElementCreate("GieVkTxp4HG", "10", changeLogs.get(4)),
() -> assertFieldCreate("occurredAt", "2022-04-22 06:00:38.343", changeLogs.get(5)),
() -> assertFieldCreate("scheduledAt", "2022-04-26 06:00:34.323", changeLogs.get(6)));
}

@Test
void shouldSortChangeLogsWhenOrderingByDataElementDesc()
throws ForbiddenException, NotFoundException {
void shouldSortChangeLogsWhenOrderingByChangeDesc() throws ForbiddenException, NotFoundException {
EventChangeLogOperationParams params =
EventChangeLogOperationParams.builder().orderBy("dataElement", SortDirection.DESC).build();
EventChangeLogOperationParams.builder().orderBy("change", SortDirection.DESC).build();
Event event = getEvent("kWjSezkXHVp");

updateDataValues(event, "GieVkTxp4HH", "20", "25");
updateDataValues(event, "GieVkTxp4HG", "20");

List<EventChangeLog> changeLogs =
getDataElementChangeLogs(
eventChangeLogService.getEventChangeLog(
UID.of("kWjSezkXHVp"), params, defaultPageParams));
eventChangeLogService
.getEventChangeLog(UID.of("kWjSezkXHVp"), params, defaultPageParams)
.getItems();

assertNumberOfChanges(5, changeLogs);
assertNumberOfChanges(7, changeLogs);
assertAll(
() -> assertDataElementUpdate("GieVkTxp4HH", "20", "25", changeLogs.get(0)),
() -> assertDataElementUpdate("GieVkTxp4HH", "15", "20", changeLogs.get(1)),
() -> assertDataElementCreate("GieVkTxp4HH", "15", changeLogs.get(2)),
() -> assertDataElementUpdate("GieVkTxp4HG", "10", "20", changeLogs.get(3)),
() -> assertDataElementCreate("GieVkTxp4HG", "10", changeLogs.get(4)));
() -> assertFieldCreate("scheduledAt", "2022-04-26 06:00:34.323", changeLogs.get(0)),
() -> assertFieldCreate("occurredAt", "2022-04-22 06:00:38.343", changeLogs.get(1)),
() -> assertDataElementUpdate("GieVkTxp4HG", "10", "20", changeLogs.get(2)),
() -> assertDataElementCreate("GieVkTxp4HG", "10", changeLogs.get(3)),
() -> assertDataElementUpdate("GieVkTxp4HH", "20", "25", changeLogs.get(4)),
() -> assertDataElementUpdate("GieVkTxp4HH", "15", "20", changeLogs.get(5)),
() -> assertDataElementCreate("GieVkTxp4HH", "15", changeLogs.get(6)));
}

@Test
void shouldSortChangeLogsWhenOrderingByFieldAsc()
void shouldSortChangeLogsWhenOrderingByChangeAscAndChangesOnlyToEventFields()
throws ForbiddenException, NotFoundException, IOException {
EventChangeLogOperationParams params =
EventChangeLogOperationParams.builder().orderBy("field", SortDirection.ASC).build();
EventChangeLogOperationParams.builder().orderBy("change", SortDirection.ASC).build();
UID event = UID.of("QRYjLTiJTrA");

LocalDateTime currentTime = LocalDateTime.now();
Expand Down Expand Up @@ -232,10 +235,10 @@ void shouldSortChangeLogsWhenOrderingByFieldAsc()
}

@Test
void shouldSortChangeLogsWhenOrderingByFieldDesc()
void shouldSortChangeLogsWhenOrderingByChangeDescAndChangesOnlyToEventFields()
throws ForbiddenException, NotFoundException, IOException {
EventChangeLogOperationParams params =
EventChangeLogOperationParams.builder().orderBy("field", SortDirection.DESC).build();
EventChangeLogOperationParams.builder().orderBy("change", SortDirection.DESC).build();
UID event = UID.of("QRYjLTiJTrA");

LocalDateTime currentTime = LocalDateTime.now();
Expand Down
Loading