Home My Page Projects Code Snippets Project Openings jHL7v3lib
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files Mediawiki


EasyHL7 with CDA

From jHL7v3lib Wiki
Jump to: navigation, search

The following section explains how to use jEasyHL7 to parse and generate CDA documents.

View page https://forge.opensil.com/plugins/mediawiki/wiki/jhl7v3lib/index.php/Tutoriel for library installation.

jHLv3lib is made from a low level API, and a high level API called jEasyHL7.

  • Use the high level API when you want to build quick (and not so dirty) CDA document. In fact, the high level API is a simplification of the low level API.
  • Use low level API when you need to build complex CDA document or parse specific CDA documents. The low level API is an exact implementation of HL7v3 RIM and CDA specification (and so is quite complex...).

The model used by low and high level API is not exactly the same (simpler model for higher API). But you can transform high level model to low level model and low level model to high level model easily.


Parsing CDA documents

Use the following code snippet to parse CDA documents:

Object cda = JAXBLoader.loadHL7(cdaString, HL7Facade.TYPE_CDA);

cdaString is a String which contains your CDA document. The return cda object is an instance of one of the low level CDA object.

To use your newly parsed CDA, you need to cast it to the expected type.

Example: if you are expecting some CDA clinical document, cast cda instance like this:

POCDMT000040ClinicalDocument cdaLow = ((JAXBElement<POCDMT000040ClinicalDocument>)cda).getValue();

You can now use your low level CDA document as you wish.

If you want to use your CDA with jEasyHL7 (High level API), you need to convert your low level instance to high level instance. The call Mapper can do that easily for you.

Just do it like that:

CDA cdaData = (CDA) new CDAMapper().fromLowLevel(cdaLow);

Building CDA documents

You can build CDA using either high level or low level API.

Building CDA using high level API

First, you need to instantiate the high level CDA object:

CDA cda  = new CDA();

Then, use fulfill all fields you need in this instance. For example, this code snippet sets document code system to LOINC:


Do like that for any fields needed.

Here is an example of full dummy HL7 CDA document :

CDA cda  = new CDA();
		// We build CDA header
		cda.setDocumentCreationTime(new Date());
		cda.setDocumentTypeName("Consultation note");
		// We add some patient
		Patient patient = new Patient();
		patient.setBirthDate(new Date());
		patient.setSuffix("The 1st.");
		// We add some author
		Author author = new Author();
		// We add some custodian
		Custodian custodian = new Custodian();
		custodian.setName("OxyGen LABOS s.a.r.l.");
		// We add some documentationOf data
		DocumentationOf doc = new DocumentationOf();
                // For this complex task, we need to use low level objects
		CE code = new CE();
		code.setDisplayName("Examen anatomopathologique de pièce d'exérèse");
		// We add some CDA body
		CDABody body = new CDABody();
		POCDMT000040Component3 component = new POCDMT000040Component3();
		POCDMT000040Section section = new POCDMT000040Section();
		CE sectionCode = new CE();
		ST title = new ST();
		title.value = "History of Present Illness";
		StrucDocText text = new StrucDocText();
		text.getContent().add("is a 67 year old male referred for further asthma management.\nOnset of asthma in his twenties teens.");

Once done, you can generate your XML data. Just call the HL7Facade using your high level CDA instance:

String hl7Message = HL7Facade.getInstance().toHL7(cda);

Here is the output of this example:

<?xml version="1.0" encoding="UTF-8"?>
<ClinicalDocument xsi:schemaLocation="" xmlns="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <typeId root="2.16.840.1.113883.1.3" extension="POCD_HD000040"/>
   <id root="2.16.840.1.113883.19.4" extension="c266"/>
   <code code="11488-4" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC" displayName="Consultation note"/>
   <effectiveTime value="20140704122937"/>
   <confidentialityCode code="R" codeSystem="2.16.840.1.113883.5.2"/>
         <id root="2.16.840.1.113883.19.4" extension="123456"/>
               <suffix>The 1st.</suffix>
            <administrativeGenderCode code="F" codeSystem="2.16.840.1.113883.5.1"/>
            <birthTime value="20140704122937"/>
            <id root="2.16.840.1.113883.19.4"/>
      <time value="20140704122937"/>
         <id root="2.16.840.1.113883.19.5" extension="KP0017"/>
            <id root="2.16.840.1.113883.19.5"/>
            <id root="2.16.840.1.113883.19.5"/>
            <name>OxyGen LABOS s.a.r.l.</name>
         <code code="ZZQP193" codeSystem="" codeSystemName="LOINC" displayName="Examen anatomopathologique de pièce d'exérèse"/>
               <code code="10164-2" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC"/>
               <title>History of Present Illness</title>
               <text>is a 67 year old male referred for further asthma management.
Onset of asthma in his twenties teens.</text>

Building CDA using low level API

You can use low level API in two ways.

  • Build a CDA object using high level API and transform it to low level object, then edit generated document in low level mode.
  • Build a low level CDA object from scratch

Low level CDA from high level CDA

When you have some high level CDA document, use the following method to transform it to low level object:

Object lowLevelCDA = HL7Facade.getInstance().toLowLevel(cda);

Next, you have to cast your lowLevelCDA instance to the expected low level model class (POCDMT000040ClinicalDocument).

Low level CDA from scratch

When you want to build some CDA document from scratch, instantiate the correct CDA document class from low level model. Use the low level object factory to do this:

// We instaciante low level CDA
clinicalDoc = new POCDMT000040ClinicalDocument();
// Here set the data you need in clinicalDoc instance
// Then, use the ObjectFactory to transform you POCDMT000040ClinicalDocument to some marshalling friendly CDA document.
cdaClinicalDoc =   new org.hl7.v3_cda.ObjectFactory().createClinicalDocument(clinicalDoc)

When done, you will need to directly transform it to XML document. Use this code snippet to do that:

StringWriter strw = new StringWriter();
Marshaller marshaller = JAXBbuilder.getMarshaller(HL7Facade.TYPE_CDA);

Fist, we configure the Marshaller to marshall some CDA document. Then, we proceed to CDA marshalling. The result of the marshalling process will be sent to our StringWriter instance.

For some working example, see HL7FacadeTest class in test package (/src/test/java/org/oxygen/services/facade/HL7FacadeTest.java)