2.6Narrative Generation

 

HAPI provides 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 setDivAsString() method.

Patient pat = new Patient();
pat.getText().setStatus(org.hl7.fhir.r4.model.Narrative.NarrativeStatus.GENERATED);
pat.getText().setDivAsString("<div>This is the narrative text<br/>this is line 2</div>");

2.6.1Automatic Narrative Generation

 

HAPI FHIR 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](/hapi-fhir/apidocs/hapi-fhir-base/ca/uhn/fhir/context/FhirContext.html#setNarrativeGenerator(ca.uhn.fhir.narrative.INarrativeGenerator).

Patient patient = new Patient();
patient.addIdentifier().setSystem("urn:foo").setValue("7000135");
patient.addName().setFamily("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>

2.6.2Built-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.

2.6.3Creating your own Templates

 

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

<!--/*-->
<html>
   <head>
      <link rel="stylesheet" type="text/css" href="narrative.css"/>
   </head>
   <body>
<!--*/-->
   <div>
      <h1>Operation Outcome</h1>
      <table border="0">
         <tr th:each="issue : ${resource.issue}">
            <td th:text="${issue.severityElement.value}" style="font-weight: bold;"></td>
            <td th:text="${issue.location}"></td>
            <th:block th:switch="${fhirVersion}">
               <th:block th:case="'DSTU1'">
                  <td><pre th:text="${issue.details}"/></td>
               </th:block>
               <th:block th:case="*">
                  <td><pre th:text="${issue.diagnostics}"/></td>
               </th:block>
            </th:block>
         </tr>
      </table>
   </div>
<!--/*-->
   </body>
</html>
<!--*/-->

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.resourceType=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.resourceType=HumanNameDt
humanname.narrative=classpath:ca/uhn/fhir/narrative/HumanNameDt.html]]></source>

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

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

ctx.setNarrativeGenerator(gen);