in fhir/src/main/java/org/acme/fhir/Routes.java [45:128]
public void configure() {
onException(ProtocolException.class)
.handled(true)
.log(LoggingLevel.ERROR,
"Error connecting to FHIR server with URL:{{camel.component.fhir.server-url}}, please check the application.properties file ${exception.message}")
.end();
onException(HL7Exception.class)
.handled(true)
.log(LoggingLevel.ERROR, "Error unmarshalling HL7 data ${exception.message}")
.end();
onException(ResourceNotFoundException.class)
.handled(true)
.choice().when(simple("${header.id} != null"))
.setBody(simple("Patient with id ${header.id} not found"))
.otherwise()
.setBody(simple("FHIR Server ${{camel.component.fhir.server-url}} not found"))
.end()
.setHeader(Exchange.HTTP_RESPONSE_CODE).constant(404);
// Must set FhirContext to ensure native mode support
FhirJsonDataFormat fhirJson = new FhirJsonDataFormat();
fhirJson.setFhirContext(fhirContext);
// Simple REST API to create / read patient data
rest("/api/patients")
.post()
.to("direct:patientUpload")
.get("/{id}")
.to("direct:getPatient");
// Processes uploaded patient details
from("direct:patientUpload")
// unmarshall file to hl7 message
.unmarshal().hl7()
.process(exchange -> {
ORU_R01 msg = exchange.getIn().getBody(ORU_R01.class);
final PID pid = msg.getPATIENT_RESULT().getPATIENT().getPID();
String surname = pid.getPatientName()[0].getFamilyName().getFn1_Surname().getValue();
String name = pid.getPatientName()[0].getGivenName().getValue();
String patientId = msg.getPATIENT_RESULT().getPATIENT().getPID().getPatientID().getCx1_ID().getValue();
Patient patient = new Patient();
patient.addName().addGiven(name);
patient.getNameFirstRep().setFamily(surname);
patient.setId(patientId);
exchange.getIn().setBody(patient);
})
// marshall to JSON for logging
.marshal(fhirJson)
.convertBodyTo(String.class)
.log("Inserting Patient: ${body}")
// create Patient in our FHIR server
.to("fhir://create/resource?inBody=resourceAsString")
// log the outcome
.log("Patient created successfully: ${body}")
// Return a JSON response to the client
.process(exchange -> {
Message message = exchange.getMessage();
MethodOutcome methodOutcome = message.getBody(MethodOutcome.class);
IParser parser = fhirContext.newJsonParser();
String response = parser.encodeResourceToString(methodOutcome.getResource());
message.setBody(response);
message.setHeader(Exchange.CONTENT_TYPE, "application/json");
});
// Retrieves a patient
from("direct:getPatient")
.process(exchange -> {
String patientId = exchange.getMessage().getHeader("id", String.class);
IdType iIdType = new IdType(patientId);
exchange.getMessage().setHeader("CamelFhir.id", iIdType);
})
.to("fhir://read/resourceById?resourceClass=Patient")
.process(exchange -> {
Message message = exchange.getMessage();
Patient patient = message.getBody(Patient.class);
IParser parser = fhirContext.newJsonParser();
String response = parser.encodeResourceToString(patient);
message.setBody(response);
message.setHeader(Exchange.CONTENT_TYPE, "application/json");
});
}