Last active
April 11, 2021 13:28
-
-
Save Korkmatik/a4cccb692393356b7118dcbdce9ba6ae to your computer and use it in GitHub Desktop.
FHIR Reference, Patient, Practioner, Organization, Observation and Search example implemented with Java HAPI FHIR library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ca.uhn.fhir.context.FhirContext; | |
import ca.uhn.fhir.parser.IParser; | |
import ca.uhn.fhir.rest.client.api.IGenericClient; | |
import org.hl7.fhir.r4.model.*; | |
public class Main { | |
public static void main(String[] args) { | |
FhirContext ctx = FhirContext.forR4(); | |
String serverBase = "http://hapi.fhir.org/baseR4/"; // TODO: Change if using another FHIR server | |
IGenericClient client = ctx.newRestfulGenericClient(serverBase); | |
/* | |
Part 1: Create an Organization | |
*/ | |
System.out.println("***\nPart 1: Create an Organizstion\n***"); | |
Organization organization = new Organization(); | |
organization.setActive(true); | |
organization.setName("Labor Top&Schnell"); | |
// Type of the organization | |
CodeableConcept codeableConcept = new CodeableConcept(); | |
Coding coding = new Coding(); | |
coding.setCode("prov"); | |
coding.setDisplay("Einrichtung des Gesundheitswesens - Labor"); | |
coding.setSystem("http://terminology.hl7.org/CodeSystem/organization-type"); | |
codeableConcept.addCoding(coding); | |
codeableConcept.setText("Art der Einrichtung des Gesundheitswesens"); | |
organization.addType(codeableConcept); | |
// Address of the organization | |
Address address = new Address(); | |
address.setText("Hauptstrasse 1, 01234 Neustadt, Deutschland"); | |
address.setPostalCode("01234"); | |
address.setCity("Neustadt"); | |
address.setCountry("Deutschland"); | |
organization.addAddress(address); | |
System.out.println("The organization so far (LOCAL):"); | |
PrettyPrint(organization, ctx); | |
// Creating the organization on the server | |
Organization createdOrganization = | |
(Organization) client.create().resource(organization).execute().getResource(); | |
System.out.println("Result from the creation transaction (REMOTE):"); | |
PrettyPrint(createdOrganization, ctx); | |
System.out.println("Organization ID: " + createdOrganization.getIdElement().getValue()); | |
/* | |
Part 2: Create a practitioner locally and show it | |
*/ | |
System.out.println("\n***\nPart 2: Create a new Practitioner\n***"); | |
Practitioner practitioner = new Practitioner(); | |
practitioner.setActive(true); | |
// Setting practitioner name | |
HumanName name = practitioner.addName(); | |
name.addGiven("Holger"); | |
name.setFamily("Alleswirdgut"); | |
name.addPrefix("Dr. med."); | |
name.setUse(HumanName.NameUse.OFFICIAL); | |
// Setting practitioner address | |
practitioner.addAddress() | |
.setCity("Neustadt") | |
.setCountry("Deutschland") | |
.setText("Schlossallee 27, 01234 Neustadt, Deutschland") | |
.setPostalCode("01234"); | |
// Adding phone number | |
practitioner.addTelecom() | |
.setSystem(ContactPoint.ContactPointSystem.PHONE) | |
.setValue("055512345589"); | |
// Print the local practitioner | |
System.out.println("The practitioner so far (LOCAL):"); | |
PrettyPrint(practitioner, ctx); | |
// Creating the practitioner on the remote server | |
Practitioner createdPractitioner = | |
(Practitioner) client.create().resource(practitioner).execute().getResource(); | |
System.out.println("Result from the creation transaction (REMOTE): "); | |
PrettyPrint(createdPractitioner, ctx); | |
System.out.println("Practitioner ID: " + createdPractitioner.getIdElement().getValue()); | |
/* | |
Part 3: Create a patient locally and show it | |
*/ | |
Patient patient = new Patient(); | |
patient.addName() | |
.addGiven("Max") | |
.setFamily("Mustermann") | |
.setUse(HumanName.NameUse.OFFICIAL); | |
patient.addAddress() | |
.setCity("Neustadt") | |
.setCountry("Deutschland") | |
.setText("Hintere Gasse 5, 01234 Neustadt, Deutschland") | |
.setPostalCode("01234"); | |
// Adding reference from patient to his general practitioner | |
Reference generalPractitionerRef = new Reference(createdPractitioner.getIdElement().getValue()); | |
generalPractitionerRef.setType("Practitioner"); | |
patient.addGeneralPractitioner(generalPractitionerRef); | |
// Showing the patient so far | |
System.out.println("The patient so far (LOCAL):"); | |
PrettyPrint(patient, ctx); | |
// Creating the patient on the server | |
Patient createdPatient = | |
(Patient) client.create().resource(patient).execute().getResource(); | |
System.out.println("Result from the creation transaction (REMOTE): "); | |
PrettyPrint(createdPatient, ctx); | |
System.out.println("Patient ID on remote server: " + createdPatient.getIdElement().getValue()); | |
/* | |
Part 4: Adding a Observation | |
*/ | |
Observation creatinineClearance = new Observation(); | |
// Setting codeable concept of creatinine clearence | |
CodeableConcept creatinineClearenceCC = new CodeableConcept(); | |
creatinineClearenceCC.addCoding() | |
.setCode("33914-3") | |
.setDisplay("Glomerular filtration rate/1.73 sq M.predicted by Creatinine-based formula (MDRD)") | |
.setSystem("http://loinc.org"); | |
creatinineClearance.setCode(creatinineClearenceCC); | |
// Setting reference to lab | |
Reference labRef = new Reference(createdOrganization.getIdElement().getValue()); | |
labRef.setType("Organization"); | |
creatinineClearance.addPerformer(labRef); | |
// Setting reference to patient | |
Reference patientRef = new Reference(createdPatient.getIdElement().getValue()); | |
patientRef.setType("Patient"); | |
creatinineClearance.setSubject(patientRef); | |
// Creating the observation on the remote server | |
Observation createdCreatinineClearence = | |
(Observation) client.create().resource(creatinineClearance).execute().getResource(); | |
System.out.println("Creatinine Clearence created:"); | |
PrettyPrint(createdCreatinineClearence, ctx); | |
/* | |
Part 5: Perform searches with references | |
*/ | |
// Searching for patients that were treated by a specific practitioner | |
Bundle patients = client.search() | |
.forResource(Patient.class) | |
.where(Patient.GENERAL_PRACTITIONER.hasId(createdPractitioner.getIdElement().getValue())) | |
.returnBundle(Bundle.class) | |
.execute(); | |
System.out.println("Number of patients: " + patients.getTotal()); | |
// Search for all observations for a specific patient | |
Bundle observations = client.search() | |
.forResource(Observation.class) | |
.where(Observation.SUBJECT.hasId(createdPatient.getIdElement().getValue())) | |
.returnBundle(Bundle.class) | |
.execute(); | |
System.out.println( | |
String.format( | |
"Number of observations for patient %s %s: %d", | |
createdPatient.getName().get(0).getGiven().get(0), | |
createdPatient.getName().get(0).getFamily(), | |
observations.getTotal() | |
) | |
); | |
/* | |
Part 6: Add different observations to a patient | |
*/ | |
// Adding glucose observation | |
Observation glucoseObservation = new Observation(); | |
glucoseObservation.setValue(new SampledData().setData("6.3")); | |
glucoseObservation.setStatus(Observation.ObservationStatus.FINAL); | |
CodeableConcept glucoseObservationCC = new CodeableConcept(); | |
glucoseObservationCC.addCoding() | |
.setSystem("https://loinc.org") | |
.setCode("15074-8") | |
.setDisplay("'Glucose [Moles/volume] in Blood', given as 'Glucose [Moles/volume] in Blood'"); | |
glucoseObservation.setCode(glucoseObservationCC); | |
glucoseObservation.addPerformer(labRef); | |
glucoseObservation.setSubject(patientRef); | |
boolean created = client.create() | |
.resource(glucoseObservation) | |
.execute() | |
.getCreated(); | |
if (!created) { | |
System.out.println("Failed to create glucose observation"); | |
} | |
// Adding hemoglobin observation | |
Observation hemoglobinObservation = new Observation(); | |
hemoglobinObservation.setValue(new SampledData().setData("7.2")); | |
hemoglobinObservation.setStatus(Observation.ObservationStatus.FINAL); | |
CodeableConcept hemoglobinCC = new CodeableConcept(); | |
hemoglobinCC.addCoding() | |
.setSystem("https://loinc.org") | |
.setCode("718-7") | |
.setDisplay("'Hemoglobin [Mass/volume] in Blood', given as 'Hemoglobin [Mass/volume] in Blood'"); | |
hemoglobinObservation.setCode(hemoglobinCC); | |
hemoglobinObservation.addPerformer(labRef); | |
hemoglobinObservation.setSubject(patientRef); | |
created = client.create() | |
.resource(hemoglobinObservation) | |
.execute() | |
.getCreated(); | |
if (!created) { | |
System.out.println("Failed to created hemoglobin observation"); | |
} | |
// Temperatur observation | |
Observation temperatureObservation = new Observation(); | |
temperatureObservation.setValue(new SampledData().setData("39")); | |
CodeableConcept tempCC = new CodeableConcept(); | |
tempCC.addCoding() | |
.setSystem("https://loinc.org") | |
.setCode("8310-5") | |
.setDisplay("'Body temperature', given as 'Body temperature'"); | |
temperatureObservation.setCode(tempCC); | |
CodeableConcept tempInterpretationCC = new CodeableConcept(); | |
tempInterpretationCC.addCoding() | |
.setSystem("http://terminology.hl7.org/CodeSystem/v3-ObservationInterpretation") | |
.setCode("H") | |
.setDisplay("High"); | |
temperatureObservation.addInterpretation(tempInterpretationCC); | |
created = client.create() | |
.resource(temperatureObservation) | |
.execute() | |
.getCreated(); | |
if (!created) { | |
System.out.println("Failed to created temperature observation"); | |
} | |
/* | |
Part 7: Create observations for several patients | |
*/ | |
Patient patient1 = new Patient(); | |
patient1.addName() | |
.setFamily("Heuvel") | |
.addGiven("Peter") | |
.setUse(HumanName.NameUse.OFFICIAL); | |
patient1.addAddress() | |
.setPostalCode("01234") | |
.setCity("Neustadt") | |
.setCountry("Deutschland") | |
.setText("Schlossstraße 12, 01234 Neustadt, Deutschland"); | |
if (!client.create().resource(patient1).execute().getCreated()) { | |
System.out.println("Could not create patient Peter Heuvel"); | |
} | |
Observation carbonDioxidInBloodObservation = new Observation(); | |
CodeableConcept co2CC = new CodeableConcept(); | |
co2CC.addCoding() | |
.setDisplay("'Carbon dioxide [Partial pressure] in Blood', given as 'Carbon dioxide in blood'") | |
.setCode("11557-6") | |
.setSystem("https://loinc.org"); | |
carbonDioxidInBloodObservation.setCode(co2CC); | |
carbonDioxidInBloodObservation.addPerformer(labRef); | |
carbonDioxidInBloodObservation.setSubject(new Reference(patient1.getIdElement().getValue()).setType("Patient")); | |
if (!client.create().resource(carbonDioxidInBloodObservation).execute().getCreated()) { | |
System.out.println("Could not create carbon dioxid in blood observation for Peter Heuvel!"); | |
} | |
// Adding patient for blood pressure cancel observation | |
Patient patient2 = new Patient(); | |
patient2.addName() | |
.setFamily("Chalmers") | |
.addGiven("Peter") | |
.setUse(HumanName.NameUse.OFFICIAL); | |
patient2.addAddress() | |
.setCountry("Deutschland") | |
.setPostalCode("01234") | |
.setCity("Neustadt") | |
.setText("Flussstraße 32, 01234 Neustadt, Deutschland"); | |
Observation bloodPressureCancel = new Observation(); | |
bloodPressureCancel.setSubject(new Reference(patient2.getIdElement().getValue()).setType("Patient")); | |
bloodPressureCancel.addPerformer(generalPractitionerRef); | |
bloodPressureCancel.setStatus(Observation.ObservationStatus.CANCELLED); | |
CodeableConcept bloodPressureCC = new CodeableConcept(); | |
bloodPressureCC.addCoding() | |
.setSystem("https://loinc.org") | |
.setCode("85354-9") | |
.setDisplay("'Blood pressure panel with all children optional', given as 'Blood pressure panel with all children optional'"); | |
bloodPressureCancel.setCode(bloodPressureCC); | |
var bloodPressurePatientResult = client.create().resource(bloodPressureCancel).execute(); | |
if (!bloodPressurePatientResult.getCreated()) { | |
System.out.println("Could not create blood pressure canceled"); | |
} | |
/* | |
Part 8: for a specific patient, search for all observations that were performed by a specific performer | |
*/ | |
Bundle response = client.search() | |
.forResource(Observation.class) | |
.where(Observation.SUBJECT.hasId(createdPatient.getIdElement().getValue())) | |
.and(Observation.PERFORMER.hasId(createdOrganization.getIdElement().getValue())) | |
.returnBundle(Bundle.class) | |
.execute(); | |
System.out.println( | |
"Number observations for a specific patient that were performed by a specific practitioner: " | |
+ response.getTotal() | |
); | |
} | |
public static void PrettyPrint(Resource resource, FhirContext context) { | |
IParser parser = context.newJsonParser(); | |
parser.setPrettyPrint(true); | |
System.out.println(parser.encodeResourceToString(resource)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>org.example</groupId> | |
<artifactId>lab3</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<properties> | |
<maven.compiler.source>1.11</maven.compiler.source> | |
<maven.compiler.target>1.11</maven.compiler.target> | |
</properties> | |
<dependencies> | |
<!-- Maven compiler plugin --> | |
<dependency> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>3.8.1</version> | |
</dependency> | |
<!-- https://mvnrepository.com/artifact/ca.uhn.hapi.fhir/hapi-fhir-base --> | |
<dependency> | |
<groupId>ca.uhn.hapi.fhir</groupId> | |
<artifactId>hapi-fhir-base</artifactId> | |
<version>5.3.0</version> | |
</dependency> | |
<!-- https://mvnrepository.com/artifact/ca.uhn.hapi.fhir/hapi-fhir-structures-r4 --> | |
<dependency> | |
<groupId>ca.uhn.hapi.fhir</groupId> | |
<artifactId>hapi-fhir-structures-r4</artifactId> | |
<version>5.3.0</version> | |
</dependency> | |
<!-- https://mvnrepository.com/artifact/ca.uhn.hapi.fhir/hapi-fhir-client --> | |
<dependency> | |
<groupId>ca.uhn.hapi.fhir</groupId> | |
<artifactId>hapi-fhir-client</artifactId> | |
<version>5.3.0</version> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment