HAPI FHIR miscellaneous coding tricks

Russell Bateman
February 2024
last update:




I finally got around to composing some beginning notes on HAPI FHIR data structures and how to use them. I am also scraping together some tricks I use to boilerplate their use, reduce complexity, save vertical space, etc.

Also, these notes go a very long way to connect HAPI FHIR (Java) coding to what you're used to seeing in formal HL7FHIR standard documentation.

Often at first, it puzzled me as whether to use a value, a coding, the display or the text. There is not a great number of good examples or sample code for HAPI FHIR. I have tried to gather the antidote for that in my various HAPI FHIR notes on this site.


CodeableConcept, how it works and tricks in using it...

Let's examine HAPI FHIR CodeableConcept. I'm going to use the instance consumed by FHIR Condition to explore this. At the link just here, scroll down to field clinicalStatus for the context of this illustration.

Creating clinicalStatus from scratch...

...for a Condition using subfields text or coding:

import java.util.ArrayList;
import java.util.List;

import org.hl7.fhir.r4.model.CodeableConcept;
import org.hl7.fhir.r4.model.Coding;
import org.hl7.fhir.r4.model.Condition;
import com.windofkeltia.fhir.FhirCode;

Condition condition = new Condition();

condition.setClinicalStatus( new CodeableConcept().setCoding( FhirCode.addCode( "active" ) ) );

condition.setClinicalStatus( new CodeableConcept().setText( "The subject is currently experiencing the condition" ) );

Extracting clinicalStatus from an existing...

...Condition's text or coding subfields:

fhirPath = "Condition.clinicalStatus";    // hmmm... this isn't exactly right

// Model-level stuff
// Imagine we're passed a Condition and need to dig out the important thing:
if( condition.hasClinicalStatus() )
{
  String          text           = null;
  String          code           = null;
  CodeableConcept clinicalStatus = condition.getClinicalStatus();

  if( clinicalStatus.hasCoding() )        // prioritize what's in the Coding
  {
    Coding coding = clinicalStatus.getCodingFirstRep();
    coding.getCode();
  }
  else if( clinicalStatus.hasText() )     // accept text only if that's all there is
  {
    text = clinicalStatus.getText();
  }

  // surfacing the results to the View-level:
  if( !StringUtilities.isEmpty( code ) )
    ...
  else if( !StringUtilities.isEmpty( text ) )
    ...
}

FhirCode.java:

Here's a method I use to build an obligatory list that only needs one element and keep it on one line in the caller.

/**
 * Helper (static) method to add the passed-in code to a Coding
 * for a list of codings in creating a CodeableConcept. This
 * facilitates writing code such as:
 *
 * CodeableConcept codeableConcept = new CodeableConcept().setCoding( addCode( "active" ) );
 *
 * @param code the exact code as a string.
 * @return the list that a method such as in the sample code above might take.
 */
public static List< Coding > addCode( final String code )
{
  Coding         coding = new Coding();      coding.setCode( code );
  List< Coding > list   = new ArrayList<>(); list.add( coding );
  return list;
}