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();
}
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;
}
}
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();
}
}
Aggregations