Generalising

The function created in Part B is good for the particular message dealt with. We want it to work for any message dealt with, and thus must generalise it. The following code has generalised it, by allowing any number of OBR segments to exist in the message, with any number of them having a translatable OBR-4 field. To allow us to see that the translation is working, we have also called showmessage (in javascript) or MsgBox (in VBscript) at two key points in the script. This will pop up a dialogue box to allow us to keep track of the script's progress.

Language    Script
VBScript   
function translate(aEvent)

  OBRNumber = 0
  segmentno = aEvent.message.hl7.SegmentCount

  For i=0 To (segmentno -1)
    If aEvent.message.hl7.segmentbyindex(i).code = "OBR" Then
     OBRNumber = OBRNumber + 1
    End if
  Next

  For i=0 To (OBRNumber - 1)

    Err.Clear
    rcodein  = aEvent.message.hl7.segment("OBR",i).Element("4").AsString
    MsgBox "test code: " & rcodein,, "HL7Connect Development Environment"
    count=1

    While (Err.number = 0) and (count < 8)
      On Error Resume Next
        Select Case count
          Case 1
            tcodekeyin = Lex.LexGetTermByCode("urn://www.hl7connect.com/SysCodes", rcodein)
          Case 2
            MsgBox "lex key for test code: " & tcodekeyin,, "HL7Connect Development Environment"
            tcodekeyout = Lex.LexTranslate(tcodekeyin, "urn://www.hl7connect.com/LOINC", 0)
          Case 3
            tcodeout = Lex.LexGetTermRepresentation(tcodekeyout, "Code")
          Case 4
            tdescout = Lex.LexGetTermRepresentation(tcodekeyout, "Defn")
          Case 5
            aEvent.message.hl7.segment("OBR",i).element("4.1").AsString = tcodeout
          Case 6
            aEvent.message.hl7.segment("OBR",i).element("4.2").AsString = tdescout
          Case 7
            aEvent.message.hl7.segment("OBR",i).element("4.3").AsString = "HL7Connect"
        end Select
      count = count + 1
    Wend

    If Err.number <> 0 Then
      Kernel.WriteToLog DBG_WARNING, "DBERROR: " & Err.Description
    End If
  Next

End function
   
Javascript   
function translate(aEvent)
{
  var rcodein;
  var segmentno;
  var OBRNumber;
  var i;
  var m
  var tcodekeyin
  var tcodekeyout
  var tcodeout
  var tdescout

  OBRNumber=0;
  segmentno = aEvent.message.hl7.SegmentCount;

  for (i=0; i<segmentno; i++)
  {
    if (aEvent.message.hl7.segmentbyindex(i).code=="OBR")
    {
     OBRNumber++;
    }
  }
  try
  {
    for (i=0; i<OBRNumber; i++)
    {
      rcodein  = aEvent.message.hl7.segment("OBR",i).Element("4").AsString;
      showmessage("test code: " + rcodein);
      try
      {
      tcodekeyin = Lex.LexGetTermByCode("urn://www.hl7connect.com/SysCodes", rcodein);
      showmessage("lex key for test code: " + tcodekeyin);
      tcodekeyout = Lex.LexTranslate(tcodekeyin, "urn://www.hl7connect.com/LOINC", 0);
      tcodeout = Lex.LexGetTermRepresentation(tcodekeyout, "Code");
      tdescout = Lex.LexGetTermRepresentation(tcodekeyout, "Defn");
      }

      catch(e)
      {
          Kernel.WriteToLog(DBG_WARNING,'DBERROR: ' + LastErrorMessage);
          continue;
      }

        aEvent.message.hl7.segment("OBR",i).element("4.1").AsString = tcodeout;
        aEvent.message.hl7.segment("OBR",i).element("4.2").AsString = tdescout;
        aEvent.message.hl7.segment("OBR",i).element("4.3").AsString = "HL7Connect";
    }

  }

  catch(e)
  {
    Kernel.WriteToLog(DBG_WARNING,'DBERROR: ' + LastErrorMessage);
  }
  return(true);
}
   

Expanding

In most situations, we are not simply going to want to want to translate one field, but multiple. Thankfully, it is a fairly simple procedure to add another field. As illustrated in the scripts below, all that needs to be done is to copy the for blocks relating to the OBR segment and modify them for any additional segments.

You should be able to see that if we were to do this, we would have a large duplication of code. Our function could be greatly simplified by using a helper function. Let's call this function translateSegment and let it have four arguments, aEvent (the event that caused this script to be run), seg (a String specifying the segment type to be translated), segnum (an Integer representing the number of segments in the message) and elems (an Array containing the list of elements to be translated within the segment).

In addition, there is one change to the default behaviour of HL7Connect that we would like to effect. By default HL7Connect will analyse the message structure, and record its "best guess" at what it is in the MSH-9-3 element. For our purposes however, it is best to leave it at its initial value. Thus we should add two lines to the beginning of the translate function (highlighted in red).

Here are the completed scripts. Notice that here we are also translating the OBX-3 elements. Copy and paste over the old lex8.vbs OR lex8.js as appropriate to test. After saving it can be tested using the same settings in the currently open test case.

Language    Script
VBScript   
function translateSegment(aEvent, seg, segnum, elems)

  segnu_ = 0

  For i=0 To (segnum - 1)
    If aEvent.message.hl7.segmentbyindex(i).code = seg Then
     segnu_ = segnu_ + 1
    End if
  Next

  For a=0 To Ubound(elems)

    For i=0 To (segnu_ - 1)

      Err.Clear
      rcodein  = aEvent.message.hl7.segment(seg,i).Element(elems(a)).AsString
      MsgBox "test code: " & rcodein,, "HL7Connect Development Environment"
      count=1

      While (Err.number = 0) and (count < 8)
        On Error Resume Next
          Select Case count
            Case 1
              tcodekeyin = Lex.LexGetTermByCode("urn://www.hl7connect.com/SysCodes", rcodein)
            Case 2
              MsgBox "lex key for test code: " & tcodekeyin,, "HL7Connect Development Environment"
              tcodekeyout = Lex.LexTranslate(tcodekeyin, "urn://www.hl7connect.com/LOINC", 0)
            Case 3
              tcodeout = Lex.LexGetTermRepresentation(tcodekeyout, "Code")
            Case 4
              tdescout = Lex.LexGetTermRepresentation(tcodekeyout, "Defn")
            Case 5
              aEvent.message.hl7.segment(seg,i).element(elems(a) & ".1").AsString = tcodeout
            Case 6
              aEvent.message.hl7.segment(seg,i).element(elems(a) & ".2").AsString = tdescout
            Case 7
              aEvent.message.hl7.segment(seg,i).element(elems(a) & ".3").AsString = "HL7Connect"
          end Select
        count = count + 1
      Wend

      If Err.number <> 0 Then
        Kernel.WriteToLog DBG_WARNING, "DBERROR: " & Err.Description
      End If
    Next
  Next

End function

function translate(aEvent)
  
  set m = aEvent.message
  m.hl7.AddStructName = false
  
  segmentno = aEvent.message.hl7.SegmentCount

  translateSegment aEvent, "OBR", segmentno, Array("4")
  translateSegment aEvent, "OBX", segmentno, Array("3")

End function
   
Javascript   
function translateSegment(aEvent, seg, segnum, elems)
{
  var segnu_ = 0;
  var rcodein;
  var i;
  var a;
  var tcodekeyin;
  var tcodekeyout;
  var tcodeout;
  var tdescout;

  for (i=0; i<segnum; i++)
  {
    if (aEvent.message.hl7.segmentbyindex(i).code==seg)
    {
     segnu_++;
    }
  }

  for (a=0; a<elems.length; a++)
  {
    try
    {
      for (i=0; i<segnu_; i++)
      {
        rcodein  = aEvent.message.hl7.segment(seg,i).Element(elems[a]).AsString;
        showmessage("test code: " + rcodein);
        try
        {
          tcodekeyin = Lex.LexGetTermByCode("urn://www.hl7connect.com/SysCodes", rcodein);
          showmessage("lex key for test code: " + tcodekeyin);
          tcodekeyout = Lex.LexTranslate(tcodekeyin, "urn://www.hl7connect.com/LOINC", 0);
          tcodeout = Lex.LexGetTermRepresentation(tcodekeyout, "Code");
          tdescout = Lex.LexGetTermRepresentation(tcodekeyout, "Defn");
        }
        catch(e)
        {
          Kernel.WriteToLog(DBG_WARNING,'DBERROR: ' + LastErrorMessage);
          continue;
        }
        aEvent.message.hl7.segment(seg,i).element(elems[a] + ".1").AsString = tcodeout;
        aEvent.message.hl7.segment(seg,i).element(elems[a] + ".2").AsString = tdescout;
        aEvent.message.hl7.segment(seg,i).element(elems[a] + ".3").AsString = "HL7Connect";
      }
    }
    catch(e)
    {
      Kernel.WriteToLog(DBG_WARNING,'DBERROR: ' + LastErrorMessage);
    }
  }
  return;
}

function translate(aEvent)
{
  var segmentno;
  var m;
  
  m = aEvent.message;
  m.hl7.AddStructName = false;
  
  segmentno = aEvent.message.hl7.SegmentCount;

  translateSegment(aEvent, "OBR", segmentno, new Array("4"));
  translateSegment(aEvent, "OBX", segmentno, new Array("3"));

  return(true);
}
   


© Kestral Computing P/L 2000-2010.