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 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
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
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2024 Google LLC
* Copyright 2022-2025 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 All @@ -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,57 @@ 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
check(
!(questionnaireItem.type == Questionnaire.QuestionnaireItemType.GROUP ||
questionnaireItem.type == Questionnaire.QuestionnaireItemType.DISPLAY) ||
(questionnaireItem.initial.isEmpty() && questionnaireItem.initialExpression == null),
) {
"QuestionnaireItem item is not allowed to have initial value or initial expression for groups or display items. See rule at http://build.fhir.org/ig/HL7/sdc/expressions.html#initialExpression."
}

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

// If questionnaireItem.repeats is false only first value is selected from initialExpression
// result set
val evaluatedExpressionResult =
if (questionnaireItem.repeats) {
it.map { it.asExpectedType(questionnaireItem.type) }
} else {
listOf(it.first().asExpectedType(questionnaireItem.type))
}

// For answer options, the initialSelected extension is used to highlight initial values.
// Note: If the initial expression evaluates to 1, 2, 3, 4, 5, but only 3 answer options (1,
// 2, 3) exist,
// then 4 and 5 will be ignored. These values are not added as additional options, nor would
// it make sense to do so.
// This behavior ensures the answer options remain consistent with the defined set.
if (questionnaireItem.answerOption.isNotEmpty()) {
parthfloyd marked this conversation as resolved.
Show resolved Hide resolved
questionnaireItem.answerOption.forEach { answerOption ->
answerOption.initialSelected =
evaluatedExpressionResult.any { answerOption.value.equalsDeep(it) }
parthfloyd marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
questionnaireItem.initial =
evaluatedExpressionResult.map {
Questionnaire.QuestionnaireItemInitialComponent()
.setValue(
it,
)
}
}
}

populateInitialValues(questionnaireItem.item, launchContexts)
Expand Down
Loading
Loading