hapi-fhir/examples/src/main/java/example/FhirDataModel.java
James be5c5ebecd Merge in HAPI 3.0.0 working branch!
Squashed commit of the following:

commit 12f89a423a1691fdbc360706fd94a03bd9144d17
Author: James <jamesagnew@gmail.com>
Date:   Sun Aug 13 14:38:51 2017 -0400

    Minimize validation resources

commit f6868cce5c73b34d5ecd53bf1a220fdccd6b4e09
Merge: 3b80779fd3 1e158311d8
Author: James <jamesagnew@gmail.com>
Date:   Sun Aug 13 14:05:34 2017 -0400

    Forward port fix for #710

    Merge branch 'master' into hapi3_refactor

commit 3b80779fd3905cbf5322e6973334eb0ecb3d8426
Merge: 1f534985e8 356d9acaf7
Author: James <jamesagnew@gmail.com>
Date:   Sun Aug 13 12:31:09 2017 -0400

    Forward port #705, #708, and #710

    Merge branch 'master' into hapi3_refactor

commit 1f534985e8106347d9cc9da2dd8145fdd869547f
Merge: 7c39a47852 dedd3d635b
Author: James <jamesagnew@gmail.com>
Date:   Sun Aug 13 10:52:59 2017 -0400

    Forward port #695

    Merge branch 'master' into hapi3_refactor

commit 7c39a47852142bee398101120678217ab0917c7e
Merge: e0ffb84d21 6efafe62f1
Author: James <jamesagnew@gmail.com>
Date:   Sun Aug 13 09:53:17 2017 -0400

    Forward port #688

    Merge branch 'master' into hapi3_refactor

commit e0ffb84d2129c209ccffc2611038592f37dddfd2
Merge: 52388c11c1 d19b00ff09
Author: James <jamesagnew@gmail.com>
Date:   Sat Aug 12 14:59:46 2017 -0400

    Merge branch 'master' into hapi3_refactor

commit 52388c11c17cf038591244548f2a592f6b655cee
Author: James <jamesagnew@gmail.com>
Date:   Sat Aug 12 06:21:46 2017 -0400

    Cleanup

commit 209752cd638266b39574ae11c5989b624d5e85d3
Author: James <jamesagnew@gmail.com>
Date:   Thu Aug 10 11:18:19 2017 -0400

    Fix tests

commit 4543408dc8d63b5b9682a275e261d3168cdc2196
Author: James <jamesagnew@gmail.com>
Date:   Sat Aug 5 06:55:50 2017 -0400

    Fix a potential deadlock

commit ee360f537692672df473f94dae75c176b2287415
Author: James <jamesagnew@gmail.com>
Date:   Sat Aug 5 06:22:06 2017 -0400

    Add R4 code to CLI

commit 1a95ba3b6529afdaf81232b82cc303718c0187d6
Author: James Agnew <jamesagnew@gmail.com>
Date:   Thu Aug 3 06:14:01 2017 -0400

    More cleanup

commit f0d88026817296edaa65132accd67d0c8ad8088c
Author: James <jamesagnew@gmail.com>
Date:   Wed Aug 2 11:27:43 2017 -0400

    Tests are working!

commit a4cbda357e8e7d024f24e3ee9a660b5366983ca1
Author: James Agnew <jamesagnew@gmail.com>
Date:   Wed Aug 2 10:42:04 2017 -0400

    Connection handling cleanup for new tests

commit 0e2cecfbd0d7a546444a66e8411600040fd9a17b
Author: James Agnew <jamesagnew@gmail.com>
Date:   Wed Aug 2 10:16:28 2017 -0400

    Clean up R4 JPA tests

commit 40317a650d72c86e2529d5179cffe63399301de5
Author: James <jamesagnew@gmail.com>
Date:   Wed Aug 2 09:12:38 2017 -0400

    Work on R4 for JPA server

commit e7f8f8c30d72ed30d739979964db38d41c5baec1
Author: James <jamesagnew@gmail.com>
Date:   Tue Aug 1 20:43:47 2017 -0400

    More work on porting tests

commit 43c9003258696ab33e7bb335e882ab1c66fb61aa
Author: James <jamesagnew@gmail.com>
Date:   Tue Aug 1 07:09:29 2017 -0400

    Work on porting DSTU1 tests

commit 602857f1e26a69a2284e176b5e44a860d9b828ff
Author: James Agnew <jamesagnew@gmail.com>
Date:   Mon Jul 31 22:34:08 2017 -0400

    More work on bring unit tests up to date

commit e326a7b0cdb8368009119bba41886838a973e03e
Author: James Agnew <jamesagnew@gmail.com>
Date:   Mon Jul 31 17:36:38 2017 -0400

    Credit for #686 and forward port the fix to R4 validator

commit 96543c3992adcb406df3c8899dab79cf4bd5b4b4
Merge: 3fb75aa61a 9901b802c4
Author: James Agnew <jamesagnew@gmail.com>
Date:   Mon Jul 31 17:12:33 2017 -0400

    Merge branch 'master' into hapi3_refactor

commit 3fb75aa61ad29d9f3876a1c30c912627486147af
Author: James <jamesagnew@gmail.com>
Date:   Mon Jul 31 15:21:30 2017 -0400

    More work on cleanup

commit b02fbb6804127e77c61c4792eefd9fc0d23d63d0
Author: James Agnew <jamesagnew@gmail.com>
Date:   Sun Jul 30 22:11:07 2017 -0400

    Work on porting STU1 tests

commit 1ae37b0db3929ea4f37955adf8f026e33ccf43b2
Author: James Agnew <jamesagnew@gmail.com>
Date:   Sun Jul 30 20:56:10 2017 -0400

    Try to get coverage report working

commit 72b88849b30a94a9ff08d3a1ab50a05b2acbdfdd
Author: James Agnew <jamesagnew@gmail.com>
Date:   Sun Jul 30 20:27:02 2017 -0400

    Fix android tests

commit e5f6c35aeab8bbc056870a038e1862181049a020
Author: James <jamesagnew@gmail.com>
Date:   Sun Jul 30 19:31:18 2017 -0400

    More work on getting legacy code cleaned up

commit 0b513b0845a082b5c20b0033dfab4e29f5e4a934
Author: James <jamesagnew@gmail.com>
Date:   Sun Jul 30 18:41:13 2017 -0400

    Continue work on removing deprecated API

commit defea69aa38a2e0f4137ed9c8527956872ff70bf
Author: James Agnew <jamesagnew@gmail.com>
Date:   Sun Jul 30 17:10:01 2017 -0400

    More cleanup of legacy code

commit 9ae7295705cb58f5edcf7ee1259f023cbbe2fe51
Author: James <jamesagnew@gmail.com>
Date:   Sun Jul 30 07:11:45 2017 -0400

    More cleanup of legacy code

commit ebd3eeb5ee793cf5805cc726db353f3def35ab00
Author: James Agnew <jamesagnew@gmail.com>
Date:   Sun Jul 30 06:43:25 2017 -0400

    More work on removing legacy code

commit 92224c2532faf70171473d64429c3ceaf3e3406c
Author: James <jamesagnew@gmail.com>
Date:   Sat Jul 29 18:44:06 2017 -0400

    Remove DSTU1 Bundle

commit c52cacf71bae3f5c02ffae03881c21de1f3aba22
Author: James <jamesagnew@gmail.com>
Date:   Sat Jul 29 14:27:42 2017 -0400

    Now compiling

commit b405e51773baf4ab3a3c387458cdc59541394cd1
Merge: c3ddf04e25 cb2cea54d7
Author: James Agnew <jamesagnew@gmail.com>
Date:   Fri Jul 28 06:21:02 2017 -0400

    Merge branch 'master' into hapi3_refactor

commit c3ddf04e2598b8d6214975ab0ae795f850396811
Author: James <jamesagnew@gmail.com>
Date:   Thu Jul 27 11:06:06 2017 -0400

    Sync R4 releases in

commit b13333c3c03ddb9a5061c3b22bf011f4592104f8
Author: James <jamesagnew@gmail.com>
Date:   Fri Jul 14 05:52:33 2017 -0400

    JPA server is now able to handle placeholder IDs (e.g. urn:uuid:00....000) being used in Bundle.entry.request.url as a part of the conditional URL within transactions.

commit 2e60ff7521b42890a32b97c2dda88ab233f5c91e
Author: James Agnew <jamesagnew@gmail.com>
Date:   Thu Jul 13 20:02:46 2017 -0400

    Fix imports

commit a92ace2e0dac6e24f6dada53bf97d03861fd21a1
Merge: 3196db96d1 1a6b3ea867
Author: James Agnew <jamesagnew@gmail.com>
Date:   Thu Jul 13 12:02:27 2017 -0400

    Merge branch 'master' into hapi3_refactor

commit 3196db96d1c485310e746833df59c0e010b5b161
Author: James Agnew <jamesagnew@gmail.com>
Date:   Thu Jul 13 11:48:10 2017 -0400

    Don't add false paging link to request

commit bd4e1d338855a664045caa271d41616aed973a4d
Author: James <jamesagnew@gmail.com>
Date:   Sun Jul 9 21:32:16 2017 -0400

    Finally building correctly!

commit 6464ce9304703cb3c5ecc58491282ddacc900853
Author: James <jamesagnew@gmail.com>
Date:   Sun Jul 9 16:38:28 2017 -0400

    Work on refactor

commit 0059f2e48e9d7c812c1d42a57c5b6059814f5155
Author: James <jamesagnew@gmail.com>
Date:   Sat Jul 8 07:16:20 2017 -0400

    Keep working on refactor

commit 6c2e87e8cc19863a811c11623d1c878c8b48f031
Author: James Agnew <jamesagnew@gmail.com>
Date:   Thu Jul 6 22:35:13 2017 -0400

    Lots of work on refactor

commit 11cab975047a0c28190e6533074fd3f11ddde240
Merge: 34ec6b8807 6c47bd4c51
Author: James Agnew <jamesagnew@gmail.com>
Date:   Thu Jul 6 21:43:57 2017 -0400

    Merge branch 'master' into hapi3_refactor

commit 34ec6b8807946aa6c97f0b5581dca9bd5f7b7a3e
Merge: f8e647511b c520e60ac1
Author: James Agnew <jamesagnew@gmail.com>
Date:   Thu Jul 6 21:43:49 2017 -0400

    Merge branch 'hapi3_refactor' of github.com:jamesagnew/hapi-fhir into hapi3_refactor

commit f8e647511b4a82e7fde71a8850b8800e3d698b1f
Author: James Agnew <jamesagnew@gmail.com>
Date:   Thu Jul 6 18:46:55 2017 -0400

    Work on hapi3 changes

commit c520e60ac1198f340cfb9090a7a33f91aa2d1e61
Author: James <jamesagnew@gmail.com>
Date:   Wed Jul 5 08:08:40 2017 -0400

    Keep working on refactor

commit f1d2ee90926f7db900a5b3b4ab9a8948d18e49f2
Author: James Agnew <jamesagnew@gmail.com>
Date:   Mon Jul 3 22:10:59 2017 -0400

    Continue refactor for HAPI 3

commit 9281ccafc32354222932c5a9f5ee8b0f206ebc05
Merge: ea1264cd8e 294d080bd3
Author: James Agnew <jamesagnew@gmail.com>
Date:   Mon Jul 3 20:34:16 2017 -0400

    Merge branch 'master' into hapi3_refactor

commit ea1264cd8e9b8c5297d218e91cf14bd48d0a92c7
Author: James <jamesagnew@gmail.com>
Date:   Wed Jun 28 10:26:01 2017 -0400

    Continue work on refactor

commit fbe2f98a0238f3d4e065db4550dd2ef1b460c0d8
Merge: b2bef47100 0a4dcc32ec
Author: James <jamesagnew@gmail.com>
Date:   Wed Jun 28 06:21:22 2017 -0400

    Merge branch 'master' into hapi3_refactor

commit b2bef47100370e1248834523d73a630950032d7a
Author: James <jamesagnew@gmail.com>
Date:   Tue Jun 27 21:13:23 2017 -0400

    Work on refactor

commit 8f76e4e46385daa9ce3442e2d9342453ce373ba3
Author: James Agnew <jamesagnew@gmail.com>
Date:   Sun Jun 25 21:55:35 2017 -0400

    Lots of work on refactoring
2017-08-13 14:39:47 -04:00

225 lines
8.1 KiB
Java

package example;
import java.util.*;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
import ca.uhn.fhir.model.dstu2.composite.*;
import ca.uhn.fhir.model.dstu2.resource.Observation;
import ca.uhn.fhir.model.dstu2.resource.Patient;
import ca.uhn.fhir.model.dstu2.valueset.*;
import ca.uhn.fhir.model.primitive.InstantDt;
import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.rest.client.api.IGenericClient;
public class FhirDataModel {
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.
Date date = obs.getIssuedElement().getValue(); // A date object
String dateString = obs.getIssuedElement().getValueAsString(); // "2014-03-08T12:59:58.068-05:00"
// END SNIPPET: datatypes
System.out.println(date);
System.out.println(dateString);
}
@SuppressWarnings("unused")
public void nonNull() {
// START SNIPPET: nonNull
Observation observation = new Observation();
// None of these calls will not return null, but instead create their
// respective
// child elements.
List<IdentifierDt> identifierList = observation.getIdentifier();
CodeableConceptDt code = observation.getCode();
StringDt textElement = observation.getCode().getTextElement();
// DateTimeDt is a FHIR primitive however, so the following will return
// null
// unless a value has been placed there.
Date active = observation.addIdentifier().getPeriod().getStartElement().getValue();
// END SNIPPET: nonNull
}
@SuppressWarnings("unused")
public static void codes() {
// START SNIPPET: codes
Patient patient = new Patient();
// 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);
// END SNIPPET: codes
}
@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");
patient.getMaritalStatus().getCodingFirstRep().setUserSelected(true);
// 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
}
public static void main(String[] args) {
tmp();
datatypes();
// START SNIPPET: observation
// Create an Observation instance
Observation observation = new Observation();
// Give the observation a status
observation.setStatus(ObservationStatusEnum.FINAL);
// 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);
// Set the reference range
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);
// END SNIPPET: observation
}
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);
}
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
}
}