4

I'm using Mirth to read HL7 messages from our DB and send them out to a client's EMR. This particular EMR requires that the OBR and OBX for the embedded PDF are formatted a specific way. If OBR.4.1 and OBR.4.2 have "0PDF^PDF Report" we need to insert "^PDFReport^PDF^Base64" into OBX.5.1, OBX.5.2 and OBX.5.3 as shown in the example below.

OBR|2||13PS061163CYT|0PDF^PDF Report|
OBX|1|ED|0PDF^PDF Report|1|^PDFReport^PDF^Base64^JVBERi0xLjMNJf////

The code we are currently using works 99% of the time, but seems to be breaking on specific report types. Especially when there are more OBR's than OBX's.

Any help to resolve this issue would be appreciated. The code we are currently using is below.

for (var i=0;i<msg['OBX'].length();i++ ){
    var Access=msg['OBR'][i]['OBR.3']['OBR.3.1'].toString()
    var Report=msg['OBX'][i]['OBX.5']['OBX.5.1'].toString()
    var ID=msg['OBR'][i]['OBR.2']['OBR.2.1'].toString()

    if(msg['OBX'][i]['OBX.3']['OBX.3.1'].toString() == Access + ".PDF"){
        msg['OBX'][i]['OBX.3']['OBX.3.1'] = "0PDF"
        msg['OBX'][i]['OBX.3']['OBX.3.2'] = "PDF Report"
        msg['OBX'][i]['OBX.5']['OBX.5.1'] = ID
        msg['OBX'][i]['OBX.5']['OBX.5.2'] = "PDFReport"
        msg['OBX'][i]['OBX.5']['OBX.5.3'] = "PDF"
        msg['OBX'][i]['OBX.5']['OBX.5.4'] = "Base64"
        msg['OBX'][i]['OBX.5']['OBX.5.5'] = Report
        i--;
    }
}
csj
  • 21,818
  • 2
  • 20
  • 26
user2109799
  • 41
  • 1
  • 2
  • Your code doesn't match your description of what you're trying to do. You say that when an OBR segment has particular values, then you want to set particular values in the OBX. However, looking at your code, it's clear that you are comparing values between the OBR and OBX. – csj Feb 26 '13 at 18:53
  • Add this question to http://area51.stackexchange.com/proposals/51758/healthcare-it. We are trying to get a Healthcare stack exchange site going, and this would be a perfect question – ChronoFish Feb 28 '13 at 01:51

1 Answers1

3

The crux of your problem is that your code assumes that OBR and OBX segments will always appear in pairs.

OBR|1|...
OBX|1|...
OBR|2|...
OBX|1|...
OBR|3|...
OBX|1|...

However, as soon as you run into a case where the numbers of OBR and OBX segments don't match, or don't appear in strictly alternating fashion, things start to break.

OBR|1|... oh oh, this obr is followed by two obx segments
OBX|1|...
OBX|2|... 
OBR|2|... oh oh, this obr isn't followed by an obx segment at all.
OBR|3|...
OBX|1|...

You need to first understand what is meant by the following lines of code.

var obrSegments = msg['OBR'];
var obxSegments = msg['OBX'];

In this example, obrSegments is an array of, you guessed it, obr segments. Similarly, obxSegments is an array of obxSegments. Neither of these arrays were constructed with any awareness of how the obx segments were positioned with respect to the obr segments.

If you can guarantee that the obr segments and obx segments in your message will always appear in strictly alternating fashion, then you can guarantee that obrSegments[i] and obxSegments[i] will always be sequential.

On the other hand, if the number of obr and obx segments isn't identical, or, even if the number IS identical, if the segments don't appear in a strictly alternating fashion, then there's no guarantee that obrSegments[i] will be immediately followed by obxSegments[i].

The phrasing of your question isn't 100% clear. However, I am inferring that whenever you examine the contents of an OBR, you then want to conditionally change the contents of ALL obx segments that immediately follow it.

I recommend something more like this.

var obrSegments = msg['OBR'];

// iterate through the OBR segments
for each (var obr in obrSegments) {

    // extract the field components that you need from this OBR
    // don't assume I've done this correctly. The text of your
    // question didn't match your example code, so I don't exactly know what you need
    var OBR4_1 = obr['OBR.4']['OBR.4.1'];
    var OBR4_2 = obr['OBR.4']['OBR.4.2'];

    // now iterate through the OBX segments that immediately follow this OBR.
    // This is a bit tricky to do, but here's how I approach the problem

    // you need an XML list of all the segments, not just the current OBR
    var segments = obr.parent().children();

    // you need to know the current OBR's index in the segments list
    var obrIndex = obr.childIndex();

    // theoretically, at this point, obr should refer to exactly the same
    // data as segments[obrIndex]

    // to do the actual OBX iteration:
    var nextIndex = obrIndex + 1;
    while (nextIndex < segments.length()) {
        var nextSegment = segments[nextIndex];
        var nextSegmentType = nextSegment.localName();

        if (nextSegmentType == 'OBX') {
            var obx = nextSegment;

            // Bearing in mind that I haven't tested this code and have just been spewing it
            // off into my web browser,
            // you can now be confident that variable obx refers to one of the obx
            // segments immediately following variable obr.

            if (OBR4_1 == blah blah blah) {
                obx['OBX.5']['OBX.5.1'] = blah blah blah;
                obx['OBX.5']['OBX.5.2'] = blah blah blah;
        }
        else {
            // looks like we've finished processing all the OBX segments for the current
            // obr, time to break out of the inner loop.
            break;
        }

        ++nextIndex;
    }
} 
csj
  • 21,818
  • 2
  • 20
  • 26
  • Exactly! That's what he was missing. It is supposed to iterate through all the OBRs in the message and extract relevant data and then move to OBX. +1 – Sid May 29 '13 at 01:42
  • 1
    If it helped someone in the real world, then we're all having a good day. – csj May 30 '13 at 06:30