More tutorial code

This commit is contained in:
James Agnew 2014-11-20 10:15:58 -05:00
parent 74b15e2295
commit 39cddf59ac
10 changed files with 217 additions and 6 deletions

View File

@ -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.
* <p>
* Users of HAPI should only interact with this method in Server applications
* </p>
*
* @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

View File

@ -88,4 +88,4 @@
</plugins>
</build>
</project>
</project>

View File

@ -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");
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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<IdentifierDt> identifiers = pat.getIdentifier();
String idSystemString = identifiers.get(0).getSystem().getValueAsString();
String idValueString = identifiers.get(0).getValue().getValueAsString();
System.out.println(idSystemString + " " + idValueString);
}
}

View File

@ -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());
}
}

View File

@ -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

View File

@ -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());
}
}

View File

@ -0,0 +1,16 @@
<configuration scan="true" scanPeriod="30 seconds">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} %msg%n</pattern>
</encoder>
</appender>
<root>
<appender-ref ref="STDOUT" />
</root>
</configuration>