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

[MWCore] Add Resource Tagging #2499

Merged
merged 45 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
3e90564
rework auth items
sevenreup Apr 26, 2023
1d2b0e0
Merge branch 'mwcore-dev' into mwcore-migrate-auth-code
sevenreup May 26, 2023
3ed6fb2
init tagging
sevenreup Jun 5, 2023
ffff742
update register
sevenreup Jun 6, 2023
a4224b4
update
sevenreup Jun 6, 2023
6aaa2fd
update sync
sevenreup Jun 9, 2023
0b05614
Merge branch 'mwcore-dev' into mwcore-migrate-auth-code
sevenreup Jun 9, 2023
39564ac
update sync key
sevenreup Jun 9, 2023
dc34c49
fix location syncs
sevenreup Jun 18, 2023
639c7af
fix activity listener
sevenreup Jun 21, 2023
92536ca
test fixes
sevenreup Jun 26, 2023
c638406
fixes
sevenreup Jun 26, 2023
cea8e95
replace app with default
sevenreup Jun 27, 2023
7e5da06
tests fixed
sevenreup Jun 27, 2023
a72ff10
Merge branch 'mwcore-dev' into mwcore-migrate-auth-code
sevenreup Jun 27, 2023
2a34a3e
Merge branch 'mwcore-dev' into mwcore-migrate-auth-code
sevenreup Jun 27, 2023
2a28a52
stuff
sevenreup Jun 27, 2023
9991c0b
test fixes
sevenreup Jun 27, 2023
a525453
Merge branch 'mwcore-migrate-auth-code' of github.com:opensrp/fhircor…
sevenreup Jun 27, 2023
067b2ca
Update CqlContentTest.kt
sevenreup Jun 27, 2023
79f8267
add tests
sevenreup Jun 28, 2023
3718c03
add tests
sevenreup Jun 28, 2023
d0c116a
update appsettings
sevenreup Jun 29, 2023
cbef459
update
sevenreup Jul 4, 2023
9c2b9e9
update
sevenreup Jul 5, 2023
5b8c799
update QuestViewmodel
sevenreup Jul 11, 2023
b99b092
Update QuestionnaireActivity.kt
sevenreup Jul 12, 2023
eee0185
Update LoginViewModel.kt
sevenreup Jul 12, 2023
3807eb9
Update TokenAuthenticator.kt
sevenreup Jul 13, 2023
cf69b88
update auth
sevenreup Jul 17, 2023
39695d3
Update AppSettingActivityTest.kt
sevenreup Jul 17, 2023
c1de593
updates
sevenreup Jul 18, 2023
2d0962b
update resource tags
sevenreup Jul 20, 2023
cb62edf
block to finish saving data
sevenreup Jul 26, 2023
1c099a5
update
sevenreup Jul 26, 2023
518b38a
spotless run
sevenreup Jul 26, 2023
59d95d4
Update network_security_config.xml
sevenreup Jul 28, 2023
49fc301
Merge branch 'mwcore-dev' into mwcore-migrate-auth-code
LZRS Jul 28, 2023
183cc6a
Fix failed AppNotIdleException for some Compose tests
LZRS Jul 30, 2023
3c91f1b
Disable catching of non-test related exceptions
LZRS Jul 30, 2023
875eb8f
add tests
sevenreup Jul 31, 2023
0631d83
Update SharedPreferencesHelperTest.kt
sevenreup Jul 31, 2023
1002bc7
Merge branch 'mwcore-dev' into mwcore-migrate-auth-code
sevenreup Jul 31, 2023
7885fa2
Update AndroidExtensions.kt
sevenreup Jul 31, 2023
3449870
update tests
sevenreup Aug 1, 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
update
  • Loading branch information
sevenreup committed Jun 6, 2023
commit a4224b4de6dc6b9e6e1165675572454b8f6b5bb5
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,35 @@
package org.smartregister.fhircore.engine.configuration

import android.content.Context
import com.google.android.fhir.FhirEngine
import com.google.android.fhir.db.ResourceNotFoundException
import com.google.android.fhir.get
import com.google.android.fhir.logicalId
import com.google.android.fhir.search.search
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.Json
import org.hl7.fhir.r4.model.Binary
import org.hl7.fhir.r4.model.Composition
import org.hl7.fhir.r4.model.Identifier
import org.hl7.fhir.r4.model.Resource
import org.hl7.fhir.r4.model.ResourceType
import org.smartregister.fhircore.engine.configuration.app.AppConfigClassification
import org.smartregister.fhircore.engine.configuration.view.DataFiltersConfiguration
import org.smartregister.fhircore.engine.data.local.DefaultRepository
import org.smartregister.fhircore.engine.data.remote.fhir.resource.FhirResourceDataSource
import org.smartregister.fhircore.engine.util.DispatcherProvider
import org.smartregister.fhircore.engine.util.SharedPreferencesHelper
import org.smartregister.fhircore.engine.util.extension.decodeJson
import org.smartregister.fhircore.engine.util.extension.decodeResourceFromString
import org.smartregister.fhircore.engine.util.extension.extractId
import org.smartregister.fhircore.engine.util.extension.generateMissingId
import org.smartregister.fhircore.engine.util.extension.updateFrom
import org.smartregister.fhircore.engine.util.extension.updateLastUpdated
import timber.log.Timber

/**
Expand All @@ -50,10 +60,10 @@ class ConfigurationRegistry
@Inject
constructor(
@ApplicationContext val context: Context,
val fhirEngine: FhirEngine,
val fhirResourceDataSource: FhirResourceDataSource,
val sharedPreferencesHelper: SharedPreferencesHelper,
val dispatcherProvider: DispatcherProvider,
val repository: DefaultRepository
val dispatcherProvider: DispatcherProvider
) {

val configurationsMap = mutableMapOf<String, Configuration>()
Expand Down Expand Up @@ -124,8 +134,7 @@ constructor(
suspend fun loadConfigurations(appId: String, configsLoadedCallback: (Boolean) -> Unit) {
this.appId = appId

repository
.searchCompositionByIdentifier(appId)
searchCompositionByIdentifier(appId)
.also { if (it == null) configsLoadedCallback(false) }
?.section
?.filter { isWorkflowPoint(it) }
Expand All @@ -135,7 +144,7 @@ constructor(
WorkflowPoint(
classification = it.focus.identifier.value,
description = it.title,
resource = repository.getBinary(it.focus.extractId()),
resource = getBinary(it.focus.extractId()),
workflowPoint = it.focus.identifier.value
)
workflowPointsMap[workflowPointName] = workflowPoint
Expand Down Expand Up @@ -207,8 +216,8 @@ constructor(
CoroutineScope(ioDispatcher).launch {
try {
Timber.i("Fetching non-workflow resources for app $appId")
repository
.searchCompositionByIdentifier(appId)

searchCompositionByIdentifier(appId)
?.section
?.groupBy { it.focus.reference?.split(TYPE_REFERENCE_DELIMITER)?.get(0) ?: "" }
?.entries
Expand All @@ -221,9 +230,7 @@ constructor(
sectionComponent.focus.extractId()
}
val searchPath = resourceGroup.key + "?${Composition.SP_RES_ID}=$resourceIds"
fhirResourceDataSource.loadData(searchPath).entry.forEach {
repository.addOrUpdate(false, it.resource)
}
fhirResourceDataSource.loadData(searchPath).entry.forEach { addOrUpdate(it.resource) }
}
} catch (exception: Exception) {
Timber.e("Error fetching non-workflow resources for app $appId")
Expand All @@ -242,6 +249,45 @@ constructor(
}
}

suspend fun searchCompositionByIdentifier(identifier: String): Composition? =
fhirEngine
.search<Composition> {
filter(Composition.IDENTIFIER, { value = of(Identifier().apply { value = identifier }) })
}
.firstOrNull()

suspend fun getBinary(id: String): Binary = fhirEngine.get(id)

/**
* Using this [FhirEngine] and [DispatcherProvider], update this stored resources with the passed
* resource, or create it if not found.
*/
suspend fun <R : Resource> addOrUpdate(resource: R) {
withContext(dispatcherProvider.io()) {
resource.updateLastUpdated()
try {
fhirEngine.get(resource.resourceType, resource.logicalId).run {
fhirEngine.update(updateFrom(resource))
}
} catch (resourceNotFoundException: ResourceNotFoundException) {
create(resource)
}
}
}

/**
* Using this [FhirEngine] and [DispatcherProvider], for all passed resources, make sure they all
* have IDs or generate if they don't, then pass them to create.
*
* @param resources vararg of resources
*/
suspend fun create(vararg resources: Resource): List<String> {
return withContext(dispatcherProvider.io()) {
resources.onEach { it.generateMissingId() }
fhirEngine.create(*resources)
}
}

companion object {
const val DEFAULT_APP_ID = "appId"
const val BASE_CONFIG_PATH = "configs/$DEFAULT_APP_ID"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ import org.smartregister.fhircore.engine.util.extension.updateLastUpdated
open class DefaultRepository
@Inject
constructor(
open val fhirEngine: FhirEngine,
open val dispatcherProvider: DispatcherProvider,
open val sharedPreferencesHelper: SharedPreferencesHelper,
open val configurationRegistry: ConfigurationRegistry,
open val configService: ConfigService
open val fhirEngine: FhirEngine,
open val dispatcherProvider: DispatcherProvider,
open val sharedPreferencesHelper: SharedPreferencesHelper,
open val configurationRegistry: ConfigurationRegistry,
open val configService: ConfigService
) {

suspend inline fun <reified T : Resource> loadResource(resourceId: String): T? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@ import org.smartregister.fhircore.engine.util.SharedPreferencesHelper
class AppRegisterRepository
@Inject
constructor(
override val fhirEngine: FhirEngine,
override val dispatcherProvider: DefaultDispatcherProvider,
override val sharedPreferencesHelper: SharedPreferencesHelper,
override val configurationRegistry: ConfigurationRegistry,
val registerDaoFactory: RegisterDaoFactory,
override val configService: ConfigService,
val tracer: PerformanceReporter
override val fhirEngine: FhirEngine,
override val dispatcherProvider: DefaultDispatcherProvider,
override val sharedPreferencesHelper: SharedPreferencesHelper,
override val configurationRegistry: ConfigurationRegistry,
val registerDaoFactory: RegisterDaoFactory,
override val configService: ConfigService,
val tracer: PerformanceReporter
) :
RegisterRepository,
DefaultRepository(
fhirEngine = fhirEngine,
dispatcherProvider = dispatcherProvider,
sharedPreferencesHelper = sharedPreferencesHelper,
configurationRegistry = configurationRegistry,
configService = configService
) {
DefaultRepository(
fhirEngine = fhirEngine,
dispatcherProvider = dispatcherProvider,
sharedPreferencesHelper = sharedPreferencesHelper,
configurationRegistry = configurationRegistry,
configService = configService
) {

override suspend fun loadRegisterData(
currentPage: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,15 @@ constructor(
family.member.map { member ->
defaultRepository.loadResource<Patient>(member.entity.extractId())?.let { patient ->
patient.active = false
defaultRepository.addOrUpdate(true,patient)
defaultRepository.addOrUpdate(true, patient)
}
}
}
}
family.member.clear()
family.active = false

defaultRepository.addOrUpdate(true,family)
defaultRepository.addOrUpdate(true, family)
}
}

Expand Down Expand Up @@ -253,7 +253,7 @@ constructor(
}
}
}
defaultRepository.addOrUpdate(true,patient)
defaultRepository.addOrUpdate(true, patient)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ constructor(
if (!this.active) throw IllegalStateException("Patient already deleted")
this.active = false
}
defaultRepository.addOrUpdate(true,patient)
defaultRepository.addOrUpdate(true, patient)
}

suspend fun transformChildrenPatientToRegisterData(patients: List<Patient>): List<RegisterData> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ constructor(
reference = "${ResourceType.RelatedPerson.name}/${resource.logicalId}"
}
}
defaultRepository.addOrUpdate(true,this)
defaultRepository.addOrUpdate(true, this)
}
}

Expand Down Expand Up @@ -467,7 +467,7 @@ constructor(
it.valueCodeableConcept.coding.forEach { questionnaireResponse.meta.addTag(it) }
}

defaultRepository.addOrUpdate(true,questionnaireResponse)
defaultRepository.addOrUpdate(true, questionnaireResponse)
}

suspend fun performExtraction(
Expand All @@ -489,7 +489,7 @@ constructor(

suspend fun saveBundleResources(bundle: Bundle) {
if (!bundle.isEmpty) {
bundle.entry.forEach { defaultRepository.addOrUpdate(true,it.resource) }
bundle.entry.forEach { defaultRepository.addOrUpdate(true, it.resource) }
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 Ona Systems, Inc
* Copyright 2021 Ona Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import ca.uhn.fhir.rest.gclient.ReferenceClientParam
import com.google.android.fhir.datacapture.extensions.createQuestionnaireResponseItem
import com.google.android.fhir.logicalId
import java.util.Date
import java.util.LinkedList
import java.util.UUID
import org.hl7.fhir.r4.model.Base
import org.hl7.fhir.r4.model.BaseDateTimeType
Expand All @@ -48,7 +49,6 @@ import org.json.JSONObject
import org.smartregister.fhircore.engine.data.local.DefaultRepository
import org.smartregister.fhircore.engine.util.fhirpath.FhirPathDataExtractor
import timber.log.Timber
import java.util.LinkedList

private val fhirR4JsonParser = FhirContext.forR4Cached().newJsonParser()

Expand Down