Search in sources :

Example 1 with HL7ToFHIRConverter

use of io.github.linuxforhealth.hl7.HL7ToFHIRConverter in project hl7v2-fhir-converter by LinuxForHealth.

the class Hl7CustomMessageTest method getBundleEntryFromHL7Message.

// Need custom convert sequence with options that turn off FHIR validation.
private static List<BundleEntryComponent> getBundleEntryFromHL7Message(String hl7message) {
    // Testing loading of config which happens once per instantiation
    HL7ToFHIRConverter ftv = new HL7ToFHIRConverter();
    // Need custom options that turn off FHIR validation.
    String json = ftv.convert(hl7message, OPTIONS);
    assertThat(json).isNotNull();
    FHIRContext context = new FHIRContext();
    IBaseResource bundleResource = context.getParser().parseResource(json);
    assertThat(bundleResource).isNotNull();
    Bundle b = (Bundle) bundleResource;
    return b.getEntry();
}
Also used : FHIRContext(io.github.linuxforhealth.fhir.FHIRContext) HL7ToFHIRConverter(io.github.linuxforhealth.hl7.HL7ToFHIRConverter) Bundle(org.hl7.fhir.r4.model.Bundle) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource)

Example 2 with HL7ToFHIRConverter

use of io.github.linuxforhealth.hl7.HL7ToFHIRConverter 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 3 with HL7ToFHIRConverter

use of io.github.linuxforhealth.hl7.HL7ToFHIRConverter in project hl7v2-fhir-converter by LinuxForHealth.

the class FHIRConverterRunFile method main.

public static void main(String[] args) throws IOException {
    String inputFileName = System.getProperty("hl7.input.file");
    if (inputFileName == null) {
        System.out.println("Java property hl7.input.file not found");
        return;
    }
    File inputFile = new File(inputFileName);
    if (!inputFile.exists()) {
        System.out.println("Input file " + inputFile + " not found");
        return;
    }
    String outputFolderName = System.getProperty("hl7.output.folder");
    File outputFolder = null;
    if (outputFolderName != null) {
        outputFolder = new File(outputFolderName);
        if (!outputFolder.exists()) {
            System.out.println("Output folder " + outputFolderName + " not found");
            return;
        }
        if (!outputFolder.isDirectory()) {
            System.out.println("Output folder " + outputFolderName + " not a directory");
            return;
        }
    }
    System.out.println("Converting file: " + inputFile);
    HL7ToFHIRConverter ftv = new HL7ToFHIRConverter();
    ConverterOptions options = new Builder().withBundleType(BundleType.COLLECTION).withProperty("TENANT", "tenantid").withValidateResource().withPrettyPrint().build();
    String json = ftv.convert(inputFile, options);
    if (json == null)
        json = "Unable to convert the HL7 file - see logs";
    System.out.println("----------------");
    System.out.println(json);
    System.out.println("----------------");
    if (outputFolderName != null) {
        int dot = inputFileName.lastIndexOf('.');
        int pathSep = inputFileName.lastIndexOf(File.separator);
        String outfilename = outputFolderName + inputFileName.substring(pathSep, dot) + ".json";
        System.out.println("Writing results to " + outfilename);
        BufferedWriter writer = new BufferedWriter(new FileWriter(outfilename));
        writer.write(json);
        writer.close();
    }
}
Also used : HL7ToFHIRConverter(io.github.linuxforhealth.hl7.HL7ToFHIRConverter) ConverterOptions(io.github.linuxforhealth.hl7.ConverterOptions) Builder(io.github.linuxforhealth.hl7.ConverterOptions.Builder) FileWriter(java.io.FileWriter) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Aggregations

HL7ToFHIRConverter (io.github.linuxforhealth.hl7.HL7ToFHIRConverter)3 HL7Exception (ca.uhn.hl7v2.HL7Exception)1 FHIRContext (io.github.linuxforhealth.fhir.FHIRContext)1 ConverterOptions (io.github.linuxforhealth.hl7.ConverterOptions)1 Builder (io.github.linuxforhealth.hl7.ConverterOptions.Builder)1 HL7HapiParser (io.github.linuxforhealth.hl7.parsing.HL7HapiParser)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)1 Bundle (org.hl7.fhir.r4.model.Bundle)1