web stats
Evaulating SWITCH versus IF Statement - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 06-20-2018, 02:42 PM
Geekn Geekn is offline
Mirth Newb
 
Join Date: May 2018
Posts: 25
Geekn is on a distinguished road
Default Evaulating SWITCH versus IF Statement

Since Mirth doesn't seem to have interactive debugger, can anyone explain why the following code in a destination transform will only log from the IF statement and not from the SWITCH.

Code:
for each (node in msg['PID'].children()) 
{
	if(node.name() == "PID.3")
        {
		logger.info("IF Succeeded");
	}
	 
	switch(node.name())
	{
		case "PID.3": 
		     logger.info("SWITCH Succeeded"); // This line never logs
		     break;
	}
}
Using Mirth 3.6
Reply With Quote
  #2  
Old 06-20-2018, 09:38 PM
aTom aTom is offline
OBX.3 Kenobi
 
Join Date: Feb 2016
Location: Lake Geneva Area
Posts: 115
aTom is on a distinguished road
Default

Hi,

It fails for the same reason the following statement will fail:

Code:
if(node.name() === "PID.3") {
  logger.info("IF succeeded"); // won't work
}
What I understand is that node.name() is a java.lang.String while "PID.3" is a javascript string. The == statement returns true because it makes some implicit conversion (and I suppose at the same time Rhino converts Java String from/to JS string ).

If you want to use SWITCH-CASE statements then you have to explicitely convert your String/string to the same object type. Same with my example :

Code:
if(String(node.name()) === 'PID.3') {
  logger.info("IF succeeded"); // works when both are JS strings
}
if(node.name() === new java.lang.String('PID.3')) {
  logger.info("IF succeeded"); // works when both are Java Strings
}
__________________
Tom
Reply With Quote
  #3  
Old 06-21-2018, 05:46 AM
Geekn Geekn is offline
Mirth Newb
 
Join Date: May 2018
Posts: 25
Geekn is on a distinguished road
Default

Ahh...that makes sense (have to be the exact same data type in switch). Thank you.
Reply With Quote
  #4  
Old 06-21-2018, 07:54 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 1,176
agermano is on a distinguished road
Default

The conclusion is correct, but the cause is not. node.name() returns a javascript object containing both a uri for the namespace (which is empty here) and the local name. Any of the following will produce a javascript string, which should work in the switch.

node.name().toString()
node.name().localName
node.localName()
Reply With Quote
  #5  
Old 06-21-2018, 12:27 PM
Geekn Geekn is offline
Mirth Newb
 
Join Date: May 2018
Posts: 25
Geekn is on a distinguished road
Default

Testing a few of these, the first of which actually logs message but fails to run the channel.

Quote:
Originally Posted by agermano View Post
node.name().localName
node.localName()
This one logs name of the node but generates an error during the source transformation.

HTML Code:
111: // Initializes a patient based on a 2x version of an HL7 document
112: function Initialize2xPatient(patient)
113: {
114: 	for each (node in msg['PID'].children()) 
115: 	{
116: 		logger.info(node.name().localName);
117: 		switch(node.name().localName)
118: 		{
119: 			case 'PID.3': 
120: 				if (node['PID.3.5'] == "MR")
LINE NUMBER:	116
DETAILS:	TypeError: Cannot call property name in object 
function () {...}. It is not a function, it is "string".
Using node.localName() has same result as above (writes to log but throws error)

HTML Code:
111: // Initializes a patient based on a 2x version of an HL7 document
112: function Initialize2xPatient(patient)
113: {
114: 	for each (node in msg['PID'].children()) 
115: 	{
116: 		logger.info(node.localName());
117: 		switch(node.localName())
118: 		{
119: 			case 'PID.3': 
120: 				if (node['PID.3.5'] == "MR")
LINE NUMBER:	116
DETAILS:	TypeError: Cannot find function localName in object 
function () {...}.
Using node.localName (without parenthesis) does not cause error but nothing is logged out...so strange.

HTML Code:
function Initialize2xPatient(patient)
{
	for each (node in msg['PID'].children()) 
	{
		logger.info(node.localName);
		switch(node.localName)
		{
			case 'PID.3': 
				if (node['PID.3.5'] == "MR")
				{
					patient.ID = node['PID.3.1']; 
					patient.ID_AssigningAuthority = node['PID.3.4']; 
				}

Last edited by Geekn; 06-21-2018 at 12:34 PM.
Reply With Quote
  #6  
Old 06-21-2018, 02:08 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 1,176
agermano is on a distinguished road
Default

I have no idea why you are getting the errors... it works for me. What version of mirth are you on?

The 3rd one doesn't do anything because it is trying to access a child element of node named 'localName' that doesn't exist, so it comes back with an empty xml object that won't match any of your switch cases.

You can also try e4x filtering for a possibly cleaner approach.

Code:
// find the pid.3 where pid.3.5 == 'MR'
// (will return an XMLList if there is more than 1 match)
var pid3 = msg['PID']['PID.3'].(function() { return new XML(this)['PID.3.5'] == "MR"; }());
patient.ID = pid3['PID.3.1'].toString(); 
patient.ID_AssigningAuthority = pid3['PID.3.4'].toString();
Reply With Quote
  #7  
Old 06-21-2018, 02:17 PM
Geekn Geekn is offline
Mirth Newb
 
Join Date: May 2018
Posts: 25
Geekn is on a distinguished road
Default

I restarted the mirth server and it works as expected...go figure. I'll take a look at the e4x approach to see if that works better for what I'm trying to accomplish which is creating a set of objects that are available do downstream transforms that encapsulate the complexity of finding the values in the message (ex: Patient, Provider).

Last edited by Geekn; 06-21-2018 at 02:21 PM.
Reply With Quote
  #8  
Old 06-26-2018, 01:11 PM
Geekn Geekn is offline
Mirth Newb
 
Join Date: May 2018
Posts: 25
Geekn is on a distinguished road
Default

Would you mind explaining why you had to put that in a function instead of writing the E4X like the following (mine doesn't work by the way, but documentation would seem to imply that it should). It's tough figuring this stuff out in mirth without a debugger...ugh!

Code:
var pid3 = this.MessageData['PID']['PID.3'].(['PID.3.5'] == 'MR');
Reply With Quote
  #9  
Old 06-27-2018, 06:15 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 1,176
agermano is on a distinguished road
Default

I did it because that's the only way that it works

You can get an interactive Rhino shell outside of Mirth that will let you play around with e4x.

https://developer.mozilla.org/en-US/...ts/Rhino/Shell

The problem is in the definition of how e4x filtering works. It creates a javascript 'with' context on the nodes which you are trying to filter. That context does not allow bracket notation. This causes an issue because our xml elements have dots in the name.

The function lets you get at the object the with context was created on so that you can access child elements using bracket notation.
Reply With Quote
  #10  
Old 06-27-2018, 08:53 AM
Geekn Geekn is offline
Mirth Newb
 
Join Date: May 2018
Posts: 25
Geekn is on a distinguished road
Default

You're the king dude! Thanks. So if I have multiple conditions for finding the PID3 it would look something like this which will give me the PID if it equals "MR" or any other PID if it's not the social security number or drivers license PID:

Code:
var pid3 = this.MessageData['PID']['PID.3'].(function() 
		{ 
			var currentPID = new XML(this);
			if (currentPID['PID.3.5'] == "MR")
				return currentPID;
			else if (currentPID['PID.3.4']['PID.3.4.2'] != this.SSN_AUTHORITY_ID && currentPID['PID.3.5'] != 'DL')
				return currentPID;
			else
				return null;
		}());

Last edited by Geekn; 06-27-2018 at 09:42 AM.
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 03:57 AM.


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