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

Upload local changed resources using Bundle transaction #1084

Merged
merged 26 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a2c2e2b
Upload local changed resources using Bundle transaction
aditya-07 Jan 27, 2022
f0346d7
Merge branch 'master' into ak/bundle-transaction-for-upload
aditya-07 Jan 30, 2022
e7806f8
Spotless apply
aditya-07 Jan 30, 2022
f99721d
Code refactoring and added tests
aditya-07 Jan 31, 2022
8751445
Spotless
aditya-07 Jan 31, 2022
ad9effa
Merge branch 'master' into ak/bundle-transaction-for-upload
aditya-07 Jan 31, 2022
f97436e
Merge branch 'master' into ak/bundle-transaction-for-upload
aditya-07 Jan 31, 2022
d110c01
Merge branch 'master' into ak/bundle-transaction-for-upload
aditya-07 Feb 1, 2022
534981e
Merge branch 'master' into ak/bundle-transaction-for-upload
aditya-07 Feb 2, 2022
ef49423
Merge branch 'master' into ak/bundle-transaction-for-upload
aditya-07 Feb 3, 2022
9995696
Merge branch 'master' into ak/bundle-transaction-for-upload
aditya-07 Feb 7, 2022
5f450fa
Merge branch 'master' into ak/bundle-transaction-for-upload
aditya-07 Feb 8, 2022
d97efae
Merge branch 'master' into ak/bundle-transaction-for-upload
aditya-07 Feb 8, 2022
5b562e1
Merge branch 'master' into ak/bundle-transaction-for-upload
aditya-07 Feb 9, 2022
5f048c6
Merge branch 'master' into ak/bundle-transaction-for-upload
aditya-07 Feb 14, 2022
af66ad8
Review comment changes
aditya-07 Feb 15, 2022
a67da54
Merge branch 'master' into ak/bundle-transaction-for-upload
aditya-07 Feb 18, 2022
e4b9a73
Merge branch 'master' into ak/bundle-transaction-for-upload
aditya-07 Feb 18, 2022
15e2a3d
Refactored bundle generator code
aditya-07 Feb 18, 2022
a8d58b5
Review comment changes.
aditya-07 Feb 21, 2022
6b2c206
Handling of resource location in the Bundle transaction response to e…
aditya-07 Feb 21, 2022
f659cc6
Merge branch 'master' into ak/bundle-transaction-for-upload
aditya-07 Feb 21, 2022
8b68bfc
Updated the code for resource id and type extraction from location
aditya-07 Feb 21, 2022
20d9f5c
Review comments: Added/updated kdocs. Also added constants for conten…
aditya-07 Feb 24, 2022
92eaf4f
Merge branch 'master' into ak/bundle-transaction-for-upload
aditya-07 Feb 24, 2022
a99bfd9
Merge branch 'master' into ak/bundle-transaction-for-upload
aditya-07 Feb 25, 2022
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
Review comment changes
  • Loading branch information
aditya-07 committed Feb 15, 2022
commit af66ad8f297d2e29fa1a9e7add6fd02c99278a54
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import retrofit2.http.Body
import retrofit2.http.DELETE
import retrofit2.http.GET
import retrofit2.http.PATCH
import retrofit2.http.POST
import retrofit2.http.PUT
import retrofit2.http.Path
import retrofit2.http.Url
Expand All @@ -52,7 +53,7 @@ interface HapiFhirService {
@DELETE("{type}/{id}")
suspend fun deleteResource(@Path("type") type: String, @Path("id") id: String): OperationOutcome

@retrofit2.http.POST(".") suspend fun postData(@Body body: RequestBody): Resource
@POST(".") suspend fun postData(@Body body: RequestBody): Resource

companion object {
const val BASE_URL = "https://hapi.fhir.org/baseR4/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.android.fhir.sync.bundle

import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.context.FhirVersionEnum
import com.google.android.fhir.db.impl.dao.LocalChangeToken
import com.google.android.fhir.db.impl.dao.SquashedLocalChange
import com.google.android.fhir.sync.DataSource
Expand All @@ -37,8 +38,9 @@ internal class BundleUploader(val dataSource: DataSource) : Uploader {
override suspend fun upload(
localChanges: List<SquashedLocalChange>,
): Flow<UploadResult> = flow {
BundlePayloadGenerator(
createRequest = HttpPutForCreateEntryComponent(FhirContext.forR4().newJsonParser()),
TransactionBundleGenerator(
createRequest =
HttpPutForCreateEntryComponent(FhirContext.forCached(FhirVersionEnum.R4).newJsonParser()),
updateRequest = HttpPatchForUpdateEntryComponent(),
deleteRequest = HttpDeleteEntryComponent(),
localChangeProvider = DefaultLocalChangeProvider(localChanges)
Expand All @@ -48,7 +50,9 @@ internal class BundleUploader(val dataSource: DataSource) : Uploader {
try {
val response =
dataSource.postBundle(
FhirContext.forR4().newJsonParser().encodeResourceToString(bundle)
FhirContext.forCached(FhirVersionEnum.R4)
.newJsonParser()
.encodeResourceToString(bundle)
)
emit(getUploadResult(response, localChangeTokens))
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ typealias ResourceBundleAndAssociatedLocalChangeTokens = Pair<Bundle, List<Local
* Generates pairs of Transaction [Bundle] and [LocalChangeToken]s associated with the resources
* present in the transaction bundle.
*/
internal class BundlePayloadGenerator(
private val bundleType: Bundle.BundleType = Bundle.BundleType.TRANSACTION,
internal class TransactionBundleGenerator(
private val createRequest: HttpVerbBasedBundleEntryComponent,
private val updateRequest: HttpVerbBasedBundleEntryComponent,
private val deleteRequest: HttpVerbBasedBundleEntryComponent,
Expand All @@ -45,7 +44,7 @@ internal class BundlePayloadGenerator(
localChanges: List<SquashedLocalChange>
): ResourceBundleAndAssociatedLocalChangeTokens {
return Bundle().apply {
type = bundleType
type = Bundle.BundleType.TRANSACTION
localChanges.forEach {
this.addEntry(
getHttpVerbBasedBundleEntryComponentForLocalChangeType(it.localChange.type).getEntry(it)
Expand All @@ -71,7 +70,7 @@ internal interface LocalChangeProvider {
}

/**
* Tells [BundlePayloadGenerator] that all the local changes should be part of a single [Bundle]
* Tells [TransactionBundleGenerator] that all the local changes should be part of a single [Bundle]
* transaction.
*/
internal class DefaultLocalChangeProvider(private val localChanges: List<SquashedLocalChange>) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.android.fhir.sync.bundle

import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.context.FhirVersionEnum
import com.google.android.fhir.db.impl.dao.LocalChangeToken
import com.google.android.fhir.db.impl.dao.SquashedLocalChange
import com.google.android.fhir.db.impl.entities.LocalChangeEntity
Expand Down Expand Up @@ -99,7 +100,7 @@ class BundleUploaderTest {
resourceId = "Patient-001",
type = LocalChangeEntity.Type.INSERT,
payload =
FhirContext.forR4()
FhirContext.forCached(FhirVersionEnum.R4)
.newJsonParser()
.encodeResourceToString(
Patient().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.android.fhir.sync.bundle

import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.context.FhirVersionEnum
import com.google.android.fhir.db.impl.dao.LocalChangeToken
import com.google.android.fhir.db.impl.dao.LocalChangeUtils
import com.google.android.fhir.db.impl.dao.SquashedLocalChange
Expand All @@ -32,13 +33,14 @@ import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class BundleTransactionPayloadGeneratorTest {
class TransactionBundleGeneratorTest {

@Test
fun `generate() should return empty list if there are no local changes`() = runBlocking {
val generator =
BundlePayloadGenerator(
createRequest = HttpPutForCreateEntryComponent(FhirContext.forR4().newJsonParser()),
TransactionBundleGenerator(
createRequest =
HttpPutForCreateEntryComponent(FhirContext.forCached(FhirVersionEnum.R4).newJsonParser()),
updateRequest = HttpPatchForUpdateEntryComponent(),
deleteRequest = HttpDeleteEntryComponent(),
localChangeProvider =
Expand All @@ -56,7 +58,7 @@ class BundleTransactionPayloadGeneratorTest {

@Test
fun `generate() should return single Transaction Bundle with 3 entries`() = runBlocking {
val jsonParser = FhirContext.forR4().newJsonParser()
val jsonParser = FhirContext.forCached(FhirVersionEnum.R4).newJsonParser()
val changes =
listOf(
SquashedLocalChange(
Expand Down Expand Up @@ -135,8 +137,9 @@ class BundleTransactionPayloadGeneratorTest {
)
)
val generator =
BundlePayloadGenerator(
createRequest = HttpPutForCreateEntryComponent(FhirContext.forR4().newJsonParser()),
TransactionBundleGenerator(
createRequest =
HttpPutForCreateEntryComponent(FhirContext.forCached(FhirVersionEnum.R4).newJsonParser()),
updateRequest = HttpPatchForUpdateEntryComponent(),
deleteRequest = HttpDeleteEntryComponent(),
localChangeProvider = DefaultLocalChangeProvider(changes)
Expand Down