From 64a55e6920cb6280cf02a0d68152d9c03266518d Mon Sep 17 00:00:00 2001 From: Rkareko <47570855+Rkareko@users.noreply.github.com> Date: Thu, 24 Oct 2024 12:40:08 +0300 Subject: [PATCH] Add service method for mapping resources to extracted values string (#3572) * Add service method for mapping resources to extracted values string * Add kdocs for the mapResourcesToExtractedValues function --- .../engine/rulesengine/RulesFactory.kt | 26 +++++++++++ .../engine/rulesengine/RulesFactoryTest.kt | 45 +++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/rulesengine/RulesFactory.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/rulesengine/RulesFactory.kt index ce6af663ca..99af4f1a6a 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/rulesengine/RulesFactory.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/rulesengine/RulesFactory.kt @@ -557,6 +557,7 @@ constructor( return source?.take(limit) ?: emptyList() } + @JvmOverloads fun mapResourcesToExtractedValues( resources: List?, fhirPathExpression: String, @@ -568,6 +569,31 @@ constructor( ?: emptyList() } + /** + * This function combines all the string values retrieved from the [resources] using the + * [fhirPathExpression] to a list separated by the [separator] + * + * e.g for a provided list of Patients we can extract a string containing the family names using + * the [Patient.name.family] as the [fhirpathExpression] and [ | ] as the [separator] the + * returned string would be [John | Jane | James] + */ + @JvmOverloads + fun mapResourcesToExtractedValues( + resources: List?, + fhirPathExpression: String, + separator: String = ",", + ): String { + if (fhirPathExpression.isEmpty()) { + return "" + } + val results: List = + mapResourcesToExtractedValues( + resources = resources, + fhirPathExpression = fhirPathExpression, + ) + return results.joinToString(separator) + } + fun computeTotalCount(relatedResourceCounts: List?): Long = relatedResourceCounts?.sumOf { it.count } ?: 0 diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/rulesengine/RulesFactoryTest.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/rulesengine/RulesFactoryTest.kt index d303149695..246507880a 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/rulesengine/RulesFactoryTest.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/rulesengine/RulesFactoryTest.kt @@ -1306,6 +1306,51 @@ class RulesFactoryTest : RobolectricTest() { } } + @Test + fun mapResourcesToExtractedValuesReturnsCorrectlyFormattedString() { + val patientsList = + listOf( + Patient().apply { + birthDate = LocalDate.parse("2015-10-03").toDate() + addName().apply { family = "alpha" } + }, + Patient().apply { + birthDate = LocalDate.parse("2017-10-03").toDate() + addName().apply { family = "beta" } + }, + Patient().apply { + birthDate = LocalDate.parse("2018-10-03").toDate() + addName().apply { family = "gamma" } + }, + ) + + val names = + rulesEngineService.mapResourcesToExtractedValues(patientsList, "Patient.name.family", " | ") + Assert.assertEquals("alpha | beta | gamma", names) + } + + @Test + fun mapResourcesToExtractedValuesReturnsEmptyStringWhenFhirPathExpressionIsEmpty() { + val patientsList = + listOf( + Patient().apply { + birthDate = LocalDate.parse("2015-10-03").toDate() + addName().apply { family = "alpha" } + }, + Patient().apply { + birthDate = LocalDate.parse("2017-10-03").toDate() + addName().apply { family = "beta" } + }, + Patient().apply { + birthDate = LocalDate.parse("2018-10-03").toDate() + addName().apply { family = "gamma" } + }, + ) + + val names = rulesEngineService.mapResourcesToExtractedValues(patientsList, "", " | ") + Assert.assertEquals("", names) + } + private fun getListOfResource(): List { return listOf( Group().apply { id = "group-id-1" },