Skip to content

Commit

Permalink
Implement new Search#has function accepting resource type as param (#…
Browse files Browse the repository at this point in the history
…1927)

* Implement new Search#has function accepting resource type as param

Signed-off-by: Elly Kitoto <[email protected]>

* Run spotlessApply

Signed-off-by: Elly Kitoto <[email protected]>

* Clean up code

- Use named paramaters as well as rename lambda parameter

Signed-off-by: Elly Kitoto <[email protected]>

---------

Signed-off-by: Elly Kitoto <[email protected]>
  • Loading branch information
ellykits committed Mar 22, 2023
1 parent e0c92b2 commit f3336e6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
47 changes: 36 additions & 11 deletions engine/src/main/java/com/google/android/fhir/search/NestedSearch.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 Google LLC
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -50,6 +50,30 @@ inline fun <reified R : Resource> Search.has(
)
}

/**
* Provide limited support for reverse chaining on [Search] (See
* [this](https://www.hl7.org/fhir/search.html#has)).
*
* Example usage (Search for all Patients with Condition - Diabetes):
*
* ```
* fhirEngine.search<Patient> {
* has(resourceType = ResourceType.Condition, referenceParam = (Condition.SUBJECT) {
* filter(Condition.CODE, Coding("http:https://snomed.info/sct", "44054006", "Diabetes"))
* }
* }
* ```
*/
fun Search.has(
resourceType: ResourceType,
referenceParam: ReferenceClientParam,
init: Search.() -> Unit
) {
nestedSearches.add(
NestedSearch(referenceParam, Search(type = resourceType)).apply { search.init() }
)
}

/**
* Generates the complete nested query going to several depths depending on the [Search] dsl
* specified by the user .
Expand All @@ -61,16 +85,17 @@ internal fun List<NestedSearch>.nestedQuery(
return if (isEmpty()) {
null
} else {
map { it.nestedQuery(type) }.let {
SearchQuery(
query =
it.joinToString(
prefix = "AND a.resourceUuid IN ",
separator = " ${operation.logicalOperator} a.resourceUuid IN"
) { "(\n${it.query}\n) " },
args = it.flatMap { it.args }
)
}
map { it.nestedQuery(type) }
.let { searchQueries ->
SearchQuery(
query =
searchQueries.joinToString(
prefix = "AND a.resourceUuid IN ",
separator = " ${operation.logicalOperator} a.resourceUuid IN"
) { searchQuery -> "(\n${searchQuery.query}\n) " },
args = searchQueries.flatMap { it.args }
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1761,7 +1761,7 @@ class SearchTest {
val query =
Search(ResourceType.Patient)
.apply {
has<Condition>(Condition.SUBJECT) {
has(resourceType = ResourceType.Condition, referenceParam = Condition.SUBJECT) {
filter(
Condition.CODE,
{ value = of(Coding("http:https://snomed.info/sct", "44054006", "Diabetes")) }
Expand Down

0 comments on commit f3336e6

Please sign in to comment.