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

Add variable Extension support #1328

Merged
merged 165 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
165 commits
Select commit Hold shift + click to select a range
64ba13c
Added root level and item level variable extension support in Questio…
shoaibmushtaq25 Apr 21, 2022
8780a17
Refactored QuestionnaireViewModel to make code more readable
shoaibmushtaq25 Apr 25, 2022
a01940c
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Apr 25, 2022
9da5928
Merge branch 'master' of github.com:opensrp/android-fhir into sm/1059…
shoaibmushtaq25 Apr 27, 2022
7e48cf1
Refactored ancestors logic and add a Path to Variables map to save an…
shoaibmushtaq25 Apr 27, 2022
02e277b
Merge branch 'sm/1059_support_variable_extension' of github.com:opens…
shoaibmushtaq25 Apr 27, 2022
5bcfab9
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Apr 27, 2022
bbd55f5
Refactored variables code to make it more readable
shoaibmushtaq25 Apr 27, 2022
1087186
Refactored ancestors variables code to make it readable
shoaibmushtaq25 Apr 27, 2022
b20068f
Refactored to make the variables update for all siblings and ancestor…
shoaibmushtaq25 Apr 27, 2022
750bb74
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Apr 27, 2022
37c6d3c
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Apr 28, 2022
3aa52c4
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 May 6, 2022
a6fb990
Code readability improvements
shoaibmushtaq25 May 9, 2022
1b27bd6
Refactored, removed the extra functions
shoaibmushtaq25 May 9, 2022
5dadc75
Move Extension URl to MoreQuestionnaireItemComponents file
shoaibmushtaq25 May 10, 2022
f035779
Added unit tests for variable extension in QuestionnaireViewModelTest
shoaibmushtaq25 May 10, 2022
c62c08f
Add more unit tests for variable extension
shoaibmushtaq25 May 10, 2022
495727d
Added more unit tests for variable extension for invalid expresssions
shoaibmushtaq25 May 11, 2022
ded2a04
Added some documemtation for Variables extensions
shoaibmushtaq25 May 11, 2022
c032a8b
Fix spellings
shoaibmushtaq25 May 11, 2022
676e1d3
Added unit tests for FHIRPathEnginehostServices
shoaibmushtaq25 May 12, 2022
b1d1db9
Fix unit test in FHIRPathEngineHostServices
shoaibmushtaq25 May 12, 2022
35d217e
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 May 12, 2022
fe95e9a
Fixed wrong ArgumentMatchers on FHIRPathEngineHostServices file
shoaibmushtaq25 May 12, 2022
2f25b7e
Merge branch 'master' into sm/1059_support_variable_extension
maimoonak May 12, 2022
74aba60
Update Variables logic based on PR feedback
shoaibmushtaq25 May 13, 2022
7b7de19
Merge branch 'sm/1059_support_variable_extension' of github.com:opens…
shoaibmushtaq25 May 13, 2022
14a85ed
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 May 13, 2022
9585c23
Update nullability of updateVariable in quetionnaireViewModel
shoaibmushtaq25 May 13, 2022
7177219
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 May 13, 2022
bf0ff0d
Docs updated
shoaibmushtaq25 May 16, 2022
63bb6b3
Merge branch 'sm/1059_support_variable_extension' of github.com:opens…
shoaibmushtaq25 May 16, 2022
b2cfce4
Docs updated
shoaibmushtaq25 May 16, 2022
5b40268
Added more tests to FHIRPathEngineHostServices
shoaibmushtaq25 May 16, 2022
e5732cb
Added more unit test for QuestionnaireViewModel
shoaibmushtaq25 May 16, 2022
4e8ee59
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 May 18, 2022
474d495
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 May 19, 2022
d845b52
Update evaluateVariable method to make it re-usable
shoaibmushtaq25 May 19, 2022
a5eff26
Merge branch 'sm/1059_support_variable_extension' of github.com:opens…
shoaibmushtaq25 May 19, 2022
8ff2d99
updated variableName
shoaibmushtaq25 May 19, 2022
5f6a049
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 May 20, 2022
0d35b86
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 May 24, 2022
6f887bf
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 May 30, 2022
38ebbc6
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 May 30, 2022
ab76d99
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jun 2, 2022
b380d62
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jun 6, 2022
da24c63
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jun 6, 2022
7d0a072
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jun 7, 2022
e008b41
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jun 7, 2022
7c883ee
Removed unnecesary configs and Test runner
shoaibmushtaq25 Jun 7, 2022
ab7dfd2
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jun 9, 2022
cb10218
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jun 10, 2022
14cb554
Updated code to add dependent variables logic in pathToVariablesMap
shoaibmushtaq25 Jun 13, 2022
27cfd74
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jun 13, 2022
d3ecaa0
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jun 15, 2022
2904941
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jun 16, 2022
3b86fc5
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jun 20, 2022
70b851b
Add findDependents logic to find linkid and variable dependents
shoaibmushtaq25 Jun 20, 2022
97586f6
Fix spotless check
shoaibmushtaq25 Jun 20, 2022
7bea0c6
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jun 22, 2022
eecdbce
Fix broken unit tests
shoaibmushtaq25 Jun 22, 2022
0cd8441
Merge branch 'sm/1059_support_variable_extension' of github.com:opens…
shoaibmushtaq25 Jun 22, 2022
760befe
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jun 22, 2022
c0abb76
Fix broken unit tests, make variable null instead of empty
shoaibmushtaq25 Jun 22, 2022
c94c23e
Add more unit tests for QuestionnaireViewModel
shoaibmushtaq25 Jun 23, 2022
29925b9
Add more unit tests
shoaibmushtaq25 Jun 23, 2022
1dbb776
Fix regex for linkId and variable name
shoaibmushtaq25 Jun 23, 2022
0b1b57a
Added more tests
shoaibmushtaq25 Jun 24, 2022
b212dd7
Added documentation
shoaibmushtaq25 Jun 24, 2022
0e44bcd
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jun 24, 2022
ae004f5
Refactor some unit tests of QuestionnaireViewModel
shoaibmushtaq25 Jun 24, 2022
a2e0f26
Merge branch 'sm/1059_support_variable_extension' of github.com:opens…
shoaibmushtaq25 Jun 24, 2022
624c3a4
Added more test coverage
shoaibmushtaq25 Jun 28, 2022
bfdeaf9
Added more test case and require check for expression language
shoaibmushtaq25 Jun 28, 2022
8bfa16b
Fix regex to match variable name
shoaibmushtaq25 Jun 29, 2022
b39f2ed
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jun 30, 2022
63d894b
Added missing unit tests for page flow
shoaibmushtaq25 Jul 1, 2022
710c4fd
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jul 1, 2022
fa0c5c6
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jul 1, 2022
4f59276
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jul 5, 2022
66e176b
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jul 5, 2022
558eaa9
Fix spotless Check
shoaibmushtaq25 Jul 5, 2022
0f93bbf
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jul 6, 2022
e0be917
Fix spotless check
shoaibmushtaq25 Jul 6, 2022
35d2ec2
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jul 6, 2022
efdd925
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jul 8, 2022
f564ac7
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jul 13, 2022
94fc5f3
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jul 18, 2022
a937dc8
Spotless apply
shoaibmushtaq25 Jul 18, 2022
b203b56
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jul 20, 2022
c7c9a72
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jul 21, 2022
b462f1a
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jul 21, 2022
e0b110d
Changed solution approach, wrote a method to use it to calculate expr…
shoaibmushtaq25 Jul 22, 2022
3ddebef
Remove questionnairePreOrderList
shoaibmushtaq25 Jul 22, 2022
c01a57a
Refactored findVariable method to make it more readable
shoaibmushtaq25 Jul 22, 2022
4dcd62c
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jul 22, 2022
c0a103b
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jul 26, 2022
e9b1895
Removed broken, unrelated unit tests of QuestionnaireViewModel
shoaibmushtaq25 Jul 26, 2022
fa8769d
Make evaluateExpression internal to access it for use and testing
shoaibmushtaq25 Jul 26, 2022
e3ecb4c
Spotless apply
shoaibmushtaq25 Jul 26, 2022
91ff3c3
Added unit tests for root level and questionnaire item, origin variables
shoaibmushtaq25 Jul 26, 2022
aff9e23
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jul 26, 2022
9844af8
Added more tests for variables in QuestionnaireViewModelTest
shoaibmushtaq25 Jul 26, 2022
7b7f375
Merge branch 'sm/1059_support_variable_extension' of github.com:opens…
shoaibmushtaq25 Jul 26, 2022
86828d5
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jul 27, 2022
2f72822
Added failure cases and their test cases for fhirpath expressions
shoaibmushtaq25 Jul 27, 2022
f611540
Merge branch 'sm/1059_support_variable_extension' of github.com:opens…
shoaibmushtaq25 Jul 27, 2022
721e5b8
Remove un-related unit tests
shoaibmushtaq25 Jul 27, 2022
30a833b
feedback resolved around documentation
shoaibmushtaq25 Jul 27, 2022
86907e6
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Jul 27, 2022
680ca34
Merge branch 'sm/1059_support_variable_extension' of github.com:opens…
shoaibmushtaq25 Jul 27, 2022
9aafd1b
Fix some documentation and spotless apply
shoaibmushtaq25 Jul 27, 2022
ddae2f4
Revert "Fix some documentation and spotless apply"
shoaibmushtaq25 Jul 28, 2022
59f09d0
Refactored documentation
shoaibmushtaq25 Jul 28, 2022
c8d2131
spelling correction
shoaibmushtaq25 Jul 28, 2022
3a62325
Resolved feedback comments around documentation and immutability of m…
shoaibmushtaq25 Jul 29, 2022
a3a4d1d
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Aug 2, 2022
7c5cfcd
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Aug 2, 2022
c24bddd
Fix docs
shoaibmushtaq25 Aug 2, 2022
c9ea5f6
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Aug 3, 2022
2999aec
Fix docs , apply spotless
shoaibmushtaq25 Aug 4, 2022
68ada2d
Fix docs , apply spotless
shoaibmushtaq25 Aug 4, 2022
d24bee3
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Aug 7, 2022
21ff143
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Aug 10, 2022
537d888
Separate out apis for varaibles expression into ExpressionEvaluator
shoaibmushtaq25 Aug 11, 2022
26f0745
Changed Junit Assertions to Truth Assertions
shoaibmushtaq25 Aug 11, 2022
da5662b
Add a unit test, update documentation, spotless apply
shoaibmushtaq25 Aug 11, 2022
d8490b1
Rename unit test
shoaibmushtaq25 Aug 11, 2022
32ec1bb
Added more documentation
shoaibmushtaq25 Aug 12, 2022
55dc6a6
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Aug 12, 2022
13c9a09
Improve code readibility in Expression Evaluator and remove commented…
shoaibmushtaq25 Aug 12, 2022
e5f72d7
Fix documentation
shoaibmushtaq25 Aug 12, 2022
49b026e
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Aug 12, 2022
2adb305
Update datacapture/src/main/java/com/google/android/fhir/datacapture/…
shoaibmushtaq25 Aug 12, 2022
e2eb5b6
Update datacapture/src/main/java/com/google/android/fhir/datacapture/…
shoaibmushtaq25 Aug 12, 2022
eb481d0
Update datacapture/src/main/java/com/google/android/fhir/datacapture/…
shoaibmushtaq25 Aug 12, 2022
0dcbf71
Update datacapture/src/main/java/com/google/android/fhir/datacapture/…
shoaibmushtaq25 Aug 12, 2022
a4126dc
Update datacapture/src/main/java/com/google/android/fhir/datacapture/…
shoaibmushtaq25 Aug 12, 2022
1f39f8e
Update datacapture/src/main/java/com/google/android/fhir/datacapture/…
shoaibmushtaq25 Aug 12, 2022
0c81269
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Aug 12, 2022
a00889d
Added docs, Did refatoring in ExpressionEvaluator
shoaibmushtaq25 Aug 13, 2022
3085ece
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Aug 15, 2022
661bc27
Fix documentation
shoaibmushtaq25 Aug 15, 2022
b6e2aef
Update datacapture/src/main/java/com/google/android/fhir/datacapture/…
shoaibmushtaq25 Aug 18, 2022
3a8ce89
Update datacapture/src/main/java/com/google/android/fhir/datacapture/…
shoaibmushtaq25 Aug 18, 2022
9992144
Update datacapture/src/main/java/com/google/android/fhir/datacapture/…
shoaibmushtaq25 Aug 18, 2022
d95d068
Update datacapture/src/main/java/com/google/android/fhir/datacapture/…
shoaibmushtaq25 Aug 18, 2022
10ea5b1
Update datacapture/src/main/java/com/google/android/fhir/datacapture/…
shoaibmushtaq25 Aug 18, 2022
3ad9705
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Aug 18, 2022
dd5c954
Refactored methods to make it more readable
shoaibmushtaq25 Aug 18, 2022
aef2a0a
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Aug 19, 2022
076c3d0
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Aug 19, 2022
ad0efa0
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Aug 19, 2022
2614bc4
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Aug 21, 2022
b5ad3ec
Update datacapture/src/main/java/com/google/android/fhir/datacapture/…
shoaibmushtaq25 Aug 22, 2022
db6fbae
Renamed maps and fix documentation
shoaibmushtaq25 Aug 22, 2022
cdba224
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Aug 24, 2022
54dbc4d
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Aug 24, 2022
3e45be7
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Aug 24, 2022
be11e35
Fix documentation
shoaibmushtaq25 Aug 24, 2022
852d1fd
Renamed variablesMap
shoaibmushtaq25 Aug 25, 2022
b738ea6
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Aug 25, 2022
5189736
Merge branch 'master' into sm/1059_support_variable_extension
shoaibmushtaq25 Aug 26, 2022
203687c
Added a todo, apply spotless
shoaibmushtaq25 Aug 30, 2022
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
@@ -0,0 +1,71 @@
/*
shoaibmushtaq25 marked this conversation as resolved.
Show resolved Hide resolved
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.fhir.datacapture
shoaibmushtaq25 marked this conversation as resolved.
Show resolved Hide resolved

import org.hl7.fhir.r4.model.Base
import org.hl7.fhir.r4.model.TypeDetails
import org.hl7.fhir.r4.model.ValueSet
import org.hl7.fhir.r4.utils.FHIRPathEngine

object FHIRPathEngineHostServices : FHIRPathEngine.IEvaluationContext {
override fun resolveConstant(appContext: Any?, name: String?, beforeContext: Boolean): Base? {
return if ((appContext as Map<*, *>).containsKey(name)) appContext["$name"] as Base else null
shoaibmushtaq25 marked this conversation as resolved.
Show resolved Hide resolved
}

override fun resolveConstantType(appContext: Any?, name: String?): TypeDetails {
TODO("Not yet implemented")
}

override fun log(argument: String?, focus: MutableList<Base>?): Boolean {
TODO("Not yet implemented")
}

override fun resolveFunction(
functionName: String?
): FHIRPathEngine.IEvaluationContext.FunctionDetails {
TODO("Not yet implemented")
}

override fun checkFunction(
appContext: Any?,
functionName: String?,
parameters: MutableList<TypeDetails>?
): TypeDetails {
TODO("Not yet implemented")
}

override fun executeFunction(
appContext: Any?,
focus: MutableList<Base>?,
functionName: String?,
parameters: MutableList<MutableList<Base>>?
): MutableList<Base> {
TODO("Not yet implemented")
}

override fun resolveReference(appContext: Any?, url: String?): Base {
TODO("Not yet implemented")
}

override fun conformsToProfile(appContext: Any?, item: Base?, url: String?): Boolean {
TODO("Not yet implemented")
}

override fun resolveValueSet(appContext: Any?, url: String?): ValueSet {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.context.FhirVersionEnum
import ca.uhn.fhir.context.support.DefaultProfileValidationSupport
import com.google.android.fhir.datacapture.enablement.EnablementEvaluator
import com.google.android.fhir.datacapture.validation.QuestionnaireResponseValidator.checkQuestionnaireResponse
import com.google.android.fhir.datacapture.views.QuestionnaireItemViewItem
Expand All @@ -31,12 +32,20 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn
import org.hl7.fhir.exceptions.PathEngineException
import org.hl7.fhir.instance.model.api.IBaseDatatype
import org.hl7.fhir.r4.hapi.ctx.HapiWorkerContext
import org.hl7.fhir.r4.model.Base
import org.hl7.fhir.r4.model.CodeableConcept
import org.hl7.fhir.r4.model.Coding
import org.hl7.fhir.r4.model.Expression
import org.hl7.fhir.r4.model.Extension
import org.hl7.fhir.r4.model.Questionnaire
import org.hl7.fhir.r4.model.QuestionnaireResponse
import org.hl7.fhir.r4.model.ResourceType
import org.hl7.fhir.r4.model.Type
import org.hl7.fhir.r4.model.ValueSet
import org.hl7.fhir.r4.utils.FHIRPathEngine
import timber.log.Timber

internal class QuestionnaireViewModel(application: Application, state: SavedStateHandle) :
Expand Down Expand Up @@ -103,6 +112,12 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
private val linkIdToQuestionnaireResponseItemMap =
createLinkIdToQuestionnaireResponseItemMap(questionnaireResponse.item)

private val linkIdToQuestionnaireItemPathMap =
createLinkIdToQuestionnaireItemPathMap(questionnaire.item)
shoaibmushtaq25 marked this conversation as resolved.
Show resolved Hide resolved

private val questionnaireResponseItemPathToVariableMap =
shoaibmushtaq25 marked this conversation as resolved.
Show resolved Hide resolved
createQuestionnaireResponseItemPathToVariableMap(questionnaireResponse.item)

/** Map from link IDs to questionnaire items. */
private val linkIdToQuestionnaireItemMap = createLinkIdToQuestionnaireItemMap(questionnaire.item)

Expand All @@ -126,9 +141,183 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
}
}
}
calculateVariables()
modificationCount.value += 1
}

private fun calculateVariables() {
calculateRootVariables()

for ((key, _) in linkIdToQuestionnaireItemPathMap) {
linkIdToQuestionnaireItemMap[key]?.let { questionnaireItem ->
shoaibmushtaq25 marked this conversation as resolved.
Show resolved Hide resolved
linkIdToQuestionnaireResponseItemMap[key]?.let { questionnaireResponseItem ->
calculateItemVariables(questionnaireItem, questionnaireResponseItem)
}
}
}
}

private fun calculateItemVariables(
shoaibmushtaq25 marked this conversation as resolved.
Show resolved Hide resolved
questionnaireItem: Questionnaire.QuestionnaireItemComponent,
questionnaireResponseItem: QuestionnaireResponse.QuestionnaireResponseItemComponent
) {
val fhirPathEngine: FHIRPathEngine =
with(FhirContext.forCached(FhirVersionEnum.R4)) {
FHIRPathEngine(HapiWorkerContext(this, DefaultProfileValidationSupport(this))).apply {
shoaibmushtaq25 marked this conversation as resolved.
Show resolved Hide resolved
hostServices = FHIRPathEngineHostServices
}
}

questionnaireItem.extension.forEach { extension ->
if (extension.url == VARIABLE_EXTENSION_URL) {

val variableValue =
evaluateItemVariables(fhirPathEngine, extension, questionnaireResponseItem)

val variables =
questionnaireResponseItemPathToVariableMap[
linkIdToQuestionnaireItemPathMap[questionnaireResponseItem.linkId]]
updateVariable(variables, extension, variableValue)
}
}
}

private fun updateVariable(
variables: MutableList<Variable>?,
shoaibmushtaq25 marked this conversation as resolved.
Show resolved Hide resolved
extension: Extension,
variableValue: Any?
) {
variables?.find { it.id == (extension.value as Expression).name }.also { variable ->
if (variable == null) {
variableValue?.let {
variables?.add(Variable(id = (extension.value as Expression).name, value = it as Type))
}
} else {
if (variableValue != null) variable.value = variableValue as Type
else variables?.remove(variable)
}
}
}

private fun evaluateItemVariables(
fhirPathEngine: FHIRPathEngine,
extension: Extension,
questionnaireResponseItem: QuestionnaireResponse.QuestionnaireResponseItemComponent
) =
try {
fhirPathEngine
.evaluate(
findAncestorVariables(extension, questionnaireResponseItem),
questionnaireResponse,
questionnaireResponse,
questionnaireResponse,
(extension.value as Expression).expression
shoaibmushtaq25 marked this conversation as resolved.
Show resolved Hide resolved
)
.firstOrNull()
} catch (exception: PathEngineException) {
Timber.d("Could not evaluate expression with FHIRPathEngine", exception)
}

private fun calculateRootVariables() {
val fhirPathEngine: FHIRPathEngine =
with(FhirContext.forCached(FhirVersionEnum.R4)) {
FHIRPathEngine(HapiWorkerContext(this, DefaultProfileValidationSupport(this))).apply {
shoaibmushtaq25 marked this conversation as resolved.
Show resolved Hide resolved
hostServices = FHIRPathEngineHostServices
}
}

questionnaire.extension.forEach { extension ->
shoaibmushtaq25 marked this conversation as resolved.
Show resolved Hide resolved
if (extension.url == VARIABLE_EXTENSION_URL) {

val variableValue = evaluateRootVariables(fhirPathEngine, extension)

if (questionnaireResponseItemPathToVariableMap.containsKey(ROOT_VARIABLES)) {
val variables = questionnaireResponseItemPathToVariableMap[ROOT_VARIABLES]
updateVariable(variables, extension, variableValue)
} else {
questionnaireResponseItemPathToVariableMap[ROOT_VARIABLES] =
mutableListOf(
Variable(id = (extension.value as Expression).name, value = variableValue as Type)
)
}
}
}
}

private fun evaluateRootVariables(fhirPathEngine: FHIRPathEngine, extension: Extension): Base? =
try {
shoaibmushtaq25 marked this conversation as resolved.
Show resolved Hide resolved
fhirPathEngine
.evaluate(
findRootVariables(extension),
questionnaireResponse,
questionnaireResponse,
questionnaireResponse,
(extension.value as Expression).expression
)
.firstOrNull()
} catch (exception: PathEngineException) {
Timber.d("Could not evaluate expression with FHIRPathEngine", exception)
shoaibmushtaq25 marked this conversation as resolved.
Show resolved Hide resolved
null
}

private fun findRootVariables(extension: Extension): Map<String, Any> {
shoaibmushtaq25 marked this conversation as resolved.
Show resolved Hide resolved
val map = mutableMapOf<String, Base>()
if (questionnaireResponseItemPathToVariableMap.containsKey(ROOT_VARIABLES)) {
val variables = questionnaireResponseItemPathToVariableMap[ROOT_VARIABLES]
variables?.forEach {
if (it.id != (extension.value as Expression).name) {
map[it.id] = it.value as Type
}
}
}
return map
}

private fun findAncestorVariables(
extension: Extension,
questionnaireResponseItem: QuestionnaireResponse.QuestionnaireResponseItemComponent
): Map<String, Any> {
val map = mutableMapOf<String, Base>()

// check root level variables
if (questionnaireResponseItemPathToVariableMap.containsKey(ROOT_VARIABLES)) {
val rootVariables = questionnaireResponseItemPathToVariableMap[ROOT_VARIABLES]
rootVariables?.forEach {
if (it.id != (extension.value as Expression).name) {
map[it.id] = it.value as Type
}
}
}

// check variables in ancestors
var path = linkIdToQuestionnaireItemPathMap[questionnaireResponseItem.linkId]
do {
if (path?.contains(".") == true) {
path = path.substringBeforeLast(".")
val variables = questionnaireResponseItemPathToVariableMap[path]
variables?.forEach {
if (it.id != (extension.value as Expression).name) {
map[it.id] = it.value as Type
}
}
} else {
path = ""
}
} while (path?.isNotEmpty() == true)

// check current item variables
val itemVariables =
questionnaireResponseItemPathToVariableMap[
linkIdToQuestionnaireItemPathMap[questionnaireResponseItem.linkId]]
itemVariables?.forEach {
if (it.id != (extension.value as Expression).name) {
map[it.id] = it.value as Type
}
}

return map
}

private val pageFlow = MutableStateFlow(questionnaire.getInitialPagination())

private val answerValueSetMap =
Expand Down Expand Up @@ -229,6 +418,46 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
return linkIdToQuestionnaireResponseItemMap
}

private fun createLinkIdToQuestionnaireItemPathMap(
questionnaireItemList: List<Questionnaire.QuestionnaireItemComponent>,
pathPrefix: String = ""
): MutableMap<String, String> {

val linkIdToQuestionnaireItemPathMap =
questionnaireItemList
.associate { it.linkId to if (pathPrefix == "") it.linkId else "$pathPrefix.${it.linkId}" }
.toMutableMap()

for (item in questionnaireItemList) {
linkIdToQuestionnaireItemPathMap.putAll(
createLinkIdToQuestionnaireItemPathMap(
item.item,
if (pathPrefix == "") item.linkId else "$pathPrefix.${item.linkId}"
)
)
}
return linkIdToQuestionnaireItemPathMap
}

private fun createQuestionnaireResponseItemPathToVariableMap(
questionnaireResponseItemList: List<QuestionnaireResponse.QuestionnaireResponseItemComponent>
): MutableMap<String, MutableList<Variable>> {
val questionnaireResponseItemPathToVariableMap =
questionnaireResponseItemList
.associate { questionnaireResponseItem ->
linkIdToQuestionnaireItemPathMap[questionnaireResponseItem.linkId]!! to
mutableListOf<Variable>()
}
.toMutableMap()

for (item in questionnaireResponseItemList) {
questionnaireResponseItemPathToVariableMap.putAll(
createQuestionnaireResponseItemPathToVariableMap(item.item)
)
}
return questionnaireResponseItemPathToVariableMap
}

private fun createLinkIdToQuestionnaireItemMap(
questionnaireItemList: List<Questionnaire.QuestionnaireItemComponent>
): Map<String, Questionnaire.QuestionnaireItemComponent> {
Expand Down Expand Up @@ -400,3 +629,8 @@ internal fun QuestionnairePagination.nextPage(): QuestionnairePagination {
check(hasNextPage) { "Can't call nextPage() if hasNextPage is false ($this)" }
return copy(currentPageIndex = currentPageIndex + 1)
}

data class Variable(val id: String, var value: IBaseDatatype)

internal const val VARIABLE_EXTENSION_URL = "http://hl7.org/fhir/StructureDefinition/variable"
internal const val ROOT_VARIABLES = "root_variables"
shoaibmushtaq25 marked this conversation as resolved.
Show resolved Hide resolved