Search in sources :

Example 1 with HL7HapiParser

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);
}
Also used : FHIRContext(io.github.linuxforhealth.fhir.FHIRContext) BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) Message(ca.uhn.hl7v2.model.Message) Hl7InputStreamMessageStringIterator(ca.uhn.hl7v2.util.Hl7InputStreamMessageStringIterator) HL7HapiParser(io.github.linuxforhealth.hl7.parsing.HL7HapiParser) InputStream(java.io.InputStream) Bundle(org.hl7.fhir.r4.model.Bundle) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) Resource(org.hl7.fhir.r4.model.Resource) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) Test(org.junit.jupiter.api.Test)

Example 2 with HL7HapiParser

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();
        }
    }
}
Also used : Message(ca.uhn.hl7v2.model.Message) HL7HapiParser(io.github.linuxforhealth.hl7.parsing.HL7HapiParser) Bundle(org.hl7.fhir.r4.model.Bundle) HL7Exception(ca.uhn.hl7v2.HL7Exception)

Example 3 with HL7HapiParser

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;
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) HL7ToFHIRConverter(io.github.linuxforhealth.hl7.HL7ToFHIRConverter) HL7HapiParser(io.github.linuxforhealth.hl7.parsing.HL7HapiParser) BufferedReader(java.io.BufferedReader) HL7Exception(ca.uhn.hl7v2.HL7Exception)

Example 4 with HL7HapiParser

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);
}
Also used : FHIRContext(io.github.linuxforhealth.fhir.FHIRContext) BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) Message(ca.uhn.hl7v2.model.Message) Hl7InputStreamMessageStringIterator(ca.uhn.hl7v2.util.Hl7InputStreamMessageStringIterator) HL7HapiParser(io.github.linuxforhealth.hl7.parsing.HL7HapiParser) InputStream(java.io.InputStream) Bundle(org.hl7.fhir.r4.model.Bundle) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) Resource(org.hl7.fhir.r4.model.Resource) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) Test(org.junit.jupiter.api.Test)

Aggregations

HL7HapiParser (io.github.linuxforhealth.hl7.parsing.HL7HapiParser)4 Message (ca.uhn.hl7v2.model.Message)3 InputStream (java.io.InputStream)3 Bundle (org.hl7.fhir.r4.model.Bundle)3 HL7Exception (ca.uhn.hl7v2.HL7Exception)2 Hl7InputStreamMessageStringIterator (ca.uhn.hl7v2.util.Hl7InputStreamMessageStringIterator)2 FHIRContext (io.github.linuxforhealth.fhir.FHIRContext)2 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)2 BundleEntryComponent (org.hl7.fhir.r4.model.Bundle.BundleEntryComponent)2 Resource (org.hl7.fhir.r4.model.Resource)2 Test (org.junit.jupiter.api.Test)2 HL7ToFHIRConverter (io.github.linuxforhealth.hl7.HL7ToFHIRConverter)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1