Query Response ACK

Verified
Added by iNTERFACEWARE

Query a database to identify patient in incoming HL7 messages. If a patient is not found return an ACK with a "Patient does not exist" message.

Source Code
local function MakeMSH(MSH, Q)  
   MSH[3][1] = Q.MSH[5][1]
   MSH[4][1] = Q.MSH[6][1]
   MSH[5][1] = Q.MSH[3][1]
   MSH[6][1] = Q.MSH[4][1]
   MSH[10] = Q.MSH[10]
   MSH[9][1] = 'ADR'
   MSH[9][2] = 'A19'
   MSH[11][1] = 'P'
   MSH[12][1] = '2.6.1'
   MSH[7] = os.date('%Y%m%d%h%M%S')
   return MSH
end

local function AddPatient(PID, R) 
   PID[5][1] = R.LastName
   PID[5][2] = R.GivenName
   PID[2][1] = R.Id
   PID[8] = R.Sex
   return PID
end

-- you will need a MySQL database with a Patient table 
-- change name, user and password to match your DB
local function FindPatient(R, Id)
   local Q = db.query{api=db.MY_SQL, name='test',
      user='root', password='EU5992eu', live=true,
      sql = 'SELECT * FROM Patient WHERE Id ="'..Id..'"'}
   for i=1,#Q do
      AddPatient(R.Group1[i].PID, Q[i])      
   end
end

-- main() is given the original HL7 message.
function main(Data)
   local Msg = hl7.parse{vmd='query.vmd', data=Data}
   local R = hl7.message{vmd='query.vmd', name='Response'}

   if Msg:nodeName() == 'Unrecognized' then
      ack.send('Unrecognized message')
      return
   end

   MakeMSH(R.MSH, Msg)
   FindPatient(R, Msg.QRD[8][1][1])
   ack.send(R:S())
end
Description
Query a database to identify patient in incoming HL7 messages. If a patient is not found return an ACK with a "Patient does not exist" message.
Usage Details

This code reads the Patient Id incoming HL7 messages, then queries a database to see the patient exists. If the patient does not exist it returns an ACK with with a “Patient does not exist” message.

How to use the code:

  • Create a channel with an LLP Listener component
  • Either load the query_response_ACK_LLP_Listener.zip project file or paste in the code
  • Download the query.vmd file and add it to the project
  • Load the sample message from attached SampleData.txt
  • Examine the code and annotations to see how it works