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

Add Composition.entry and Composition.focus #3662

Merged
merged 14 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -334,26 +334,42 @@ constructor(
}
}
} else {
composition.retrieveCompositionSections().forEach {
if (it.hasFocus() && it.focus.hasReferenceElement() && it.focus.hasIdentifier()) {
val configIdentifier = it.focus.identifier.value
val referenceResourceType = it.focus.reference.substringBefore(TYPE_REFERENCE_DELIMITER)
if (isAppConfig(referenceResourceType) && !isIconConfig(configIdentifier)) {
val extractedId = it.focus.extractId()
try {
val configBinary = fhirEngine.get<Binary>(extractedId)
configsJsonMap[configIdentifier] = configBinary.content.decodeToString()
} catch (resourceNotFoundException: ResourceNotFoundException) {
Timber.e("Missing Binary file with ID :$extractedId")
withContext(dispatcherProvider.main()) { configsLoadedCallback(false) }
}
composition.retrieveCompositionSections().forEach { sectionComponent ->
if (sectionComponent.hasFocus() && sectionComponent.focus.hasReferenceElement() && sectionComponent.focus.hasIdentifier()) {
val configIdentifier = sectionComponent.focus.identifier.value
val referenceResourceType = sectionComponent.focus.reference.substringBefore(TYPE_REFERENCE_DELIMITER)
sharon2719 marked this conversation as resolved.
Show resolved Hide resolved
addBinaryToConfigsJsonMap(referenceResourceType, configIdentifier, sectionComponent, configsLoadedCallback)
sharon2719 marked this conversation as resolved.
Show resolved Hide resolved
}
if (sectionComponent.hasEntry() && sectionComponent.entry.isNotEmpty()){
sectionComponent.entry.forEach{ entryReference ->
val configIdentifier = entryReference.identifier.value
val referenceResourceType = entryReference.reference.substringBefore(TYPE_REFERENCE_DELIMITER)
sharon2719 marked this conversation as resolved.
Show resolved Hide resolved
addBinaryToConfigsJsonMap(referenceResourceType, configIdentifier, sectionComponent, configsLoadedCallback)
sharon2719 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
configsLoadedCallback(true)
}

private suspend fun addBinaryToConfigsJsonMap(
referenceResourceType: String,
configIdentifier: String,
sectionComponent: Composition.SectionComponent,
sharon2719 marked this conversation as resolved.
Show resolved Hide resolved
configsLoadedCallback: (Boolean) -> Unit
) {
if (isAppConfig(referenceResourceType) && !isIconConfig(configIdentifier)) {
sharon2719 marked this conversation as resolved.
Show resolved Hide resolved
val extractedId = sectionComponent.focus.extractId()
sharon2719 marked this conversation as resolved.
Show resolved Hide resolved
try {
val configBinary = fhirEngine.get<Binary>(extractedId)
configsJsonMap[configIdentifier] = configBinary.content.decodeToString()
} catch (resourceNotFoundException: ResourceNotFoundException) {
Timber.e("Missing Binary file with ID :$extractedId")
withContext(dispatcherProvider.main()) { configsLoadedCallback(false) }
}
}
}

private fun isAppConfig(referenceResourceType: String) =
referenceResourceType in arrayOf(ResourceType.Binary.name, ResourceType.Parameters.name)

Expand Down Expand Up @@ -411,42 +427,30 @@ constructor(
val parsedAppId = appId.substringBefore(TYPE_REFERENCE_DELIMITER).trim()
val compositionResource = fetchRemoteCompositionByAppId(parsedAppId)
compositionResource?.let { composition ->
composition
.retrieveCompositionSections()
.asSequence()
.filter { it.hasFocus() && it.focus.hasReferenceElement() }
.groupBy { section ->
section.focus.reference.substringBefore(
TYPE_REFERENCE_DELIMITER,
missingDelimiterValue = "",
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 = "",
)
}
.filter { entry -> entry.key in FILTER_RESOURCE_LIST }
.forEach { entry: Map.Entry<String, List<Composition.SectionComponent>> ->
if (entry.key == ResourceType.List.name) {
processCompositionListResources(entry)
} else {
val chunkedResourceIdList = entry.value.chunked(MANIFEST_PROCESSOR_BATCH_SIZE)

chunkedResourceIdList.forEach { sectionComponents ->
Timber.d(
"Fetching config resource ${entry.key}: with ids ${
sectionComponents.joinToString(
",",
)
}",
)
fetchResources(
resourceType = entry.key,
resourceIdList =
sectionComponents.map { sectionComponent ->
sectionComponent.focus.extractId()
},
)
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)

// Save composition after fetching all the referenced section resources
addOrUpdate(compositionResource)

Expand All @@ -455,6 +459,34 @@ constructor(
}
}

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)
} else {
val chunkedResourceIdList = entry.value.chunked(MANIFEST_PROCESSOR_BATCH_SIZE)

chunkedResourceIdList.forEach { sectionComponents ->
Timber.d(
"Fetching config resource ${entry.key}: with ids ${
sectionComponents.joinToString(
",",
)
}",
)
fetchResources(
resourceType = entry.key,
resourceIdList =
sectionComponents.map { sectionComponent ->
sectionComponent.focus.extractId()
},
)
}
}
}
}

suspend fun fetchRemoteImplementationGuideByAppId(
appId: String?,
appVersionCode: Int?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import org.hl7.fhir.r4.model.ResourceType
import org.hl7.fhir.r4.model.StructureMap
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertThrows
import org.junit.Assert.assertTrue
Expand Down Expand Up @@ -1080,4 +1081,190 @@ class ConfigurationRegistryTest : RobolectricTest() {
assertEquals(questionnaireId, questionnaire.logicalId)
}
}
// @Test
// fun testPopulateConfigurationsMapWithAllFocus() = runTest {
// val composition = Composition().apply {
// section = listOf(
// SectionComponent().apply {
// focus = Reference().apply {
// identifier = Identifier().apply { value = "focus-1" }
// reference = "ResourceType/1"
// }
// },
// SectionComponent().apply {
// focus = Reference().apply {
// identifier = Identifier().apply { value = "focus-2" }
// reference = "ResourceType/2"
// }
// }
// )
// }
//
// configRegistry.populateConfigurationsMap(context, composition, false, "app-id") {}
//
// println("ConfigsJsonMap: ${configRegistry.configsJsonMap}")
//
// assertTrue("focus-1 should be in configsJsonMap", configRegistry.configsJsonMap.containsKey("focus-1"))
// assertTrue("focus-2 should be in configsJsonMap", configRegistry.configsJsonMap.containsKey("focus-2"))
// }
//
// @Test
// fun testPopulateConfigurationsMapWithAllEntry() = runTest {
// val composition = Composition().apply {
// section = listOf(
// SectionComponent().apply {
// entry = listOf(
// Reference().apply {
// identifier = Identifier().apply { value = "entry-1" }
// reference = "ResourceType/1"
// }
// )
// },
// SectionComponent().apply {
// entry = listOf(
// Reference().apply {
// identifier = Identifier().apply { value = "entry-2" }
// reference = "ResourceType/2"
// }
// )
// }
// )
// }
//
// configRegistry.populateConfigurationsMap(context, composition, false, "app-id") {}
//
// println("ConfigsJsonMap: ${configRegistry.configsJsonMap}")
//
// assertTrue("entry-1 should be in configsJsonMap", configRegistry.configsJsonMap.containsKey("entry-1"))
// assertTrue("entry-2 should be in configsJsonMap", configRegistry.configsJsonMap.containsKey("entry-2"))
// }
//
// @Test
// fun testPopulateConfigurationsMapWithBothFocusAndEntryInSameSection() = runTest {
// val composition = Composition().apply {
// section = listOf(
// SectionComponent().apply {
// focus = Reference().apply {
// identifier = Identifier().apply { value = "focus-1" }
// reference = "ResourceType/1"
// }
// entry = listOf(
// Reference().apply {
// identifier = Identifier().apply { value = "entry-1" }
// reference = "ResourceType/2"
// }
// )
// }
// )
// }
//
// configRegistry.populateConfigurationsMap(context, composition, false, "app-id") {}
//
// println("ConfigsJsonMap: ${configRegistry.configsJsonMap}")
//
// assertTrue("focus-1 should be in configsJsonMap", configRegistry.configsJsonMap.containsKey("focus-1"))
// assertFalse("entry-1 should not be in configsJsonMap", configRegistry.configsJsonMap.containsKey("entry-1"))
// }
//
// @Test
// fun testFetchNonWorkflowConfigResourcesWithAllEntry() = runTest {
// val appId = "app-id"
// val composition = Composition().apply {
// identifier = Identifier().apply { value = appId }
// section = listOf(
// SectionComponent().apply {
// entry = listOf(
// Reference().apply {
// identifier = Identifier().apply { value = "entry-1" }
// reference = "ResourceType/1"
// }
// )
// }
// )
// }
//
// configRegistry.fetchNonWorkflowConfigResources()
//
// println("ConfigsJsonMap: ${configRegistry.configsJsonMap}")
//
// assertTrue("entry-1 should be in configsJsonMap", configRegistry.configsJsonMap.containsKey("entry-1"))
// }
//
// @Test
// fun testFetchNonWorkflowConfigResourcesWithBothFocusAndEntryInSameSection() = 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"
// }
// entry = listOf(
// Reference().apply {
// identifier = Identifier().apply { value = "entry-1" }
// reference = "ResourceType/2"
// }
// )
// }
// )
// }
//
// configRegistry.fetchNonWorkflowConfigResources()
//
// println("ConfigsJsonMap: ${configRegistry.configsJsonMap}")
//
// assertTrue("focus-1 should be in configsJsonMap", configRegistry.configsJsonMap.containsKey("focus-1"))
// assertFalse("entry-1 should not be in configsJsonMap", configRegistry.configsJsonMap.containsKey("entry-1"))
// }


@Test
fun testFetchNonWorkflowConfigResourcesWithNoFocusOrEntry() = runTest {
val appId = "app-id"
val composition = Composition().apply {
identifier = Identifier().apply { value = appId }
section = listOf(SectionComponent()) // Neither focus nor entry
}

configRegistry.fetchNonWorkflowConfigResources()

// Validate no crash occurs and configsJsonMap remains empty
assertTrue(configRegistry.configsJsonMap.isEmpty())
}

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

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

assertTrue(configRegistry.configsJsonMap.isEmpty())
}

@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"
}
}
)
}

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

configRegistry.fetchNonWorkflowConfigResources()

}
}
Loading