@Override public IParser newParser(FhirContext theContext) { return theContext.newJsonParser(); } },
public IParser newParser(FhirContext theContext) { return theContext.newJsonParser(); }
public IBaseResource getNewPayload(FhirContext theCtx) { if (myPayloadDecoded == null && isNotBlank(myPayload)) { myPayloadDecoded = theCtx.newJsonParser().parseResource(myPayload); } return myPayloadDecoded; }
private org.hl7.fhir.instance.model.Resource toDstu2(IBaseResource theResponseResource) { if (theResponseResource instanceof IResource) { return (org.hl7.fhir.instance.model.Resource) myCtxDstu2Hl7Org.newJsonParser().parseResource(myCtxDstu2.newJsonParser().encodeResourceToString(theResponseResource)); } return (org.hl7.fhir.instance.model.Resource) theResponseResource; }
public IBaseResource getPayload(FhirContext theCtx) { IBaseResource retVal = myPayload; if (retVal == null && isNotBlank(myPayloadString)) { retVal = theCtx.newJsonParser().parseResource(myPayloadString); myPayload = retVal; } return retVal; }
@SuppressWarnings("unused") public void getResourceTags() { // START SNIPPET: context // Create a DSTU2 context, which will use DSTU2 semantics FhirContext ctx = FhirContext.forDstu2(); // This parser supports DSTU2 IParser parser = ctx.newJsonParser(); // This client supports DSTU2 IGenericClient client = ctx.newRestfulGenericClient("http://fhirtest.uhn.ca/baseDstu2"); // END SNIPPET: context }
/** * This method is called for nested bundles (e.g. if we received a transaction with an entry that * was a GET search, this method is called on the bundle for the search result, that will be placed in the * outer bundle). This method applies the _summary and _content parameters to the output of * that bundle. * <p> * TODO: This isn't the most efficient way of doing this.. hopefully we can come up with something better in the future. */ private IBaseResource filterNestedBundle(RequestDetails theRequestDetails, IBaseResource theResource) { IParser p = myContext.newJsonParser(); RestfulServerUtils.configureResponseParser(theRequestDetails, p); return p.parseResource(theResource.getClass(), p.encodeResourceToString(theResource)); }
@Override public <T extends IBaseResource> T load(Class<T> theType, IIdType theId) throws ResourceNotFoundException { /* * The QuestionnaireResponse validator uses RI structures, so for now we need to convert between that and HAPI * structures. This is a bit hackish, but hopefully it will go away at some point. */ if ("ValueSet".equals(theType.getSimpleName())) { IFhirResourceDao<ValueSet> dao = getDao(ValueSet.class); ValueSet in = dao.read(theId, null); String encoded = getContext().newJsonParser().encodeResourceToString(in); // TODO: this is temporary until structures-dstu2 catches up to structures-hl7org.dstu2 encoded = encoded.replace("\"define\"", "\"codeSystem\""); return myRefImplCtx.newJsonParser().parseResource(theType, encoded); } else if ("Questionnaire".equals(theType.getSimpleName())) { IFhirResourceDao<Questionnaire> dao = getDao(Questionnaire.class); Questionnaire vs = dao.read(theId, null); return myRefImplCtx.newJsonParser().parseResource(theType, getContext().newJsonParser().encodeResourceToString(vs)); } else { // Should not happen, validator will only ask for these two throw new IllegalStateException("Unexpected request to load resource of type " + theType); } }
/** * This method is called for nested bundles (e.g. if we received a transaction with an entry that * was a GET search, this method is called on the bundle for the search result, that will be placed in the * outer bundle). This method applies the _summary and _content parameters to the output of * that bundle. * <p> * TODO: This isn't the most efficient way of doing this.. hopefully we can come up with something better in the future. */ private IBaseResource filterNestedBundle(RequestDetails theRequestDetails, IBaseResource theResource) { IParser p = getContext().newJsonParser(); RestfulServerUtils.configureResponseParser(theRequestDetails, p); return p.parseResource(theResource.getClass(), p.encodeResourceToString(theResource)); }
public void setPayload(FhirContext theCtx, IBaseResource thePayload) { myPayload = thePayload; myPayloadString = theCtx.newJsonParser().encodeResourceToString(thePayload); myPayloadId = thePayload.getIdElement().toUnqualified().getValue(); }
@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 }
private ValueSet loadValueSetForExpansion(IIdType theId) { if (theId.getValue().startsWith("http://hl7.org/fhir/")) { org.hl7.fhir.instance.model.ValueSet valueSet = myValidationSupport.fetchResource(myRiCtx, org.hl7.fhir.instance.model.ValueSet.class, theId.getValue()); if (valueSet != null) { return getContext().newJsonParser().parseResource(ValueSet.class, myRiCtx.newJsonParser().encodeResourceToString(valueSet)); } } BaseHasResource sourceEntity = readEntity(theId); if (sourceEntity == null) { throw new ResourceNotFoundException(theId); } ValueSet source = (ValueSet) toResource(sourceEntity, false); return source; }
@Override protected List<ValidationMessage> validate(IValidationContext<?> theCtx) { Object resource = theCtx.getResource(); if (!(theCtx.getResource() instanceof IBaseResource)) { ourLog.debug("Not validating object of type {}", theCtx.getResource().getClass()); return Collections.emptyList(); } if (resource instanceof QuestionnaireResponse) { return doValidate(theCtx, (QuestionnaireResponse) resource); } RuntimeResourceDefinition def = theCtx.getFhirContext().getResourceDefinition((IBaseResource) resource); if ("QuestionnaireResponse".equals(def.getName()) == false) { return Collections.emptyList(); } /* * If we have a non-RI structure, convert it */ IParser p = theCtx.getFhirContext().newJsonParser(); String string = p.encodeResourceToString((IBaseResource) resource); QuestionnaireResponse qa = p.parseResource(QuestionnaireResponse.class, string); return doValidate(theCtx, qa); }
public static IParser getNewParser(FhirContext theContext, FhirVersionEnum theForVersion, RequestDetails theRequestDetails) { FhirContext context = getContextForVersion(theContext, theForVersion); // Determine response encoding EncodingEnum responseEncoding = RestfulServerUtils.determineResponseEncodingWithDefault(theRequestDetails).getEncoding(); IParser parser; switch (responseEncoding) { case JSON: parser = context.newJsonParser(); break; case XML: default: parser = context.newXmlParser(); break; } configureResponseParser(theRequestDetails, parser); return parser; }
private void setNewPayload(FhirContext theCtx, IBaseResource theNewPayload) { /* * References with placeholders would be invalid by the time we get here, and * would be caught before we even get here. This check is basically a last-ditch * effort to make sure nothing has broken in the various safeguards that * should prevent this from happening (hence it only being an assert as * opposed to something executed all the time). */ assert payloadContainsNoPlaceholderReferences(theCtx, theNewPayload); /* * Note: Don't set myPayloadDecoded in here- This is a false optimization since * it doesn't actually get used if anyone is doing subscriptions at any * scale using a queue engine, and not going through the serialize/deserialize * as we would in a queue engine can mask bugs. * -JA */ myPayload = theCtx.newJsonParser().encodeResourceToString(theNewPayload); myPayloadId = theNewPayload.getIdElement().toUnqualified().getValue(); }
@Override public ValueSet expandByIdentifier(String theUri, String theFilter) { if (isBlank(theUri)) { throw new InvalidRequestException("URI must not be blank or missing"); } ValueSet source; org.hl7.fhir.instance.model.ValueSet defaultValueSet = myDefaultProfileValidationSupport.fetchResource(myRiCtx, org.hl7.fhir.instance.model.ValueSet.class, theUri); if (defaultValueSet != null) { source = getContext().newJsonParser().parseResource(ValueSet.class, myRiCtx.newJsonParser().encodeResourceToString(defaultValueSet)); } else { SearchParameterMap params = new SearchParameterMap(); params.setLoadSynchronousUpTo(1); params.add(ValueSet.SP_URL, new UriParam(theUri)); IBundleProvider ids = search(params); if (ids.size() == 0) { throw new InvalidRequestException("Unknown ValueSet URI: " + theUri); } source = (ValueSet) ids.getResources(0, 1).get(0); } return expand(source, theFilter); }
public void fluent() throws DataFormatException { FhirContext ctx = new FhirContext(Patient.class, Observation.class); String encoded; //START SNIPPET: encodeMsgFluent Patient patient = new Patient(); patient.addIdentifier().setSystem("http://example.com/fictitious-mrns").setValue("MRN001"); patient.addName().setUse(NameUseEnum.OFFICIAL).addFamily("Tester").addGiven("John").addGiven("Q"); encoded = ctx.newJsonParser().setPrettyPrint(true).encodeResourceToString(patient); System.out.println(encoded); //END SNIPPET: encodeMsgFluent }
@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 static void main(String[] args) throws DataFormatException, IOException { //START SNIPPET: example1 Patient patient = new Patient(); patient.addIdentifier().setSystem("urn:foo").setValue("7000135"); patient.addName().addFamily("Smith").addGiven("John").addGiven("Edward"); patient.addAddress().addLine("742 Evergreen Terrace").setCity("Springfield").setState("ZZ"); FhirContext ctx = FhirContext.forDstu2(); // Use the narrative generator ctx.setNarrativeGenerator(new DefaultThymeleafNarrativeGenerator()); // Encode the output, including the narrative String output = ctx.newJsonParser().setPrettyPrint(true).encodeResourceToString(patient); System.out.println(output); //END SNIPPET: example1 }
@SuppressWarnings("unused") public static void main(String[] args) throws DataFormatException, IOException { Patient patient = new Patient(); patient.addIdentifier().setUse(IdentifierUseEnum.OFFICIAL).setSystem("urn:fake:mrns").setValue("7000135"); patient.addIdentifier().setUse(IdentifierUseEnum.SECONDARY).setSystem("urn:fake:otherids").setValue("3287486"); patient.addName().addFamily("Smith").addGiven("John").addGiven("Q").addSuffix("Junior"); patient.setGender(AdministrativeGenderEnum.MALE); FhirContext ctx = FhirContext.forDstu2(); String xmlEncoded = ctx.newXmlParser().encodeResourceToString(patient); String jsonEncoded = ctx.newJsonParser().encodeResourceToString(patient); MyClientInterface client = ctx.newRestfulClient(MyClientInterface.class, "http://foo/fhir"); IdentifierDt searchParam = new IdentifierDt("urn:someidentifiers", "7000135"); List<Patient> clients = client.findPatientsByIdentifier(searchParam); }