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

Add API to support saving local only resources #2178

Draft
wants to merge 21 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
e31fc6f
New API to support saving local only resources
ndegwamartin Aug 23, 2023
efd3444
Add API to support saving local only resources
ndegwamartin Sep 16, 2023
f43abf5
Merge branch 'master' into local-only-resources
ndegwamartin Sep 16, 2023
3a33df4
Merge branch 'master' into local-only-resources
ndegwamartin Sep 21, 2023
78d5bb5
Refactor create local only changes API
ndegwamartin Sep 21, 2023
d8401b9
Merge branch 'master' into local-only-resources
ndegwamartin Sep 21, 2023
563c233
Rename test cases
ndegwamartin Sep 21, 2023
7d7d440
Revert return type for Test Utilites create method signature
ndegwamartin Sep 21, 2023
0fc5d21
Merge branch 'master' into local-only-resources
ndegwamartin Oct 2, 2023
fcb6691
Merge branch 'master' into local-only-resources
ndegwamartin Oct 13, 2023
c7d2465
Merge branch 'master' into local-only-resources
ndegwamartin Oct 17, 2023
824e171
Merge branch 'master' into local-only-resources
ndegwamartin Oct 24, 2023
b695b46
Merge branch 'master' into local-only-resources
ndegwamartin Oct 30, 2023
afbfd9a
Fix build for Insert Local Only
ndegwamartin Oct 30, 2023
9615372
Merge branch 'master' into local-only-resources
f-odhiambo Nov 7, 2023
000fa42
Merge branch 'master' into local-only-resources
Rkareko Nov 24, 2023
675602b
Merge branch 'master' into local-only-resources
ndegwamartin Feb 7, 2024
afb0f8c
Merge branch 'master' into local-only-resources
ndegwamartin Mar 19, 2024
05e9ed6
Merge branch 'master' into local-only-resources
ndegwamartin Mar 27, 2024
ae42ddb
Merge branch 'master' into local-only-resources
ndegwamartin Apr 19, 2024
54f2d14
Merge branch 'master' into local-only-resources
ndegwamartin Jun 21, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ interface FhirEngine {
/**
* Creates one or more FHIR [resource]s in the local storage.
*
* @param isLocalOnly - Setting the value to [true] instructs engine that the resource and its
* subsequent updates should never be synced to the server.
* @return the logical IDs of the newly created resources.
*/
suspend fun create(vararg resource: Resource): List<String>
suspend fun create(vararg resource: Resource, isLocalOnly: Boolean = false): List<String>

/** Loads a FHIR resource given the class and the logical ID. */
suspend fun get(type: ResourceType, id: String): Resource
Expand Down
9 changes: 9 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 @@ -39,6 +39,15 @@ internal interface Database {
*/
suspend fun <R : Resource> insert(vararg resource: R): List<String>

/**
* Inserts a list of local `resources` into the FHIR resource database. If any of the resources
* already exists, it will be overwritten.
*
* @param <R> The resource type
* @return the logical IDs of the newly created resources.
*/
suspend fun <R : Resource> insertLocalOnly(vararg resource: R): List<String>

/**
* Inserts a list of remote `resources` into the FHIR resource database. If any of the resources
* already exists, it will be overwritten.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import java.time.Instant
import java.util.UUID
import org.hl7.fhir.r4.model.Resource
import org.hl7.fhir.r4.model.ResourceType
import java.util.Collections

/**
* The implementation for the persistence layer using Room. See docs for
Expand Down Expand Up @@ -141,6 +142,10 @@ internal class DatabaseImpl(
return logicalIds
}

override suspend fun <R : Resource> insertLocalOnly(vararg resource: R): List<String> {
return db.withTransaction { resourceDao.insertAllRemote(resource.toList()).map { it.toString() }.toList() }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is the same as insertRemote, do you think we should handle local changes differently in the DB?

}

override suspend fun <R : Resource> insertRemote(vararg resource: R) {
db.withTransaction { resourceDao.insertAllRemote(resource.toList()) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ import org.hl7.fhir.r4.model.ResourceType
/** Implementation of [FhirEngine]. */
internal class FhirEngineImpl(private val database: Database, private val context: Context) :
FhirEngine {
override suspend fun create(vararg resource: Resource): List<String> {
return database.insert(*resource)
override suspend fun create(vararg resource: Resource, isLocalOnly: Boolean): List<String> {
return if (isLocalOnly) database.insertLocalOnly(*resource) else database.insert(*resource)
}

override suspend fun get(type: ResourceType, id: String): Resource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ open class TestDownloadManagerImpl(
}

object TestFhirEngineImpl : FhirEngine {
override suspend fun create(vararg resource: Resource) = emptyList<String>()
override suspend fun create(vararg resource: Resource, isLocalOnly: Boolean) = emptyList<String>()

override suspend fun update(vararg resource: Resource) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,31 @@ class FhirEngineImplTest {
assertResourceEquals(TEST_PATIENT_2, fhirEngine.get<Patient>(TEST_PATIENT_2_ID))
}

@Test
fun create_isLocalOnlyTrue_shouldCreateResourceWithoutFlaggingLocalChange() = runBlocking {
val totalLocalChangesPatient2 =
fhirEngine.getLocalChanges(ResourceType.Patient, TEST_PATIENT_2_ID).size
fhirEngine.create(TEST_PATIENT_2, isLocalOnly = true)
assertResourceEquals(TEST_PATIENT_2, fhirEngine.get<Patient>(TEST_PATIENT_2_ID))
assertThat(fhirEngine.getLocalChanges(ResourceType.Patient, TEST_PATIENT_2_ID).size)
.isEqualTo(totalLocalChangesPatient2)
}

@Test
fun createAll_isLocalOnlyTrue_shouldCreateResourceWithoutFlaggingLocalChange() = runBlocking {
val totalLocalChangesPatient1 =
fhirEngine.getLocalChanges(ResourceType.Patient, TEST_PATIENT_1_ID).size
val totalLocalChangesPatient2 =
fhirEngine.getLocalChanges(ResourceType.Patient, TEST_PATIENT_2_ID).size
fhirEngine.create(TEST_PATIENT_1, TEST_PATIENT_2, isLocalOnly = true)
assertResourceEquals(TEST_PATIENT_1, fhirEngine.get<Patient>(TEST_PATIENT_1_ID))
assertResourceEquals(TEST_PATIENT_2, fhirEngine.get<Patient>(TEST_PATIENT_2_ID))
assertThat(fhirEngine.getLocalChanges(ResourceType.Patient, TEST_PATIENT_1_ID).size)
.isEqualTo(totalLocalChangesPatient1)
assertThat(fhirEngine.getLocalChanges(ResourceType.Patient, TEST_PATIENT_2_ID).size)
.isEqualTo(totalLocalChangesPatient2)
}

@Test
fun create_resourceWithoutId_shouldCreateResourceWithAssignedId() = runBlocking {
val patient =
Expand Down
Loading