Announcement

Collapse
No announcement yet.

Move OBR/OBX/NTE to end of message

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Move OBR/OBX/NTE to end of message

    I'm trying to move OBR/OBX/NTE to end of message if OBX.5.1 == 'Canceled'
    NTE will always exist below OBX when OBX.5.1 == 'Canceled'

    The message will contain multiple OBR/OBX groups.

    I searched around and see appendChild() will move to end of message, but having no luck. Can someone help with this?

  • #2
    Code:
    function ObrGroup(x) {
    	this.isCancelled = false;
    	this.xmlList = x;
    	
    	this.cancel = function () {
    		if (!this.isCancelled) {
    			cancelled.push(this);
    			this.isCancelled = true;
    		}
    	}
    	this.complete = function() {
    		if (!this.isCancelled) {
    			reordered += this.xmlList;
    		}
    	}
    	this.add = function(x) {
    		this.xmlList += x;
    	}
    }
    
    var reordered = new XMLList();
    var cancelled = [];
    var groupFound = false;
    var obrGroup;
    var kids = msg.children();
    
    for (var i = 0; i < kids.length(); i++) {
    	if (groupFound) {
    		switch(kids[i].localName()) {
    			case 'OBX':
    				if (kids[i]['OBX.5']['OBX.5.1'].toString() == 'Cancelled') {
    					obrGroup.cancel();
    				}
    				// no break
    			case 'NTE':
    				obrGroup.add(kids[i]);
    				break;
    			case 'OBR':
    				obrGroup.complete();
    				obrGroup = new ObrGroup(kids[i]);
    				break;
    			default:
    				obrGroup.complete();
    				reordered += kids[i];
    				groupFound = false;
    		}
    	}
    	else if (kids[i].localName() == 'OBR') {
    		obrGroup = new ObrGroup(kids[i]);
    		groupFound = true;
    	}
    	else {
    		reordered += kids[i];
    	}
    }
    if (groupFound) {
    	obrGroup.complete();
    }
    cancelled.forEach(function(obrGroup) {
    	reordered += obrGroup.xmlList;
    });
    if (cancelled.length > 0) {
    	msg.setChildren(reordered);
    }

    Comment


    • #3
      Thank you Agermano! This worked perfectly.

      Comment


      • #4
        wow! I never used 'this' in my life..Thanks for teaching.
        HL7v2.7 Certified Control Specialist!

        Comment


        • #5
          Originally posted by siddharth View Post
          wow! I never used 'this' in my life..Thanks for teaching.
          That is how you do classes in the version of javascript supported by mirth. Newer versions actually have a 'class' keyword. My solution works, but I actually didn't do it "correctly."

          This:
          Code:
          function ObrGroup(x) {
          	this.isCancelled = false;
          	this.xmlList = x;
          	
          	this.cancel = function () {
          		if (!this.isCancelled) {
          			cancelled.push(this);
          			this.isCancelled = true;
          		}
          	}
          	this.complete = function() {
          		if (!this.isCancelled) {
          			reordered += this.xmlList;
          		}
          	}
          	this.add = function(x) {
          		this.xmlList += x;
          	}
          }
          Should have been this:
          Code:
          function ObrGroup(x) {
          	this.isCancelled = false;
          	this.xmlList = x;
          }
          
          ObrGroup.prototype.cancel = function () {
          	if (!this.isCancelled) {
          		cancelled.push(this);
          		this.isCancelled = true;
          	}
          }
          
          ObrGroup.prototype.complete = function() {
          	if (!this.isCancelled) {
          		reordered += this.xmlList;
          	}
          }
          
          ObrGroup.prototype.add = function(x) {
          	this.xmlList += x;
          }
          Which basically allows ObrGroup objects to share the functions instead of each having its own copy. I was in a hurry, and still learning myself

          Comment


          • #6
            Originally posted by jg12345 View Post
            I'm trying to move OBR/OBX/NTE to end of message if OBX.5.1 == 'Canceled'
            NTE will always exist below OBX when OBX.5.1 == 'Canceled'

            The message will contain multiple OBR/OBX groups.

            I searched around and see appendChild() will move to end of message, but having no luck. Can someone help with this?
            How do I move entire group ORC/OBR/OBX/NTE to end of message if OBX.5.1 == 'Canceled'. Script currently ignores ORC.

            Comment


            • #7
              I didn't test it, but give this a try. I bolded the changes.

              Code:
              // ...
              for (var i = 0; i < kids.length(); i++) {
              	if (groupFound) {
              		switch(kids[i].localName()) {
              			case 'OBX':
              				if (kids[i]['OBX.5']['OBX.5.1'].toString() == 'Cancelled') {
              					obrGroup.cancel();
              				}
              				// no break
              			case 'OBR':
              			case 'NTE':
              				obrGroup.add(kids[i]);
              				break;
              			case 'ORC':
              				obrGroup.complete();
              				obrGroup = new ObrGroup(kids[i]);
              				break;
              			default:
              				obrGroup.complete();
              				reordered += kids[i];
              				groupFound = false;
              		}
              	}
              	else if (kids[i].localName() == 'ORC') {
              		obrGroup = new ObrGroup(kids[i]);
              		groupFound = true;
              	}
              	else {
              		reordered += kids[i];
              	}
              }
              // ...

              Comment


              • #8
                That worked perfect.. thank-you!

                Comment

                Working...
                X