diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/event/HibernateEventChangeLogStore.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/event/HibernateEventChangeLogStore.java index 7f762d67f06c..53766ba38842 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/event/HibernateEventChangeLogStore.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/event/HibernateEventChangeLogStore.java @@ -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 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>, String> FILTERABLE_FIELDS = Map.ofEntries( diff --git a/dhis-2/dhis-support/dhis-support-test/src/main/resources/tracker/simple_metadata.json b/dhis-2/dhis-support/dhis-support-test/src/main/resources/tracker/simple_metadata.json index ef773fcfb09e..2b61abbb750d 100644 --- a/dhis-2/dhis-support/dhis-support-test/src/main/resources/tracker/simple_metadata.json +++ b/dhis-2/dhis-support/dhis-support-test/src/main/resources/tracker/simple_metadata.json @@ -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" }, @@ -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" } diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/event/OrderAndFilterEventChangeLogTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/event/OrderAndFilterEventChangeLogTest.java index 943e3318264c..c01879abbcb2 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/event/OrderAndFilterEventChangeLogTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/event/OrderAndFilterEventChangeLogTest.java @@ -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 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 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(); @@ -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();