Code: Filtering out messages you don’t want to forward

Remember that the code we supply here is intended as a basis for you to adapt, rather than a boilerplate solution.

Please contact support at support@interfaceware.com if you need further help.

Sample Code [top]

You can download the Message_Filtering_Filter.zip project file (code and six test messages) or copy the code from below.

Code for main():

local filter = require 'message_filtering'

function main(Data)
   -- Parse the HL7 message
   local Msg, Name = hl7.parse{vmd = 'example/demo.vmd', data = Data}
   local Out       = db.tables{vmd = 'example/demo.vmd', name=Name}
   
   -- Filter out unwanted messages
   if not filter.filterMessage(Msg) then
   -- Map the complete message      
   -- Transform the message      
   -- Write the data to a database
   end
end

Code for the module:

-- this module contains functions customized for this channel only
-- so we named it after the channel we used = unique name (and obvious)

local filter ={}

function filter.filterMessage(Msg) 
   if Msg:nodeName() == 'Catchall' then
      iguana.logError('Unexpected message type.')
      return true
      -- use your own filter conditions and error messages
      elseif Msg.MSH[3][1]:nodeValue():lower() == 'interfaceware' then      
      iguana.logError('Rejecting test message from iNTERFACEWARE.')
      return true
      elseif Msg.PID[5][1][1][1]:nodeValue():lower() == 'addams' then      
      iguana.logError('No thing shall lurch or fester here!')
      return true
   end
   return false
end

return filter

Using the code [top]

  • This code would usually be used in a Filter component script
  • Use your own VMD file that contains the structure for your message(s)
  • Because the module contains functions customized for this channel only, we recommend indicating it is private by naming the module after the channel:
    • This gives you an unique name that is easy to remember
    • The unique name prevents the module from being “accidentally discovered” and re-used
    • This module should only contain code unique to this module
  • Adapt the conditions in filter.filterMessage() (in the module) to match your requirements

How it works [top]

  • The if statement in main() uses the filter.filterMessage() function to test for messages to be filtered out
  • The filter.filterMessage() function does the following:
    • It filters out messages that match specified conditions
    • It logs a meaningful error in each case
    • It returns true (boolean) if a message should be filtered (false if it is ok to transmit)

Best Practices [top]

  • Make error messages as clear as possible, ideally they should be obvious to all users
  • Use simple and clear conditions in the filter.filterMessage() function
    • Add a comment if the reason for the condition is not obvious
    • You might want to split a compound (or’ed) condition into several single conditions to make the code easier to understand (this is a matter of style sometimes a compound condition is clearer)

What not to do [top]

  • Use obscure error messages

Leave A Comment?