Skip to content

Commit

Permalink
Merge branch 'add-entry-to-composition' of github.com:opensrp/fhircor…
Browse files Browse the repository at this point in the history
…e into add-entry-to-composition
  • Loading branch information
sharon2719 committed Dec 20, 2024
2 parents 85f5bfb + 8aa3b39 commit 9e061ac
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import javax.inject.Singleton
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.Json
import okhttp3.RequestBody.Companion.toRequestBody
import org.hl7.fhir.r4.model.Reference
import org.hl7.fhir.r4.model.Base
import org.hl7.fhir.r4.model.Binary
import org.hl7.fhir.r4.model.Bundle
Expand All @@ -47,6 +46,7 @@ import org.hl7.fhir.r4.model.ImplementationGuide
import org.hl7.fhir.r4.model.ListResource
import org.hl7.fhir.r4.model.MetadataResource
import org.hl7.fhir.r4.model.Parameters
import org.hl7.fhir.r4.model.Reference
import org.hl7.fhir.r4.model.Resource
import org.hl7.fhir.r4.model.ResourceType
import org.hl7.fhir.r4.model.SearchParameter
Expand Down Expand Up @@ -336,15 +336,30 @@ constructor(
}
} else {
composition.retrieveCompositionSections().forEach { sectionComponent ->
val referenceResourceType = sectionComponent.focus.reference.substringBefore(TYPE_REFERENCE_DELIMITER)
if (sectionComponent.hasFocus() && sectionComponent.focus.hasReferenceElement() && sectionComponent.focus.hasIdentifier()) {
val referenceResourceType =
sectionComponent.focus.reference.substringBefore(TYPE_REFERENCE_DELIMITER)
if (
sectionComponent.hasFocus() &&
sectionComponent.focus.hasReferenceElement() &&
sectionComponent.focus.hasIdentifier()
) {
val configIdentifier = sectionComponent.focus.identifier.value
addBinaryToConfigsJsonMap(referenceResourceType, configIdentifier, sectionComponent.focus, configsLoadedCallback)
addBinaryToConfigsJsonMap(
referenceResourceType,
configIdentifier,
sectionComponent.focus,
configsLoadedCallback,
)
}
if (sectionComponent.hasEntry() && sectionComponent.entry.isNotEmpty()){
sectionComponent.entry.forEach{ entryReference ->
if (sectionComponent.hasEntry() && sectionComponent.entry.isNotEmpty()) {
sectionComponent.entry.forEach { entryReference ->
val configIdentifier = entryReference.identifier.value
addBinaryToConfigsJsonMap(referenceResourceType, configIdentifier, entryReference, configsLoadedCallback)
addBinaryToConfigsJsonMap(
referenceResourceType,
configIdentifier,
entryReference,
configsLoadedCallback,
)
}
}
}
Expand All @@ -356,7 +371,7 @@ constructor(
referenceResourceType: String,
configIdentifier: String,
entryReference: Reference,
configsLoadedCallback: (Boolean) -> Unit
configsLoadedCallback: (Boolean) -> Unit,
) {
if (isAppConfig(referenceResourceType) && !isIconConfig(configIdentifier)) {
val extractedId = entryReference.identifier
Expand Down Expand Up @@ -428,26 +443,27 @@ constructor(
val compositionResource = fetchRemoteCompositionByAppId(parsedAppId)
compositionResource?.let { composition ->
val compositionSections = composition.retrieveCompositionSections()
val sectionComponentMap = mutableMapOf<String,MutableList<Composition.SectionComponent>>()
compositionSections.forEach { sectionComponent ->
if ( sectionComponent.hasFocus() && sectionComponent.focus.hasReferenceElement()){
val key = sectionComponent.focus.reference.substringBefore(
delimiter = TYPE_REFERENCE_DELIMITER,
missingDelimiterValue = "",
)
sectionComponentMap.getOrPut(key){ mutableListOf() }.apply { add(sectionComponent) }
}
if ( sectionComponent.hasEntry() && sectionComponent.entry.isNotEmpty()){
sectionComponent.entry.forEach {
val key = it.reference.substringBefore(
delimiter = TYPE_REFERENCE_DELIMITER,
val sectionComponentMap = mutableMapOf<String, MutableList<Composition.SectionComponent>>()
compositionSections.forEach { sectionComponent ->
if (sectionComponent.hasFocus() && sectionComponent.focus.hasReferenceElement()) {
val key =
sectionComponent.focus.reference.substringBefore(
delimiter = TYPE_REFERENCE_DELIMITER,
missingDelimiterValue = "",
)
sectionComponentMap.getOrPut(key){ mutableListOf() }.apply { add(sectionComponent) }
}
sectionComponentMap.getOrPut(key) { mutableListOf() }.apply { add(sectionComponent) }
}
if (sectionComponent.hasEntry() && sectionComponent.entry.isNotEmpty()) {
sectionComponent.entry.forEach {
val key =
it.reference.substringBefore(
delimiter = TYPE_REFERENCE_DELIMITER,
missingDelimiterValue = "",
)
sectionComponentMap.getOrPut(key) { mutableListOf() }.apply { add(sectionComponent) }
}

}
}

processCompositionSectionComponent(sectionComponentMap)

Expand All @@ -459,8 +475,11 @@ constructor(
}
}

private suspend fun processCompositionSectionComponent(sectionComponentMap: Map<String,List<Composition.SectionComponent>>){
sectionComponentMap.filter { entry -> entry.key in FILTER_RESOURCE_LIST }
private suspend fun processCompositionSectionComponent(
sectionComponentMap: Map<String, List<Composition.SectionComponent>>,
) {
sectionComponentMap
.filter { entry -> entry.key in FILTER_RESOURCE_LIST }
.forEach { entry: Map.Entry<String, List<Composition.SectionComponent>> ->
if (entry.key == ResourceType.List.name) {
processCompositionListResources(entry)
Expand All @@ -470,17 +489,15 @@ constructor(
chunkedResourceIdList.forEach { sectionComponents ->
Timber.d(
"Fetching config resource ${entry.key}: with ids ${
sectionComponents.joinToString(
",",
)
}",
sectionComponents.joinToString(
",",
)
}",
)
fetchResources(
resourceType = entry.key,
resourceIdList =
sectionComponents.map { sectionComponent ->
sectionComponent.focus.extractId()
},
sectionComponents.map { sectionComponent -> sectionComponent.focus.extractId() },
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1085,10 +1085,11 @@ class ConfigurationRegistryTest : RobolectricTest() {
@Test
fun testFetchNonWorkflowConfigResourcesWithNoFocusOrEntry() = runTest {
val appId = "app-id"
val composition = Composition().apply {
identifier = Identifier().apply { value = appId }
section = listOf(SectionComponent()) // Neither focus nor entry
}
val composition =
Composition().apply {
identifier = Identifier().apply { value = appId }
section = listOf(SectionComponent()) // Neither focus nor entry
}

configRegistry.fetchNonWorkflowConfigResources()

Expand All @@ -1098,9 +1099,7 @@ class ConfigurationRegistryTest : RobolectricTest() {

@Test
fun testPopulateConfigurationsMapWithNeitherFocusNorEntry() = runTest {
val composition = Composition().apply {
section = listOf(SectionComponent())
}
val composition = Composition().apply { section = listOf(SectionComponent()) }

configRegistry.populateConfigurationsMap(context, composition, false, "app-id") {}

Expand All @@ -1110,23 +1109,24 @@ class ConfigurationRegistryTest : RobolectricTest() {
@Test
fun testFetchNonWorkflowConfigResourcesWithAllFocus() = runTest {
val appId = "app-id"
val composition = Composition().apply {
identifier = Identifier().apply { value = appId }
section = listOf(
SectionComponent().apply {
focus = Reference().apply {
identifier = Identifier().apply { value = "focus-1" }
reference = "ResourceType/1"
}
}
)
}
val composition =
Composition().apply {
identifier = Identifier().apply { value = appId }
section =
listOf(
SectionComponent().apply {
focus =
Reference().apply {
identifier = Identifier().apply { value = "focus-1" }
reference = "ResourceType/1"
}
},
)
}

coEvery { fhirResourceDataSource.getResource(any()) } returns Bundle().apply {
addEntry().resource = composition
}
coEvery { fhirResourceDataSource.getResource(any()) } returns
Bundle().apply { addEntry().resource = composition }

configRegistry.fetchNonWorkflowConfigResources()

}
}

0 comments on commit 9e061ac

Please sign in to comment.