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

Fix loading related resources data for LIST #3539

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ data class ListProperties(
val showDivider: Boolean = true,
val emptyList: NoResultsConfig? = null,
val orientation: ListOrientation = ListOrientation.VERTICAL,
val resources: List<ListResource> = emptyList(),
val resources: List<ListResourceConfig> = emptyList(),
) : ViewProperties(), Parcelable {
override fun interpolate(computedValuesMap: Map<String, Any>): ListProperties {
return this.copy(
Expand All @@ -61,12 +61,12 @@ enum class ListOrientation {

@Serializable
@Parcelize
data class ListResource(
data class ListResourceConfig(
val id: String? = null,
val relatedResourceId: String? = null,
val resourceType: ResourceType,
val conditionalFhirPathExpression: String? = null,
val sortConfig: SortConfig? = null,
val fhirPathExpression: String? = null,
val relatedResources: List<ListResource> = emptyList(),
val relatedResources: List<ListResourceConfig> = emptyList(),
) : Parcelable, java.io.Serializable
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.runtime.snapshots.SnapshotStateMap
import com.google.android.fhir.datacapture.extensions.logicalId
import javax.inject.Inject
import org.hl7.fhir.r4.model.Resource
import org.jeasy.rules.api.Facts
import org.smartregister.fhircore.engine.configuration.view.ListProperties
import org.smartregister.fhircore.engine.configuration.view.ListResource
import org.smartregister.fhircore.engine.configuration.view.ListResourceConfig
import org.smartregister.fhircore.engine.domain.model.RepositoryResourceData
import org.smartregister.fhircore.engine.domain.model.ResourceData
import org.smartregister.fhircore.engine.domain.model.RuleConfig
import org.smartregister.fhircore.engine.domain.model.ViewType
import org.smartregister.fhircore.engine.util.extension.extractLogicalIdUuid
import javax.inject.Inject

/**
* This class is used to fire rules used to extract and manipulate data from FHIR resources.
Expand Down Expand Up @@ -73,17 +73,15 @@ class ResourceDataRulesExecutor @Inject constructor(val rulesFactory: RulesFacto
listResourceDataStateMap: SnapshotStateMap<String, SnapshotStateList<ResourceData>>,
) {
listProperties.resources.forEach { listResource ->
// Initialize to be updated incrementally as resources are transformed into ResourceData
val resourceDataSnapshotStateList = mutableStateListOf<ResourceData>()
listResourceDataStateMap[listProperties.id] = resourceDataSnapshotStateList

filteredListResources(relatedResourcesMap, listResource)
.mapToResourceData(
listResource = listResource,
listResourceConfig = listResource,
relatedResourcesMap = relatedResourcesMap,
ruleConfigs = listProperties.registerCard.rules,
computedValuesMap = computedValuesMap,
resourceDataSnapshotStateList = resourceDataSnapshotStateList,
resourceDataSnapshotStateList =
listResourceDataStateMap.getOrPut(listProperties.id) { mutableStateListOf() },
listResourceDataStateMap = listResourceDataStateMap,
)
}
}
Expand All @@ -107,36 +105,63 @@ class ResourceDataRulesExecutor @Inject constructor(val rulesFactory: RulesFacto
}

private fun List<Resource>.mapToResourceData(
listResource: ListResource,
listResourceConfig: ListResourceConfig,
relatedResourcesMap: Map<String, List<Resource>>,
ruleConfigs: List<RuleConfig>,
computedValuesMap: Map<String, Any>,
resourceDataSnapshotStateList: SnapshotStateList<ResourceData>,
listResourceDataStateMap: SnapshotStateMap<String, SnapshotStateList<ResourceData>>,
) {
this.forEach { resource ->
this.forEach { baseListResource ->
val relatedResourcesQueue =
ArrayDeque<Pair<Resource, List<ListResourceConfig>>>().apply {
addFirst(Pair(baseListResource, listOf(listResourceConfig)))
}

val listItemRelatedResources = mutableMapOf<String, List<Resource>>()
listResource.relatedResources.forEach { relatedListResource ->
val retrieveRelatedResources: List<Resource>? =
relatedListResource.fhirPathExpression.let {
rulesFactory.rulesEngineService.retrieveRelatedResources(
resource = resource,
relatedResourceKey =
relatedListResource.relatedResourceId ?: relatedListResource.resourceType.name,
referenceFhirPathExpression = it,
relatedResourcesMap = relatedResourcesMap,
)
}
if (!retrieveRelatedResources.isNullOrEmpty()) {
listItemRelatedResources[
relatedListResource.id ?: relatedListResource.resourceType.name,
] =
if (!relatedListResource.conditionalFhirPathExpression.isNullOrEmpty()) {
rulesFactory.rulesEngineService.filterResources(
retrieveRelatedResources,
relatedListResource.conditionalFhirPathExpression,
while (relatedResourcesQueue.isNotEmpty()) {
val (currentResource, currentListResourceConfig) = relatedResourcesQueue.removeFirst()
currentListResourceConfig.forEach { relatedListResourceConfig ->
val retrievedRelatedResources: List<Resource>? =
relatedListResourceConfig.fhirPathExpression?.let {
rulesFactory.rulesEngineService.retrieveRelatedResources(
resource = currentResource,
relatedResourceKey =
relatedListResourceConfig.relatedResourceId
?: relatedListResourceConfig.resourceType.name,
referenceFhirPathExpression = it,
relatedResourcesMap = relatedResourcesMap,
)
} else {
retrieveRelatedResources
}
rulesFactory.rulesEngineService
.filterResources(
resources = retrievedRelatedResources,
conditionalFhirPathExpression =
relatedListResourceConfig.conditionalFhirPathExpression,
)
.also { filteredResources ->
// Add to queue for processing
filteredResources.forEach {
relatedResourcesQueue.addLast(Pair(it, relatedListResourceConfig.relatedResources))
}

// Apply configurable sorting to related resources
val sortConfig = relatedListResourceConfig.sortConfig
if (sortConfig == null || sortConfig.fhirPathExpression.isBlank()) {
listItemRelatedResources[
relatedListResourceConfig.id ?: relatedListResourceConfig.resourceType.name,
] = filteredResources
} else {
listItemRelatedResources[
relatedListResourceConfig.id ?: relatedListResourceConfig.resourceType.name,
] =
rulesFactory.rulesEngineService.sortResources(
resources = filteredResources,
fhirPathExpression = sortConfig.fhirPathExpression,
dataType = sortConfig.dataType.name,
order = sortConfig.order.name,
) ?: filteredResources
}
}
}
}
Expand All @@ -146,19 +171,19 @@ class ResourceDataRulesExecutor @Inject constructor(val rulesFactory: RulesFacto
ruleConfigs = ruleConfigs,
repositoryResourceData =
RepositoryResourceData(
resourceRulesEngineFactId = null,
resource = resource,
resourceRulesEngineFactId = listResourceConfig.id,
resource = baseListResource,
relatedResourcesMap = listItemRelatedResources,
),
params = emptyMap(),
)

resourceDataSnapshotStateList.add(
ResourceData(
baseResourceId = resource.logicalId.extractLogicalIdUuid(),
baseResourceType = resource.resourceType,
computedValuesMap =
computedValuesMap.plus(listComputedValuesMap), // Reuse computed values
baseResourceId = baseListResource.logicalId,
baseResourceType = baseListResource.resourceType,
computedValuesMap = computedValuesMap.plus(listComputedValuesMap),
listResourceDataMap = listResourceDataStateMap,
),
)
}
Expand All @@ -167,41 +192,33 @@ class ResourceDataRulesExecutor @Inject constructor(val rulesFactory: RulesFacto
/**
* This function returns a list of filtered resources. The required list is obtained from
* [relatedResourceMap], then a filter is applied based on the condition returned from the
* extraction of the [ListResource] conditional FHIR path expression
* extraction of the [ListResourceConfig] conditional FHIR path expression. The list is sorted if
* configurations for sorting are provided.
*/
private fun filteredListResources(
relatedResourceMap: Map<String, List<Resource>>,
listResource: ListResource,
listResource: ListResourceConfig,
): List<Resource> {
val relatedResourceKey = listResource.relatedResourceId ?: listResource.resourceType.name
val newListRelatedResources = relatedResourceMap[relatedResourceKey]

// conditionalFhirPath expression e.g. "Task.status == 'ready'" to filter tasks that are due
// Filter by condition derived from fhirPathExpression otherwise return original or empty list
val resources =
if (
newListRelatedResources != null &&
!listResource.conditionalFhirPathExpression.isNullOrEmpty()
) {
rulesFactory.rulesEngineService.filterResources(
resources = newListRelatedResources,
conditionalFhirPathExpression = listResource.conditionalFhirPathExpression,
)
} else {
newListRelatedResources ?: listOf()
}
rulesFactory.rulesEngineService.filterResources(
resources = relatedResourceMap[relatedResourceKey],
conditionalFhirPathExpression = listResource.conditionalFhirPathExpression,
)

// Sort resources if valid sort configuration is provided
val sortConfig = listResource.sortConfig

// Sort resources if sort configuration is provided
return if (sortConfig != null && sortConfig.fhirPathExpression.isNotEmpty()) {
return if (sortConfig == null || sortConfig.fhirPathExpression.isEmpty()) {
resources
} else {
rulesFactory.rulesEngineService.sortResources(
resources = resources,
fhirPathExpression = sortConfig.fhirPathExpression,
dataType = sortConfig.dataType.name,
order = sortConfig.order.name,
) ?: resources
} else {
resources
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -425,14 +425,15 @@ constructor(

/**
* This function filters resources provided the condition extracted from the
* [conditionalFhirPathExpression] is met
* [conditionalFhirPathExpression] is met. Returns the original source or empty resources list
* if FHIR path expression is null.
*/
fun filterResources(
resources: List<Resource>?,
conditionalFhirPathExpression: String,
conditionalFhirPathExpression: String?,
): List<Resource> {
if (conditionalFhirPathExpression.isEmpty()) {
return emptyList()
if (conditionalFhirPathExpression.isNullOrBlank()) {
return resources ?: emptyList()
}
return resources?.filter {
fhirPathDataExtractor.extractValue(it, conditionalFhirPathExpression).toBoolean()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import org.smartregister.fhircore.engine.app.fakes.Faker
import org.smartregister.fhircore.engine.configuration.ConfigurationRegistry
import org.smartregister.fhircore.engine.configuration.register.RegisterCardConfig
import org.smartregister.fhircore.engine.configuration.view.ListProperties
import org.smartregister.fhircore.engine.configuration.view.ListResource
import org.smartregister.fhircore.engine.configuration.view.ListResourceConfig
import org.smartregister.fhircore.engine.data.local.DefaultRepository
import org.smartregister.fhircore.engine.domain.model.RepositoryResourceData
import org.smartregister.fhircore.engine.domain.model.ResourceData
Expand Down Expand Up @@ -158,7 +158,7 @@ class ResourceDataRulesExecutorTest : RobolectricTest() {
val anotherPatient =
Faker.buildPatient(id = "anotherPatient", given = "Abel", family = "Mandela")
val listResource =
ListResource(
ListResourceConfig(
"id",
resourceType = ResourceType.Patient,
sortConfig =
Expand Down Expand Up @@ -211,7 +211,7 @@ class ResourceDataRulesExecutorTest : RobolectricTest() {
val viewType = ViewType.CARD
val patient = Faker.buildPatient()
val listResource =
ListResource(
ListResourceConfig(
"id",
resourceType = ResourceType.Patient,
conditionalFhirPathExpression = "Patient.active",
Expand Down Expand Up @@ -254,13 +254,13 @@ class ResourceDataRulesExecutorTest : RobolectricTest() {
val viewType = ViewType.CARD
val patient = Faker.buildPatient()
val listResource =
ListResource(
ListResourceConfig(
"id",
resourceType = ResourceType.Patient,
conditionalFhirPathExpression = "Patient.active",
relatedResources =
listOf(
ListResource(
ListResourceConfig(
null,
resourceType = ResourceType.Task,
fhirPathExpression = "Task.for.reference",
Expand Down Expand Up @@ -320,12 +320,12 @@ class ResourceDataRulesExecutorTest : RobolectricTest() {
val patient = Faker.buildPatient()
val anotherPatient = Faker.buildPatient("anotherId")
val listResource =
ListResource(
ListResourceConfig(
resourceType = ResourceType.Patient,
conditionalFhirPathExpression = "Patient.active",
relatedResources =
listOf(
ListResource(
ListResourceConfig(
id = patientReadyTasks,
resourceType = ResourceType.Task,
conditionalFhirPathExpression = "Task.status = 'ready'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import org.smartregister.fhircore.engine.configuration.register.RegisterCardConf
import org.smartregister.fhircore.engine.configuration.view.CompoundTextProperties
import org.smartregister.fhircore.engine.configuration.view.ListOrientation
import org.smartregister.fhircore.engine.configuration.view.ListProperties
import org.smartregister.fhircore.engine.configuration.view.ListResource
import org.smartregister.fhircore.engine.configuration.view.ListResourceConfig
import org.smartregister.fhircore.engine.domain.model.ResourceData
import org.smartregister.fhircore.engine.domain.model.ViewType
import org.smartregister.fhircore.engine.ui.theme.DefaultColor
Expand Down Expand Up @@ -187,7 +187,7 @@ private fun ListWithHorizontalOrientationPreview() {
borderRadius = 10,
emptyList = NoResultsConfig(message = ""),
resources =
listOf(ListResource(id = "carePlanList", resourceType = ResourceType.CarePlan)),
listOf(ListResourceConfig(id = "carePlanList", resourceType = ResourceType.CarePlan)),
fillMaxHeight = true,
registerCard =
RegisterCardConfig(
Expand Down Expand Up @@ -251,7 +251,7 @@ private fun ListWithVerticalOrientationPreview() {
borderRadius = 10,
emptyList = NoResultsConfig(message = "No care Plans"),
resources =
listOf(ListResource(id = "carePlanList", resourceType = ResourceType.CarePlan)),
listOf(ListResourceConfig(id = "carePlanList", resourceType = ResourceType.CarePlan)),
fillMaxWidth = true,
registerCard =
RegisterCardConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,7 @@
.conditional(
properties.clickable.toBoolean(),
{
clickable {
(properties as RowProperties)
.actions
.handleClickEvent(navController, resourceData)
}
clickable { properties.actions.handleClickEvent(navController, resourceData) }

Check warning on line 138 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/shared/components/ViewGenerator.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/shared/components/ViewGenerator.kt#L138

Added line #L138 was not covered by tests
},
),
verticalArrangement =
Expand Down Expand Up @@ -212,11 +208,7 @@
.conditional(
properties.clickable.toBoolean(),
{
clickable {
(properties as RowProperties)
.actions
.handleClickEvent(navController, resourceData)
}
clickable { properties.actions.handleClickEvent(navController, resourceData) }

Check warning on line 211 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/shared/components/ViewGenerator.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/shared/components/ViewGenerator.kt#L211

Added line #L211 was not covered by tests
},
),
horizontalArrangement =
Expand Down
Loading