use of io.github.linuxforhealth.hl7.parsing.HL7HapiParser in project hl7v2-fhir-converter by LinuxForHealth.
the class FHIRConverterTest method test_adt_40_message.
@Test
// Test an example of a message with no message structure specifed
void test_adt_40_message() throws Exception {
Message hl7message = null;
// Test that an ADT A40 message with no MSH-9.3 is successfully parsed and converted.
String hl7messageString = "MSH|^~\\&|REGADT|MCM|RSP1P8|MCM|200301051530|SEC|ADT^A40|00000003|P|2.6\n" + "PID|||MR1^^^XYZ||MAIDENNAME^EVE\n" + "MRG|MR2^^^XYZ\n";
InputStream ins = IOUtils.toInputStream(hl7messageString, StandardCharsets.UTF_8);
Hl7InputStreamMessageStringIterator iterator = new Hl7InputStreamMessageStringIterator(ins);
if (iterator.hasNext()) {
HL7HapiParser hparser = new HL7HapiParser();
hl7message = hparser.getParser().parse(iterator.next());
}
String messageType = HL7DataExtractor.getMessageType(hl7message);
assertThat(messageType).isEqualTo("ADT_A40");
// Convert and check for a patient resource
String json = ftv.convert(hl7messageString, ConverterOptions.SIMPLE_OPTIONS);
FHIRContext context = new FHIRContext();
IBaseResource bundleResource = context.getParser().parseResource(json);
assertThat(bundleResource).isNotNull();
Bundle b = (Bundle) bundleResource;
assertThat(b.getType()).isEqualTo(BundleType.COLLECTION);
assertThat(b.getId()).isNotNull();
assertThat(b.getMeta().getLastUpdated()).isNotNull();
List<BundleEntryComponent> e = b.getEntry();
List<Resource> patientResource = e.stream().filter(v -> ResourceType.Patient == v.getResource().getResourceType()).map(BundleEntryComponent::getResource).collect(Collectors.toList());
assertThat(patientResource).hasSize(2);
}
use of io.github.linuxforhealth.hl7.parsing.HL7HapiParser in project hl7v2-fhir-converter by LinuxForHealth.
the class HL7MessageModel method convert.
public String convert(String message, MessageEngine engine) throws IOException {
Preconditions.checkArgument(StringUtils.isNotBlank(message), "Input Hl7 message cannot be blank");
HL7HapiParser hparser = null;
try {
hparser = new HL7HapiParser();
Message hl7message = hparser.getParser().parse(message);
Bundle bundle = convert(hl7message, engine);
return engine.getFHIRContext().encodeResourceToString(bundle);
} catch (HL7Exception e) {
throw new IllegalArgumentException("Cannot parse the message.", e);
} finally {
if (hparser != null) {
hparser.getContext().close();
}
}
}
use of io.github.linuxforhealth.hl7.parsing.HL7HapiParser in project health-patterns by LinuxForHealth.
the class HL7ToFhirProcessor method onTrigger.
/**
* This method will be called when it is scheduled to be run or when work exists in the form of flowfiles present on the input queue
*
* @param context
* information about how the processor is currently configured
* @param session
* provides a mechanism to get and create/put flowfiles
* @throws ProcessException
*/
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
FlowFile inputFlowFile = session.get();
if (inputFlowFile == null) {
// if there is no flowfile present then stop
return;
}
getLogger().info("Reading text data from FlowFile");
InputStream is = session.read(inputFlowFile);
StringBuilder inputMessage = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
String line = null;
while ((line = reader.readLine()) != null) {
inputMessage.append(line + "\n");
}
} catch (IOException e) {
getLogger().error("Error reading flowfile", e);
session.transfer(inputFlowFile, FAIL_RELATIONSHIP);
// if an error occurred reading the input flowfile then stop
return;
}
if (inputMessage == null || inputMessage.length() == 0) {
session.transfer(inputFlowFile, FAIL_RELATIONSHIP);
// if the incoming data is empty then stop
return;
}
try {
new HL7HapiParser().getParser().parse(inputMessage.toString());
} catch (HL7Exception e) {
session.transfer(inputFlowFile, HL7_NOT_DETECTED_RELATIONSHIP);
// if the input data can't be parsed as HL7 then stop
return;
}
try {
String hl7Message = inputMessage.toString();
HL7ToFHIRConverter ftv = new HL7ToFHIRConverter();
// generated a FHIR output
String fhirMessage = ftv.convert(hl7Message);
FlowFile outputFlowFile = session.clone(inputFlowFile);
try (OutputStream newFlowOutput = session.write(outputFlowFile)) {
newFlowOutput.write(fhirMessage.getBytes());
} catch (IOException e) {
getLogger().error("Error writing FHIR message to output flow", e);
session.transfer(inputFlowFile, FAIL_RELATIONSHIP);
// if the input data can't be parsed as HL7 then stop
return;
}
session.putAttribute(outputFlowFile, CoreAttributes.MIME_TYPE.key(), APPLICATION_JSON);
session.transfer(outputFlowFile, SUCCESS_RELATIONSHIP);
// remove the original flow file and stop
session.remove(inputFlowFile);
getLogger().info("Pass FHIR flowfile to success queue");
} catch (UnsupportedOperationException | IllegalArgumentException e) {
getLogger().error("Error converting HL7 data to FHIR", e);
session.transfer(inputFlowFile, FAIL_RELATIONSHIP);
// if the input data can't be converted to FHIR then stop
return;
}
}
use of io.github.linuxforhealth.hl7.parsing.HL7HapiParser in project hl7v2-fhir-converter by LinuxForHealth.
the class FHIRConverterTest method test_adt_40_message_with_adt_a39_structure_specified.
@Test
// Test an example of a message with message structure specified
void test_adt_40_message_with_adt_a39_structure_specified() throws Exception {
Message hl7message = null;
// Test that an ADT A40 message with MSH-9.3 of 'ADT_A39' is successfully parsed and converted as an ADT A40 message.
// Note that ADT_A39 is the expected structure of an ADT_A40 message.
String hl7messageString = "MSH|^~\\&|REGADT|MCM|RSP1P8|MCM|200301051530|SEC|ADT^A40^ADT_A39|00000003|P|2.6\n" + "PID|||MR1^^^XYZ||MAIDENNAME^EVE\n" + "MRG|MR2^^^XYZ\n";
InputStream ins = IOUtils.toInputStream(hl7messageString, StandardCharsets.UTF_8);
Hl7InputStreamMessageStringIterator iterator = new Hl7InputStreamMessageStringIterator(ins);
if (iterator.hasNext()) {
HL7HapiParser hparser = new HL7HapiParser();
hl7message = hparser.getParser().parse(iterator.next());
}
String messageType = HL7DataExtractor.getMessageType(hl7message);
assertThat(messageType).isEqualTo("ADT_A40");
// Convert and check for a patient resource
String json = ftv.convert(hl7messageString, ConverterOptions.SIMPLE_OPTIONS);
FHIRContext context = new FHIRContext();
IBaseResource bundleResource = context.getParser().parseResource(json);
assertThat(bundleResource).isNotNull();
Bundle b = (Bundle) bundleResource;
assertThat(b.getType()).isEqualTo(BundleType.COLLECTION);
assertThat(b.getId()).isNotNull();
assertThat(b.getMeta().getLastUpdated()).isNotNull();
List<BundleEntryComponent> e = b.getEntry();
List<Resource> patientResource = e.stream().filter(v -> ResourceType.Patient == v.getResource().getResourceType()).map(BundleEntryComponent::getResource).collect(Collectors.toList());
assertThat(patientResource).hasSize(2);
}
Aggregations