HL7 to HL7

Modify the Birthdate

We have a great secret weapon for parsing arbitrary dates and times: tt’s a fuzzy date/time parser library which can parse many date formats automatically.

We’ll need to have require('dateparse') in place:

require 'dateparse'

Then this code snippet does the trick:

os.date() is part of the standard Lua library. For more information on the fuzzy date/time parser, see the wiki.

If you have gotten out of sync with us up until this point then here is the code to get you caught up:

require 'split'
require 'zsegment'
require 'diff'
require 'hl7util'
require 'dateparse'

local function trace(a,b,c,d) return end

function main(Data)
   local Orig = hl7.parse  {vmd = 'transform.vmd', data = Data}

   if Orig:nodeName() == 'Catchall' then
      iguana.logInfo('Filtered '..Orig.MSH[9][1]..'^'..Orig.MSH[9][2])
      return
   end

   local Out  = hl7.message{vmd = 'transform.vmd', name = Orig:nodeName()}

   Out:mapTree(Orig)
   local Copy = zsegment.copyZSegments(Data, Out:S())
   CheckTransform(Data:StripLastReturns(), Copy:StripLastReturns())

   AlterMSH(Out.MSH)
   local PID = hl7util.findSegment(Out, FindPID)
   if PID then
      AlterPID(PID)   
   end

   trace(Out)
   local Diff = diff.Compare(Orig:S(), Out:S(), 'transform.vmd')
   local DataOut = Out:S()
   DataOut = zsegment.copyZSegments(Data, DataOut)
   DataOut = DataOut:StripLastReturns()
   trace(DataOut)
   queue.push{data = DataOut}
end

function FindPID(Segment)
   if Segment:nodeName() == 'PID' then
      return true
   end
end

function AlterPID(PID)
   local Date = dateparse.parse(PID[7]:nodeValue())
   PID[7][1] = os.date('%Y%m%d', Date)
   return PID
end

function AlterMSH(MSH)
   MSH[3][1] = 'Acme'
   MSH[4][1] = 'Lab'
   return MSH
end

function CheckTransform(Orig, Copy)
   if Orig ~= Copy then
      trace(Orig)
      trace(Copy)
      error('Copy of HL7 message does not match the original')
   end   
end

function string.StripLastReturns(S)
   -- strip return(s) "\r" & "\n" from the end of a string
   local i = #S
   while S:byte(i) == 10 or S:byte(i) == 13 do
      i = i - 1
   end
   return S:sub(1,i)
end