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

OpenSIL

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.


Contents

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:

cda.setCodeSystem(CodeSystems.getCodeSystem(CodeSystems.CODESYSTEM_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.setCodeSystem(CodeSystems.getCodeSystem(CodeSystems.CODESYSTEM_LOINC));
		cda.setDocumentCreationTime(new Date());
		cda.setDocumentType("11488-4");
		cda.setDocumentTypeName("Consultation note");
		cda.setOrganizationExtension("c266");
		cda.setOrganizationId("2.16.840.1.113883.19.4");
		cda.setDocumentConfidentiality(CDA.CONFIDENTIALITY_RESTRICTED);
		// We add some patient
		Patient patient = new Patient();
		patient.setBirthDate(new Date());
		patient.setFamilyName("Levin");
		patient.setGender(Patient.SEX_FEMALE);
		patient.setPatientIdentifier("123456");
		patient.setPatientSurname("Chloe");
		patient.setSuffix("The 1st.");
		cda.setPatient(patient);
		// We add some author
		Author author = new Author();
		author.setExtension("KP0017");
		author.setName("Robert");
		author.setFamilyName("Dolin");
		author.setSuffix("MD");
		author.setOrganizationId("2.16.840.1.113883.19.5");
		cda.setAuthor(author);
		// We add some custodian
		Custodian custodian = new Custodian();
		custodian.setId("2.16.840.1.113883.19.5");
		custodian.setName("OxyGen LABOS s.a.r.l.");
		cda.setCustodian(custodian);
		// 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.setCode("ZZQP193");
		code.setDisplayName("Examen anatomopathologique de pièce d'exérèse");
		code.setCodeSystem("1.2.250.1.213.2.5");
		code.setCodeSystemName("LOINC");
		doc.setCode(code);
		cda.setDocumentationOf(doc);
		// We add some CDA body
		CDABody body = new CDABody();
		cda.setBody(body);
		POCDMT000040Component3 component = new POCDMT000040Component3();
		body.getComponent().add(component);
		POCDMT000040Section section = new POCDMT000040Section();
		component.setSection(section);
		CE sectionCode = new CE();
		section.setCode(sectionCode);
		sectionCode.setCode("10164-2");
		sectionCode.setCodeSystem("2.16.840.1.113883.6.1");
		sectionCode.setCodeSystemName("LOINC");
		ST title = new ST();
		title.value = "History of Present Illness";
		section.setTitle(title);
		StrucDocText text = new StrucDocText();
		section.setText(text);
		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"/>
   <recordTarget>
      <patientRole>
         <id root="2.16.840.1.113883.19.4" extension="123456"/>
         <patient>
            <name>
               <given>Chloe</given>
               <family>Levin</family>
               <suffix>The 1st.</suffix>
            </name>
            <administrativeGenderCode code="F" codeSystem="2.16.840.1.113883.5.1"/>
            <birthTime value="20140704122937"/>
         </patient>
         <providerOrganization>
            <id root="2.16.840.1.113883.19.4"/>
         </providerOrganization>
      </patientRole>
   </recordTarget>
   <author>
      <time value="20140704122937"/>
      <assignedAuthor>
         <id root="2.16.840.1.113883.19.5" extension="KP0017"/>
         <assignedPerson>
            <name>
               <given>Robert</given>
               <family>Dolin</family>
               <suffix>MD</suffix>
            </name>
         </assignedPerson>
         <representedOrganization>
            <id root="2.16.840.1.113883.19.5"/>
         </representedOrganization>
      </assignedAuthor>
   </author>
   <custodian>
      <assignedCustodian>
         <representedCustodianOrganization>
            <id root="2.16.840.1.113883.19.5"/>
            <name>OxyGen LABOS s.a.r.l.</name>
         </representedCustodianOrganization>
      </assignedCustodian>
   </custodian>
   <documentationOf>
      <serviceEvent>
         <code code="ZZQP193" codeSystem="1.2.250.1.213.2.5" codeSystemName="LOINC" displayName="Examen anatomopathologique de pièce d'exérèse"/>
      </serviceEvent>
   </documentationOf>
   <component>
      <structuredBody>
         <component>
            <section>
               <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>
            </section>
         </component>
      </structuredBody>
   </component>
</ClinicalDocument>

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);
marshaller.marshal(
				cdaClinicalDoc,
				strw
				);

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)