From 0769151d9c570ccac08e49828f9a63d73231b2f7 Mon Sep 17 00:00:00 2001 From: Marcos Campos Date: Tue, 11 Jun 2024 16:10:49 +0200 Subject: [PATCH] [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 ||