Sidebar

How can I setup a channel to process a fixed width file with multilple lines that make up a record?

0 votes
494 views
asked Aug 8, 2014 by rich-c-2789 (16,180 points)

How can I setup a channel to process a fixed width file with multilple lines that make up a record?  

The challenge here is there are multiple rows per record, up to 12 (could be less). The file has a header row that defines the start of the file, and a footer row that defines the end of the file. Each row of every record has a start field that describes the current row (or sub record type) such as 01, 02, 09, 11, etc. There are multiple records in the dat file with each record starting with row 01. Each of the 12 rows is clearly defined, but are different from one another. So, row 02 is different and has different widths and fields from the rest of the rows.

In this case I need to convert each record to an hl7 message.

 

1 Answer

0 votes

See this sample channel:

https://www.qvera.com/files/qie_Sample_Fixed_Width_Multi_Line_Record.zip

Below is the scipt in the source node.  Note this could be improved to better handle possible errors like invalid path to the source etc.  Additionally, it might be better to simplify the core of this script to pull the paths from the cannel cache and move record mappings into system variables etc.  For that matter the functions to process each record type could extracted to published functions.  But here is a proof of concept.  Feel free to improve this channel and submit the improved version for bragging rights.

 

var hl7record; //This could be an hl7 messages

var postRecord = false;

function processHeader(line) {
// do header processing
}

function processNewReocrd(line) {
//add the previosly built hl7Record to the channel for further processing
if (postRecord) {
qie.info('hl7record: ' + hl7record);
qie.addInboundMessage('' + hl7record, 'originalFilename');
}
//start new hl7record
postRecord = false;
hl7record = qie.createHL7Message();

//get the fields from the line read in
var patientId = StringUtils.substring(line, 2, 10);
// var firstName = StringUtils.substring(line, 10, 25);
// var lastName = StringUtils.substring(line, 25, 40);
// etc.

// put the data into the hl7 message
hl7record.setNode('PID-3', patientId);
}

// function process02and03(line) {
// 1. grab data from line
// 2. put data into hl7record
// }

/*
* This function identifies the record types
*/
function processLine(line) {
var recordType = StringUtils.substring(line, 0, 2);
var isBeginNewRecord = StringUtils.equals(recordType, "01");
var isHeader = StringUtils.equals(recordType, "he");
var isFooter = StringUtils.equals(recordType, "fo");
if (isHeader) {
processHeader(line);
} else if (isFooter) {
// add the last record to the channel
qie.addInboundMessage('' + hl7record, 'originalFilename');
// processFooter(line); //define this function
} else if (isBeginNewRecord) {
// Note this is the line starting with 01
processNewReocrd(line);
} else if (StringUtils.equals(recordType, "02") || StringUtils.equals(recordType, "03")) {
postRecord = true;
qie.info('process 02 and 03 recordType');
// process02and03(line); //define this function.
} else {
qie.info('process all other record types');
// processOther(line); //define this function
} //etc.
}

function readLinesFrom(file) {
var it = org.apache.commons.io.FileUtils.lineIterator(file, "UTF-8");
try {
while (it.hasNext()) {
var line = it.nextLine();
processLine(line);
}
} finally {
org.apache.commons.io.LineIterator.closeQuietly(it);
}
}

var sourceDir = new java.io.File("C:\\TEST");
var extensions = ["txt", "jsp"];

var files = org.apache.commons.io.FileUtils.iterateFiles(sourceDir, extensions, true);
while (files.hasNext()) {
var file = files.next();
readLinesFrom(file);
//delete or archive file as needed
qie.deleteFile(file);
// qie.moveFile(file, 'path');
}
 
answered Aug 8, 2014 by rich-c-2789 (16,180 points)
...