@@ -18,6 +18,7 @@ package org.smartregister.fhircore.engine.util.extension
1818
1919import android.content.Context
2020import ca.uhn.fhir.context.FhirContext
21+ import ca.uhn.fhir.parser.IParser
2122import ca.uhn.fhir.rest.gclient.ReferenceClientParam
2223import com.google.android.fhir.datacapture.extensions.logicalId
2324import com.google.android.fhir.get
@@ -116,6 +117,10 @@ fun CodeableConcept.stringValue(): String =
116117fun Resource.encodeResourceToString (): String =
117118 FhirContext .forR4().getCustomJsonParser().encodeResourceToString(this .copy())
118119
120+ fun Resource.encodeResourceToString (
121+ parser : IParser = FhirContext .forR4().getCustomJsonParser(),
122+ ): String = parser.encodeResourceToString(this .copy())
123+
119124fun StructureMap.encodeResourceToString (): String =
120125 FhirContext .forR4()
121126 .getCustomJsonParser()
@@ -128,49 +133,45 @@ fun StructureMap.encodeResourceToString(): String =
128133fun <T > String.decodeResourceFromString (): T =
129134 FhirContext .forR4().getCustomJsonParser().parseResource(this ) as T
130135
131- fun <T : Resource > T.updateFrom (updatedResource : Resource ): T {
132- var extensionUpdateFrom = listOf<Extension >()
133- if (updatedResource is Patient ) {
134- extensionUpdateFrom = updatedResource.extension
135- }
136- var extension = listOf<Extension >()
137- if (this is Patient ) {
138- extension = this .extension
139- }
140- val stringJson = encodeResourceToString()
136+ fun <T : Resource > T.updateFrom (
137+ updatedResource : Resource ,
138+ parser : IParser = FhirContext .forR4().getCustomJsonParser(),
139+ ): T {
140+ val extensionUpdateFrom: List <Extension > =
141+ if (updatedResource is Patient ) updatedResource.extension else emptyList()
142+ val extension: List <Extension > = if (this is Patient ) this .extension else emptyList()
143+
144+ val stringJson = this .encodeResourceToString(parser)
141145 val originalResourceJson = JSONObject (stringJson)
142146
143- originalResourceJson.updateFrom(JSONObject (updatedResource.encodeResourceToString()))
144- return FhirContext .forR4()
145- .getCustomJsonParser()
146- .parseResource(this ::class .java, originalResourceJson.toString())
147- .apply {
148- val meta = this .meta
149- val metaUpdateFrom = this @updateFrom.meta
150- if ((meta == null || meta.isEmpty)) {
151- if (metaUpdateFrom != null ) {
152- this .meta = metaUpdateFrom
153- this .meta.tag = metaUpdateFrom.tag
154- }
155- } else {
156- val setOfTags = mutableSetOf<Coding >()
157- setOfTags.addAll(meta.tag)
158- setOfTags.addAll(metaUpdateFrom.tag)
159- this .meta.tag = setOfTags.distinctBy { it.code + it.system }
147+ originalResourceJson.updateFrom(JSONObject (updatedResource.encodeResourceToString(parser)))
148+ return parser.parseResource(this ::class .java, originalResourceJson.toString()).apply {
149+ val meta = this .meta
150+ val metaUpdateFrom = this @updateFrom.meta
151+ if ((meta == null || meta.isEmpty)) {
152+ if (metaUpdateFrom != null ) {
153+ this .meta = metaUpdateFrom
154+ this .meta.tag = metaUpdateFrom.tag
160155 }
161- if ( this is Patient && this @updateFrom is Patient && updatedResource is Patient ) {
162- if (extension.isEmpty()) {
163- if (extensionUpdateFrom.isNotEmpty()) {
164- this .extension = extensionUpdateFrom
165- }
166- } else {
167- val setOfExtension = mutableSetOf< Extension >()
168- setOfExtension.addAll (extension)
169- setOfExtension.addAll (extensionUpdateFrom)
170- this .extension = setOfExtension.distinct()
156+ } else {
157+ val setOfTags = mutableSetOf< Coding >()
158+ setOfTags.addAll(meta.tag)
159+ setOfTags.addAll(metaUpdateFrom.tag)
160+ this .meta.tag = setOfTags.distinctBy { it.code + it.system }
161+ }
162+ if ( this is Patient && this @updateFrom is Patient && updatedResource is Patient ) {
163+ if (extension.isEmpty()) {
164+ if (extensionUpdateFrom.isNotEmpty()) {
165+ this .extension = extensionUpdateFrom
171166 }
167+ } else {
168+ val setOfExtension = mutableSetOf<Extension >()
169+ setOfExtension.addAll(extension)
170+ setOfExtension.addAll(extensionUpdateFrom)
171+ this .extension = setOfExtension.distinct()
172172 }
173173 }
174+ }
174175}
175176
176177@Throws(JSONException ::class )
@@ -452,6 +453,28 @@ fun String.resourceClassType(): Class<out Resource> =
452453 */
453454fun String.extractLogicalIdUuid () = this .substringAfter(" /" ).substringBefore(" /" )
454455
456+ /* *
457+ * A function that extracts only the UUID part of a resource logicalId from a URI.
458+ *
459+ * Examples:
460+ * 1. "http://smartreg.org/Library/3e2bb367-b9bb-4c30-9033-8f42657c5df7/history/3" returns
461+ *
462+ * ```
463+ * "3e2bb367-b9bb-4c30-9033-8f42657c5df7".
464+ * ```
465+ */
466+ fun String.extractLogicalIdUuidFromURI (resourceType : ResourceType ): String {
467+ val pathSegments: List <String > = this .split(" /" )
468+ val resourceIndex = pathSegments.indexOf(resourceType.name)
469+
470+ // Check if the resource type was found and if there's a segment after it
471+ return if (resourceIndex != - 1 && resourceIndex + 1 < pathSegments.size) {
472+ pathSegments[resourceIndex + 1 ]
473+ } else {
474+ " " // Return empty if the resource type or its ID is not found
475+ }
476+ }
477+
455478/* *
456479 * This suspend function updates the due date of the dependents of the current [Task], based on the
457480 * date of a related [Immunization] [Task]. The function loops through all the tasks that are
0 commit comments