Skip to content

Updated the KM to latest version alpha02 #31

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
392 changes: 354 additions & 38 deletions configurablecare/app/build.gradle.kts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4786,6 +4786,8 @@
},
{
"resource": {
"version": "1",
"url": "http://localhost/PlanDefinition/PlanDefinitionCancerScreening",
"resourceType": "PlanDefinition",
"id": "Cancer-Screening-Plan-Definition",
"identifier": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.google.fhir.examples.configurablecare
import android.app.Application
import android.content.Context
import ca.uhn.fhir.context.FhirContext
import com.google.android.fhir.BuildConfig
import com.google.android.fhir.DatabaseErrorStrategy.RECREATE_AT_OPEN
import com.google.android.fhir.FhirEngine
import com.google.android.fhir.FhirEngineConfiguration
Expand Down Expand Up @@ -71,7 +70,7 @@ class FhirApplication : Application(), DataCaptureConfig.Provider {
DataCaptureConfig().apply {
urlResolver = ReferenceUrlResolver(this@FhirApplication as Context)
valueSetResolverExternal = object : ValueSetResolver() {}
xFhirQueryResolver = XFhirQueryResolver { fhirEngine.search(it) }
xFhirQueryResolver = XFhirQueryResolver { fhirEngine.search(it).map { it.resource } }
}
ValueSetResolver.init(this@FhirApplication)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class PatientDetailsViewModel(
fhirEngine
.search<Observation> { filter(Observation.SUBJECT, { value = "Patient/$patientId" }) }
.take(MAX_RESOURCE_COUNT)
.map { createObservationItem(it, getApplication<Application>().resources) }
.map { createObservationItem(it.resource, getApplication<Application>().resources) }
.let { observations.addAll(it) }
return observations
}
Expand All @@ -74,7 +74,7 @@ class PatientDetailsViewModel(
fhirEngine
.search<Condition> { filter(Condition.SUBJECT, { value = "Patient/$patientId" }) }
.take(MAX_RESOURCE_COUNT)
.map { createConditionItem(it, getApplication<Application>().resources) }
.map { createConditionItem(it.resource, getApplication<Application>().resources) }
.let { conditions.addAll(it) }
return conditions
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class PatientListViewModel(application: Application, private val fhirEngine: Fhi
from = 0
}
.mapIndexed { index, fhirPatient ->
fhirPatient.toPatientItem(index + 1).apply {
fhirPatient.resource.toPatientItem(index + 1).apply {
pendingTasksCount =
taskManager.getTasksCount(resourceId) {
filter(Task.STATUS, { value = of(getTaskStatus(0)) })
Expand All @@ -127,6 +127,7 @@ class PatientListViewModel(application: Application, private val fhirEngine: Fhi
private suspend fun getRiskAssessments(): Map<String, RiskAssessment?> {
return fhirEngine
.search<RiskAssessment> {}
.map { it.resource }
.groupBy { it.subject.reference }
.mapValues { entry ->
entry.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import ca.uhn.fhir.context.FhirVersionEnum
import com.google.android.fhir.FhirEngine
import com.google.android.fhir.knowledge.KnowledgeManager
import com.google.android.fhir.search.search
import com.google.android.fhir.workflow.FhirOperatorBuilder
import com.google.android.fhir.workflow.FhirOperator
import java.io.File
import org.hl7.fhir.r4.model.Bundle
import org.hl7.fhir.r4.model.CanonicalType
Expand All @@ -43,12 +43,12 @@ class CarePlanManager(
fhirContext: FhirContext,
private val context: Context,
) {
private var knowledgeManager = KnowledgeManager.createInMemory(context)
private var knowledgeManager = KnowledgeManager.create(context, inMemory = false)
private var fhirOperator =
FhirOperatorBuilder(context.applicationContext)
.withFhirContext(fhirContext)
.withFhirEngine(fhirEngine)
.withIgManager(knowledgeManager)
FhirOperator.Builder(context.applicationContext)
.fhirContext(fhirContext)
.fhirEngine(fhirEngine)
.knowledgeManager(knowledgeManager)
.build()

private var taskManager: RequestResourceManager<Task> = TaskManager(fhirEngine)
Expand Down Expand Up @@ -103,8 +103,8 @@ class CarePlanManager(
*/
private suspend fun loadCarePlanResourcesFromDb() {
// Load Library resources
val availableCqlLibraries = fhirEngine.search<Library> {}
val availablePlanDefinitions = fhirEngine.search<PlanDefinition> {}
val availableCqlLibraries = fhirEngine.search<Library> {}.map { it.resource }
val availablePlanDefinitions = fhirEngine.search<PlanDefinition> {}.map { it.resource }
for (cqlLibrary in availableCqlLibraries) {
knowledgeManager.install(writeToFile(cqlLibrary))
cqlLibraryIdList.add(IdType(cqlLibrary.id).idPart)
Expand Down Expand Up @@ -133,10 +133,16 @@ class CarePlanManager(
if (cqlLibraryIdList.isEmpty()) {
loadCarePlanResourcesFromDb()
}

println("planDefinitionId: $planDefinitionId subject: Patient/$patientId")
val carePlanProposal =
fhirOperator.generateCarePlan(planDefinitionId = planDefinitionId, patientId = patientId)
as CarePlan
fhirOperator.generateCarePlan(
planDefinition =
CanonicalType("http://localhost/PlanDefinition/PlanDefinitionCancerScreening"),
subject = "Patient/$patientId"
)
// fhirOperator.generateCarePlan(planDefinitionId = CanonicalType(planDefinitionId),
// subject = patientId, encounterId = null)
as CarePlan

// Fetch existing CarePlan of record for the Patient or create a new one if it does not exist
val carePlanOfRecord = getCarePlanOfRecordForPatient(patient)
Expand Down Expand Up @@ -167,8 +173,12 @@ class CarePlanManager(
val patientId = IdType(patient.id).idPart

val carePlanProposal =
fhirOperator.generateCarePlan(planDefinitionId = planDefinitionId, patientId = patientId)
as CarePlan
// fhirOperator.generateCarePlan(planDefinition = CanonicalType(planDefinitionId),
// subject = patientId, encounterId = null)
fhirOperator.generateCarePlan(
planDefinitionId = planDefinitionId,
subject = "Patient/$patientId"
) as CarePlan

// Fetch existing CarePlan of record for the Patient or create a new one if it does not exist
val carePlanOfRecord = getCarePlanOfRecordForPatient(patient)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class TaskManager(private var fhirEngine: FhirEngine) : RequestResourceManager<T
{ value = of(task.focus.reference.substring("Questionnaire/".length)) }
)
}
return questionnaires.firstOrNull()
return questionnaires.firstOrNull()?.resource
}

/** Fetch all Tasks for a given Patient */
Expand All @@ -120,16 +120,18 @@ class TaskManager(private var fhirEngine: FhirEngine) : RequestResourceManager<T
extraFilter: (Search.() -> Unit)?,
sort: (Search.() -> Unit)?
): List<Task> {
return fhirEngine.search {
filter(Task.SUBJECT, { value = "Patient/$patientId" })
if (extraFilter != null) {
extraFilter()
return fhirEngine
.search<Task> {
filter(Task.SUBJECT, { value = "Patient/$patientId" })
if (extraFilter != null) {
extraFilter()
}
operation = Operation.AND
if (sort != null) {
sort()
}
}
operation = Operation.AND
if (sort != null) {
sort()
}
}
.map { it.resource }
}

/** Populate the requester field in the given [Task] */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.google.fhir.examples.configurablecare.data

import com.google.android.fhir.sync.DownloadWorkManager
import com.google.android.fhir.sync.Request
import com.google.android.fhir.sync.download.DownloadRequest
import com.google.fhir.examples.configurablecare.DemoDataStore
import com.google.fhir.examples.configurablecare.care.CarePlanManager
import com.google.fhir.examples.configurablecare.care.ConfigurationManager.getCareConfigurationResources
Expand Down Expand Up @@ -76,7 +76,7 @@ class TimestampBasedDownloadWorkManagerImpl(
)
)

override suspend fun getNextRequest(): Request? {
override suspend fun getNextRequest(): DownloadRequest? {
var url = urls.poll()
return if (url == null) {
constructNextRequestFromResourceReferences()
Expand All @@ -86,11 +86,11 @@ class TimestampBasedDownloadWorkManagerImpl(
dataStore.getLastUpdateTimestamp(resourceTypeToDownload)?.let {
url = affixLastUpdatedTimestamp(url, it)
}
Request.of(url)
DownloadRequest.of(url)
}
}

private fun constructNextRequestFromResourceReferences(): Request? {
private fun constructNextRequestFromResourceReferences(): DownloadRequest? {
for (resourceType in resourceReferencesDownloadOrderByTypeSequence) {
if (resourceReferences.getOrDefault(resourceType, emptyMap()).isNotEmpty()) {
val resourceSearchValues = resourceReferences[resourceType]!!
Expand All @@ -115,9 +115,9 @@ class TimestampBasedDownloadWorkManagerImpl(
resourceType: ResourceType,
searchField: String,
resourceIds: Set<String>
): Request? {
): DownloadRequest? {
return if (resourceIds.isNotEmpty()) {
Request.of("${resourceType.name}?$searchField=${resourceIds.joinToString(",")}")
DownloadRequest.of("${resourceType.name}?$searchField=${resourceIds.joinToString(",")}")
} else {
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ abstract class ValueSetResolver : ExternalAnswerValueSetResolver {

private suspend fun fetchValueSetFromDb(uri: String): List<Coding> {

val valueSets = fhirEngine.search<ValueSet> { filter(ValueSet.URL, { value = uri }) }
val valueSets =
fhirEngine.search<ValueSet> { filter(ValueSet.URL, { value = uri }) }.map { it.resource }

if (valueSets.isEmpty())
return listOf(Coding().apply { display = "No referral facility available." })
Expand Down
5 changes: 4 additions & 1 deletion configurablecare/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
buildscript {
repositories {
google()
mavenLocal()
mavenCentral()
gradlePluginPortal()
}
dependencies {
classpath("com.android.tools.build:gradle:8.0.2")
classpath("com.android.tools.build:gradle:8.1.1")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.0")
classpath("com.google.gms:google-services:4.3.15")
classpath("com.diffplug.spotless:spotless-plugin-gradle:6.6.0")
Expand All @@ -20,7 +21,9 @@ buildscript {
allprojects {
repositories {
google()
mavenLocal()
mavenCentral()
maven(url = "https://oss.sonatype.org/content/repositories/snapshots")
gradlePluginPortal()
}
configureSpotless()
Expand Down