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
Spotless
  • Loading branch information
aditya-07 committed Jan 31, 2022
commit 8751445a7af72383466a8bf918b18f7c286062ae
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ internal class FhirSynchronizer(
}
return if (exceptions.isEmpty()) {
Result.Success
} else {
} else {
emit(State.Glitch(exceptions))
Result.Error(exceptions)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ import com.google.android.fhir.db.impl.dao.LocalChangeToken
import com.google.android.fhir.db.impl.dao.SquashedLocalChange
import kotlinx.coroutines.flow.Flow

/**
* Module for uploading local changes to a [DataSource].
*/
/** Module for uploading local changes to a [DataSource]. */
internal interface Uploader {

/**
* Uploads the local changes to the [DataSource]. Particular implementations should take care of transforming the [SquashedLocalChange]s to particular network operations.
* Uploads the local changes to the [DataSource]. Particular implementations should take care of
* transforming the [SquashedLocalChange]s to particular network operations.
*/
suspend fun upload(
localChanges: List<SquashedLocalChange>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,18 @@ internal class BundlePayloadGenerator(
}

/**
* Splits up all the local changes into individual change lists to be included in particular [Bundle]s.
* Splits up all the local changes into individual change lists to be included in particular
* [Bundle]s.
*/
internal interface LocalChangeProvider {
suspend fun getLocalChanges(): List<List<SquashedLocalChange>>
}

/**
* Tells [BundlePayloadGenerator] that all the local changes should be part of a single [Bundle] transaction.
* Tells [BundlePayloadGenerator] that all the local changes should be part of a single [Bundle]
* transaction.
*/
internal class DefaultLocalChangeProvider(private val localChanges : List<SquashedLocalChange>) : LocalChangeProvider {
override suspend fun getLocalChanges(): List<List<SquashedLocalChange>> = listOf(localChanges)
internal class DefaultLocalChangeProvider(private val localChanges: List<SquashedLocalChange>) :
LocalChangeProvider {
override suspend fun getLocalChanges(): List<List<SquashedLocalChange>> = listOf(localChanges)
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,20 @@ import org.hl7.fhir.r4.model.OperationOutcome
import org.hl7.fhir.r4.model.Resource
import org.hl7.fhir.r4.model.ResourceType

/** [Uploader] implementation to work with Fhir [Bundle].*/
/** [Uploader] implementation to work with Fhir [Bundle]. */
internal class BundleUploader(val dataSource: DataSource) : Uploader {

override suspend fun upload(
localChanges: List<SquashedLocalChange>,
): Flow<UploadResult> = flow {
BundlePayloadGenerator(
createRequest = HttpPutForCreateEntryComponent(FhirContext.forR4().newJsonParser()),
updateRequest = HttpPatchForUpdateEntryComponent(),
deleteRequest = HttpDeleteEntryComponent(),
localChangeProvider = DefaultLocalChangeProvider(localChanges)
).generate().forEach { (bundle, localChangeTokens) ->
createRequest = HttpPutForCreateEntryComponent(FhirContext.forR4().newJsonParser()),
updateRequest = HttpPatchForUpdateEntryComponent(),
deleteRequest = HttpDeleteEntryComponent(),
localChangeProvider = DefaultLocalChangeProvider(localChanges)
)
.generate()
.forEach { (bundle, localChangeTokens) ->
try {
val response =
dataSource.postBundle(
Expand All @@ -60,7 +62,6 @@ internal class BundleUploader(val dataSource: DataSource) : Uploader {
response is Bundle && response.type == Bundle.BundleType.TRANSACTIONRESPONSE -> {
UploadResult.Success(LocalChangeToken(localChangeTokens.flatMap { it.ids }))
}

response is OperationOutcome && response.issue.isNotEmpty() -> {
UploadResult.Failure(
ResourceSyncException(
Expand All @@ -69,7 +70,6 @@ internal class BundleUploader(val dataSource: DataSource) : Uploader {
)
)
}

else -> {
UploadResult.Failure(
ResourceSyncException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ import org.hl7.fhir.r4.model.UriType
*/
internal abstract class HttpVerbBasedBundleEntryComponent(private val httpVerb: Bundle.HTTPVerb) {

/**
* Should return [Resource] for the [LocalChangeEntity].
*/
/** Should return [Resource] for the [LocalChangeEntity]. */
abstract fun getEntryResource(localChange: LocalChangeEntity): IBaseResource?

fun getEntry(squashedLocalChange: SquashedLocalChange): Bundle.BundleEntryComponent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ class BundleTransactionPayloadGeneratorTest {
createRequest = HttpPutForCreateEntryComponent(FhirContext.forR4().newJsonParser()),
updateRequest = HttpPatchForUpdateEntryComponent(),
deleteRequest = HttpDeleteEntryComponent(),
localChangeProvider = object : LocalChangeProvider {
override suspend fun getLocalChanges(): List<List<SquashedLocalChange>> {
return listOf(listOf(), listOf())
}
},
localChangeProvider =
object : LocalChangeProvider {
override suspend fun getLocalChanges(): List<List<SquashedLocalChange>> {
return listOf(listOf(), listOf())
}
},
)

val result = generator.generate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ class BundleUploaderTest {
}

@Test
fun `upload Bundle transaction error during upload should emit Failure`() =
runBlocking {
fun `upload Bundle transaction error during upload should emit Failure`() = runBlocking {
val result =
BundleUploader(
TestingUtils.BundleDataSource { throw ConnectException("Failed to connect to server.") }
Expand All @@ -99,7 +98,20 @@ class BundleUploaderTest {
resourceType = ResourceType.Patient.name,
resourceId = "Patient-001",
type = LocalChangeEntity.Type.INSERT,
payload = FhirContext.forR4().newJsonParser().encodeResourceToString(Patient().apply { id = "Patient-001"; addName(HumanName().apply { addGiven("John"); family = "Doe" }) })
payload =
FhirContext.forR4()
.newJsonParser()
.encodeResourceToString(
Patient().apply {
id = "Patient-001"
addName(
HumanName().apply {
addGiven("John")
family = "Doe"
}
)
}
)
)
)
)
Expand Down