From b588f636ed8103c730cf3825b574008b8eda9a5b Mon Sep 17 00:00:00 2001 From: Marcos Campos Date: Tue, 11 Jun 2024 12:18:58 +0200 Subject: [PATCH 1/4] [ANDROSDK-1866] Actively remove the enrollment when handling an event without registration --- .../internal/IdentifiableDataHandlerImpl.kt | 2 +- .../core/event/internal/EventHandler.kt | 33 ++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/hisp/dhis/android/core/arch/handlers/internal/IdentifiableDataHandlerImpl.kt b/core/src/main/java/org/hisp/dhis/android/core/arch/handlers/internal/IdentifiableDataHandlerImpl.kt index ee3073f305..83ad09e136 100644 --- a/core/src/main/java/org/hisp/dhis/android/core/arch/handlers/internal/IdentifiableDataHandlerImpl.kt +++ b/core/src/main/java/org/hisp/dhis/android/core/arch/handlers/internal/IdentifiableDataHandlerImpl.kt @@ -185,7 +185,7 @@ internal abstract class IdentifiableDataHandlerImpl( relatives: RelationshipItemRelatives?, ) - protected fun beforeCollectionHandled( + protected open fun beforeCollectionHandled( oCollection: Collection, params: IdentifiableDataHandlerParams, ): Collection { diff --git a/core/src/main/java/org/hisp/dhis/android/core/event/internal/EventHandler.kt b/core/src/main/java/org/hisp/dhis/android/core/event/internal/EventHandler.kt index 39e8e207f1..84b1f74f1f 100644 --- a/core/src/main/java/org/hisp/dhis/android/core/event/internal/EventHandler.kt +++ b/core/src/main/java/org/hisp/dhis/android/core/event/internal/EventHandler.kt @@ -40,6 +40,8 @@ import org.hisp.dhis.android.core.note.Note import org.hisp.dhis.android.core.note.internal.NoteDHISVersionManager import org.hisp.dhis.android.core.note.internal.NoteHandler import org.hisp.dhis.android.core.note.internal.NoteUniquenessManager +import org.hisp.dhis.android.core.program.ProgramType +import org.hisp.dhis.android.core.program.internal.ProgramStore import org.hisp.dhis.android.core.relationship.internal.EventRelationshipOrphanCleaner import org.hisp.dhis.android.core.relationship.internal.RelationshipDHISVersionManager import org.hisp.dhis.android.core.relationship.internal.RelationshipHandler @@ -53,6 +55,7 @@ internal class EventHandler( relationshipVersionManager: RelationshipDHISVersionManager, relationshipHandler: RelationshipHandler, eventStore: EventStore, + private val programStore: ProgramStore, private val trackedEntityDataValueHandler: TrackedEntityDataValueHandler, private val noteHandler: NoteHandler, private val noteVersionManager: NoteDHISVersionManager, @@ -60,13 +63,31 @@ internal class EventHandler( private val relationshipOrphanCleaner: EventRelationshipOrphanCleaner, ) : IdentifiableDataHandlerImpl(eventStore, relationshipVersionManager, relationshipHandler) { + private var programTypes = mutableMapOf() + + override fun beforeCollectionHandled( + oCollection: Collection, + params: IdentifiableDataHandlerParams, + ): Collection { + programStore.selectAll().forEach { + programTypes[it.uid()] = it.programType() + } + return super.beforeCollectionHandled(oCollection, params) + } + override fun beforeObjectHandled(o: Event, params: IdentifiableDataHandlerParams): Event { - return if (GeometryHelper.isValid(o.geometry())) { - o - } else { + val builder = o.toBuilder() + + if (programTypes[o.program()] == ProgramType.WITHOUT_REGISTRATION) { + builder.enrollment(null) + } + + if (!GeometryHelper.isValid(o.geometry())) { Log.i(this.javaClass.simpleName, "Event " + o.uid() + " has invalid geometry value") - o.toBuilder().geometry(null).build() + builder.geometry(null) } + + return builder.build() } override fun afterObjectHandled( @@ -106,6 +127,10 @@ internal class EventHandler( } } } + override fun afterCollectionHandled(oCollection: Collection?, params: IdentifiableDataHandlerParams) { + programTypes.clear() + super.afterCollectionHandled(oCollection, params) + } override fun deleteIfCondition(o: Event): Boolean { val validEventDate = o.eventDate() != null || From f79efd1ed89e6474db898e3ccbffe10594a45e8a Mon Sep 17 00:00:00 2001 From: Marcos Campos Date: Tue, 11 Jun 2024 12:19:28 +0200 Subject: [PATCH 2/4] [ANDROSDK-1866] Adapt EventHandlerShould --- .../dhis/android/core/event/internal/EventHandlerShould.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/src/test/java/org/hisp/dhis/android/core/event/internal/EventHandlerShould.kt b/core/src/test/java/org/hisp/dhis/android/core/event/internal/EventHandlerShould.kt index ab677fbae6..d81357bcea 100644 --- a/core/src/test/java/org/hisp/dhis/android/core/event/internal/EventHandlerShould.kt +++ b/core/src/test/java/org/hisp/dhis/android/core/event/internal/EventHandlerShould.kt @@ -37,6 +37,7 @@ import org.hisp.dhis.android.core.note.Note import org.hisp.dhis.android.core.note.internal.NoteDHISVersionManager import org.hisp.dhis.android.core.note.internal.NoteHandler import org.hisp.dhis.android.core.note.internal.NoteUniquenessManager +import org.hisp.dhis.android.core.program.internal.ProgramStore import org.hisp.dhis.android.core.relationship.internal.EventRelationshipOrphanCleaner import org.hisp.dhis.android.core.relationship.internal.RelationshipDHISVersionManager import org.hisp.dhis.android.core.relationship.internal.RelationshipHandler @@ -51,6 +52,7 @@ import org.junit.runners.JUnit4 @RunWith(JUnit4::class) class EventHandlerShould { private val eventStore: EventStore = mock() + private val programStore: ProgramStore = mock() private val trackedEntityDataValueHandler: TrackedEntityDataValueHandler = mock() private val trackedEntityDataValue: TrackedEntityDataValue = mock() private val noteHandler: NoteHandler = mock() @@ -84,6 +86,7 @@ class EventHandlerShould { relationshipVersionManager, relationshipHandler, eventStore, + programStore, trackedEntityDataValueHandler, noteHandler, noteVersionManager, From 0769151d9c570ccac08e49828f9a63d73231b2f7 Mon Sep 17 00:00:00 2001 From: Marcos Campos Date: Tue, 11 Jun 2024 16:10:49 +0200 Subject: [PATCH 3/4] [ANDROSDK-1866] Refactor code to remove enrollments on the beforeCollectionHandled method --- .../core/event/internal/EventHandler.kt | 32 +++++++------------ 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/core/src/main/java/org/hisp/dhis/android/core/event/internal/EventHandler.kt b/core/src/main/java/org/hisp/dhis/android/core/event/internal/EventHandler.kt index 84b1f74f1f..0f014418bc 100644 --- a/core/src/main/java/org/hisp/dhis/android/core/event/internal/EventHandler.kt +++ b/core/src/main/java/org/hisp/dhis/android/core/event/internal/EventHandler.kt @@ -63,31 +63,27 @@ internal class EventHandler( private val relationshipOrphanCleaner: EventRelationshipOrphanCleaner, ) : IdentifiableDataHandlerImpl(eventStore, relationshipVersionManager, relationshipHandler) { - private var programTypes = mutableMapOf() - override fun beforeCollectionHandled( oCollection: Collection, params: IdentifiableDataHandlerParams, ): Collection { - programStore.selectAll().forEach { - programTypes[it.uid()] = it.programType() + val programTypes = programStore.selectAll().associate { it.uid() to it.programType() } + + val updatedEvents = oCollection.map { + it.takeUnless { programTypes[it.program()] == ProgramType.WITHOUT_REGISTRATION } + ?: it.toBuilder().enrollment(null).build() } - return super.beforeCollectionHandled(oCollection, params) + + return super.beforeCollectionHandled(updatedEvents, params) } override fun beforeObjectHandled(o: Event, params: IdentifiableDataHandlerParams): Event { - val builder = o.toBuilder() - - if (programTypes[o.program()] == ProgramType.WITHOUT_REGISTRATION) { - builder.enrollment(null) - } - - if (!GeometryHelper.isValid(o.geometry())) { - Log.i(this.javaClass.simpleName, "Event " + o.uid() + " has invalid geometry value") - builder.geometry(null) + return if (GeometryHelper.isValid(o.geometry())) { + o + } else { + Log.i(this::class.simpleName, "Event ${o.uid()} has an invalid geometry value") + o.toBuilder().geometry(null).build() } - - return builder.build() } override fun afterObjectHandled( @@ -127,10 +123,6 @@ internal class EventHandler( } } } - override fun afterCollectionHandled(oCollection: Collection?, params: IdentifiableDataHandlerParams) { - programTypes.clear() - super.afterCollectionHandled(oCollection, params) - } override fun deleteIfCondition(o: Event): Boolean { val validEventDate = o.eventDate() != null || From 247ee3ddf254e029f0c9a1b28ebb32d829df668b Mon Sep 17 00:00:00 2001 From: Victor Garcia Date: Wed, 12 Jun 2024 06:35:31 +0200 Subject: [PATCH 4/4] [ANDROSDK-1866] Add enrollment property to mock single events --- .../sharedTest/resources/event/new_tracker_importer_events.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/src/sharedTest/resources/event/new_tracker_importer_events.json b/core/src/sharedTest/resources/event/new_tracker_importer_events.json index 8f0c0dccbe..ea755b4054 100644 --- a/core/src/sharedTest/resources/event/new_tracker_importer_events.json +++ b/core/src/sharedTest/resources/event/new_tracker_importer_events.json @@ -10,6 +10,7 @@ "program": "lxAQ7Zs9VYR", "event": "single1", "status": "COMPLETED", + "enrollment": "u9mt5Z8assp", "occurredAt": "2018-02-27T00:00:00.000", "createdAt": "2017-08-07T15:47:25.959", "completedAt": "2016-02-27T00:00:00.000", @@ -112,6 +113,7 @@ "program": "lxAQ7Zs9VYR", "event": "single2", "status": "ACTIVE", + "enrollment": "u9mt5Z8assp", "occurredAt": "2017-02-27T00:00:00.000", "createdAt": "2018-08-07T15:47:25.959", "completedAt": "2018-02-27T00:00:00.000",