Announcement

Collapse
No announcement yet.

Some weirdness with functions in the globalChannelMap

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

  • Some weirdness with functions in the globalChannelMap

    Hey everyone!

    I've recently been working on some transformers to sanitize HL7 messages - essentially to strip out data that we don't need so we minimize our PHI concerns.

    I've written some arrays and functions that I attempt to put in the global channel map in the channel's deploy script:

    Code:
    //Edited these arrays. Y'all don't need to know the specifics, just the gist of how it works
    globalChannelMap.put('allowed_segments', [
    	'MSH', 'EVN', 'PID'
    ]);
    
    globalChannelMap.put('allowed_elements', [
    	'MSH', 'EVN',
    	'PID.1', 'PID.2', 'PID.3'
    ]);
    
    // This function loops through and nullifies anything that doesn't match something in the keep list.
    globalChannelMap.put('sanitize_elements', function(obj, keep) {
        for each(child in obj.children()) {
            if(child.name() !== null && keep.indexOf(child.name().toString()) != -1) {
                continue;
            } else {
                if(child.name() !== null) {
                    sanitize_elements(child, keep);
                } else {
                    delete child.parent()[child.childIndex()];
                }
            }
        }
    });
    
    // This function will remove segments themselves, if they aren't in the keep list
    globalChannelMap.put('sanitize_segments', function(obj, keep) {
    	for each (seg in obj.children()) {
    		if (keep.indexOf(seg.name().toString()) == -1) {
    			delete obj[seg.name().toString()];
    		}
    	}
    });
    
    return;
    Later on in transformers I attempt to use those definitions:
    Code:
    logger.info('---- BEFORE ----');
    logger.info(msg);
    
    var a = globalChannelMap.get('allowed_segments');
    var b = globalChannelMap.get('sanitize_segments');
    b(msg, a);
    and
    Code:
    var a = globalChannelMap.get('allowed_elements');
    var b = globalChannelMap.get('sanitize_elements');
    b(msg, a);
    
    logger.info('---- AFTER ----');
    logger.info(msg);
    yet I always get the following error whenever the channel attempts to process a message:
    Code:
    Transformer error
    ERROR MESSAGE: Error evaluating transformer
    com.mirth.connect.server.MirthJavascriptTransformerException: 
    CHANNEL:	Test message cleanup
    CONNECTOR:	sourceConnector
    SCRIPT SOURCE:	TRANSFORMER
    SOURCE CODE:	
    47: logger.info('---- AFTER ----');
    48: logger.info(msg);
    49: if ('xml' === typeof msg && msg.hasSimpleContent()) { msg = msg.toXMLString(); }if ('xml' === typeof tmp && tmp.hasSimpleContent()) { tmp = tmp.toXMLString(); }
    50: }
    51: if (doFilter() == true) { doTransform(); return true; } else { return false; } 
    52: }
    LINE NUMBER:	52
    DETAILS:	ReferenceError: "sanitize_elements" is not defined.
    	at 3216b7d0-ede1-4e1a-a8f6-89ba83a0da12_Deploy:52
    	at d1870e7e-9923-4bf1-8a22-ca055dd395aa:45 (doTransform)
    	at d1870e7e-9923-4bf1-8a22-ca055dd395aa:51 (doScript)
    	at d1870e7e-9923-4bf1-8a22-ca055dd395aa:53
    	at com.mirth.connect.server.transformers.JavaScriptFilterTransformer$FilterTransformerTask.call(JavaScriptFilterTransformer.java:134)
    	at com.mirth.connect.server.transformers.JavaScriptFilterTransformer$FilterTransformerTask.call(JavaScriptFilterTransformer.java:100)
    	at java.util.concurrent.FutureTask.run(Unknown Source)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)
    It looks like the transformers are being put inline with some other code that we don't have access to, that's causing things to choke. I've tried to log the types of the data I retrieve back from the globalChannelMap, and their types look appropriate, so I assume I'm not making any kind of error on my end.

    Can someone please help explain what's going on?

    Thanks!

  • #2
    The exception is clear enough - the "sanitize_elements" identifier does not exist. You haven't created it. You're adding a function into the global channel map with the key "sanitize_elements", but within the function you're attempting to call sanitize_elements as if it was an identifier.

    Declare the function beforehand and assign it to an actual identifier and then you can call it recursively if you want.
    Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

    Nicholas Rupley
    Work: 949-237-6069
    Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


    - How do I foo?
    - You just bar.

    Comment


    • #3
      Thank you very much!

      I forgot about the recursion within the function itself as I tried to place the entire function in the globalChannelMap.put operation.

      By separating it back out, everything works as it should.

      Thanks again!

      P.S. for anyone who runs into this, here's what I had to do with the deploy script to make it work:
      Code:
      function sanitize_elements(obj, keep) {
          for each(child in obj.children()) {
              if(child.name() !== null && keep.indexOf(child.name().toString()) != -1) {
                  continue;
              } else {
                  if(child.name() !== null) {
                      sanitize_elements(child, keep);
                  } else {
                      delete child.parent()[child.childIndex()];
                  }
              }
          }
      }
      globalChannelMap.put('sanitize_elements', sanitize_elements);
      
      function sanitize_segments(obj, keep) {
      	for each (seg in obj.children()) {
      		if (keep.indexOf(seg.name().toString()) == -1) {
      			delete obj[seg.name().toString()];
      		}
      	}
      }
      globalChannelMap.put('sanitize_segments', sanitize_segments);

      Comment

      Working...
      X