HAPI provides a several ways to add Narrative Text to your encoded messages.

The simplest way is to simply place the narrative text directly in the resource via the getText() method.

Patient pat = new Patient();
pat.getText().setStatus(NarrativeStatusEnum.GENERATED);
pat.getText().setDiv("<div>This is the narrative text<br/>this is line 2</div>");

Automatic Narrative Generartion

HAPI also comes with a built-in mechanism for automatically generating narratives based on your resources.

Warning: This built-in capability is a work in progress, and does not cover every type of resource or even every attribute in any resource. You should test it and configure it for your particular use cases.

HAPI's built-in narrative generation uses the Thymeleaf library for templating narrative texts. Thymeleaf provides a simple XHTML-based syntax which is easy to use and meshes well with the HAPI-FHIR model objects.

A Simple Example

Activating HAPI's built-in narrative generator is as simple as calling setNarrativeGenerator.

Patient patient = new Patient();
patient.addIdentifier().setSystem("urn:foo").setValue("7000135");
patient.addName().addFamily("Smith").addGiven("John").addGiven("Edward");
patient.addAddress().addLine("742 Evergreen Terrace").setCity("Springfield").setState("ZZ");

FhirContext ctx = FhirContext.forDstu2();

// Use the narrative generator
ctx.setNarrativeGenerator(new DefaultThymeleafNarrativeGenerator());

// Encode the output, including the narrative
String output = ctx.newJsonParser().setPrettyPrint(true).encodeResourceToString(patient);
System.out.println(output);

...which produces the following output:

<Patient xmlns="http://hl7.org/fhir">
   <text>
      <status value="generated"/>
      <div xmlns="http://www.w3.org/1999/xhtml">
         <div class="hapiHeaderText"> John Edward <b>SMITH </b></div>
         <table class="hapiPropertyTable">
            <tbody>
               <tr><td>Identifier</td><td>7000135</td></tr>
               <tr><td>Address</td><td><span>742 Evergreen Terrace</span><br/><span>Springfield</span> <span>ZZ</span></td></tr>
            </tbody>
         </table>
      </div>
   </text>
   <!-- .... snip ..... -->
</Patient>

Built-in Narrative Templates

HAPI currently only comes with built-in support for a few resource types. Our intention is that people enhance these templates and create new ones, and share these back with us so that we can continue to build out the library. To see the current template library, see the source repository here.

Note that these templates expect a few specific CSS definitions to be present in your site's CSS file. See the narrative CSS to see these.

Creating your own Templates

To use your own templates for narrative generation, simply create one or more templates, using the Thymeleaf HTML based syntax.

<div>
	<!-- 
	Normal Thymeleaf tags apply. Here, we loop through each of the 
	identifiers that the practitioner has, and create a DIV tag for
	each one, with the text "Identifier: [value]"
	-->
	<div th:each="identifier : ${resource.identifier}" th:text="'Identifier: ' + ${identifier.value}"></div>
	
	<!-- 
	HAPI also defines a custom tag attribute, th:narrative="value", 
	which is used to render a datatype into more HTML. In the example
	below, the value of ${resource.name} (which is the name property
	of the Practitioner resource, a HumanName datatype instance)
	is rendered, and its contents placed in a DIV tag. That DIV tag is
	then given a .nameElement CSS style.
	 -->
	<h1>Name</h1>
	<div th:narrative="${resource.name}" class="nameElement"></div>
	
	<h1>Address</h1>
	<div th:narrative="${resource.address}"></div>


</div>

Then create a properties file which describes your templates. In this properties file, each resource to be defined has a pair or properties.

The first (name.class) defines the class name of the resource to define a template for. The second (name.narrative) defines the path/classpath to the template file. The format of this path is file:/path/foo.html or classpath:/com/classpath/foo.html

# Two property lines in the file per template
practitioner.class=ca.uhn.fhir.model.dstu.resource.Practitioner
practitioner.narrative=file:src/test/resources/narrative/Practitioner.html

observation.class=ca.uhn.fhir.model.dstu.resource.Observation
observation.narrative=file:src/test/resources/narrative/Observation.html

# etc...

You may also override/define behaviour for datatypes. These datatype narrative definitions will be used as content within th:narrative blocks in resource templates. See the example resource template above for an example.

# datatypes use the same format as resources
humanname.class=ca.uhn.fhir.model.dstu.composite.HumanNameDt
humanname.narrative=classpath:ca/uhn/fhir/narrative/HumanNameDt.html

Finally, use the CustomThymeleafNarrativeGenerator and provide it to the FhirContext.

String propFile = "classpath:/com/foo/customnarrative.properties";
CustomThymeleafNarrativeGenerator gen = new CustomThymeleafNarrativeGenerator(propFile);

FhirContext ctx = FhirContext.forDstu2();
ctx.setNarrativeGenerator(gen);

Back to top

Reflow Maven skin by Andrius Velykis.