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

LaunchContext for initialExpression #2025

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
9fa64b6
Use launchContext for initialExpression
FikriMilano Jul 31, 2023
8b765fd
Test ResourceMapper
FikriMilano Jul 31, 2023
59c7d6a
Fix test
FikriMilano Jul 31, 2023
a4c8c8f
Merge branch 'master' of github.com:google/android-fhir into 2024-ini…
FikriMilano Jul 31, 2023
45a6ae0
Fix launchContexts for demo app when editing patient
FikriMilano Jul 31, 2023
9686da8
spotlessApply
FikriMilano Jul 31, 2023
d2d53a2
WIP
FikriMilano Aug 2, 2023
14fa29c
Merge branch 'master' of github.com:google/android-fhir into 2024-ini…
FikriMilano Aug 23, 2023
9c4210d
Revert "WIP"
FikriMilano Aug 23, 2023
33b3d03
spotlessApply
FikriMilano Aug 23, 2023
45c6dbf
Fix test
FikriMilano Aug 24, 2023
6a47a51
Refactor validateLaunchContextExtension
FikriMilano Sep 12, 2023
3f0b4f6
Remove QuestionnaireLaunchContextSet enum class
FikriMilano Sep 12, 2023
ddc1dfc
Rename vars and functions
FikriMilano Sep 12, 2023
65cbee1
Add code comment for MoreResourceTypes.kt
FikriMilano Sep 12, 2023
8f6a9c4
Unit testing
FikriMilano Sep 12, 2023
597fe7e
Merge branch 'master' of github.com:google/android-fhir into 2024-ini…
FikriMilano Sep 12, 2023
6e90a9d
Merge branch 'master' of github.com:google/android-fhir into 2024-ini…
FikriMilano Sep 13, 2023
3bff3b2
Fix post-merge-conflict
FikriMilano Sep 13, 2023
1206cfa
spotlessApply
FikriMilano Sep 13, 2023
28c86be
Address review
FikriMilano Oct 9, 2023
a6436e2
Merge branch 'master' of github.com:google/android-fhir into 2024-ini…
FikriMilano Oct 9, 2023
287512c
spotlessApply
FikriMilano Oct 9, 2023
d5d4069
Merge branch 'master' of github.com:google/android-fhir into 2024-ini…
FikriMilano Oct 9, 2023
8fec91e
Revert un-intended changes
FikriMilano Oct 9, 2023
a21e6f7
Fix failing checks
FikriMilano Oct 9, 2023
8130aef
Remove check of must contain 2 sub-extensions
FikriMilano Oct 13, 2023
f6f6b3c
Update Kdoc
FikriMilano Oct 13, 2023
b412e16
Merge branch 'master' of github.com:google/android-fhir into 2024-ini…
FikriMilano Oct 13, 2023
ad51a70
Revert
FikriMilano Oct 13, 2023
41a957d
validate launch context when using populate public API
FikriMilano Oct 13, 2023
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
Prev Previous commit
Next Next commit
Address review
  • Loading branch information
FikriMilano committed Oct 9, 2023
commit 28c86be67fa409a12b94b94cbbc3064a8f04723f
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ class QuestionnaireFragment : Fragment() {
* @param launchContextMap map of launchContext name and serialized resources
*/
fun setQuestionnaireLaunchContextMap(launchContextMap: Map<String, String>) = apply {
args.add(EXTRA_QUESTIONNAIRE_LAUNCH_CONTEXT_MAP_STRINGS to launchContextMap)
args.add(EXTRA_QUESTIONNAIRE_LAUNCH_CONTEXT_MAP to launchContextMap)
}

/**
Expand Down Expand Up @@ -419,8 +419,7 @@ class QuestionnaireFragment : Fragment() {
/**
* A map of launchContext name and JSON encoded strings extra for each questionnaire context.
*/
internal const val EXTRA_QUESTIONNAIRE_LAUNCH_CONTEXT_MAP_STRINGS =
"questionnaire-launch-contexts"
internal const val EXTRA_QUESTIONNAIRE_LAUNCH_CONTEXT_MAP = "questionnaire-launch-contexts"
/**
* A [URI][android.net.Uri] extra for streaming a JSON encoded questionnaire response.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import com.google.android.fhir.datacapture.extensions.allItems
import com.google.android.fhir.datacapture.extensions.cqfExpression
import com.google.android.fhir.datacapture.extensions.createQuestionnaireResponseItem
import com.google.android.fhir.datacapture.extensions.entryMode
import com.google.android.fhir.datacapture.extensions.filterLaunchContextsByCodeInNameExtension
import com.google.android.fhir.datacapture.extensions.filterByCodeInNameExtension
import com.google.android.fhir.datacapture.extensions.flattened
import com.google.android.fhir.datacapture.extensions.hasDifferentAnswerSet
import com.google.android.fhir.datacapture.extensions.isDisplayItem
Expand Down Expand Up @@ -167,19 +167,16 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat

init {
questionnaireLaunchContextMap =
if (state.contains(QuestionnaireFragment.EXTRA_QUESTIONNAIRE_LAUNCH_CONTEXT_MAP_STRINGS)) {
if (state.contains(QuestionnaireFragment.EXTRA_QUESTIONNAIRE_LAUNCH_CONTEXT_MAP)) {

val launchContextMapString: Map<String, String> =
state[QuestionnaireFragment.EXTRA_QUESTIONNAIRE_LAUNCH_CONTEXT_MAP_STRINGS]!!
state[QuestionnaireFragment.EXTRA_QUESTIONNAIRE_LAUNCH_CONTEXT_MAP]!!

val launchContextMapResource =
launchContextMapString.mapValues { parser.parseResource(it.value) as Resource }
questionnaire.questionnaireLaunchContexts?.let { launchContextExtensions ->
validateLaunchContextExtensions(launchContextExtensions)
filterLaunchContextsByCodeInNameExtension(
launchContextMapResource,
launchContextExtensions
)
filterByCodeInNameExtension(launchContextMapResource, launchContextExtensions)
}
} else {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

package com.google.android.fhir.datacapture.extensions

import org.hl7.fhir.exceptions.FHIRException
import org.hl7.fhir.r4.model.CanonicalType
import org.hl7.fhir.r4.model.CodeType
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.Resource
import org.hl7.fhir.r4.model.ResourceType

/**
* The StructureMap url in the
Expand Down Expand Up @@ -65,12 +67,7 @@ internal fun Questionnaire.findVariableExpression(variableName: String): Express
*/
internal fun validateLaunchContextExtensions(launchContextExtensions: List<Extension>) =
launchContextExtensions.forEach { launchExtension ->
validateLaunchContextExtension(
Extension().apply {
addExtension(launchExtension.extension.firstOrNull { it.url == "name" })
addExtension(launchExtension.extension.firstOrNull { it.url == "type" })
}
)
validateLaunchContextExtension(launchExtension)
}

/**
Expand All @@ -79,21 +76,31 @@ internal fun validateLaunchContextExtensions(launchContextExtensions: List<Exten
*/
private fun validateLaunchContextExtension(launchExtension: Extension) {
check(launchExtension.extension.size == 2) {
"The extension:name or extension:type extension is missing in $EXTENSION_SDC_QUESTIONNAIRE_LAUNCH_CONTEXT"
"Expected 2 extensions (name and type) in $EXTENSION_SDC_QUESTIONNAIRE_LAUNCH_CONTEXT but found ${launchExtension.extension.size}."
FikriMilano marked this conversation as resolved.
Show resolved Hide resolved
}

val nameCoding = launchExtension.getExtensionByUrl("name").value as Coding
val typeCodeType = launchExtension.getExtensionByUrl("type").value as CodeType

val hasNameCodingSystem = nameCoding.system == EXTENSION_LAUNCH_CONTEXT
val isValidResourceType = MoreResourceTypes.isValidCode(typeCodeType.value)

val isValidExtension = hasNameCodingSystem && isValidResourceType
val nameCoding =
launchExtension.getExtensionByUrl("name")?.value as? Coding
?: error(
"The extension:name is missing or is not of type Coding in $EXTENSION_SDC_QUESTIONNAIRE_LAUNCH_CONTEXT"
)

val typeCodeType =
launchExtension.getExtensionByUrl("type")?.value as? CodeType
?: error(
"The extension:type is missing or is not of type CodeType in $EXTENSION_SDC_QUESTIONNAIRE_LAUNCH_CONTEXT"
)

val isValidResourceType =
try {
ResourceType.fromCode(typeCodeType.value) != null
} catch (exception: FHIRException) {
false
}

if (!isValidExtension) {
if (nameCoding.system != EXTENSION_LAUNCH_CONTEXT || !isValidResourceType) {
error(
"The extension:name extension and/or extension:type extension do not follow the format " +
"specified in $EXTENSION_SDC_QUESTIONNAIRE_LAUNCH_CONTEXT"
"The extension:name and/or extension:type do not follow the format specified in $EXTENSION_SDC_QUESTIONNAIRE_LAUNCH_CONTEXT"
)
}
}
Expand All @@ -102,14 +109,14 @@ private fun validateLaunchContextExtension(launchExtension: Extension) {
* Filters the provided launch contexts by matching the keys with the `code` values found in the
* "name" extensions.
*/
internal fun filterLaunchContextsByCodeInNameExtension(
internal fun filterByCodeInNameExtension(
launchContexts: Map<String, Resource>,
launchContextExtensions: List<Extension>
): Map<String, Resource> {
val nameCodes =
launchContextExtensions.mapNotNull { extension ->
(extension.getExtensionByUrl("name").value as? Coding)?.code
}
launchContextExtensions
.mapNotNull { extension -> (extension.getExtensionByUrl("name").value as? Coding)?.code }
.toSet()

return launchContexts.filterKeys { nameCodes.contains(it) }
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package com.google.android.fhir.datacapture.mapping

import com.google.android.fhir.datacapture.DataCapture
import com.google.android.fhir.datacapture.extensions.createQuestionnaireResponseItem
import com.google.android.fhir.datacapture.extensions.filterLaunchContextsByCodeInNameExtension
import com.google.android.fhir.datacapture.extensions.filterByCodeInNameExtension
import com.google.android.fhir.datacapture.extensions.initialExpression
import com.google.android.fhir.datacapture.extensions.logicalId
import com.google.android.fhir.datacapture.extensions.questionnaireLaunchContexts
Expand Down Expand Up @@ -226,7 +226,7 @@ object ResourceMapper {
launchContexts: Map<String, Resource>
FikriMilano marked this conversation as resolved.
Show resolved Hide resolved
): QuestionnaireResponse {
val filteredLaunchContexts =
filterLaunchContextsByCodeInNameExtension(
filterByCodeInNameExtension(
launchContexts,
questionnaire.questionnaireLaunchContexts ?: listOf()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.parser.IParser
import com.google.android.fhir.datacapture.QuestionnaireFragment.Companion.EXTRA_ENABLE_REVIEW_PAGE
import com.google.android.fhir.datacapture.QuestionnaireFragment.Companion.EXTRA_QUESTIONNAIRE_JSON_STRING
import com.google.android.fhir.datacapture.QuestionnaireFragment.Companion.EXTRA_QUESTIONNAIRE_LAUNCH_CONTEXT_MAP_STRINGS
import com.google.android.fhir.datacapture.QuestionnaireFragment.Companion.EXTRA_QUESTIONNAIRE_LAUNCH_CONTEXT_MAP
import com.google.android.fhir.datacapture.QuestionnaireFragment.Companion.EXTRA_QUESTIONNAIRE_RESPONSE_JSON_STRING
import com.google.android.fhir.datacapture.QuestionnaireFragment.Companion.EXTRA_READ_ONLY
import com.google.android.fhir.datacapture.QuestionnaireFragment.Companion.EXTRA_SHOW_REVIEW_PAGE_FIRST
Expand Down Expand Up @@ -4170,7 +4170,7 @@ class QuestionnaireViewModelTest {
}
state.set(EXTRA_QUESTIONNAIRE_JSON_STRING, printer.encodeResourceToString(questionnaire))
state.set(
EXTRA_QUESTIONNAIRE_LAUNCH_CONTEXT_MAP_STRINGS,
EXTRA_QUESTIONNAIRE_LAUNCH_CONTEXT_MAP,
mapOf("patient" to printer.encodeResourceToString(patient))
)

Expand Down
Loading