This topic contains 1 reply, has 2 voices, and was last updated by  lev 8 years, 2 months ago.

inserting OBX segements from columns in external db

  • While I am sure this is probably something staring me in the face that I am missing here, I can’t seem to tie this all together. It’s been a very long time since I’ve done programming, and it is showing! 🙂 I am trying to insert OBX’s to an existing HL7 message using data from a SQL Server source. I have two loops, nested where each works ok independently of the other, but I can’t figure out how to tie the two together; or maybe I just need to put it all in the same loop; I’m not sure. The ultimate goals is to take in an existing HL7 message, insert, in this case, five new rows (db query produces five results in this case) of repeating OBX’s BEFORE the last OBX in the original message; the last OBX repeat in the original message would be the last in the new message. Right now, my trace shows the correct number of OBX repeats, ordered/numbered ok (first loop), but the data in all five inserted OBX’s is the result of the first iteration of the second loop instead of the five rows of differing data. Finally, it takes the edited HL7 message with the inserted OBX’s and needs to send that off via https. The xxxx below are where I overwrote sensitive/private data. Here is the code I have:

    require ‘hl7util’

    function main(ToCust)

    local ToCustIn = hl7.parse{vmd=’medgis_result.vmd’,data=ToCust}
    local ToCustOut = hl7.message{vmd=’llau_result.vmd’,name=node.nodeName(ToCustIn)}

    ToCustOut:mapTree(ToCustIn)

    trace(ToCustOut)

    MapOBX(ToCustOut,OBXdata)

    net.http.put{url=’https://xxx.xxx.xxx.xxx:xxxxx/’,data=tostring(ToCustOut)}
    end

    function MapOBX(ToCustOut, OBXdata)
    local conn = db.connect{api=db.SQL_SERVER, name=’embase_dev’, user=’iguanasa’,password=’xxxxxxxxxxxx’}
    local OBXdata = conn:execute{sql=[[SELECT * FROM [dbo].[fn_get_carrier_hl7_obx](‘A’, ‘xxxxxxxxxxx’)]],live= true}
    local Lines = ToCustOut.OBX:childCount()
    local Rows = #OBXdata

    for i = Lines, Lines+Rows-1, 1 do
    ToCustOut.OBX[i+1] = ToCustOut.OBX[i]
    ToCustOut.OBX[i+1][1] = ‘0’..i+1
    ToCustOut.OBX[i][2] = ‘ST’

    for j = 1, Rows, 1 do
    ToCustOut.OBX[i][3] = OBXdata[j].obx_03
    ToCustOut.OBX[i][4] = OBXdata[j].obx_04
    ToCustOut.OBX[i][5] = OBXdata[j].obx_05
    ToCustOut.OBX[i][8] = OBXdata[j].obx_08
    ToCustOut.OBX[i][13] = OBXdata[j].obx_13

    end

    end

    trace(ToCustOut)

    end

    There are many ways to do it. One of them is to utilize two outbound objects.
    This can simplify and eliminate the need for a nested loop, which you complain about.
    First complete mappings and transformations from inbound to 1st outbound object, without paying attention to 5 additional segments.
    Next, map from 1st outbound object to 2nd all but last OBX segment.
    Now, map 5 additional OBX.
    Last, map the last OBX.

You must be logged in to reply to this topic.