@Override public String encode() { return theContext.newXmlParser().encodeResourceToString(theResource); }
public static <T extends IBaseResource> T apply(FhirContext theCtx, T theResourceToUpdate, String thePatchBody) { @SuppressWarnings("unchecked") Class<T> clazz = (Class<T>) theResourceToUpdate.getClass(); String inputResource = theCtx.newXmlParser().encodeResourceToString(theResourceToUpdate); ByteArrayOutputStream result = new ByteArrayOutputStream(); try { Patcher.patch(new ByteArrayInputStream(inputResource.getBytes(Constants.CHARSET_UTF8)), new ByteArrayInputStream(thePatchBody.getBytes(Constants.CHARSET_UTF8)), result); } catch (IOException e) { throw new InternalErrorException(e); } String resultString = new String(result.toByteArray(), Constants.CHARSET_UTF8); T retVal = theCtx.newXmlParser().parseResource(clazz, resultString); return retVal; }
@Override public String encode() { return theCtx.getFhirContext().newXmlParser().encodeResourceToString(theResource); }
/** * 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)); }
public static byte[] encodeResource(IBaseResource theResource, ResourceEncodingEnum theEncoding, Set<String> theExcludeElements, FhirContext theContext) { byte[] bytes; IParser parser = theEncoding.newParser(theContext); parser.setDontEncodeElements(theExcludeElements); String encoded = parser.encodeResourceToString(theResource); switch (theEncoding) { case JSON: bytes = encoded.getBytes(Charsets.UTF_8); break; case JSONC: bytes = GZipUtil.compress(encoded); break; default: case DEL: bytes = new byte[0]; break; } ourLog.debug("Encoded {} chars of resource body as {} bytes", encoded.length(), bytes.length); return bytes; }
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 void bbbbb() throws DataFormatException, IOException { //START SNIPPET: metadataClientUsage FhirContext ctx = FhirContext.forDstu2(); MetadataClient client = ctx.newRestfulClient(MetadataClient.class, "http://spark.furore.com/fhir"); CapabilityStatement metadata = client.getServerMetadata(); System.out.println(ctx.newXmlParser().encodeResourceToString(metadata)); //END SNIPPET: metadataClientUsage }
/** * 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(); }
private static void testSearchForPatients() { List<IBaseResource> resources = new IncludesExamples().searchForPatients(); // Create a bundle with both FhirContext ctx = FhirContext.forDstu2(); Dstu2BundleFactory bf = new Dstu2BundleFactory(ctx); bf.initializeBundleFromResourceList(null, resources, "http://example.com/base", "http://example.com/base/Patient", 1, BundleTypeEnum.SEARCHSET); IBaseResource b = bf.getResourceBundle(); // Encode the bundle String encoded = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(b); System.out.println(encoded); }
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); }
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(); }
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; }
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 main(String[] args) throws DataFormatException, IOException { //START SNIPPET: patientUse MyPatient patient = new MyPatient(); patient.setPetName(new StringDt("Fido")); patient.getImportantDates().add(new DateTimeDt("2010-01-02")); patient.getImportantDates().add(new DateTimeDt("2014-01-26T11:11:11")); patient.addName().addFamily("Smith").addGiven("John").addGiven("Quincy").addSuffix("Jr"); IParser p = FhirContext.forDstu2().newXmlParser().setPrettyPrint(true); String messageString = p.encodeResourceToString(patient); System.out.println(messageString); //END SNIPPET: patientUse //START SNIPPET: patientParse IParser parser = FhirContext.forDstu2().newXmlParser(); MyPatient newPatient = parser.parseResource(MyPatient.class, messageString); //END SNIPPET: patientParse { FhirContext ctx2 = FhirContext.forDstu2(); RuntimeResourceDefinition def = ctx2.getResourceDefinition(patient); System.out.println(ctx2.newXmlParser().setPrettyPrint(true).encodeResourceToString(def.toProfile())); } }
@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 manualContained() { // START SNIPPET: manualContained // Create an organization, and give it a local ID Organization org = new Organization(); org.setId("#localOrganization"); org.getNameElement().setValue("Contained Test Organization"); // Create a patient Patient patient = new Patient(); patient.setId("Patient/1333"); patient.addIdentifier().setSystem("urn:mrns").setValue("253345"); // Set the reference, and manually add the contained resource patient.getManagingOrganization().setReference("#localOrganization"); patient.getContained().getContainedResources().add(org); String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient); System.out.println(encoded); // END SNIPPET: manualContained }
@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); }
@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); }
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 }