2014-03-10 12:43:49 -04:00
|
|
|
package example;
|
|
|
|
|
2017-08-13 14:39:47 -04:00
|
|
|
import java.util.*;
|
2014-03-10 12:43:49 -04:00
|
|
|
|
2015-11-20 10:30:10 +01:00
|
|
|
import ca.uhn.fhir.context.FhirContext;
|
2014-03-10 12:43:49 -04:00
|
|
|
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
|
2017-08-13 14:39:47 -04:00
|
|
|
import ca.uhn.fhir.model.dstu2.composite.*;
|
2015-02-09 14:26:08 -05:00
|
|
|
import ca.uhn.fhir.model.dstu2.resource.Observation;
|
|
|
|
import ca.uhn.fhir.model.dstu2.resource.Patient;
|
2017-08-13 14:39:47 -04:00
|
|
|
import ca.uhn.fhir.model.dstu2.valueset.*;
|
2014-03-10 12:43:49 -04:00
|
|
|
import ca.uhn.fhir.model.primitive.InstantDt;
|
2014-03-24 08:32:28 -04:00
|
|
|
import ca.uhn.fhir.model.primitive.StringDt;
|
2017-08-13 14:39:47 -04:00
|
|
|
import ca.uhn.fhir.rest.client.api.IGenericClient;
|
2014-03-10 12:43:49 -04:00
|
|
|
|
|
|
|
public class FhirDataModel {
|
|
|
|
|
2014-09-10 14:12:11 -04:00
|
|
|
public static void datatypes() {
|
|
|
|
// START SNIPPET: datatypes
|
|
|
|
Observation obs = new Observation();
|
|
|
|
|
|
|
|
// These are all equivalent
|
|
|
|
obs.setIssued(new InstantDt(new Date()));
|
|
|
|
obs.setIssued(new Date(), TemporalPrecisionEnum.MILLI);
|
|
|
|
obs.setIssuedWithMillisPrecision(new Date());
|
|
|
|
|
|
|
|
// The InstantDt also lets you work with the instant as a Java Date
|
|
|
|
// object or as a FHIR String.
|
2015-02-09 14:26:08 -05:00
|
|
|
Date date = obs.getIssuedElement().getValue(); // A date object
|
|
|
|
String dateString = obs.getIssuedElement().getValueAsString(); // "2014-03-08T12:59:58.068-05:00"
|
2014-09-10 14:12:11 -04:00
|
|
|
// END SNIPPET: datatypes
|
|
|
|
|
|
|
|
System.out.println(date);
|
|
|
|
System.out.println(dateString);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
public void nonNull() {
|
|
|
|
// START SNIPPET: nonNull
|
|
|
|
Observation observation = new Observation();
|
|
|
|
|
2015-03-02 22:39:53 -05:00
|
|
|
// None of these calls will not return null, but instead create their
|
2014-09-10 14:12:11 -04:00
|
|
|
// respective
|
|
|
|
// child elements.
|
2015-03-02 22:39:53 -05:00
|
|
|
List<IdentifierDt> identifierList = observation.getIdentifier();
|
|
|
|
CodeableConceptDt code = observation.getCode();
|
|
|
|
StringDt textElement = observation.getCode().getTextElement();
|
2014-09-10 14:12:11 -04:00
|
|
|
|
|
|
|
// DateTimeDt is a FHIR primitive however, so the following will return
|
|
|
|
// null
|
|
|
|
// unless a value has been placed there.
|
2015-03-02 22:39:53 -05:00
|
|
|
Date active = observation.addIdentifier().getPeriod().getStartElement().getValue();
|
2014-09-10 14:12:11 -04:00
|
|
|
// END SNIPPET: nonNull
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-09 14:26:08 -05:00
|
|
|
@SuppressWarnings("unused")
|
2014-09-10 14:12:11 -04:00
|
|
|
public static void codes() {
|
|
|
|
// START SNIPPET: codes
|
|
|
|
Patient patient = new Patient();
|
|
|
|
|
2015-02-09 14:26:08 -05:00
|
|
|
// You can set this code using a String if you want. Note that
|
|
|
|
// for "closed" valuesets (such as the one used for Patient.gender)
|
|
|
|
// you must use one of the strings defined by the FHIR specification.
|
|
|
|
// You must not define your own.
|
|
|
|
patient.getGenderElement().setValue("male");
|
|
|
|
|
|
|
|
// HAPI also provides Java enumerated types which make it easier to
|
|
|
|
// deal with coded values. This code achieves the exact same result
|
|
|
|
// as the code above.
|
|
|
|
patient.setGender(AdministrativeGenderEnum.MALE);
|
|
|
|
|
|
|
|
// You can also retrieve coded values the same way
|
|
|
|
String genderString = patient.getGenderElement().getValueAsString();
|
|
|
|
AdministrativeGenderEnum genderEnum = patient.getGenderElement().getValueAsEnum();
|
|
|
|
|
|
|
|
// The following is a shortcut to create
|
|
|
|
patient.setMaritalStatus(MaritalStatusCodesEnum.M);
|
2014-09-10 14:12:11 -04:00
|
|
|
// END SNIPPET: codes
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-09 14:26:08 -05:00
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
public static void codeableConcepts() {
|
|
|
|
// START SNIPPET: codeableConcepts
|
|
|
|
Patient patient = new Patient();
|
|
|
|
|
|
|
|
// Coded types can naturally be set using plain strings
|
|
|
|
CodingDt statusCoding = patient.getMaritalStatus().addCoding();
|
|
|
|
statusCoding.setSystem("http://hl7.org/fhir/v3/MaritalStatus");
|
|
|
|
statusCoding.setCode("M");
|
|
|
|
statusCoding.setDisplay("Married");
|
|
|
|
|
|
|
|
// You could add a second coding to the field if needed too. This
|
|
|
|
// can be useful if you want to convey the concept using different
|
|
|
|
// codesystems.
|
|
|
|
CodingDt secondStatus = patient.getMaritalStatus().addCoding();
|
|
|
|
secondStatus.setCode("H");
|
|
|
|
secondStatus.setSystem("http://example.com#maritalStatus");
|
|
|
|
secondStatus.setDisplay("Happily Married");
|
|
|
|
|
|
|
|
// CodeableConcept also has a text field meant to convey
|
|
|
|
// a user readable version of the concepts it conveys.
|
|
|
|
patient.getMaritalStatus().setText("Happily Married");
|
|
|
|
|
|
|
|
// There are also accessors for retrieving values
|
|
|
|
String firstCode = patient.getMaritalStatus().getCoding().get(0).getCode();
|
|
|
|
String secondCode = patient.getMaritalStatus().getCoding().get(1).getCode();
|
|
|
|
// END SNIPPET: codeableConcepts
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
public static void codeableConceptEnums() {
|
|
|
|
// START SNIPPET: codeableConceptEnums
|
|
|
|
Patient patient = new Patient();
|
|
|
|
|
|
|
|
// Set the CodeableConcept's first coding to use the code
|
|
|
|
// and codesystem associated with the M value.
|
|
|
|
patient.setMaritalStatus(MaritalStatusCodesEnum.M);
|
|
|
|
|
|
|
|
// If you need to set other fields (such as the display name) after
|
|
|
|
// using the Enum type, you may still do so.
|
|
|
|
patient.getMaritalStatus().getCodingFirstRep().setDisplay("Married");
|
|
|
|
patient.getMaritalStatus().getCodingFirstRep().setVersion("1.0");
|
2015-07-25 11:56:47 -04:00
|
|
|
patient.getMaritalStatus().getCodingFirstRep().setUserSelected(true);
|
2015-02-09 14:26:08 -05:00
|
|
|
|
|
|
|
// You can use accessors to retrieve values from CodeableConcept fields
|
|
|
|
|
|
|
|
// Returns "M"
|
|
|
|
String code = patient.getMaritalStatus().getCodingFirstRep().getCode();
|
|
|
|
|
|
|
|
// Returns "http://hl7.org/fhir/v3/MaritalStatus". This value was also
|
|
|
|
// populated via the enum above.
|
|
|
|
String codeSystem = patient.getMaritalStatus().getCodingFirstRep().getCode();
|
|
|
|
|
|
|
|
// In many cases, Enum types can be used to retrieve values as well. Note that
|
|
|
|
// the setter takes a single type, but the getter returns a Set, because the
|
|
|
|
// field can technicaly contain more than one code and codesystem. BE CAREFUL
|
|
|
|
// when using this method however, as no Enum will be returned in the case
|
|
|
|
// that the field contains only a code other than the ones defined by the Enum.
|
|
|
|
Set<MaritalStatusCodesEnum> status = patient.getMaritalStatus().getValueAsEnum();
|
|
|
|
// END SNIPPET: codeableConceptEnums
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-10 14:12:11 -04:00
|
|
|
public static void main(String[] args) {
|
2015-11-20 10:30:10 +01:00
|
|
|
tmp();
|
|
|
|
|
|
|
|
|
2014-09-10 14:12:11 -04:00
|
|
|
datatypes();
|
|
|
|
|
|
|
|
// START SNIPPET: observation
|
2015-11-20 10:30:10 +01:00
|
|
|
// Create an Observation instance
|
2014-09-10 14:12:11 -04:00
|
|
|
Observation observation = new Observation();
|
|
|
|
|
2015-11-20 10:30:10 +01:00
|
|
|
// Give the observation a status
|
|
|
|
observation.setStatus(ObservationStatusEnum.FINAL);
|
2014-09-10 14:12:11 -04:00
|
|
|
|
2015-11-20 10:30:10 +01:00
|
|
|
// Give the observation a code (what kind of observation is this)
|
|
|
|
CodingDt coding = observation.getCode().addCoding();
|
|
|
|
coding.setCode("29463-7").setSystem("http://loinc.org").setDisplay("Body Weight");
|
|
|
|
|
|
|
|
// Create a quantity datatype
|
|
|
|
QuantityDt value = new QuantityDt();
|
|
|
|
value.setValue(83.9).setSystem("http://unitsofmeasure.org").setCode("kg");
|
|
|
|
observation.setValue(value);
|
2014-09-10 14:12:11 -04:00
|
|
|
|
|
|
|
// Set the reference range
|
2015-11-20 10:30:10 +01:00
|
|
|
SimpleQuantityDt low = new SimpleQuantityDt();
|
|
|
|
low.setValue(45).setSystem("http://unitsofmeasure.org").setCode("kg");
|
|
|
|
observation.getReferenceRangeFirstRep().setLow(low);
|
|
|
|
SimpleQuantityDt high = new SimpleQuantityDt();
|
|
|
|
low.setValue(90).setSystem("http://unitsofmeasure.org").setCode("kg");
|
|
|
|
observation.getReferenceRangeFirstRep().setHigh(high);
|
2014-09-10 14:12:11 -04:00
|
|
|
|
|
|
|
// END SNIPPET: observation
|
|
|
|
|
|
|
|
|
2015-11-20 10:30:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void tmp() {
|
|
|
|
// Create a FHIR Context
|
|
|
|
FhirContext ctx = FhirContext.forDstu2();
|
|
|
|
|
|
|
|
// Create a client
|
|
|
|
IGenericClient client = ctx.newRestfulGenericClient("http://fhirtest.uhn.ca/baseDstu2");
|
|
|
|
|
|
|
|
// Read a patient with the given ID
|
|
|
|
Patient patient = client
|
|
|
|
.read()
|
|
|
|
.resource(Patient.class)
|
|
|
|
.withId("952975")
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
// Print the patient's name
|
|
|
|
String string = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient);
|
|
|
|
System.out.println(string);
|
|
|
|
|
2014-09-10 14:12:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void namesHard() {
|
|
|
|
// START SNIPPET: namesHard
|
|
|
|
Patient patient = new Patient();
|
|
|
|
HumanNameDt name = patient.addName();
|
|
|
|
StringDt family = name.addFamily();
|
|
|
|
family.setValue("Smith");
|
|
|
|
StringDt firstName = name.addGiven();
|
|
|
|
firstName.setValue("Rob");
|
|
|
|
StringDt secondName = name.addGiven();
|
|
|
|
secondName.setValue("Bruce");
|
|
|
|
// END SNIPPET: namesHard
|
|
|
|
}
|
|
|
|
|
|
|
|
public void namesEasy() {
|
|
|
|
// START SNIPPET: namesEasy
|
|
|
|
Patient patient = new Patient();
|
|
|
|
patient.addName().addFamily("Smith").addGiven("Rob").addGiven("Bruce");
|
|
|
|
// END SNIPPET: namesEasy
|
|
|
|
}
|
2014-03-24 08:32:28 -04:00
|
|
|
|
2014-03-10 12:43:49 -04:00
|
|
|
}
|