mirthmirth-connect

Mirth: Evaulating SWITCH versus IF Statement


I'm using a product called Mirth (version 3.6) to execute transform code. It doesn't seem to have an interactive debugger so can anyone explain why the following code in a destination transform will only log from the IF statement and not from the SWITCH statement? Works perfectly from regular java running in Eclipse but this code is executing inside a JavaScript engine that Mirth uses (Rhino if I'm not mistaken).

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;
    }
}

Solution

  • This is a common issue with Mirth Connect since it uses Rhino as it's engine (uses java as well as javascript). The Switch statement doesn't work with a Java String object The simple solution is to convert any unknown string type to a native JS string by concatenating an empty string to the questionable string (i.e. str + '')

    // See in action
    for each (node in msg['PID'].children()) 
    {
        var nodeName = node.name();
        if (nodeName != "PID.3")
        {
           continue;
        }
        var type = typeof(nodeName);
    
        logger.debug('NodeName: ' + nodeName + '; Type: ' + type);
        if (type == 'object') {
            logger.debug('Type is object');
            if (nodeName instanceof String) {
                logger.debug('Object is Java String instance');
            } else {
                logger.debug('Object is not Java String instance');
            }
        } else if (type === 'string') {
            logger.debug('Type is JS string.');
        } else {
            logger.debug('Type is not object or string. Type: ' + type);
        }
    
        // Works with String object or JS string
        if (nodeName == "PID.3")
        {
           logger.info("IF Succeeded");
        }
    
        // Only works with JS string
        switch (nodeName)
        {
            case "PID.3": 
                logger.info("SWITCH Succeeded"); // This line never logs
                break;
            default:
                logger.info("SWITCH not matched: " + nodeName);
                break;
        }
    
        logger.debug('Convert to JS string');
        nodeName = nodeName + '';
        type = typeof(nodeName);
        logger.debug('Converted Type: ' + type);
        if (type == 'object') {
            logger.debug('Converted Type is object');
            if (nodeName instanceof String) {
                logger.debug('Converted Object is String instance');
            } else if (nodeName instanceof string) {
                logger.debug('Converted Object is string instance');
            } else {
                logger.debug('Converted Object is not Java String instance');
            }
        } else if (type === 'string') {
            logger.debug('Converted Type is JS string.');
        } else {
            logger.debug('Converted Type is not object or string. Type: ' + type);
        }
    
        switch(nodeName)
        {
            case "PID.3": 
                logger.info("SWITCH with js string Succeeded");
                break;
            default:
                logger.info("SWITCH with js string not matched: " + nodeName);
                break;
        }
        break;
    }