Skip to content

Commit

Permalink
Update response metadata for default resource consolidator.
Browse files Browse the repository at this point in the history
  • Loading branch information
Santosh Pingle committed Apr 29, 2024
1 parent 366e3d5 commit 26cb5a7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
5 changes: 5 additions & 0 deletions engine/src/main/java/com/google/android/fhir/db/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ internal interface Database {
lastUpdated: Instant,
)

/** Updates the `resource` meta in the FHIR resource database. */
suspend fun updateVersionIdAndLastUpdated(
resource: Resource,
)

/**
* Updates existing [Resource] present in the [ResourceEntity] for metadata such as versionId,
* resourceId, lastModifiedTime, and reference value for other referring resources. In the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.android.fhir.db.impl

import android.content.Context
import android.util.Log
import androidx.annotation.VisibleForTesting
import androidx.room.Room
import androidx.room.withTransaction
Expand All @@ -34,11 +35,16 @@ import com.google.android.fhir.db.impl.dao.ForwardIncludeSearchResult
import com.google.android.fhir.db.impl.dao.ReverseIncludeSearchResult
import com.google.android.fhir.db.impl.entities.ResourceEntity
import com.google.android.fhir.index.ResourceIndexer
import com.google.android.fhir.lastUpdated
import com.google.android.fhir.logicalId
import com.google.android.fhir.search.SearchQuery
import com.google.android.fhir.toLocalChange
import com.google.android.fhir.versionId
import java.time.Instant
import java.util.UUID
import java.util.*
import org.hl7.fhir.r4.model.IdType
import org.hl7.fhir.r4.model.InstantType
import org.hl7.fhir.r4.model.Meta
import org.hl7.fhir.r4.model.Resource
import org.hl7.fhir.r4.model.ResourceType

Expand Down Expand Up @@ -170,6 +176,37 @@ internal class DatabaseImpl(
versionId,
lastUpdated,
)
resourceDao.getResourceEntity(resourceId, resourceType)?.let {
val preSyncResource = iParser.parseResource(it.serializedResource) as Resource
preSyncResource.meta =
Meta().apply {
versionIdElement = IdType(versionId)
lastUpdatedElement = InstantType(Date.from(lastUpdated))
}
resourceDao.updatePayloadPostSync(
preSyncResource.logicalId,
preSyncResource.resourceType,
iParser.encodeResourceToString(preSyncResource),
)
}
}
}

override suspend fun updateVersionIdAndLastUpdated(
resource: Resource,
) {
db.withTransaction {
resourceDao.updateAndIndexRemoteVersionIdAndLastUpdate(
resource.logicalId,
resource.resourceType,
resource.meta.versionId,
resource.meta.lastUpdated.toInstant(),
)
resourceDao.updatePayloadPostSync(
resource.logicalId,
resource.resourceType,
iParser.encodeResourceToString(resource),
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,20 @@ internal abstract class ResourceDao {
payloadPostSync: String,
)

@Query(
"""
UPDATE ResourceEntity
SET serializedResource = :postSyncSerializedResource
WHERE resourceId = :resourceId
AND resourceType = :resourceType
""",
)
abstract suspend fun updatePayloadPostSync(
resourceId: String,
resourceType: ResourceType,
postSyncSerializedResource: String,
)

@Query(
"""
UPDATE ResourceEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ internal class DefaultResourceConsolidator(private val database: Database) : Res
private suspend fun updateVersionIdAndLastUpdated(resource: DomainResource) {
if (resource.hasMeta() && resource.meta.hasVersionId() && resource.meta.hasLastUpdated()) {
database.updateVersionIdAndLastUpdated(
resource.id,
resource.resourceType,
resource.meta.versionId,
resource.meta.lastUpdated.toInstant(),
resource,
)
}
}
Expand Down

0 comments on commit 26cb5a7

Please sign in to comment.