From 320dde8edc0ee1df208d383770bdbb30d2645d50 Mon Sep 17 00:00:00 2001 From: Marc Date: Mon, 20 Jan 2025 20:16:18 +0100 Subject: [PATCH 1/2] feat: Add data element name as a fallback when sorting [DHIS2-18871] --- .../event/HibernateEventChangeLogStore.java | 4 ++- .../OrderAndFilterEventChangeLogTest.java | 25 +++++++++++++++++++ 2 files changed, 28 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 53766ba3884..98aebbf7a0f 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,9 @@ 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(d.name, ''), COALESCE(" + + COLUMN_CHANGELOG_FIELD + + ", ''))"; private static final String DEFAULT_ORDER = COLUMN_CHANGELOG_CREATED + " " + SortDirection.DESC.getValue(); 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 c01879abbcb..1e2aa12cc8f 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 @@ -268,6 +268,31 @@ void shouldSortChangeLogsWhenOrderingByChangeDescAndChangesOnlyToEventFields() () -> assertFieldCreate("geometry", "(-11.419700, 8.103900)", changeLogs.get(4))); } + @Test + void shouldSortChangeLogsByNameWhenOrderingByChangeAndDataElementDoesNotHaveFormName() + throws ForbiddenException, NotFoundException { + EventChangeLogOperationParams params = + EventChangeLogOperationParams.builder().orderBy("change", SortDirection.DESC).build(); + Event event = getEvent("pTzf9KYMk72"); + + updateDataValues(event, "DATAEL00001", "value00002", "value00003"); + + List changeLogs = + eventChangeLogService + .getEventChangeLog(UID.of("pTzf9KYMk72"), params, defaultPageParams) + .getItems(); + + assertNumberOfChanges(7, changeLogs); + assertAll( + () -> assertDataElementCreate("DATAEL00005", "option1", changeLogs.get(0)), + () -> assertDataElementCreate("DATAEL00006", "88", changeLogs.get(1)), + () -> assertDataElementUpdate("DATAEL00001", "value00002", "value00003", changeLogs.get(2)), + () -> assertDataElementUpdate("DATAEL00001", "value00001", "value00002", changeLogs.get(3)), + () -> assertDataElementCreate("DATAEL00001", "value00001", changeLogs.get(4)), + () -> assertFieldCreate("scheduledAt", "2019-01-28 12:32:38.100", changeLogs.get(5)), + () -> assertFieldCreate("occurredAt", "2019-01-25 12:10:38.100", changeLogs.get(6))); + } + @Test void shouldFilterChangeLogsWhenFilteringByUser() throws ForbiddenException, NotFoundException { EventChangeLogOperationParams params = From fd9be7aa098ab753ddd2b541243f4e281b3c7cde Mon Sep 17 00:00:00 2001 From: Marc Date: Tue, 21 Jan 2025 08:58:04 +0100 Subject: [PATCH 2/2] feat: Map DE to name in test [DHIS2-18871] --- .../resources/tracker/simple_metadata.json | 1 + .../OrderAndFilterEventChangeLogTest.java | 36 +++++++++++++------ 2 files changed, 26 insertions(+), 11 deletions(-) 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 24a0f27d194..70d7052d2b3 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 @@ -390,6 +390,7 @@ }, "name": "test-dataelement1", "shortName": "test-dataelement1", + "formName": "test-dataelement1", "user": { "id": "tTgjgobT1oS" }, 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 1e2aa12cc8f..12fdf7d6a91 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 @@ -277,20 +277,24 @@ void shouldSortChangeLogsByNameWhenOrderingByChangeAndDataElementDoesNotHaveForm updateDataValues(event, "DATAEL00001", "value00002", "value00003"); - List changeLogs = + List changeLogs = eventChangeLogService .getEventChangeLog(UID.of("pTzf9KYMk72"), params, defaultPageParams) - .getItems(); + .getItems() + .stream() + .map(this::getDisplayName) + .toList(); - assertNumberOfChanges(7, changeLogs); - assertAll( - () -> assertDataElementCreate("DATAEL00005", "option1", changeLogs.get(0)), - () -> assertDataElementCreate("DATAEL00006", "88", changeLogs.get(1)), - () -> assertDataElementUpdate("DATAEL00001", "value00002", "value00003", changeLogs.get(2)), - () -> assertDataElementUpdate("DATAEL00001", "value00001", "value00002", changeLogs.get(3)), - () -> assertDataElementCreate("DATAEL00001", "value00001", changeLogs.get(4)), - () -> assertFieldCreate("scheduledAt", "2019-01-28 12:32:38.100", changeLogs.get(5)), - () -> assertFieldCreate("occurredAt", "2019-01-25 12:10:38.100", changeLogs.get(6))); + assertEquals( + List.of( + "with-option-set", + "test-dataelement6", + "test-dataelement1", + "test-dataelement1", + "test-dataelement1", + "scheduledAt", + "occurredAt"), + changeLogs); } @Test @@ -499,4 +503,14 @@ private void updateDataValues(Event event, String dataElementUid, String... valu private String getFirstDataElement(Event event) { return event.getEventDataValues().iterator().next().getDataElement(); } + + private String getDisplayName(EventChangeLog cl) { + if (cl.getEventField() != null) { + return cl.getEventField(); + } else if (cl.getDataElement().getFormName() != null) { + return cl.getDataElement().getFormName(); + } else { + return cl.getDataElement().getName(); + } + } }