Skip to content

Commit

Permalink
Update JsonParser usages for Dao classes
Browse files Browse the repository at this point in the history
Spotless clean
  • Loading branch information
ndegwamartin committed Dec 5, 2024
1 parent e6aa55e commit 7b5e762
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class DatabaseImplTest {
@JvmField @Parameterized.Parameter(0) var encrypted: Boolean = false

private val context: Context = ApplicationProvider.getApplicationContext()
private val parser = FhirContext.forCached(FhirVersionEnum.R4).newJsonParser()
private val parser = FhirContext.forR4Cached().newJsonParser()
private lateinit var services: FhirServices
private lateinit var database: Database

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class EncryptedDatabaseErrorTest {
private val context: Context = ApplicationProvider.getApplicationContext()
private val parser = FhirContext.forR4().newJsonParser()
private val terser = FhirTerser(FhirContext.forCached(FhirVersionEnum.R4))
private val terser = FhirTerser(FhirContext.forR4Cached())
private val resourceIndexer = ResourceIndexer(SearchParamDefinitionsProviderImpl())

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,9 @@ internal class DatabaseImpl(
.build()
}

private val resourceDao by lazy {
db.resourceDao().also {
it.iParser = FhirContext.forCached(FhirVersionEnum.R4).newJsonParser()
it.resourceIndexer = resourceIndexer
}
}
private val resourceDao by lazy { db.resourceDao().also { it.resourceIndexer = resourceIndexer } }

private val localChangeDao =
db.localChangeDao().also {
it.iParser = FhirContext.forCached(FhirVersionEnum.R4).newJsonParser()
it.fhirTerser = fhirTerser
}
private val localChangeDao = db.localChangeDao().also { it.fhirTerser = fhirTerser }

override suspend fun <R : Resource> insert(vararg resource: R): List<String> {
val logicalIds = mutableListOf<String>()
Expand Down Expand Up @@ -204,7 +195,7 @@ internal class DatabaseImpl(

override suspend fun select(type: ResourceType, id: String): Resource {
return resourceDao.getResource(resourceId = id, resourceType = type)?.let {
FhirContext.forCached(FhirVersionEnum.R4).newJsonParser().parseResource(it) as Resource
FhirContext.forR4Cached().newJsonParser().parseResource(it) as Resource
}
?: throw ResourceNotFoundException(type.name, id)
}
Expand Down Expand Up @@ -380,7 +371,7 @@ internal class DatabaseImpl(
val updatedReferenceValue = "${updatedResource.resourceType.name}/${updatedResource.logicalId}"
referringResourcesUuids.forEach { resourceUuid ->
resourceDao.getResourceEntity(resourceUuid)?.let {
val iParser = FhirContext.forCached(FhirVersionEnum.R4).newJsonParser()
val iParser = FhirContext.forR4Cached().newJsonParser()
val referringResource = iParser.parseResource(it.serializedResource) as Resource
val updatedReferringResource =
addUpdatedReferenceToResource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.parser.IParser
import ca.uhn.fhir.util.FhirTerser
import ca.uhn.fhir.util.ResourceReferenceInfo
Expand Down Expand Up @@ -50,8 +51,6 @@ import timber.log.Timber
*/
@Dao
internal abstract class LocalChangeDao {

lateinit var iParser: IParser
lateinit var fhirTerser: FhirTerser

@Insert(onConflict = OnConflictStrategy.REPLACE)
Expand All @@ -70,7 +69,7 @@ internal abstract class LocalChangeDao {
open suspend fun addInsert(resource: Resource, resourceUuid: UUID, timeOfLocalChange: Instant) {
val resourceId = resource.logicalId
val resourceType = resource.resourceType
val resourceString = iParser.encodeResourceToString(resource)
val resourceString = FhirContext.forR4Cached().newJsonParser().encodeResourceToString(resource)

val localChangeEntity =
LocalChangeEntity(
Expand Down Expand Up @@ -128,6 +127,7 @@ internal abstract class LocalChangeDao {
"Unexpected DELETE when updating $resourceType/$resourceId. UPDATE failed.",
)
}
val iParser = FhirContext.forR4Cached().newJsonParser()
val oldResource = iParser.parseResource(oldEntity.serializedResource) as Resource
val jsonDiff = diff(iParser, oldResource, updatedResource)
if (jsonDiff.length() == 0) {
Expand Down Expand Up @@ -475,6 +475,7 @@ internal abstract class LocalChangeDao {
oldReference: String,
updatedReference: String,
): LocalChangeEntity {
val iParser = FhirContext.forR4Cached().newJsonParser()
return when (localChange.type) {
LocalChangeEntity.Type.INSERT -> {
val insertResourcePayload = iParser.parseResource(localChange.payload) as Resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.RawQuery
import androidx.sqlite.db.SupportSQLiteQuery
import ca.uhn.fhir.parser.IParser
import ca.uhn.fhir.context.FhirContext
import com.google.android.fhir.db.ResourceNotFoundException
import com.google.android.fhir.db.impl.entities.DateIndexEntity
import com.google.android.fhir.db.impl.entities.DateTimeIndexEntity
Expand Down Expand Up @@ -55,7 +55,6 @@ import org.hl7.fhir.r4.model.ResourceType
internal abstract class ResourceDao {
// this is ugly but there is no way to inject these right now in Room as it is the one creating
// the dao
lateinit var iParser: IParser
lateinit var resourceIndexer: ResourceIndexer

/**
Expand All @@ -69,7 +68,8 @@ internal abstract class ResourceDao {
getResourceEntity(resource.logicalId, resource.resourceType)?.let {
val entity =
it.copy(
serializedResource = iParser.encodeResourceToString(resource),
serializedResource =
FhirContext.forR4Cached().newJsonParser().encodeResourceToString(resource),
lastUpdatedLocal = timeOfLocalChange,
lastUpdatedRemote = resource.meta.lastUpdated?.toInstant() ?: it.lastUpdatedRemote,
)
Expand All @@ -86,7 +86,8 @@ internal abstract class ResourceDao {
val entity =
it.copy(
resourceId = updatedResource.logicalId,
serializedResource = iParser.encodeResourceToString(updatedResource),
serializedResource =
FhirContext.forR4Cached().newJsonParser().encodeResourceToString(updatedResource),
lastUpdatedRemote = updatedResource.lastUpdated ?: it.lastUpdatedRemote,
versionId = updatedResource.versionId ?: it.versionId,
)
Expand All @@ -107,7 +108,8 @@ internal abstract class ResourceDao {
getResourceEntity(resource.logicalId, resource.resourceType)?.let {
val entity =
it.copy(
serializedResource = iParser.encodeResourceToString(resource),
serializedResource =
FhirContext.forR4Cached().newJsonParser().encodeResourceToString(resource),
lastUpdatedRemote = resource.meta.lastUpdated?.toInstant(),
versionId = resource.versionId,
)
Expand Down Expand Up @@ -267,7 +269,8 @@ internal abstract class ResourceDao {
resourceType = resource.resourceType,
resourceUuid = resourceUuid,
resourceId = resource.logicalId,
serializedResource = iParser.encodeResourceToString(resource),
serializedResource =
FhirContext.forR4Cached().newJsonParser().encodeResourceToString(resource),
versionId = resource.versionId,
lastUpdatedRemote = resource.lastUpdated,
lastUpdatedLocal = lastUpdatedLocal,
Expand Down Expand Up @@ -297,7 +300,10 @@ internal abstract class ResourceDao {
lastUpdatedRemote: Instant?,
) {
getResourceEntity(resourceId, resourceType)?.let { oldResourceEntity ->
val resource = iParser.parseResource(oldResourceEntity.serializedResource) as Resource
val resource =
FhirContext.forR4Cached()
.newJsonParser()
.parseResource(oldResourceEntity.serializedResource) as Resource
resource.updateMeta(versionId, lastUpdatedRemote)
updateResourceWithUuid(oldResourceEntity.resourceUuid, resource)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.google.android.fhir.sync.upload.request

import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.context.FhirVersionEnum
import com.google.android.fhir.ContentTypes
import com.google.android.fhir.sync.upload.patch.Patch
import com.google.android.fhir.sync.upload.patch.PatchMapping
Expand Down Expand Up @@ -58,8 +57,6 @@ internal class UrlRequestGenerator(

companion object Factory {

private val parser = FhirContext.forCached(FhirVersionEnum.R4).newJsonParser()

private val createMapping =
mapOf(
HttpVerb.POST to this::postForCreateResource,
Expand Down Expand Up @@ -107,21 +104,24 @@ internal class UrlRequestGenerator(
UrlUploadRequest(
httpVerb = HttpVerb.DELETE,
url = "${patch.resourceType}/${patch.resourceId}",
resource = parser.parseResource(patch.payload) as Resource,
resource =
FhirContext.forR4Cached().newJsonParser().parseResource(patch.payload) as Resource,
)

private fun postForCreateResource(patch: Patch) =
UrlUploadRequest(
httpVerb = HttpVerb.POST,
url = patch.resourceType,
resource = parser.parseResource(patch.payload) as Resource,
resource =
FhirContext.forR4Cached().newJsonParser().parseResource(patch.payload) as Resource,
)

private fun putForCreateResource(patch: Patch) =
UrlUploadRequest(
httpVerb = HttpVerb.PUT,
url = "${patch.resourceType}/${patch.resourceId}",
resource = parser.parseResource(patch.payload) as Resource,
resource =
FhirContext.forR4Cached().newJsonParser().parseResource(patch.payload) as Resource,
)

private fun patchForUpdateResource(patch: Patch) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.google.android.fhir.impl

import androidx.test.core.app.ApplicationProvider
import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.context.FhirVersionEnum
import ca.uhn.fhir.rest.gclient.TokenClientParam
import ca.uhn.fhir.rest.param.ParamPrefixEnum
import com.google.android.fhir.FhirServices.Companion.builder
Expand Down Expand Up @@ -76,7 +75,7 @@ import org.robolectric.RobolectricTestRunner
class FhirEngineImplTest {
private val services = builder(ApplicationProvider.getApplicationContext()).inMemory().build()
private val fhirEngine = services.fhirEngine
private val parser = FhirContext.forCached(FhirVersionEnum.R4).newJsonParser()
private val parser = FhirContext.forR4Cached().newJsonParser()

@Before fun setUp(): Unit = runBlocking { fhirEngine.create(TEST_PATIENT_1) }

Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
android-fhir-common = "0.1.0-alpha05"
android-fhir-engine = "0.1.0-beta05"
android-fhir-knowledge = "0.1.0-beta01"
androidx-acivity = "1.7.2"
androidx-activity = "1.7.2"
androidx-appcompat = "1.6.1"
androidx-arch-core = "2.2.0"
androidx-benchmark = "1.1.1"
Expand Down Expand Up @@ -37,7 +37,7 @@ material = "1.9.0"
android-fhir-common = { module = "com.google.android.fhir:common", version.ref = "android-fhir-common" }
android-fhir-engine = { module = "com.google.android.fhir:engine", version.ref = "android-fhir-engine" }
android-fhir-knowledge = { module = "com.google.android.fhir:knowledge", version.ref = "android-fhir-knowledge" }
androidx-activity = { module = "androidx.activity:activity", version.ref = "androidx-acivity" }
androidx-activity = { module = "androidx.activity:activity", version.ref = "androidx-activity" }
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "androidx-appcompat" }
androidx-arch-core-testing = { module = "androidx.arch.core:core-testing", version.ref = "androidx-arch-core" }
androidx-benchmark-junit4 = { module = "androidx.benchmark:benchmark-junit4", version.ref = "androidx-benchmark" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package com.google.android.fhir.knowledge
import android.content.Context
import androidx.room.Room
import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.context.FhirVersionEnum
import com.google.android.fhir.knowledge.db.KnowledgeDatabase
import com.google.android.fhir.knowledge.db.entities.ImplementationGuideEntity
import com.google.android.fhir.knowledge.db.entities.ResourceMetadataEntity
Expand Down Expand Up @@ -295,8 +294,7 @@ internal constructor(
private suspend fun readResourceOrNull(file: File): IBaseResource? =
withContext(Dispatchers.IO) {
try {
FileInputStream(file)
.use(FhirContext.forCached(FhirVersionEnum.R4).newJsonParser()::parseResource)
FileInputStream(file).use(FhirContext.forR4Cached().newJsonParser()::parseResource)
} catch (e: Exception) {
Timber.e(e, "Unable to load resource from $file")
null
Expand Down

0 comments on commit 7b5e762

Please sign in to comment.