Search in sources :

Example 1 with Hl7InputStreamMessageStringIterator

use of ca.uhn.hl7v2.util.Hl7InputStreamMessageStringIterator 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 Hl7InputStreamMessageStringIterator

use of ca.uhn.hl7v2.util.Hl7InputStreamMessageStringIterator in project hl7v2-fhir-converter by LinuxForHealth.

the class HL7ToFHIRConverter method getHl7Message.

private static Message getHl7Message(String data) {
    Message hl7message = null;
    try (InputStream ins = IOUtils.toInputStream(data, StandardCharsets.UTF_8)) {
        Hl7InputStreamMessageStringIterator iterator = new Hl7InputStreamMessageStringIterator(ins);
        // only supports single message conversion.
        if (iterator.hasNext()) {
            hl7message = hparser.getParser().parse(iterator.next());
        }
    } catch (HL7Exception e) {
        throw new IllegalArgumentException("Cannot parse the message.", e);
    } catch (IOException ioe) {
        throw new IllegalArgumentException("IOException encountered.", ioe);
    }
    try {
        if (hl7message != null) {
            String messageStructureInfo = hl7message.printStructure();
            StringBuilder output = new StringBuilder();
            String[] messageStructureInfoLines = messageStructureInfo.split(System.getProperty("line.separator"));
            for (String line : messageStructureInfoLines) {
                if (!line.contains("|")) {
                    output.append(line);
                } else {
                    int firstDash = line.indexOf("-");
                    output.append(line.substring(0, firstDash + 5));
                }
                output.append("\n");
            }
            if (output.length() > 0) {
                LOGGER.info("HL7_MESSAGE_STRUCTURE=\n{}", output);
            }
        }
    } catch (HL7Exception e) {
        throw new IllegalArgumentException("Error printing message structure.", e);
    }
    return hl7message;
}
Also used : Message(ca.uhn.hl7v2.model.Message) Hl7InputStreamMessageStringIterator(ca.uhn.hl7v2.util.Hl7InputStreamMessageStringIterator) InputStream(java.io.InputStream) HL7Exception(ca.uhn.hl7v2.HL7Exception) IOException(java.io.IOException)

Example 3 with Hl7InputStreamMessageStringIterator

use of ca.uhn.hl7v2.util.Hl7InputStreamMessageStringIterator 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

Message (ca.uhn.hl7v2.model.Message)3 Hl7InputStreamMessageStringIterator (ca.uhn.hl7v2.util.Hl7InputStreamMessageStringIterator)3 InputStream (java.io.InputStream)3 FHIRContext (io.github.linuxforhealth.fhir.FHIRContext)2 HL7HapiParser (io.github.linuxforhealth.hl7.parsing.HL7HapiParser)2 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)2 Bundle (org.hl7.fhir.r4.model.Bundle)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 HL7Exception (ca.uhn.hl7v2.HL7Exception)1 IOException (java.io.IOException)1