web stats
Move OBR/OBX/NTE to end of message - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 03-09-2018, 03:30 PM
jg12345 jg12345 is offline
OBX.3 Kenobi
 
Join Date: Oct 2012
Posts: 105
jg12345 is on a distinguished road
Default 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?
Reply With Quote
  #2  
Old 03-11-2018, 07:06 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 1,036
agermano is on a distinguished road
Default

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);
}
Reply With Quote
  #3  
Old 03-12-2018, 07:20 AM
jg12345 jg12345 is offline
OBX.3 Kenobi
 
Join Date: Oct 2012
Posts: 105
jg12345 is on a distinguished road
Default

Thank you Agermano! This worked perfectly.
Reply With Quote
  #4  
Old 03-12-2018, 10:09 AM
siddharth siddharth is offline
Mirth Guru
 
Join Date: Feb 2013
Posts: 835
siddharth is on a distinguished road
Default

wow! I never used 'this' in my life..Thanks for teaching.
__________________
HL7v2.7 Certified Control Specialist!
Reply With Quote
  #5  
Old 03-12-2018, 01:16 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 1,036
agermano is on a distinguished road
Default

Quote:
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
Reply With Quote
  #6  
Old 05-23-2019, 11:27 AM
jg12345 jg12345 is offline
OBX.3 Kenobi
 
Join Date: Oct 2012
Posts: 105
jg12345 is on a distinguished road
Default

Quote:
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.
Reply With Quote
  #7  
Old 05-23-2019, 04:18 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 1,036
agermano is on a distinguished road
Default

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];
	}
}
// ...
Reply With Quote
  #8  
Old 05-24-2019, 06:48 AM
jg12345 jg12345 is offline
OBX.3 Kenobi
 
Join Date: Oct 2012
Posts: 105
jg12345 is on a distinguished road
Default

That worked perfect.. thank-you!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 05:11 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Mirth Corporation