diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/DefaultRepository.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/DefaultRepository.kt index 50f46b433a..ad6eb116a1 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/DefaultRepository.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/DefaultRepository.kt @@ -479,7 +479,7 @@ constructor( val relatedResourcesQueue = ArrayDeque?>>().apply { addFirst(Pair(resource, relatedResourcesConfigs)) } - while (!relatedResourcesQueue.isEmpty()) { + while (relatedResourcesQueue.isNotEmpty()) { val (currentResource, currentRelatedResourceConfigs) = relatedResourcesQueue.removeFirst() val relatedResourceCountConfigs = currentRelatedResourceConfigs ?.asSequence() @@ -525,7 +525,7 @@ constructor( } val searchResults = searchIncludedResources( - relatedResourcesConfigs = relatedResourcesConfigs, + relatedResourcesConfigs = currentRelatedResourceConfigs, resource = currentResource, configComputedRuleValues = configComputedRuleValues ) @@ -579,7 +579,7 @@ constructor( if (!id.isNullOrBlank()) { relatedResourceWrapper.relatedResourceMap[id] = relatedResourceWrapper.relatedResourceMap.getOrPut(id) { mutableListOf() }.apply { - addAll(value) + addAll(value.distinctBy { it.logicalId }) } value.forEach { resource -> with(resourceConfigs?.relatedResources) { @@ -601,7 +601,7 @@ constructor( }, ): Long = kotlin - .runCatching { withContext(dispatcherProvider.io()) { fhirEngine.count(this@count) } } + .runCatching { fhirEngine.count(this@count) } .onSuccess { count -> onSuccess(count) } .onFailure { throwable -> onFailure(throwable) } .getOrDefault(0) @@ -630,13 +630,12 @@ constructor( relatedResourcesConfigs?.revIncludeRelatedResourceConfigs(true) search.apply { - val thisSearch = this reverseIncludeResourceConfigs?.forEach { resourceConfig -> revInclude( resourceConfig.resource, ReferenceClientParam(resourceConfig.searchParameter), ) { - thisSearch.applyConfiguredSortAndFilters( + (this as Search).applyConfiguredSortAndFilters( resourceConfig = resourceConfig, sortData = true, configComputedRuleValues = configComputedRuleValues, @@ -649,7 +648,7 @@ constructor( resourceConfig.resource, ReferenceClientParam(resourceConfig.searchParameter), ) { - thisSearch.applyConfiguredSortAndFilters( + (this as Search).applyConfiguredSortAndFilters( resourceConfig = resourceConfig, sortData = true, configComputedRuleValues = configComputedRuleValues, diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/register/RegisterRepository.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/register/RegisterRepository.kt index e50a868683..0363fc2061 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/register/RegisterRepository.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/register/RegisterRepository.kt @@ -97,73 +97,79 @@ constructor( fhirResourceConfig: FhirResourceConfig?, paramsMap: Map?, ): Long { - val registerConfiguration = retrieveRegisterConfiguration(registerId, paramsMap) - val fhirResource = fhirResourceConfig ?: registerConfiguration.fhirResource - val baseResourceConfig = fhirResource.baseResource - val configComputedRuleValues = registerConfiguration.configRules.configRulesComputedValues() - val filterByRelatedEntityLocation = registerConfiguration.filterDataByRelatedEntityLocation - val filterActiveResources = registerConfiguration.activeResourceFilters - if (filterByRelatedEntityLocation) { - val syncLocationIds = context.retrieveRelatedEntitySyncLocationIds() - val locationIds = - syncLocationIds - .map { retrieveFlattenedSubLocations(it).map { subLocation -> subLocation.logicalId }} - .asSequence() - .flatten() - .toHashSet() - val countSearch = - Search(baseResourceConfig.resource).apply { - applyConfiguredSortAndFilters( - resourceConfig = baseResourceConfig, - sortData = false, - filterActiveResources = filterActiveResources, - configComputedRuleValues = configComputedRuleValues, - ) - } - val totalCount = fhirEngine.count(countSearch) - var searchResultsCount = 0L - var pageNumber = 0 - var count = 0 - while (count < totalCount) { - val baseResourceSearch = - createSearch( - baseResourceConfig = baseResourceConfig, - filterActiveResources = filterActiveResources, - configComputedRuleValues = configComputedRuleValues, - currentPage = pageNumber, - count = COUNT, - ) - searchResultsCount += fhirEngine.search(baseResourceSearch) - .asSequence() - .map { it.resource } - .filter { resource -> - when (resource.resourceType) { - ResourceType.Location -> locationIds.contains(resource.logicalId) - else -> resource.meta.tag.any { - it.system == context.getString(R.string.sync_strategy_related_entity_location_system) - && locationIds.contains(it.code) + return withContext(dispatcherProvider.io()) { + val registerConfiguration = retrieveRegisterConfiguration(registerId, paramsMap) + val fhirResource = fhirResourceConfig ?: registerConfiguration.fhirResource + val baseResourceConfig = fhirResource.baseResource + val configComputedRuleValues = registerConfiguration.configRules.configRulesComputedValues() + val filterByRelatedEntityLocation = registerConfiguration.filterDataByRelatedEntityLocation + val filterActiveResources = registerConfiguration.activeResourceFilters + if (filterByRelatedEntityLocation) { + val syncLocationIds = context.retrieveRelatedEntitySyncLocationIds() + val locationIds = + syncLocationIds + .map { retrieveFlattenedSubLocations(it).map { subLocation -> subLocation.logicalId } } + .asSequence() + .flatten() + .toHashSet() + val countSearch = + Search(baseResourceConfig.resource).apply { + applyConfiguredSortAndFilters( + resourceConfig = baseResourceConfig, + sortData = false, + filterActiveResources = filterActiveResources, + configComputedRuleValues = configComputedRuleValues, + ) + } + val totalCount = fhirEngine.count(countSearch) + var searchResultsCount = 0L + var pageNumber = 0 + var count = 0 + while (count < totalCount) { + val baseResourceSearch = + createSearch( + baseResourceConfig = baseResourceConfig, + filterActiveResources = filterActiveResources, + configComputedRuleValues = configComputedRuleValues, + currentPage = pageNumber, + count = COUNT, + ) + searchResultsCount += fhirEngine.search(baseResourceSearch) + .asSequence() + .map { it.resource } + .filter { resource -> + when (resource.resourceType) { + ResourceType.Location -> locationIds.contains(resource.logicalId) + else -> resource.meta.tag.any { + it.system == context.getString(R.string.sync_strategy_related_entity_location_system) + && locationIds.contains(it.code) + } } + }.count().toLong() + count += COUNT + pageNumber++ + } + searchResultsCount + } else { + val search = + Search(baseResourceConfig.resource).apply { + applyConfiguredSortAndFilters( + resourceConfig = baseResourceConfig, + sortData = false, + filterActiveResources = registerConfiguration.activeResourceFilters, + configComputedRuleValues = configComputedRuleValues, + ) } - }.count().toLong() - count += COUNT - pageNumber++ - } - return searchResultsCount - } - val search = - Search(baseResourceConfig.resource).apply { - applyConfiguredSortAndFilters( - resourceConfig = baseResourceConfig, - sortData = false, - filterActiveResources = registerConfiguration.activeResourceFilters, - configComputedRuleValues = configComputedRuleValues, + search.count( + onFailure = { + Timber.e( + it, + "Error counting register data for register id: ${registerConfiguration.id}" + ) + }, ) } - return search.count( - onFailure = { - Timber.e(it, "Error counting register data for register id: ${registerConfiguration.id}") - }, - ) + } } override suspend fun loadProfileData(