Wiki

Clone wiki

itk-payloads / noncodedcda / Helper

Creating a Non-Coded CDA Document (The easy way)

There are two ways that the itk-payloads library can be used to create a Non-Coded CDA document. The easiest way is to make use of a “helper” provided with the library. This is a simple class that takes a set of fields in the form of a NonCodedCDACommonFields object, and uses them to create the various Java objects that represent the common fields in the document (sometimes referred to as the “left-hand side of CDA”. Once these common fields have been populated, the helper can generate a basic document. You can then choose to add either a “NonXML Body” (e.g. a binary attachment), or a “Structured Body” (i.e. a set of structured human-readable text sections).

Firstly, download the latest jar files from the downloads section above, and put it in your classpath.

Now, you can create an instance of the NonCodedCDACommonFields object:

import uk.nhs.interoperability.payloads.DateValue;
import uk.nhs.interoperability.payloads.commontypes.*;
import uk.nhs.interoperability.payloads.exceptions.MissingMandatoryFieldException;
import uk.nhs.interoperability.payloads.helpers.*;
import uk.nhs.interoperability.payloads.noncodedcdav2.ClinicalDocument;
import uk.nhs.interoperability.payloads.util.FileWriter;
import uk.nhs.interoperability.payloads.vocabularies.generated.*;
import uk.nhs.interoperability.payloads.vocabularies.internal.*;
public class NonCodedCDADocumentTest {
	public static void main(String[] args) {
		NonCodedCDACommonFields fields = new NonCodedCDACommonFields();

And set some values:

		fields.setPatientNHSNo("993254128");
		fields.setPatientNHSNoIsTraced(false);

Date values can be set using the DateValue class (for more details see this page ):

		fields.setPatientBirthDate(new DateValue("19490101"));

And fields that contain clinical codes can be set using the provided Java enumerations which provide the various values for each field vocabulary (for more details see this page:../pages/CodedEntries.textile ):

		fields.setPatientGender(Sex._Male);

Some fields will themselves be a class – for example the “PatientAddress” field. To add these fields, first create the relevant object and then add it to the EndOfLifeCareISBFields object (note – all getters and setters return this to allow the builder pattern to be used as shown below):

		fields.setPatientAddress(new Address()
					.addAddressLine("17, County Court")
					.addAddressLine("Woodtown")
					.setPostcode("Medway"));

If the class is a simple one, you may find it more convenient to use the constructor to initialise the values (refer to the javadoc for details of constructor arguments):

		fields.setPatientName(new PersonName("Mr", "Mark", "Smith"));

Once you have set all the relevant values in the NonCodedCDACommonFields object, you can use the NonCodedCDADocumentCreationHelper class to generate the more complex set of objects that represent the CDA document contents. This will throw an exception if any mandatory fields are missing:

		ClinicalDocument doc;
		try {
			doc = NonCodedCDADocumentCreationHelper.createDocument(fields);
		} catch (MissingMandatoryFieldException e) {
			System.out.println("Unable to generate CDA document - some mandatory information is missing");
			e.printStackTrace();
			return;
		}

The ClinicalDocument object that is returned contains many other objects representing the various structured sections within the CDA document. If necessary you can use these objects to add any additional information that might be required that has not been done by the helper.

Now we need to add either a “NonXML Body” or a “Structured Body” with our actual clinical content. In this example we will use a simple base64 encoded chunk of arbitrary XML:

		String data = "PFRlc3RYTUw+PC9UZXN0WE1MPg==";
		doc = NonCodedCDADocumentCreationHelper.addNonXMLBody(doc, AttachmentType.Base64, "text/xml", data);

Now we can simply call the serialise method on this document to generate a full CDA document that can be sent over ITK (or via other channels if required):

		String xml = doc.serialise();

As a convenience, the standard renderer provided by the messaging team is also provided with the library, and can easily be used to generate a rendered HTML version of the document using the DocumentRenderer class:

		String html = DocumentRenderer.generateHTMLDocument(xml);

IMPORTANT NOTE: The helper makes a number of assumptions in order to simplify the process of creating a Non-Coded CDA document. For example it assumes that all patients will be identified using an NHS number, and that all staff will be identified using an SDS ID. These assumptions may not fit the specific needs of teams implementing this library in their solution. Developers are encouraged to use the helper as a starting point to build on/tweak as required (see Creating a Non-Coded CDA Document: The more complex way). If necessary the author of the itk-payloads library can provide support and advice on this on a best-endeavours basis.

Back

Updated