From 6f7651f48bfb5741baa621c96e788de5753044b8 Mon Sep 17 00:00:00 2001 From: Marc Date: Tue, 17 Dec 2024 10:30:20 +0100 Subject: [PATCH 1/7] feat: Sort data elements and event fields as single unit [DHIS2-18012] --- .../event/HibernateEventChangeLogStore.java | 12 +++--- .../OrderAndFilterEventChangeLogTest.java | 43 ++++++++++--------- 2 files changed, 27 insertions(+), 28 deletions(-) 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..f04b04d3759e 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_DATA_ITEM_EXPRESSION = + "CONCAT(COALESCE(d.shortName, ''), 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 for dataItem. 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("dataItem", ORDER_DATA_ITEM_EXPRESSION)); private static final Map>, String> FILTERABLE_FIELDS = Map.ofEntries( 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..dc9f8efa9373 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,7 +153,7 @@ void shouldSortChangeLogsWhenOrderingByCreatedAtAsc() void shouldSortChangeLogsWhenOrderingByDataElementAsc() throws ForbiddenException, NotFoundException { EventChangeLogOperationParams params = - EventChangeLogOperationParams.builder().orderBy("dataElement", SortDirection.ASC).build(); + EventChangeLogOperationParams.builder().orderBy("dataItem", SortDirection.ASC).build(); Event event = getEvent("kWjSezkXHVp"); updateDataValues(event, "GieVkTxp4HH", "20", "25"); @@ -166,42 +166,43 @@ void shouldSortChangeLogsWhenOrderingByDataElementAsc() assertNumberOfChanges(5, 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))); } @Test - void shouldSortChangeLogsWhenOrderingByDataElementDesc() + void shouldSortChangeLogsWhenOrderingByDataItemDesc() throws ForbiddenException, NotFoundException { EventChangeLogOperationParams params = - EventChangeLogOperationParams.builder().orderBy("dataElement", SortDirection.DESC).build(); + EventChangeLogOperationParams.builder().orderBy("dataItem", 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); 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 shouldSortChangeLogsWhenOrderingByDataItemAscAndChangesOnlyToEventFields() throws ForbiddenException, NotFoundException, IOException { EventChangeLogOperationParams params = - EventChangeLogOperationParams.builder().orderBy("field", SortDirection.ASC).build(); + EventChangeLogOperationParams.builder().orderBy("dataItem", SortDirection.ASC).build(); UID event = UID.of("QRYjLTiJTrA"); LocalDateTime currentTime = LocalDateTime.now(); @@ -232,10 +233,10 @@ void shouldSortChangeLogsWhenOrderingByFieldAsc() } @Test - void shouldSortChangeLogsWhenOrderingByFieldDesc() + void shouldSortChangeLogsWhenOrderingByDataItemDescAndChangesOnlyToEventFields() throws ForbiddenException, NotFoundException, IOException { EventChangeLogOperationParams params = - EventChangeLogOperationParams.builder().orderBy("field", SortDirection.DESC).build(); + EventChangeLogOperationParams.builder().orderBy("dataItem", SortDirection.DESC).build(); UID event = UID.of("QRYjLTiJTrA"); LocalDateTime currentTime = LocalDateTime.now(); From 9fc96ac0355e9ad755bb9f795600dfb9ff184ea5 Mon Sep 17 00:00:00 2001 From: Marc Date: Tue, 17 Dec 2024 10:48:14 +0100 Subject: [PATCH 2/7] feat: Sort data elements and event fields as single unit [DHIS2-18012] --- .../event/OrderAndFilterEventChangeLogTest.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) 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 dc9f8efa9373..532eda938b0c 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 @@ -160,17 +160,19 @@ void shouldSortChangeLogsWhenOrderingByDataElementAsc() 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))); + () -> 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 @@ -188,6 +190,7 @@ void shouldSortChangeLogsWhenOrderingByDataItemDesc() .getEventChangeLog(UID.of("kWjSezkXHVp"), params, defaultPageParams) .getItems(); + assertNumberOfChanges(7, changeLogs); assertAll( () -> assertFieldCreate("scheduledAt", "2022-04-26 06:00:34.323", changeLogs.get(0)), () -> assertFieldCreate("occurredAt", "2022-04-22 06:00:38.343", changeLogs.get(1)), From ba88db8c896eef36465167af34ca83ded9060caa Mon Sep 17 00:00:00 2001 From: Marc Date: Wed, 18 Dec 2024 14:30:43 +0100 Subject: [PATCH 3/7] feat: Sort data elements and event fields as single unit [DHIS2-18012] --- .../event/OrderAndFilterEventChangeLogTest.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) 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 532eda938b0c..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,7 +153,7 @@ void shouldSortChangeLogsWhenOrderingByCreatedAtAsc() void shouldSortChangeLogsWhenOrderingByDataElementAsc() throws ForbiddenException, NotFoundException { EventChangeLogOperationParams params = - EventChangeLogOperationParams.builder().orderBy("dataItem", SortDirection.ASC).build(); + EventChangeLogOperationParams.builder().orderBy("change", SortDirection.ASC).build(); Event event = getEvent("kWjSezkXHVp"); updateDataValues(event, "GieVkTxp4HH", "20", "25"); @@ -176,10 +176,9 @@ void shouldSortChangeLogsWhenOrderingByDataElementAsc() } @Test - void shouldSortChangeLogsWhenOrderingByDataItemDesc() - throws ForbiddenException, NotFoundException { + void shouldSortChangeLogsWhenOrderingByChangeDesc() throws ForbiddenException, NotFoundException { EventChangeLogOperationParams params = - EventChangeLogOperationParams.builder().orderBy("dataItem", SortDirection.DESC).build(); + EventChangeLogOperationParams.builder().orderBy("change", SortDirection.DESC).build(); Event event = getEvent("kWjSezkXHVp"); updateDataValues(event, "GieVkTxp4HH", "20", "25"); @@ -202,10 +201,10 @@ void shouldSortChangeLogsWhenOrderingByDataItemDesc() } @Test - void shouldSortChangeLogsWhenOrderingByDataItemAscAndChangesOnlyToEventFields() + void shouldSortChangeLogsWhenOrderingByChangeAscAndChangesOnlyToEventFields() throws ForbiddenException, NotFoundException, IOException { EventChangeLogOperationParams params = - EventChangeLogOperationParams.builder().orderBy("dataItem", SortDirection.ASC).build(); + EventChangeLogOperationParams.builder().orderBy("change", SortDirection.ASC).build(); UID event = UID.of("QRYjLTiJTrA"); LocalDateTime currentTime = LocalDateTime.now(); @@ -236,10 +235,10 @@ void shouldSortChangeLogsWhenOrderingByDataItemAscAndChangesOnlyToEventFields() } @Test - void shouldSortChangeLogsWhenOrderingByDataItemDescAndChangesOnlyToEventFields() + void shouldSortChangeLogsWhenOrderingByChangeDescAndChangesOnlyToEventFields() throws ForbiddenException, NotFoundException, IOException { EventChangeLogOperationParams params = - EventChangeLogOperationParams.builder().orderBy("dataItem", SortDirection.DESC).build(); + EventChangeLogOperationParams.builder().orderBy("change", SortDirection.DESC).build(); UID event = UID.of("QRYjLTiJTrA"); LocalDateTime currentTime = LocalDateTime.now(); From 9e3c4adbc694a5a7d74c6bfa26879784603c71de Mon Sep 17 00:00:00 2001 From: Marc Date: Wed, 18 Dec 2024 14:48:36 +0100 Subject: [PATCH 4/7] feat: Sort data elements and event fields as single unit [DHIS2-18012] --- .../tracker/export/event/HibernateEventChangeLogStore.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 f04b04d3759e..f9a56f5cdf4b 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 @@ -64,14 +64,14 @@ public class HibernateEventChangeLogStore { /** * Event change logs can be ordered by given fields which correspond to fields on {@link - * EventChangeLog}. Maps fields to DB columns, except for dataItem. In that case we need to sort - * by concatenation, to treat the dataElement and eventField as a single entity. + * 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("dataItem", ORDER_DATA_ITEM_EXPRESSION)); + entry("change", ORDER_DATA_ITEM_EXPRESSION)); private static final Map>, String> FILTERABLE_FIELDS = Map.ofEntries( From 86919cc299a405fb10f3ef9796770bc5ce84274e Mon Sep 17 00:00:00 2001 From: Marc Date: Wed, 18 Dec 2024 16:33:15 +0100 Subject: [PATCH 5/7] feat: Sort data elements and event fields as single unit [DHIS2-18012] --- .../dhis/tracker/export/event/HibernateEventChangeLogStore.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 f9a56f5cdf4b..af20e21fb6a9 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 @@ -58,7 +58,7 @@ public class HibernateEventChangeLogStore { private static final String COLUMN_CHANGELOG_DATA_ELEMENT = "d.uid"; private static final String COLUMN_CHANGELOG_FIELD = "ecl.eventField"; private static final String ORDER_DATA_ITEM_EXPRESSION = - "CONCAT(COALESCE(d.shortName, ''), COALESCE(" + COLUMN_CHANGELOG_FIELD + ", ''))"; + "CONCAT(COALESCE(d.formname, ''), COALESCE(" + COLUMN_CHANGELOG_FIELD + ", ''))"; private static final String DEFAULT_ORDER = COLUMN_CHANGELOG_CREATED + " " + SortDirection.DESC.getValue(); From d4c9da3b2befa831827de8e65d0e13e68dc1503f Mon Sep 17 00:00:00 2001 From: Marc Date: Wed, 18 Dec 2024 21:05:44 +0100 Subject: [PATCH 6/7] feat: Sort data elements and event fields as single unit [DHIS2-18012] --- .../tracker/export/event/HibernateEventChangeLogStore.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 af20e21fb6a9..8c99d2fed626 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,7 +57,7 @@ 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_DATA_ITEM_EXPRESSION = + 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(); @@ -71,7 +71,7 @@ public class HibernateEventChangeLogStore { Map.ofEntries( entry("createdAt", COLUMN_CHANGELOG_CREATED), entry("username", COLUMN_CHANGELOG_USER), - entry("change", ORDER_DATA_ITEM_EXPRESSION)); + entry("change", ORDER_CHANGE_EXPRESSION)); private static final Map>, String> FILTERABLE_FIELDS = Map.ofEntries( From c21c59163119b11179bcfce45d9613f16bd54af9 Mon Sep 17 00:00:00 2001 From: Marc Date: Wed, 18 Dec 2024 21:37:46 +0100 Subject: [PATCH 7/7] feat: Fix typo in data element attribute in query [DHIS2-18012] --- .../dhis/tracker/export/event/HibernateEventChangeLogStore.java | 2 +- .../src/main/resources/tracker/simple_metadata.json | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) 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 8c99d2fed626..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 @@ -58,7 +58,7 @@ public class HibernateEventChangeLogStore { 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 + ", ''))"; + "CONCAT(COALESCE(d.formName, ''), COALESCE(" + COLUMN_CHANGELOG_FIELD + ", ''))"; private static final String DEFAULT_ORDER = COLUMN_CHANGELOG_CREATED + " " + SortDirection.DESC.getValue(); 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" }