forked from google/android-fhir
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[1205] FhirEngine Sync Download API Re-design (google#1206)
* FhirEngine Sync Download API Redesign See: https://docs.google.com/document/d/1UZ7Ndb8JRmB2UdXpfIkPT-VAra6X2aLSzdGI_a8KJkg/edit# TESTED=all unit tests passed. Added one more unit test as well Co-authored-by: omarismail <[email protected]>
- Loading branch information
1 parent
62f5674
commit 6dae038
Showing
15 changed files
with
295 additions
and
97 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
demo/src/main/java/com/google/android/fhir/demo/data/DownloadManagerImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http:https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.android.fhir.demo.data | ||
|
||
import com.google.android.fhir.sync.DownloadManager | ||
import org.hl7.fhir.r4.model.Bundle | ||
import org.hl7.fhir.r4.model.ListResource | ||
import org.hl7.fhir.r4.model.Reference | ||
import org.hl7.fhir.r4.model.Resource | ||
|
||
class DownloadManagerImpl : DownloadManager { | ||
override fun getInitialUrl(): String { | ||
return "Patient?address-city=NAIROBI" | ||
} | ||
|
||
override fun createDownloadUrl(preProcessUrl: String, lastUpdate: String?): String { | ||
var downloadUrl = preProcessUrl | ||
|
||
// Affix lastUpdate to a $everything query using _since as per: | ||
// https://hl7.org/fhir/operation-patient-everything.html | ||
if (lastUpdate != null && downloadUrl.contains("\$everything")) { | ||
downloadUrl = "$downloadUrl?_since=$lastUpdate" | ||
} | ||
|
||
// Affix lastUpdate to non-$everything queries as per: | ||
// https://hl7.org/fhir/operation-patient-everything.html | ||
if (lastUpdate != null && !downloadUrl.contains("\$everything")) { | ||
downloadUrl = "$downloadUrl&_lastUpdated=gt$lastUpdate" | ||
} | ||
|
||
// Do not modify any URL set by a server that specifies the token of the page to return. | ||
if (downloadUrl.contains("&page_token")) { | ||
downloadUrl = preProcessUrl | ||
} | ||
|
||
return downloadUrl | ||
} | ||
|
||
override fun extractResourcesFromResponse(resourceResponse: Resource): Collection<Resource> { | ||
var bundleCollection: Collection<Resource> = mutableListOf() | ||
|
||
if (resourceResponse is Bundle && resourceResponse.type == Bundle.BundleType.SEARCHSET) { | ||
bundleCollection = resourceResponse.entry.map { it.resource } | ||
} | ||
return bundleCollection | ||
} | ||
|
||
override fun extractNextUrlsFromResource(resourceResponse: Resource): Collection<String> { | ||
val queueWork = mutableListOf<String>() | ||
|
||
// If the resource returned is a List, extract Patient references and fetch all resources | ||
// related to the patient using the $everything operation. | ||
if (resourceResponse is ListResource) { | ||
for (entry in resourceResponse.entry) { | ||
val reference = Reference(entry.item.reference) | ||
if (reference.referenceElement.resourceType.equals("Patient")) { | ||
val patientUrl = "${entry.item.reference}/\$everything" | ||
queueWork.add(patientUrl) | ||
} | ||
} | ||
} | ||
|
||
// If the resource returned is a Bundle, check to see if there is a "next" relation referenced | ||
// in the Bundle.link component, if so, append the URL referenced to list of URLs to download. | ||
if (resourceResponse is Bundle) { | ||
val nextUrl = | ||
resourceResponse.link.firstOrNull { component -> component.relation == "next" }?.url | ||
if (nextUrl != null) { | ||
queueWork.add(nextUrl) | ||
} | ||
} | ||
return queueWork | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
engine/src/main/java/com/google/android/fhir/sync/DownloadManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http:https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.android.fhir.sync | ||
|
||
import org.hl7.fhir.r4.model.Resource | ||
|
||
/** Module for downloading changes from a [DataSource]. */ | ||
interface DownloadManager { | ||
|
||
/** | ||
* Implement this method to set the initial resource to fetch. Can be a search query, such as | ||
* "Patient?address-city=NAIROBI or a resource Id, such as "List/123" | ||
*/ | ||
fun getInitialUrl(): String | ||
|
||
/** | ||
* Implement this method to affix the last update timestamp to the URL. | ||
* @preProcessUrl is the entire URL | ||
* @param lastUpdate retrieved from implementation of | ||
* [com.google.android.fhir.SyncDownloadContext.getLatestTimestampFor] | ||
*/ | ||
fun createDownloadUrl(preProcessUrl: String, lastUpdate: String?): String | ||
|
||
/** | ||
* Implement this method to take the FHIR resource returned by the server, and return the | ||
* resources that need to be saved into the local database. | ||
*/ | ||
fun extractResourcesFromResponse(resourceResponse: Resource): Collection<Resource> | ||
|
||
/** | ||
* Implement this method to parse the FHIR resource returned by the server to extract the next | ||
* URLs to download. | ||
*/ | ||
fun extractNextUrlsFromResource(resourceResponse: Resource): Collection<String> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.