From 39cddf59acada90c07aa018935cdb77000a2bfab Mon Sep 17 00:00:00 2001 From: James Agnew Date: Thu, 20 Nov 2014 10:15:58 -0500 Subject: [PATCH] More tutorial code --- .../ca/uhn/fhir/rest/api/MethodOutcome.java | 3 ++ hapi-fhir-tutorial/skeleton-project/pom.xml | 2 +- .../example/Example01_CreateAPatient.java | 13 ++++--- .../example/Example02_CreateAPatient.java | 27 +++++++++++++ .../example/Example03_EncodeResource.java | 32 +++++++++++++++ .../fhir/example/Example04_ParseResource.java | 29 ++++++++++++++ .../fhir/example/Example05_ClientCreate.java | 31 +++++++++++++++ .../Example06_ClientReadAndUpdate.java | 39 +++++++++++++++++++ .../fhir/example/Example07_ClientSearch.java | 31 +++++++++++++++ .../src/main/resources/logback.xml | 16 ++++++++ 10 files changed, 217 insertions(+), 6 deletions(-) create mode 100644 hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example02_CreateAPatient.java create mode 100644 hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example03_EncodeResource.java create mode 100644 hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example04_ParseResource.java create mode 100644 hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example05_ClientCreate.java create mode 100644 hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example06_ClientReadAndUpdate.java create mode 100644 hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example07_ClientSearch.java create mode 100644 hapi-fhir-tutorial/skeleton-project/src/main/resources/logback.xml diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/api/MethodOutcome.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/api/MethodOutcome.java index a4a538e23b0..4ebdf627d49 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/api/MethodOutcome.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/api/MethodOutcome.java @@ -138,6 +138,9 @@ public class MethodOutcome { /** * If not null, indicates whether the resource was created (as opposed to being updated). This is generally not needed, since the server can assume based on the method being called whether the * result was a creation or an update. However, it can be useful if you are implementing an update method that does a create if the ID doesn't already exist. + *

+ * Users of HAPI should only interact with this method in Server applications + *

* * @param theCreated * If not null, indicates whether the resource was created (as opposed to being updated). This is generally not needed, since the server can assume based on the method being called diff --git a/hapi-fhir-tutorial/skeleton-project/pom.xml b/hapi-fhir-tutorial/skeleton-project/pom.xml index 1c7bf848264..4ea64929e2b 100644 --- a/hapi-fhir-tutorial/skeleton-project/pom.xml +++ b/hapi-fhir-tutorial/skeleton-project/pom.xml @@ -88,4 +88,4 @@ - + \ No newline at end of file diff --git a/hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example01_CreateAPatient.java b/hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example01_CreateAPatient.java index fbde2755839..34155cb50ec 100644 --- a/hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example01_CreateAPatient.java +++ b/hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example01_CreateAPatient.java @@ -4,20 +4,23 @@ import ca.uhn.fhir.model.dstu.composite.HumanNameDt; import ca.uhn.fhir.model.dstu.composite.IdentifierDt; import ca.uhn.fhir.model.dstu.resource.Patient; -@SuppressWarnings("unused") public class Example01_CreateAPatient { public static void main(String[] theArgs) { + // Create a resource instance Patient pat = new Patient(); - IdentifierDt identifier = pat.addIdentifier(); - identifier.setSystem("http://acme.org/MRNs").setValue("7000135"); - + // Add a "name" element HumanNameDt name = pat.addName(); name.addFamily("Simpson").addGiven("Homer").addGiven("J"); - pat.getBirthDate().set + // Add an "identifier" element + IdentifierDt identifier = pat.addIdentifier(); + identifier.setSystem("http://acme.org/MRNs").setValue("7000135"); + + // Model is designed to be chained + pat.addIdentifier().setLabel("Library Card 12345").setValue("12345"); } diff --git a/hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example02_CreateAPatient.java b/hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example02_CreateAPatient.java new file mode 100644 index 00000000000..460f5be9387 --- /dev/null +++ b/hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example02_CreateAPatient.java @@ -0,0 +1,27 @@ +package ca.uhn.fhir.example; + +import ca.uhn.fhir.model.dstu.composite.ContactDt; +import ca.uhn.fhir.model.dstu.resource.Patient; +import ca.uhn.fhir.model.dstu.valueset.AdministrativeGenderCodesEnum; +import ca.uhn.fhir.model.dstu.valueset.ContactSystemEnum; +import ca.uhn.fhir.model.dstu.valueset.ContactUseEnum; + +public class Example02_CreateAPatient { + public static void main(String[] theArgs) { + + Patient pat = new Patient(); + + pat.addName().addFamily("Simpson").addGiven("Homer").addGiven("J"); + pat.addIdentifier().setSystem("http://acme.org/MRNs").setValue("7000135"); + pat.addIdentifier().setLabel("Library Card 12345").setValue("12345"); + + // Enumerated types are provided for many coded elements + ContactDt contact = pat.addTelecom(); + contact.setUse(ContactUseEnum.HOME); + contact.setSystem(ContactSystemEnum.PHONE); + contact.setValue("1 (416) 340-4800"); + + pat.setGender(AdministrativeGenderCodesEnum.M); + + } +} diff --git a/hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example03_EncodeResource.java b/hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example03_EncodeResource.java new file mode 100644 index 00000000000..a2ca24a1665 --- /dev/null +++ b/hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example03_EncodeResource.java @@ -0,0 +1,32 @@ +package ca.uhn.fhir.example; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.dstu.resource.Patient; +import ca.uhn.fhir.model.dstu.valueset.AdministrativeGenderCodesEnum; +import ca.uhn.fhir.model.dstu.valueset.ContactSystemEnum; +import ca.uhn.fhir.model.dstu.valueset.ContactUseEnum; +import ca.uhn.fhir.parser.IParser; + +public class Example03_EncodeResource { + public static void main(String[] theArgs) { + + // Create a Patient + Patient pat = new Patient(); + pat.addName().addFamily("Simpson").addGiven("Homer").addGiven("J"); + pat.addIdentifier().setSystem("http://acme.org/MRNs").setValue("7000135"); + pat.addIdentifier().setLabel("Library Card 12345").setValue("12345"); + pat.addTelecom().setUse(ContactUseEnum.HOME).setSystem(ContactSystemEnum.PHONE).setValue("1 (416) 340-4800"); + pat.setGender(AdministrativeGenderCodesEnum.M); + + // Create a context + FhirContext ctx = new FhirContext(); + + // Create a XML parser + IParser parser = ctx.newXmlParser(); + parser.setPrettyPrint(true); + + String encode = parser.encodeResourceToString(pat); + System.out.println(encode); + + } +} diff --git a/hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example04_ParseResource.java b/hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example04_ParseResource.java new file mode 100644 index 00000000000..5ac35b8ef5d --- /dev/null +++ b/hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example04_ParseResource.java @@ -0,0 +1,29 @@ +package ca.uhn.fhir.example; + +import java.util.List; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.dstu.composite.IdentifierDt; +import ca.uhn.fhir.model.dstu.resource.Patient; +import ca.uhn.fhir.parser.IParser; + +public class Example04_ParseResource { + public static void main(String[] theArgs) { + + String resourceBody = "{\"resourceType\":\"Patient\",\"identifier\":[{\"system\":\"http://acme.org/MRNs\",\"value\":\"7000135\"}],\"name\":[{\"family\":[\"Simpson\"],\"given\":[\"Homer\",\"J\"]}]}"; + + // Create a context + FhirContext ctx = new FhirContext(); + + // Create a JSON parser + IParser parser = ctx.newJsonParser(); + Patient pat = parser.parseResource(Patient.class, resourceBody); + + List identifiers = pat.getIdentifier(); + String idSystemString = identifiers.get(0).getSystem().getValueAsString(); + String idValueString = identifiers.get(0).getValue().getValueAsString(); + + System.out.println(idSystemString + " " + idValueString); + + } +} diff --git a/hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example05_ClientCreate.java b/hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example05_ClientCreate.java new file mode 100644 index 00000000000..e623289fd82 --- /dev/null +++ b/hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example05_ClientCreate.java @@ -0,0 +1,31 @@ +package ca.uhn.fhir.example; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.dstu.resource.Patient; +import ca.uhn.fhir.model.dstu.valueset.AdministrativeGenderCodesEnum; +import ca.uhn.fhir.rest.api.MethodOutcome; +import ca.uhn.fhir.rest.client.IGenericClient; + +public class Example05_ClientCreate { + public static void main(String[] theArgs) { + + Patient pat = new Patient(); + pat.addName().addFamily("Simpson").addGiven("Homer").addGiven("J"); + pat.addIdentifier().setSystem("http://acme.org/MRNs").setValue("7000135"); + pat.setGender(AdministrativeGenderCodesEnum.M); + + // Create a context + FhirContext ctx = new FhirContext(); + + // Create a client + String serverBaseUrl = "http://fhirtest.uhn.ca/base"; + IGenericClient client = ctx.newRestfulGenericClient(serverBaseUrl); + + // Use the client to store a new resource instance + MethodOutcome outcome = client.create().resource(pat).execute(); + + // Print the ID of the newly created resource + System.out.println(outcome.getId()); + + } +} diff --git a/hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example06_ClientReadAndUpdate.java b/hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example06_ClientReadAndUpdate.java new file mode 100644 index 00000000000..1113ff88d9a --- /dev/null +++ b/hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example06_ClientReadAndUpdate.java @@ -0,0 +1,39 @@ +package ca.uhn.fhir.example; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.api.Bundle; +import ca.uhn.fhir.model.dstu.resource.Patient; +import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.rest.client.IGenericClient; +import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; + +//@formatter:off +public class Example06_ClientReadAndUpdate { + public static void main(String[] theArgs) { + + // Create a client + FhirContext ctx = new FhirContext(); + String serverBaseUrl = "http://fhirtest.uhn.ca/base"; + IGenericClient client = ctx.newRestfulGenericClient(serverBaseUrl); + + // Log requests and responses + client.registerInterceptor(new LoggingInterceptor(true)); + + // Build a search and execute it + Bundle response = client.search() + .forResource(Patient.class) + .where(Patient.NAME.matches().value("Test")) + .and(Patient.BIRTHDATE.before().day("2014-01-01")) + .limitTo(100) + .execute(); + + // How many resources did we find? + System.out.println("Responses: " + response.size()); + + // Print the ID of the first one + IdDt firstResponseId = response.getEntries().get(0).getResource().getId(); + System.out.println(firstResponseId); + + } +} +//@formatter:on diff --git a/hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example07_ClientSearch.java b/hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example07_ClientSearch.java new file mode 100644 index 00000000000..be4a01db439 --- /dev/null +++ b/hapi-fhir-tutorial/skeleton-project/src/main/java/ca/uhn/fhir/example/Example07_ClientSearch.java @@ -0,0 +1,31 @@ +package ca.uhn.fhir.example; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.dstu.resource.Patient; +import ca.uhn.fhir.model.dstu.valueset.AdministrativeGenderCodesEnum; +import ca.uhn.fhir.rest.api.MethodOutcome; +import ca.uhn.fhir.rest.client.IGenericClient; + +public class Example07_ClientSearch { + public static void main(String[] theArgs) { + + // Create a client + String serverBaseUrl = "http://fhirtest.uhn.ca/base"; + FhirContext ctx = new FhirContext(); + IGenericClient client = ctx.newRestfulGenericClient(serverBaseUrl); + + // Use the client to read back the new instance using the + // ID we retrieved from the read + Patient patient = client.read(Patient.class, "4529"); + + // Print the ID of the newly created resource + System.out.println(patient.getId()); + + // Change the gender and send an update to the server + patient.setGender(AdministrativeGenderCodesEnum.F); + MethodOutcome outcome = client.update().resource(patient).execute(); + + System.out.println(outcome.getId()); + + } +} diff --git a/hapi-fhir-tutorial/skeleton-project/src/main/resources/logback.xml b/hapi-fhir-tutorial/skeleton-project/src/main/resources/logback.xml new file mode 100644 index 00000000000..8347a5137e6 --- /dev/null +++ b/hapi-fhir-tutorial/skeleton-project/src/main/resources/logback.xml @@ -0,0 +1,16 @@ + + + + + INFO + + + %d{HH:mm:ss.SSS} %-5level %logger{36} %msg%n + + + + + + + + \ No newline at end of file