Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Composition.focus to handle Composition.entry #3647

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rewrite this by defining a function that takes the values that have the key of either "focus" or "entry", so that you can keep all the code the same other than where you happen to get the values from?

E.g. regardless of whether the key in the composition is "focus" or "entry" all the process that happens to the

{
    "reference": ...
   "identifier": ...
}

is going to be exactly the same. So the only part that should vary is whether you get that from the a key that's called "focus" or "entry".

Also, not that in the test cases you need to handle at least the cases where

  1. the composition uses all focus
  2. the composition uses all entry
  3. the composition uses both, but not in the same section
  4. the composition uses both and at least one in the same section (ie verify it always takes either entry or focus when it sees both)
  5. the composition has neither entry or focus (yes, this is invalid but we need to check it doesn't crash)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great start!

Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,18 @@
addOrUpdate(localCompositionResource)

localCompositionResource.run {
val iconConfigs =
retrieveCompositionSections().filter {
it.focus.hasIdentifier() && isIconConfig(it.focus.identifier.value)
}
if (iconConfigs.isNotEmpty()) {
val ids = iconConfigs.joinToString(DEFAULT_STRING_SEPARATOR) { it.focus.extractId() }
val configsToProcess =
retrieveCompositionSections()
.flatMap { section ->
section.entry.ifEmpty { listOfNotNull(section.focus.takeIf { it.hasIdentifier() }) }
}
.filter { it.hasReferenceElement() && it.hasIdentifier() }

val ids = configsToProcess.joinToString(DEFAULT_STRING_SEPARATOR) { it.extractId() }

if (ids.isNotEmpty()) {
fhirResourceDataSource
.getResource(
"${ResourceType.Binary.name}?$ID=$ids&_count=$DEFAULT_COUNT",
)
.getResource("${ResourceType.Binary.name}?$ID=$ids&_count=$DEFAULT_COUNT")
.entry
.forEach { addOrUpdate(it.resource) }
}
Expand Down Expand Up @@ -334,7 +336,26 @@
}
}
} else {
// Process composition sections
composition.retrieveCompositionSections().forEach {
// Check .entry first
it.entry.forEach { entry ->
if (entry.hasReferenceElement() && entry.hasIdentifier()) {
val configIdentifier = entry.identifier.value
val referenceResourceType = entry.reference.substringBefore(TYPE_REFERENCE_DELIMITER)

Check warning on line 345 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt#L344-L345

Added lines #L344 - L345 were not covered by tests
if (isAppConfig(referenceResourceType) && !isIconConfig(configIdentifier)) {
val extractedId = entry.extractId()
try {
val configBinary = fhirEngine.get<Binary>(extractedId)
configsJsonMap[configIdentifier] = configBinary.content.decodeToString()
} catch (resourceNotFoundException: ResourceNotFoundException) {
Timber.e("Missing Binary file with ID :$extractedId")

Check warning on line 352 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt#L347-L352

Added lines #L347 - L352 were not covered by tests
withContext(dispatcherProvider.main()) { configsLoadedCallback(false) }
}
}
}
}
// If .entry doesn't provide the necessary data, fallback to .focus
if (it.hasFocus() && it.focus.hasReferenceElement() && it.focus.hasIdentifier()) {
val configIdentifier = it.focus.identifier.value
val referenceResourceType = it.focus.reference.substringBefore(TYPE_REFERENCE_DELIMITER)
Expand Down Expand Up @@ -436,13 +457,30 @@
)
}",
)
fetchResources(
resourceType = entry.key,
resourceIdList =
sectionComponents.map { sectionComponent ->
sectionComponent.focus.extractId()
},
)

// Check .entry first, fallback to .focus if necessary
sectionComponents.forEach { sectionComponent ->

Check warning on line 462 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt#L462

Added line #L462 was not covered by tests
// Prioritize .entry for resource extraction
sectionComponent.entry.forEach { entry ->

Check warning on line 464 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt#L464

Added line #L464 was not covered by tests
if (entry.hasReferenceElement() && entry.hasIdentifier()) {
val resourceId = entry.extractId()
fetchResources(
resourceType = entry.reference.substringBefore(TYPE_REFERENCE_DELIMITER),
resourceIdList = listOf(resourceId),

Check warning on line 469 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt#L466-L469

Added lines #L466 - L469 were not covered by tests
)
}
}

// Fallback to .focus if .entry doesn't provide valid reference or identifier
if (sectionComponent.hasFocus() && sectionComponent.focus.hasReferenceElement()) {
val resourceId = sectionComponent.focus.extractId()
fetchResources(

Check warning on line 477 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt#L476-L477

Added lines #L476 - L477 were not covered by tests
resourceType =
sectionComponent.focus.reference.substringBefore(TYPE_REFERENCE_DELIMITER),
resourceIdList = listOf(resourceId),

Check warning on line 480 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt#L479-L480

Added lines #L479 - L480 were not covered by tests
)
}
}
}
}
}
Expand Down Expand Up @@ -677,37 +715,61 @@
private suspend fun processCompositionListResources(
sectionComponentEntry: Map.Entry<String, List<Composition.SectionComponent>>,
) {
val resourceType = sectionComponentEntry.key
val sectionComponents = sectionComponentEntry.value

if (isNonProxy()) {
val chunkedResourceIdList = sectionComponentEntry.value.chunked(MANIFEST_PROCESSOR_BATCH_SIZE)
chunkedResourceIdList.forEach {
fetchResources(
resourceType = sectionComponentEntry.key,
resourceIdList = it.map { sectionComponent -> sectionComponent.focus.extractId() },
)
.entry
.forEach { bundleEntryComponent ->
when (bundleEntryComponent.resource) {
is ListResource -> {
addOrUpdate(bundleEntryComponent.resource)
val list = bundleEntryComponent.resource as ListResource
list.entry.forEach { listEntryComponent ->
val resourceKey =
listEntryComponent.item.reference.substringBefore(TYPE_REFERENCE_DELIMITER)
val resourceId = listEntryComponent.item.reference.extractLogicalIdUuid()
val listResourceUrlPath = "$resourceKey?$ID=$resourceId&_count=$DEFAULT_COUNT"
fetchResources(gatewayModeHeaderValue = null, url = listResourceUrlPath)
}
val chunkedSections = sectionComponents.chunked(MANIFEST_PROCESSOR_BATCH_SIZE)
chunkedSections.forEach { chunk ->
try {
val resourceIds =
chunk
.flatMap { section ->
// Include IDs from .entry and .focus
section.entry.mapNotNull { it.extractId() } +
listOfNotNull(section.focus.extractId())
}
.distinct()

val fetchedResources =
fetchResources(resourceType = resourceType, resourceIdList = resourceIds)

fetchedResources.entry.forEach { bundleEntry ->
when (val resource = bundleEntry.resource) {
is ListResource -> processListResource(resource)
}
}
} catch (exception: Exception) {
Timber.e("Error processing non-proxy resources for type $resourceType: $exception")

Check warning on line 743 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt#L742-L743

Added lines #L742 - L743 were not covered by tests
}
}
} else {
sectionComponentEntry.value.forEach {
fetchResources(
gatewayModeHeaderValue = FHIR_GATEWAY_MODE_HEADER_VALUE,
url =
"${sectionComponentEntry.key}?$ID=${it.focus.extractId()}&_page=1&_count=$DEFAULT_COUNT",
)
sectionComponents.forEach { section ->
try {
// Process .entry and .focus for gateway mode
val resourceIds =
section.entry.mapNotNull { it.extractId() } + listOfNotNull(section.focus.extractId())
resourceIds.forEach { resourceId ->
val url = "$resourceType?$ID=$resourceId&_page=1&_count=$DEFAULT_COUNT"
fetchResources(gatewayModeHeaderValue = FHIR_GATEWAY_MODE_HEADER_VALUE, url = url)
}
} catch (exception: Exception) {
Timber.e("Error processing proxy resources for type $resourceType: $exception")

Check warning on line 757 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt#L756-L757

Added lines #L756 - L757 were not covered by tests
}
}
}
}

private suspend fun processListResource(listResource: ListResource) {
addOrUpdate(listResource)
listResource.entry.forEach { listEntry ->
val reference = listEntry.item.reference
val resourceKey = reference.substringBefore(TYPE_REFERENCE_DELIMITER)
val resourceId = reference.extractLogicalIdUuid()

if (resourceKey.isNotEmpty() && resourceId.isNotEmpty()) {
val listResourceUrlPath = "$resourceKey?$ID=$resourceId&_count=$DEFAULT_COUNT"
fetchResources(gatewayModeHeaderValue = null, url = listResourceUrlPath)
}
}
}
Expand Down
Loading
Loading