Monday, June 26, 2023

GSoC 2023 at OpenMRS | Coding Period Week 04

You are welcome to this week's blog. This marks week number 4 of this year's GSoC coding period. I am excited to share what i have been able to achieve throughout this entire week. 

This week i have laboured to look into XML PATCHing and how to implement that in the OpenMRS Fhir2 Module. I started off by creating a utils class in the Util directory within the api directory. I will break down the contents of the class below, please follow along;-

First of all, it contains a static method applyXmlPatch that handles XML patch operations for resources in the context of the FHIR (Fast Healthcare Interoperability Resources) standard.

public static <T extends IBaseResource> T applyXmlPatch(FhirContext theCtx, T theResourceToUpdate, String thePatchBody) {

The applyXmlPatch method is declared as public static, which means it can be accessed without creating an instance of the XmlPatchUtils class. It takes three parameters: 

  • theCtx: An instance of FhirContext representing the FHIR context. 
  • theResourceToUpdate: An object of type T that extends IBaseResource, which represents the resource to be updated.
  • thePatchBody: A String containing the XML patch to be applied to the resource.
@SuppressWarnings("unchecked")
Class<T> clazz = (Class<T>) theResourceToUpdate.getClass();

The above line retrieves the class of the theResourceToUpdate object and casts it to Class<T>. It is used later for parsing the patched resource.

String inputResource = theCtx.newXmlParser().encodeResourceToString(theResourceToUpdate);

This line encodes the theResourceToUpdate object into an XML string using the newXmlParser() method of the theCtx object.

ByteArrayOutputStream result = new ByteArrayOutputStream();
try {
    Patcher.patch(new ByteArrayInputStream(inputResource.getBytes(Constants.CHARSET_UTF8)),
        new ByteArrayInputStream(thePatchBody.getBytes(Constants.CHARSET_UTF8)), result);
}
catch (IOException e) {
    throw new InvalidRequestException(e);
}


 The above block of code performs the XML patching operation using the Patcher.patch method. It takes the encoded input resource as a ByteArrayInputStream, the patch body as another ByteArrayInputStream, and writes the patched result to the result ByteArrayOutputStream.

return theCtx.newXmlParser().parseResource(clazz, toUtf8String(result.toByteArray()));

Finally, this line parses the patched resource from the result byte array using the newXmlParser() method of the theCtx object. It specifies the resource class (clazz) and uses the toUtf8String method to convert the byte array to a UTF-8 encoded string.

Overall, this code provides a utility method for applying XML patches to FHIR resources using the FHIR context and XML parsing capabilities provided by the FhirContext class.

To achieve the patching, we have utilized a library developed by dnaut and can be found at https://github.com/dnault/xml-patch.

I added the dependency to the root pom.xml file in the OpenMRS Module Fhir2 as shown below:-

            <dependency>
                <groupId>com.github.dnault</groupId>
                <artifactId>xml-patch</artifactId>
                <version>0.3.1</version>
            </dependency>

This and more can be accessed from the pull request at https://github.com/openmrs/openmrs-module-fhir2/pull/493.

Thanks for taking time to read through.

No comments:

Post a Comment