Skip to content

Commit

Permalink
Use subject's Related Entity Location meta tag on related resources (#…
Browse files Browse the repository at this point in the history
…3097)

* Use subject's Related Entity Location meta tag on related resources
---------

Signed-off-by: Elly Kitoto <[email protected]>
Co-authored-by: pld <[email protected]>
  • Loading branch information
ellykits and pld authored Feb 26, 2024
1 parent 2b5d44b commit 2785261
Showing 1 changed file with 42 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,9 @@ constructor(
this.id = questionnaireResponse.subject.extractId()
} else if (
extractedResourceUniquePropertyExpressionsMap.containsKey(resourceType) &&
previouslyExtractedResources.containsKey(resourceType)
previouslyExtractedResources.containsKey(
resourceType,
)
) {
val fhirPathExpression =
extractedResourceUniquePropertyExpressionsMap
Expand Down Expand Up @@ -366,7 +368,7 @@ constructor(
}

// Set the Group's Related Entity Location metadata tag on Resource before saving.
this.applyRelatedEntityLocationMetaTag(questionnaireConfig, context)
this.applyRelatedEntityLocationMetaTag(questionnaireConfig, context, subjectType)

defaultRepository.addOrUpdate(true, resource = this)

Expand Down Expand Up @@ -403,6 +405,7 @@ constructor(
questionnaireResponse.applyRelatedEntityLocationMetaTag(
questionnaireConfig,
context,
subjectType,
)
defaultRepository.addOrUpdate(resource = questionnaireResponse)
}
Expand All @@ -411,23 +414,29 @@ constructor(
private suspend fun Resource.applyRelatedEntityLocationMetaTag(
questionnaireConfig: QuestionnaireConfig,
context: Context,
subjectType: ResourceType?,
) {
questionnaireConfig.groupResource?.let {
if (it.groupIdentifier.isNotEmpty() && !it.removeGroup && !it.removeMember) {
val group =
loadResource(
ResourceType.Group,
it.groupIdentifier.extractLogicalIdUuid(),
)
as Group?
if (group != null) {
val system =
context.getString(
org.smartregister.fhircore.engine.R.string
.sync_strategy_related_entity_location_system,
)
group.meta.tag.filter { coding -> coding.system == system }.forEach(this.meta::addTag)
val resourceIdPair =
when {
!questionnaireConfig.resourceIdentifier.isNullOrEmpty() && subjectType != null -> {
Pair(subjectType, questionnaireConfig.resourceIdentifier!!)
}
!questionnaireConfig.groupResource?.groupIdentifier.isNullOrEmpty() &&
questionnaireConfig.groupResource?.removeGroup != true &&
questionnaireConfig.groupResource?.removeMember != true -> {
Pair(ResourceType.Group, questionnaireConfig.groupResource!!.groupIdentifier)
}
else -> null
}
if (resourceIdPair != null) {
val (resourceType, resourceId) = resourceIdPair
val resource = loadResource(resourceType = resourceType, resourceIdentifier = resourceId)
if (resource != null) {
val system =
context.getString(
org.smartregister.fhircore.engine.R.string.sync_strategy_related_entity_location_system,
)
resource.meta.tag.filter { coding -> coding.system == system }.forEach(this.meta::addTag)
}
}
}
Expand Down Expand Up @@ -497,7 +506,11 @@ constructor(
): ResourceType? {
val questionnaireSubjectType = questionnaire.subjectType.firstOrNull()?.code
return questionnaireConfig.resourceType
?: questionnaireSubjectType?.let { ResourceType.valueOf(it) }
?: questionnaireSubjectType?.let {
ResourceType.valueOf(
it,
)
}
}

private fun Resource?.applyResourceMetadata(
Expand Down Expand Up @@ -673,7 +686,12 @@ constructor(

if (libraryFilters.isNotEmpty()) {
defaultRepository.fhirEngine
.search<Library> { filter(Resource.RES_ID, *libraryFilters.toTypedArray()) }
.search<Library> {
filter(
Resource.RES_ID,
*libraryFilters.toTypedArray(),
)
}
.forEach { librarySearchResult ->
val result: Parameters =
fhirOperator.evaluateLibrary(
Expand All @@ -696,7 +714,11 @@ constructor(

if (BuildConfig.DEBUG) {
Timber.d(
"CQL :: Param found: ${cqlResultParameterComponent.name} with value: ${getStringRepresentation(resultParameterResource)}",
"CQL :: Param found: ${cqlResultParameterComponent.name} with value: ${
getStringRepresentation(
resultParameterResource,
)
}",
)
}
}
Expand Down

0 comments on commit 2785261

Please sign in to comment.