batch.lua

Verified
Added by iNTERFACEWARE

A module to help processing batched HL7 messages

Source Code
local batch ={}

function batch.splitBatch(Data)
   -- strip batch info off Data
   local a = Data:split('\r')    
   Data=a[3]
   trace(Data)
   for i=4,#a-2 do
      Data=Data..'\r'..a[i]
   end
   trace(Data)
 
   -- split Data into messages
   local delimiter='MSH|^~\\&|'
   local b=Data:split(delimiter)
 
   -- add MSH segment info
   trace(b)
   for i=2,#b do 
      b[i-1]=delimiter..b[i]
   end
   b[#b]=nil -- delete dup msg
   trace(b)
 
   -- SOME IDEAS THAT COULD BE USEFUL
 
   -- global variable to count messages
   MsgCnt = #b
   trace('Messages Count '..MsgCnt)
 
   -- globals for batch segment info
   FHS=a[1]:split('|')
   BHS=a[2]:split('|')
   FTS=a[#a]:split('|') 
   BTS=a[#a-1]:split('|') 
 
   return b
end
 
function batch.convertTerminators(Data)
   Data = Data:gsub('\r\n','\r')
   return Data:gsub('\n','\r')
end

return batch
Description
A module to help processing batched HL7 messages
Usage Details

This module uses the splitBatch() function to convert a batch of HL7 messages into an array of HL7 messages. It does this by removing the batch header and splitting the batch separate messages. It also contains convertTerminators() which can be used to clean up line terminators by converting them all to “\r” (solves the problem of different terminators from Linux/Unix VS Widows systems).

How to use batch.lua:

  • Use a Filter or To Translator script
    Note: You could place the code From LLP script, but it is not recommended
  • Read batched messages from the queue
  • Split batches into an array of HL7 messages using the batch.splitBatch() function
  • Optionally you can use batch.convertTerminators() to clean up line terminators
    Note: This function just changes all terminators to “\r”

Here is some sample code for main():

 

-- this code follows best practice by using local
-- functions first followed by main() at the end

require 'stringutil'
local batch = require 'batch'

local function MapData(Msg)
   local msg, name = hl7.parse{vmd='example/demo.vmd', data= Msg}
   local out = db.tables{vmd='example/demo.vmd', name=name}
   --[[ Do some mapping and 'return' 
   result to calling function]]   
   return out
end

local function processMsg(Msg)
   local Tables = MapData(Msg) 
   --[[  Some data can be written to database
   conn.merge{data=Tables,live=true}]]   
   return 
end

conn = db.connect{
   api=db.MY_SQL,
   name='test', 
   user='',
   password='',
   live=true}
 
function main(Data)
   -- convert non-conformant terminators to "\r"
   Data = batch.convertTerminators(Data)
 
   -- split batch into array of messages
   local Msgs = batch.splitBatch(Data)
 
   -- process messages
   for i=1,#Msgs do
      processMsg(Msgs[i])
   end
end