public BaseStructureParser(String theVersion, String theBaseDir) { myVersion = theVersion; myBaseDir = theBaseDir; myIsRi = myVersion.equals("dstu3"); if (myVersion.equals("r4")) { myCtx = FhirContext.forR4(); } else if (myVersion.equals("dstu3")) { myCtx = FhirContext.forDstu3(); } else if (myVersion.equals("dstu2")) { myCtx = FhirContext.forDstu2(); } }
@Bean @Primary public FhirContext fhirContextDstu3() { FhirContext retVal = FhirContext.forDstu3(); // Don't strip versions in some places ParserOptions parserOptions = retVal.getParserOptions(); parserOptions.setDontStripVersionsFromReferencesAtPaths("AuditEvent.entity.reference"); return retVal; }
@Bean @Primary public FhirContext fhirContextDstu3() { FhirContext retVal = FhirContext.forDstu3(); // Don't strip versions in some places ParserOptions parserOptions = retVal.getParserOptions(); parserOptions.setDontStripVersionsFromReferencesAtPaths("AuditEvent.entity.reference"); return retVal; }
@Override protected void initialize() throws ServletException { // ...Configure resource providers, etc... // Create a context, set the error handler and instruct // the server to use it FhirContext ctx = FhirContext.forDstu3(); ctx.setParserErrorHandler(new StrictErrorHandler()); setFhirContext(ctx); }
@SuppressWarnings("unused") public void createOkHttp() { // START SNIPPET: okhttp FhirContext ctx = FhirContext.forDstu3(); // Use OkHttp ctx.setRestfulClientFactory(new OkHttpRestfulClientFactory(ctx)); // Create the client IGenericClient genericClient = ctx.newRestfulGenericClient("http://localhost:9999/fhir"); // END SNIPPET: okhttp }
@SuppressWarnings("unused") public void enableValidation() { // START SNIPPET: clientValidation FhirContext ctx = FhirContext.forDstu3(); ctx.setParserErrorHandler(new StrictErrorHandler()); // This client will have strict parser validation enabled IGenericClient client = ctx.newRestfulGenericClient("http://fhirtest.uhn.ca/baseDstu3"); // END SNIPPET: clientValidation }
public FhirContext newContext() { switch (this) { case DSTU2: return FhirContext.forDstu2(); case DSTU2_HL7ORG: return FhirContext.forDstu2Hl7Org(); case DSTU2_1: return FhirContext.forDstu2_1(); case DSTU3: return FhirContext.forDstu3(); case R4: return FhirContext.forR4(); } throw new IllegalStateException("Unknown version: " + this); // should not happen }
public void parserValidation() { // START SNIPPET: parserValidation FhirContext ctx = FhirContext.forDstu3(); // Create a parser and configure it to use the strict error handler IParser parser = ctx.newXmlParser(); parser.setParserErrorHandler(new StrictErrorHandler()); // This example resource is invalid, as Patient.active can not repeat String input = "<Patient><active value=\"true\"/><active value=\"false\"/></Patient>"; // The following will throw a DataFormatException because of the StrictErrorHandler parser.parseResource(Patient.class, input); // END SNIPPET: parserValidation }
@SuppressWarnings("unused") private static void validateFiles() throws Exception { // START SNIPPET: validateFiles FhirContext ctx = FhirContext.forDstu3(); // Create a validator and configure it FhirValidator validator = ctx.newValidator(); validator.setValidateAgainstStandardSchema(true); validator.setValidateAgainstStandardSchematron(true); // Get a list of files in a given directory String[] fileList = new File("/home/some/dir").list(new WildcardFileFilter("*.txt")); for (String nextFile : fileList) { // For each file, load the contents into a string String nextFileContents = IOUtils.toString(new FileReader(nextFile)); // Parse that string (this example assumes JSON encoding) IBaseResource resource = ctx.newJsonParser().parseResource(nextFileContents); // Apply the validation. This will throw an exception on the first // validation failure ValidationResult result = validator.validateWithResult(resource); if (result.isSuccessful() == false) { throw new Exception("We failed!"); } } // END SNIPPET: validateFiles }
public static void main(String[] args) { FhirContext ctx = FhirContext.forDstu3(); IGenericClient source = ctx.newRestfulGenericClient("http://localhost:8080/baseDstu3"); IGenericClient target = ctx.newRestfulGenericClient("https://try.smilecdr.com:8000");
public static void main(String[] args) { // START SNIPPET: usage // Create a context. Note that we declare the custom types we'll be using // on the context before actually using them FhirContext ctx = FhirContext.forDstu3(); ctx.registerCustomType(CustomResource.class); ctx.registerCustomType(CustomDatatype.class); // Now let's create an instance of our custom resource type // and populate it with some data CustomResource res = new CustomResource(); // Add some values, including our custom datatype DateType value0 = new DateType("2015-01-01"); res.getTelevision().add(value0); CustomDatatype value1 = new CustomDatatype(); value1.setDate(new DateTimeType(new Date())); value1.setKittens(new StringType("FOO")); res.getTelevision().add(value1); res.setDogs(new StringType("Some Dogs")); // Now let's serialize our instance String output = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(res); System.out.println(output); // END SNIPPET: usage }
@SuppressWarnings("unused") public void processMessage() { // START SNIPPET: processMessage FhirContext ctx = FhirContext.forDstu3(); // Create the client IGenericClient client = ctx.newRestfulGenericClient("http://localhost:9999/fhir"); Bundle bundle = new Bundle(); // ..populate the bundle.. Bundle response = client .operation() .processMessage() // New operation for sending messages .setMessageBundle(bundle) .asynchronous(Bundle.class) .execute(); // END SNIPPET: processMessage }
@SuppressWarnings("unused") public static void creatingContextHl7org() { // START SNIPPET: creatingContextHl7org // Create a context for DSTU3 FhirContext ctx = FhirContext.forDstu3(); // Working with RI structures is similar to how it works with the HAPI structures org.hl7.fhir.dstu3.model.Patient patient = new org.hl7.fhir.dstu3.model.Patient(); patient.addName().addGiven("John").setFamily("Smith"); patient.getBirthDateElement().setValueAsString("1998-02-22"); // Parsing and encoding works the same way too String encoded = ctx.newJsonParser().encodeResourceToString(patient); // END SNIPPET: creatingContextHl7org }
public void customTypeDeclared() { //START SNIPPET: customTypeClientDeclared FhirContext ctx = FhirContext.forDstu3(); // Instruct the context that if it receives a resource which // claims to conform to the given profile (by URL), it should // use the MyPatient type to parse this resource ctx.setDefaultTypeForProfile("http://example.com/StructureDefinition/mypatient", MyPatient.class); // You can declare as many default types as you like ctx.setDefaultTypeForProfile("http://foo.com/anotherProfile", CustomObservation.class); // Create a client IGenericClient client = ctx.newRestfulGenericClient("http://fhirtest.uhn.ca/baseDstu3"); // You can also read the resource back like normal Patient patient = client.read().resource(Patient.class).withId("123").execute(); if (patient instanceof MyPatient) { // If the server supplied a resource which declared to conform // to the given profile, MyPatient will have been returned so // process it differently.. } //END SNIPPET: customTypeClientDeclared }
public void validationIntro() { // START SNIPPET: validationIntro FhirContext ctx = FhirContext.forDstu3(); // Ask the context for a validator FhirValidator validator = ctx.newValidator(); // Create some modules and register them IValidatorModule module1 = new SchemaBaseValidator(ctx); validator.registerValidatorModule(module1); IValidatorModule module2 = new SchematronBaseValidator(ctx); validator.registerValidatorModule(module2); // Pass a resource in to be validated. The resource can // be an IBaseResource instance, or can be a raw String // containing a serialized resource as text. Patient resource = new Patient(); ValidationResult result = validator.validateWithResult(resource); String resourceText = "<Patient.....>"; ValidationResult result2 = validator.validateWithResult(resourceText); // The result object now contains the validation results for (SingleValidationMessage next : result.getMessages()) { System.out.println(next.getLocationString() + " " + next.getMessage()); } // END SNIPPET: validationIntro }
@SuppressWarnings("unused") public void cacheControl() { FhirContext ctx = FhirContext.forDstu3(); // Create the client IGenericClient client = ctx.newRestfulGenericClient("http://localhost:9999/fhir"); Bundle bundle = new Bundle(); // ..populate the bundle.. // START SNIPPET: cacheControl Bundle response = client .search() .forResource(Patient.class) .returnBundle(Bundle.class) .cacheControl(new CacheControlDirective().setNoCache(true)) // <-- add a directive .execute(); // END SNIPPET: cacheControl }
public void validateProfileDstu3() { FhirContext ctx = FhirContext.forDstu3(); FhirValidator validator = ctx.newValidator();
private static void instanceValidator() throws Exception { FhirContext ctx = FhirContext.forDstu3();
public void customType() { IGenericClient client = FhirContext.forDstu3().newRestfulGenericClient("http://foo");
FhirContext ctx = FhirContext.forDstu3(); String output = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient); System.out.println(output);