Skip to content

Instantly share code, notes, and snippets.

@MichaelLeeHobbs
Created April 8, 2021 15:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelLeeHobbs/46df4122e1fb1dd771c4b4fdc7ec95e4 to your computer and use it in GitHub Desktop.
Save MichaelLeeHobbs/46df4122e1fb1dd771c4b4fdc7ec95e4 to your computer and use it in GitHub Desktop.
Complete Mirth Source Transformer Script
/*** GLOBAL Scope ***/
/*** GLOBAL Scope --- Map Functions --- ***/
function $co(key, value) { if (arguments.length == 1) { return connectorMap.get(key); } else { return connectorMap.put(key, value); } }
function $c(key, value) { if (arguments.length == 1) { return channelMap.get(key); } else { return channelMap.put(key, value); } }
function $s(key, value) { if (arguments.length == 1) { return sourceMap.get(key); } else { return sourceMap.put(key, value); } }
function $gc(key, value) { if (arguments.length == 1) { return globalChannelMap.get(key); } else { return globalChannelMap.put(key, value); } }
function $g(key, value) { if (arguments.length == 1) { return globalMap.get(key); } else { return globalMap.put(key, value); } }
function $cfg(key, value) { if (arguments.length == 1) { return configurationMap.get(key); } else { return configurationMap.put(key, value); } }
function $r(key, value) { if (arguments.length == 1) { return responseMap.get(key); } else { return responseMap.put(key, value); } }
// No need to check the code context here; the function checks whether each individual map exists first
function $(string) {
try { if(responseMap.containsKey(string)) { return $r(string); } } catch(e){}
try { if(connectorMap.containsKey(string)) { return $co(string); } } catch(e){}
try { if(channelMap.containsKey(string)) { return $c(string); } } catch(e){}
try { if(sourceMap.containsKey(string)) { return $s(string); } } catch(e){}
try { if(globalChannelMap.containsKey(string)) { return $gc(string); } } catch(e){}
try { if(globalMap.containsKey(string)) { return $g(string); } } catch(e){}
try { if(configurationMap.containsKey(string)) { return $cfg(string); } } catch(e){}
// TODO: This is temporary for the database reader and should not stay
try { if(resultMap.containsKey(string)) { return resultMap.get(string); } } catch(e){}
return '';
}
/*** GLOBAL Scope --- Attachment Functions --- ***/
function getAttachmentIds(channelId, messageId) {
if (arguments.length == 2) {
return AttachmentUtil.getMessageAttachmentIds(channelId, messageId);
} else {
return AttachmentUtil.getMessageAttachmentIds(connectorMessage);
}
}
// Helper function to access attachments (returns List<Attachment>)
function getAttachments(base64Decode) {
return AttachmentUtil.getMessageAttachments(connectorMessage, !!base64Decode || false);
}
// Helper function to access a specific attachment (returns Attachment)
function getAttachment() {
if (arguments.length >= 3) {
return AttachmentUtil.getMessageAttachment(arguments[0], arguments[1], arguments[2], !!arguments[3] || false);
} else {
return AttachmentUtil.getMessageAttachment(connectorMessage, arguments[0], !!arguments[1] || false);
}
}
// Helper function to update a specific attachment (returns Attachment)
function updateAttachment() {
if (arguments.length >= 5) {
return AttachmentUtil.updateAttachment(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], !!arguments[5] || false);
} else if (arguments.length >= 3) {
if (arguments[2] && arguments[2] instanceof Attachment) {
return AttachmentUtil.updateAttachment(arguments[0], arguments[1], arguments[2], !!arguments[3] || false);
} else {
return AttachmentUtil.updateAttachment(connectorMessage, arguments[0], arguments[1], arguments[2], !!arguments[3] || false);
}
} else {
return AttachmentUtil.updateAttachment(connectorMessage, arguments[0], !!arguments[1] || false);
}
}
// Helper function to set attachment
// if (scriptOptions != null && scriptOptions.contains("useAttachmentList")) {
function addAttachment(data, type, base64Encode) {
return AttachmentUtil.addAttachment(mirth_attachments, data, type, !!base64Encode || false);
}
// else
function addAttachment(data, type, base64Encode) {
return AttachmentUtil.createAttachment(connectorMessage, data, type, !!base64Encode || false);
}
/*** GLOBAL Scope --- CodeTemplates --- ***/
function validate(mapping, defaultValue, replacement) {
var result = mapping;
if ((result == undefined) || (result.toString().length == 0)) {
if (defaultValue == undefined) {
defaultValue = '';
}
result = defaultValue;
}
if ('string' === typeof result || result instanceof java.lang.String || 'xml' === typeof result) {
result = new java.lang.String(result.toString());
if (replacement != undefined) {
for (var i = 0; i < replacement.length; i++) {
var entry = replacement[i];
result = result.replaceAll(entry[0], entry[1]);
}
}
}
return result;
}
// Helper function to create segments
function createSegment(name, msgObj, index) {
if (arguments.length == 1) { return new XML('<' + name + '></' + name + '>'); };
if (arguments.length == 2) { index = 0; };
msgObj[name][index] = new XML('<' + name + '></' + name + '>');
return msgObj[name][index];
}
// Helper function to create segments after specified field
function createSegmentAfter(name, segment) {
var msgObj = segment;
while (msgObj.parent() != undefined) { msgObj = msgObj.parent(); }
msgObj.insertChildAfter(segment[0], new XML('<' + name + '></' + name + '>'));
return msgObj.child(segment[0].childIndex() + 1);
}
// Helper function to get the length of an XMLList or array
function getArrayOrXmlLength(obj) {
if (typeof obj == 'xml' || obj instanceof java.lang.String) {
return obj.length();
} else if (typeof obj != 'undefined' && obj != null) {
return obj.length || 0;
}
return 0;
}
// Helper function to create a new String but leave undefined/null values alone
function newStringOrUndefined(value) {
if ('undefined' !== typeof value && value != null) {
value = new String(value);
}
return value;
}
// Helper function to create a new Boolean but leave undefined/null values alone
function newBooleanOrUndefined(value) {
if ('undefined' !== typeof value && value != null) {
value = new Boolean(value);
}
return value;
}
// Helper function to create a new Number but leave undefined/null values alone
function newNumberOrUndefined(value) {
if ('undefined' !== typeof value && value != null) {
value = new Number(value);
}
return value;
}
/*
* Since we use a sealed shared scope everywhere, importClass won't be available. To allow
* this to still work for migration, we override importClass to call importPackage instead.
*/
importClass = function() {
logger.error('The importClass method has been deprecated and will soon be removed. Please use importPackage or the fully-qualified class name instead.');
for each (argument in arguments) {
var className = new Packages.java.lang.String(argument);
if (className.startsWith('class ')) {
className = className.substring(6);
}
eval('importPackage(' + Packages.java.lang.Class.forName(className).getPackage().getName() + ')');
}
}
function doScript() {
/*** GLOBAL > doScript Scope ***/
if (connectorMessage.getProcessedRawData() != null) {
msg = new String(connectorMessage.getProcessedRawData());
} else {
msg = new String(connectorMessage.getRawData());
}
function doFilter() {
/*** GLOBAL > doScript > doFilter Scope ***/
phase[0] = "filter";
return true;
}
function doTransform() {
/*** GLOBAL > doScript > doTransform Scope ***/
phase[0] = "transformer";
logger = Packages.org.apache.log4j.Logger.getLogger(phase[0]);
/*** Transformer Script Start ***/
msg = 'my message'
/*** Transformer Script Stop ***/
if ("xml" === typeof msg) {
if (msg.hasSimpleContent()) {
msg = msg.toXMLString();
}
} else {
if ("undefined" !== typeof msg && msg !== null) {
var toStringResult = Object.prototype.toString.call(msg);
if (toStringResult == "[object Object]" || toStringResult == "[object Array]") {
msg = JSON.stringify(msg);
}
}
}
if ("xml" === typeof tmp) {
if (tmp.hasSimpleContent()) {
tmp = tmp.toXMLString();
}
} else {
if ("undefined" !== typeof tmp && tmp !== null) {
var toStringResult = Object.prototype.toString.call(tmp);
if (toStringResult == "[object Object]" || toStringResult == "[object Array]") {
tmp = JSON.stringify(tmp);
}
}
}
}
if (doFilter() == true) {
doTransform();
return true;
} else {
return false;
}
}
doScript();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment