More tutorial examples

This commit is contained in:
jamesagnew 2014-11-24 12:45:12 +01:00
parent 028c349d10
commit f9e19f759f
15 changed files with 468 additions and 9 deletions

View File

@ -14,7 +14,7 @@
<groupId>ca.uhn.hapi.example</groupId>
<artifactId>hapi-fhir-example-simple-server</artifactId>
<version>0.8-SNAPSHOT</version>
<version>0.7</version>
<packaging>war</packaging>
<name>HAPI FHIR Example - Simple Server</name>
@ -35,14 +35,20 @@
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-base</artifactId>
<version>0.8-SNAPSHOT</version>
<version>0.7</version>
</dependency>
<!-- At least one "structures" JAR must also be included -->
<!--
Beginning in HAPI 0.8, at least one "structures" JAR must also be included.
For now it is commented out.
-->
<!--
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-structures-dstu</artifactId>
<version>0.8-SNAPSHOT</version>
</dependency>
-->
<!--
HAPI-FHIR uses Logback for logging support. The logback library is included

View File

@ -0,0 +1 @@
curl -H "Content-Type: application/xml+fhir" -X POST -d '<Patient xmlns="http://hl7.org/fhir"><name><family value="Fireman"/><given value="John"/></name></Patient>' "http://localhost:8080/example03/Patient"

View File

@ -0,0 +1,98 @@
package ca.uhn.fhir.example.ex3;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.rest.annotation.Create;
import ca.uhn.fhir.rest.annotation.IdParam;
import ca.uhn.fhir.rest.annotation.OptionalParam;
import ca.uhn.fhir.rest.annotation.Read;
import ca.uhn.fhir.rest.annotation.RequiredParam;
import ca.uhn.fhir.rest.annotation.ResourceParam;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.param.StringParam;
import ca.uhn.fhir.rest.server.IResourceProvider;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
/**
* This is the most basic resource provider, showing only a single
* read method on a resource provider
*/
public class Example03_PatientResourceProvider implements IResourceProvider {
private Map<Long, Patient> myPatients = new HashMap<Long, Patient>();
private Long myNextId = 1L;
/** Constructor */
public Example03_PatientResourceProvider() {
Long id = myNextId++;
Patient pat1 = new Patient();
pat1.setId(new IdDt(id));
pat1.addIdentifier().setSystem("http://acme.com/MRNs").setValue("7000135");
pat1.addName().addFamily("Simpson").addGiven("Homer").addGiven("J");
myPatients.put(id, pat1);
}
/** All Resource Providers must implement this method */
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
/** Simple implementation of the "read" method */
@Read()
public Patient read(@IdParam IdDt theId) {
Patient retVal = myPatients.get(theId.getIdPartAsLong());
if (retVal == null) {
throw new ResourceNotFoundException(theId);
}
return retVal;
}
/** Create/save a new resource */
@Create
public MethodOutcome create(@ResourceParam Patient thePatient) {
// Give the resource the next sequential ID
long id = myNextId++;
thePatient.setId(new IdDt(id));
// Store the resource in memory
myPatients.put(id, thePatient);
// Inform the server of the ID for the newly stored resource
return new MethodOutcome(thePatient.getId());
}
/** Simple "search" implementation **/
@Search
public List<Patient> search() {
List<Patient> retVal = new ArrayList<Patient>();
retVal.addAll(myPatients.values());
return retVal;
}
/** A search with a parameter */
@Search
public List<Patient> search(@RequiredParam(name="family") StringParam theParam) {
List<Patient> retVal = new ArrayList<Patient>();
// Loop through the patients looking for matches
for (Patient next : myPatients.values()) {
String familyName = next.getNameFirstRep().getFamilyAsSingleString().toLowerCase();
if (familyName.contains(theParam.getValue().toLowerCase()) == false) {
continue;
}
retVal.add(next);
}
return retVal;
}
}

View File

@ -0,0 +1,19 @@
package ca.uhn.fhir.example.ex3;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import ca.uhn.fhir.rest.server.RestfulServer;
@WebServlet("/example03/*")
public class Example03_SimpleRestfulServer extends RestfulServer {
private static final long serialVersionUID = 1L;
@Override
protected void initialize() throws ServletException {
setResourceProviders(new Example03_PatientResourceProvider());
}
}

View File

@ -0,0 +1,75 @@
package ca.uhn.fhir.example.ex4;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.rest.annotation.Create;
import ca.uhn.fhir.rest.annotation.IdParam;
import ca.uhn.fhir.rest.annotation.Read;
import ca.uhn.fhir.rest.annotation.ResourceParam;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.server.IResourceProvider;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
/**
* This is the most basic resource provider, showing only a single
* read method on a resource provider
*/
public class Example04_PatientResourceProvider implements IResourceProvider {
private Map<Long, Patient> myPatients = new HashMap<Long, Patient>();
private Long myNextId = 1L;
/** Constructor */
public Example04_PatientResourceProvider() {
Patient pat1 = new Patient();
pat1.addIdentifier().setSystem("http://acme.com/MRNs").setValue("7000135");
pat1.addName().addFamily("Simpson").addGiven("Homer").addGiven("J");
myPatients.put(myNextId++, pat1);
}
/** All Resource Providers must implement this method */
@Override
public Class<? extends IResource> getResourceType() {
return Patient.class;
}
/** Simple implementation of the "read" method */
@Read()
public Patient read(@IdParam IdDt theId) {
Patient retVal = myPatients.get(theId.getIdPartAsLong());
if (retVal == null) {
throw new ResourceNotFoundException(theId);
}
return retVal;
}
/** Create/save a new resource */
@Create
public MethodOutcome create(@ResourceParam Patient thePatient) {
// Give the resource the next sequential ID
long id = myNextId++;
thePatient.setId(new IdDt(id));
// Store the resource in memory
myPatients.put(id, thePatient);
// Inform the server of the ID for the newly stored resource
return new MethodOutcome(thePatient.getId());
}
/** Simple "search" implementation **/
@Search
public List<Patient> search() {
List<Patient> retVal = new ArrayList<Patient>();
retVal.addAll(myPatients.values());
return retVal;
}
}

View File

@ -0,0 +1,19 @@
package ca.uhn.fhir.example.ex4;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import ca.uhn.fhir.rest.server.RestfulServer;
@WebServlet("/example04/*")
public class Example04_SimpleRestfulServer extends RestfulServer {
private static final long serialVersionUID = 1L;
@Override
protected void initialize() throws ServletException {
setResourceProviders(new Example04_PatientResourceProvider());
}
}

View File

@ -13,7 +13,7 @@
<groupId>ca.uhn.hapi.example</groupId>
<artifactId>hapi-fhir-example-skeleton-project</artifactId>
<version>0.8-SNAPSHOT</version>
<version>0.7</version>
<packaging>jar</packaging>
<name>HAPI FHIR Example - Skeleton Project</name>
@ -34,15 +34,20 @@
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-base</artifactId>
<version>0.8-SNAPSHOT</version>
<version>0.7</version>
</dependency>
<!-- At least one "structures" JAR must also be included -->
<!--
Beginning in HAPI 0.8, at least one "structures" JAR must also be included.
For now it is commented out.
-->
<!--
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-structures-dstu</artifactId>
<version>0.8-SNAPSHOT</version>
</dependency>
-->
<!--
HAPI-FHIR uses Logback for logging support. The logback library is included
automatically by Maven as a part of the hapi-fhir-base dependency, but you
@ -55,6 +60,31 @@
<version>1.1.2</version>
</dependency>
<!--
The following two dependencies are required only if you are using
Schematron resource validation. Otherwise, they may be omitted.
-->
<dependency>
<groupId>com.phloc</groupId>
<artifactId>phloc-schematron</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>com.phloc</groupId>
<artifactId>phloc-commons</artifactId>
<version>4.3.3</version>
</dependency>
<!--
The following dependency is only required for narrative generator
support. If you are not using this, it may be removed.
-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
</dependencies>
<build>

View File

@ -19,13 +19,13 @@ public class Example07_ClientSearch {
Patient patient = client.read(Patient.class, "4529");
// Print the ID of the newly created resource
System.out.println(patient.getId());
System.out.println("Found ID: " + 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());
System.out.println("Now have ID: " + outcome.getId());
}
}

View File

@ -0,0 +1,34 @@
package ca.uhn.fhir.example;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.dstu.resource.Encounter;
import ca.uhn.fhir.model.dstu.resource.OperationOutcome;
import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.validation.FhirValidator;
import ca.uhn.fhir.validation.ValidationResult;
public class Example08_ValidateResource {
public static void main(String[] args) {
// Create an encounter with an invalid status and no class
Encounter enc = new Encounter();
enc.getStatus().setValueAsString("invalid_status");
// Create a new validator
FhirContext ctx = new FhirContext();
FhirValidator validator = ctx.newValidator();
// Did we succeed?
ValidationResult result = validator.validateWithResult(enc);
System.out.println("Success: " + result.isSuccessful());
// What was the result
OperationOutcome outcome = result.getOperationOutcome();
IParser parser = ctx.newXmlParser().setPrettyPrint(true);
System.out.println(parser.encodeResourceToString(outcome));
}
}

View File

@ -0,0 +1,30 @@
package ca.uhn.fhir.example;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.dstu.resource.Encounter;
import ca.uhn.fhir.model.dstu.resource.OperationOutcome;
import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator;
import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.validation.FhirValidator;
import ca.uhn.fhir.validation.ValidationResult;
public class Example09_NarrativeGenerator {
public static void main(String[] args) {
// Create an encounter with an invalid status and no class
Patient pat = new Patient();
pat.addName().addFamily("Simpson").addGiven("Homer").addGiven("Jay");
pat.addAddress().addLine("342 Evergreen Terrace").addLine("Springfield");
pat.addIdentifier().setLabel("MRN: 12345");
// Create a new context and enable the narrative generator
FhirContext ctx = new FhirContext();
ctx.setNarrativeGenerator(new DefaultThymeleafNarrativeGenerator());
String res = ctx.newJsonParser().setPrettyPrint(true).encodeResourceToString(pat);
System.out.println(res);
}
}

View File

@ -0,0 +1,24 @@
package ca.uhn.fhir.example;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.model.primitive.CodeDt;
import ca.uhn.fhir.parser.IParser;
public class Example10_Extensions {
public static void main(String[] args) {
Patient pat = new Patient();
pat.addName().addFamily("Simpson").addGiven("Homer");
String url = "http://acme.org#eyeColour";
boolean isModifier = false;
pat.addUndeclaredExtension(isModifier, url).setValue(new CodeDt("blue"));;
IParser p = new FhirContext().newXmlParser().setPrettyPrint(true);
String encoded = p.encodeResourceToString(pat);
System.out.println(encoded);
}
}

View File

@ -0,0 +1,27 @@
package ca.uhn.fhir.example;
import ca.uhn.fhir.model.api.annotation.Child;
import ca.uhn.fhir.model.api.annotation.Extension;
import ca.uhn.fhir.model.api.annotation.ResourceDef;
import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.model.primitive.CodeDt;
@ResourceDef(name="Patient")
public class Example11_ExtendedPatient extends Patient {
@Child(name = "eyeColour")
@Extension(url="http://acme.org/#extpt", definedLocally = false, isModifier = false)
private CodeDt myEyeColour;
public CodeDt getEyeColour() {
if (myEyeColour == null) {
myEyeColour = new CodeDt();
}
return myEyeColour;
}
public void setEyeColour(CodeDt theEyeColour) {
myEyeColour = theEyeColour;
}
}

View File

@ -0,0 +1,22 @@
package ca.uhn.fhir.example;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.primitive.CodeDt;
import ca.uhn.fhir.parser.IParser;
public class Example11_UseExtensions {
public static void main(String[] args) {
Example11_ExtendedPatient pat = new Example11_ExtendedPatient();
pat.addName().addFamily("Simpson").addGiven("Homer");
pat.setEyeColour(new CodeDt("blue"));
IParser p = new FhirContext().newXmlParser().setPrettyPrint(true);
String encoded = p.encodeResourceToString(pat);
System.out.println(encoded);
}
}

23
pom.xml
View File

@ -593,6 +593,29 @@
<id>ROOT</id>
<modules>
</modules>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>${maven_assembly_plugin_version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<attach>false</attach>
<descriptors>
<descriptor>${project.basedir}/src/assembly/hapi-fhir-sample-projects.xml</descriptor>
<!-- <descriptor>src/assembly/hapi-jdk14.xml</descriptor> -->
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>SIGN_ARTIFACTS</id>

View File

@ -0,0 +1,51 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>sample-projects</id>
<formats>
<format>zip</format>
<format>tar.bz2</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.basedir}/hapi-fhir-tutorial</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>simple-server/**</include>
<include>skeleton-project/**</include>
</includes>
<excludes>
<exclude>*/target/**</exclude>
<exclude>*/.classpath</exclude>
<exclude>*/.project</exclude>
<exclude>*/.settings/**</exclude>
</excludes>
</fileSet>
<!--
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
-->
</fileSets>
<!--
<dependencySets>
<dependencySet>
<outputDirectory>/lib/dependency</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>false</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
-->
</assembly>