From 01320ac1b1ddc465264610b948c86a722103fa0a Mon Sep 17 00:00:00 2001 From: jamesagnew Date: Thu, 29 Oct 2015 09:14:23 -0400 Subject: [PATCH] Site updates --- .../example/ClientTransactionExamples.java | 81 +++++++---- src/changes/changes.xml | 2 +- src/site/site.xml | 1 + src/site/xdoc/doc_rest_client_examples.xml | 126 ++++++++++++++++++ src/site/xdoc/docindex.xml | 1 + 5 files changed, 187 insertions(+), 24 deletions(-) create mode 100644 src/site/xdoc/doc_rest_client_examples.xml diff --git a/examples/src/main/java/example/ClientTransactionExamples.java b/examples/src/main/java/example/ClientTransactionExamples.java index 9f989efb2a3..790008bc66c 100644 --- a/examples/src/main/java/example/ClientTransactionExamples.java +++ b/examples/src/main/java/example/ClientTransactionExamples.java @@ -8,10 +8,12 @@ import ca.uhn.fhir.model.dstu2.resource.Observation; import ca.uhn.fhir.model.dstu2.resource.Patient; import ca.uhn.fhir.model.dstu2.valueset.AdministrativeGenderEnum; import ca.uhn.fhir.model.dstu2.valueset.BundleTypeEnum; +import ca.uhn.fhir.model.dstu2.valueset.HTTPVerbEnum; import ca.uhn.fhir.model.dstu2.valueset.ObservationStatusEnum; import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.rest.client.IGenericClient; -public class TransactionClientTest { +public class ClientTransactionExamples { public static void main(String[] args) { conditionalCreate(); @@ -19,22 +21,25 @@ public class TransactionClientTest { private static void conditionalCreate() { + //START SNIPPET: conditional // Create a patient object Patient patient = new Patient(); - patient - .addIdentifier() - .setSystem("http://acme.org/mrns") - .setValue("12345"); - patient - .addName() - .addFamily("Jones") - .addGiven("Johnson"); + patient.addIdentifier() + .setSystem("http://acme.org/mrns") + .setValue("12345"); + patient.addName() + .addFamily("Jameson") + .addGiven("J") + .addGiven("Jonah"); patient.setGender(AdministrativeGenderEnum.MALE); + // Give the patient a temporary UUID so that other resources in + // the transaction can refer to it + patient.setId(IdDt.newRandomUuid()); + // Create an observation object Observation observation = new Observation(); observation.setStatus(ObservationStatusEnum.FINAL); - observation.setSubject(new ResourceReferenceDt(patient)); observation .getCode() .addCoding() @@ -42,23 +47,53 @@ public class TransactionClientTest { .setCode("789-8") .setDisplay("Erythrocytes [#/volume] in Blood by Automated count"); observation.setValue( - new QuantityDt() - .setValue(4.12) - .setUnit("10 trillion/L") - .setSystem("http://unitsofmeasure.org") - .setCode("10*12/L")); - + new QuantityDt() + .setValue(4.12) + .setUnit("10 trillion/L") + .setSystem("http://unitsofmeasure.org") + .setCode("10*12/L")); + + // The observation refers to the patient using the ID, which is already + // set to a temporary UUID + observation.setSubject(new ResourceReferenceDt(patient.getId().getValue())); + + // Create a bundle that will be used as a transaction Bundle bundle = new Bundle(); bundle.setType(BundleTypeEnum.TRANSACTION); - patient.setId(IdDt.newRandomUuid()); - bundle - .addEntry() - .setResource(patient) - .getRequest() - .setUrl(patient.getId().getValue()); + // Add the patient as an entry. This entry is a POST with an + // If-None-Exist header (conditional create) meaning that it + // will only be created if there isn't already a Patient with + // the identifier 12345 + bundle.addEntry() + .setFullUrl(patient.getId().getValue()) + .setResource(patient) + .getRequest() + .setUrl("Patient") + .setIfNoneExist("Patient?identifier=http://acme.org/mrns|12345") + .setMethod(HTTPVerbEnum.POST); + + // Add the observation. This entry is a POST with no header + // (normal create) meaning that it will be created even if + // a similar resource already exists. + bundle.addEntry() + .setResource(observation) + .getRequest() + .setUrl("Observation") + .setMethod(HTTPVerbEnum.POST); + + // Log the request + FhirContext ctx = FhirContext.forDstu2(); + System.out.println(ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(bundle)); + + // Create a client and post the transaction to the server + IGenericClient client = ctx.newRestfulGenericClient("http://fhirtest.uhn.ca/baseDstu2"); + Bundle resp = client.transaction().withBundle(bundle).execute(); + + // Log the response + System.out.println(ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(resp)); + //END SNIPPET: conditional - System.out.println(FhirContext.forDstu2().newXmlParser().setPrettyPrint(true).encodeResourceToString(bundle)); } } diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 98743c2ec42..3395468f5ae 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -221,7 +221,7 @@ Add ability for a server REST resource provider @Search method to declare that it should allow even parameters it doesn't understand. - + diff --git a/src/site/site.xml b/src/site/site.xml index 5b516c416e2..ffd7319864e 100644 --- a/src/site/site.xml +++ b/src/site/site.xml @@ -93,6 +93,7 @@ + diff --git a/src/site/xdoc/doc_rest_client_examples.xml b/src/site/xdoc/doc_rest_client_examples.xml new file mode 100644 index 00000000000..df40463af28 --- /dev/null +++ b/src/site/xdoc/doc_rest_client_examples.xml @@ -0,0 +1,126 @@ + + + + + RESTful Client Examples + James Agnew + + + + +
+ +

+ This page contains examples of how to use the client to perform + complete tasks. If you have an example you could contribute, we'd + love to hear from you! +

+ + + +

+ The following example shows how to post a transaction with two resources, + where one resource contains a reference to the other. A temporary ID (a UUID) + is used as an ID to refer to, and this ID will be replaced by the server by + a permanent ID. +

+ + + + + + +

+ This code creates the following transaction bundle:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +]]> +

+ +

+ The server responds with the following response. Note that + the ID of the already existing patient is returned, and the + ID of the newly created Observation is too.
+ + + + + + + + + + + + + + + + + + + + + + + + +]]> +

+ +
+ +
+ + + +
diff --git a/src/site/xdoc/docindex.xml b/src/site/xdoc/docindex.xml index 35d48764805..ee6d58db50e 100644 --- a/src/site/xdoc/docindex.xml +++ b/src/site/xdoc/docindex.xml @@ -35,6 +35,7 @@
  • Annotation Client
  • Interceptors (client)
  • Client HTTP Configuration
  • +
  • Client Examples
  • RESTful Server