Skip to content

Commit

Permalink
Add service method for mapping resources to extracted values string (#…
Browse files Browse the repository at this point in the history
…3572)

* Add service method for mapping resources to extracted values string

* Add kdocs for the mapResourcesToExtractedValues function
  • Loading branch information
Rkareko authored Oct 24, 2024
1 parent 36808ea commit 64a55e6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ constructor(
return source?.take(limit) ?: emptyList()
}

@JvmOverloads
fun mapResourcesToExtractedValues(
resources: List<Resource>?,
fhirPathExpression: String,
Expand All @@ -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<Resource>?,
fhirPathExpression: String,
separator: String = ",",
): String {
if (fhirPathExpression.isEmpty()) {
return ""
}
val results: List<Any> =
mapResourcesToExtractedValues(
resources = resources,
fhirPathExpression = fhirPathExpression,
)
return results.joinToString(separator)
}

fun computeTotalCount(relatedResourceCounts: List<RelatedResourceCount>?): Long =
relatedResourceCounts?.sumOf { it.count } ?: 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Resource> {
return listOf(
Group().apply { id = "group-id-1" },
Expand Down

0 comments on commit 64a55e6

Please sign in to comment.