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

populate initial with initialExpression result collection #2751

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.google.android.fhir.datacapture.mapping
import com.google.android.fhir.datacapture.extensions.createQuestionnaireResponseItem
import com.google.android.fhir.datacapture.extensions.filterByCodeInNameExtension
import com.google.android.fhir.datacapture.extensions.initialExpression
import com.google.android.fhir.datacapture.extensions.initialSelected
import com.google.android.fhir.datacapture.extensions.logicalId
import com.google.android.fhir.datacapture.extensions.questionnaireLaunchContexts
import com.google.android.fhir.datacapture.extensions.targetStructureMap
Expand Down Expand Up @@ -248,22 +249,51 @@ object ResourceMapper {
"QuestionnaireItem item is not allowed to have both initial.value and initial expression. See rule at http://build.fhir.org/ig/HL7/sdc/expressions.html#initialExpression."
}

// Initial values can't be specified for groups or display items
if (questionnaireItem.initial.isNotEmpty() || questionnaireItem.initialExpression != null) {
check(
questionnaireItem.type != Questionnaire.QuestionnaireItemType.GROUP &&
questionnaireItem.type != Questionnaire.QuestionnaireItemType.DISPLAY,
) {
"QuestionnaireItem item is not allowed to have initial value or initial expression for groups or display items."
}
}

questionnaireItem.initialExpression
?.let {
evaluateToBase(
questionnaireResponse = null,
questionnaireResponseItem = null,
expression = it.expression,
contextMap = launchContexts,
)
.firstOrNull()
questionnaireResponse = null,
questionnaireResponseItem = null,
expression = it.expression,
contextMap = launchContexts,
)
}
?.let {
// Set initial value for the questionnaire item. Questionnaire items should not have both
// initial value and initial expression.
val value = it.asExpectedType(questionnaireItem.type)
questionnaireItem.initial =
mutableListOf(Questionnaire.QuestionnaireItemInitialComponent().setValue(value))
// Set initial value for the questionnaire item.
if (it.isEmpty()) return@let

val evaluatedExpressionResult = it.map { it.asExpectedType(questionnaireItem.type) }

if (questionnaireItem.answerOption.isNotEmpty()) {
questionnaireItem.answerOption.forEach { answerOption ->
answerOption.initialSelected =
evaluatedExpressionResult.any { answerOption.value.equalsDeep(it) }
parthfloyd marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
// Handle initial values based on whether the questionnaire item repeats
questionnaireItem.initial =
if (questionnaireItem.repeats) {
evaluatedExpressionResult.map {
Questionnaire.QuestionnaireItemInitialComponent()
.setValue(
it,
)
}
} else {
val value = evaluatedExpressionResult.first()
listOf(Questionnaire.QuestionnaireItemInitialComponent().setValue(value))
}
}
}

populateInitialValues(questionnaireItem.item, launchContexts)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1955,12 +1955,14 @@ class ResourceMapperTest {
listOf(
Questionnaire.QuestionnaireItemAnswerOptionComponent(
Coding().apply {
system = AdministrativeGender.MALE.system
code = AdministrativeGender.MALE.toCode()
display = AdministrativeGender.MALE.display
},
),
Questionnaire.QuestionnaireItemAnswerOptionComponent(
Coding().apply {
system = AdministrativeGender.FEMALE.system
code = AdministrativeGender.FEMALE.toCode()
display = AdministrativeGender.FEMALE.display
},
Expand Down Expand Up @@ -2011,12 +2013,14 @@ class ResourceMapperTest {
Coding().apply {
code = AdministrativeGender.MALE.toCode()
display = AdministrativeGender.MALE.display
system = AdministrativeGender.MALE.system
},
),
Questionnaire.QuestionnaireItemAnswerOptionComponent(
Coding().apply {
code = AdministrativeGender.FEMALE.toCode()
display = AdministrativeGender.FEMALE.display
system = AdministrativeGender.MALE.system
},
),
)
Expand Down
Loading