原文链接:FHIR Connectathon 7 for Java Dummies
FHIR Connectathon 7中罗列了三个应用场景.
让我们看一下利用现有的FHIR开源代码如何来实现这些场景. 所选场景:第一个 开发语言:JAVA(安装配置略) IDE:IDEA community版本 开源库:HAPI-FHIR 测试系统:UBUNTU14.04 32位
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.dstu.resource.Conformance;
import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.model.dstu.valueset.AdministrativeGenderCodesEnum;
import ca.uhn.fhir.rest.client.IGenericClient;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
//Code copied very substantially from http://jamesagnew.github.io/hapi-fhir/doc_rest_client.html
public class Main {
public static void main(String[] args) {
//create the FHIR context
FhirContext ctx = new FhirContext();
String serverBase = "https://fhir.orionhealth.com/blaze/fhir";
IGenericClient client = ctx.newRestfulGenericClient(serverBase);
// Retrieve the server's conformance statement and print its description
Conformance conf = client.conformance();
System.out.println(conf.getDescription().getValue());
//Read a single patient
//String id = "1"; //this is not found on the server, and will throw an exception
String id = "77662";
try {
Patient patient = client.read(Patient.class, id);
//Do something with patient
// Change the patient gender
patient.setGender(AdministrativeGenderCodesEnum.M);
//and save...
client.update(id, patient);
} catch (ResourceNotFoundException ex) {
System.out.println("No patient with the ID of " + id);
} catch (Exception ex){
System.out.println("Unexpected error: " + ex.getMessage());
}
//Create a new Patient
Patient newPatient = new Patient();
newPatient.addIdentifier("urn:oid:2.16.840.1.113883.2.18.2", "PRP1660");
newPatient.addName().addFamily("Power").addGiven("Cold");
client.create()
.resource(newPatient)
.encodedJson()
.execute();
//do a simple search
Bundle bundle = client.search()
.forResource(Patient.class)
.where(Patient.NAME.matches().value("eve"))
.execute();
//bundle will be a HAPI bundle of patients
System.out.println(bundle.getTitle());
System.out.println(bundle.getEntries().size() + " matches.");
}
}