web stats
Mirth Community - View Single Post - Mirth Tools: User defined functions
View Single Post
  #12  
Old 05-09-2012, 09:39 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,126
narupley is on a distinguished road
Default

NOTE: This code template is probably not necessary for 3.0 because we now have the ChannelUtil utility class, which has methods to deploy/undeploy/start/stop/etc. channels and connectors. However you can still use this to deploy channels on a different instance of Mirth Connect.

deployChannels: Deploys channels using an array of channel IDs and an optional client info object.

Rather self-explanatory; you just pass it an array or comma-separated string of channel IDs, and it will initiate a Client and deploy the specified channels. Client connection settings are hard-coded as defaults, but they can be overridden via the optional clientInfo argument.

The function returns an object containing a status String element, a message String element, and an overloaded toString method. The status will be "SUCCESS" if the client was able to successfully login, and at least one of the channel IDs refers to an enabled channel. Otherwise, the status is "FAILURE".

Here's the code (also attached as a code template):

Code:
/*
	Author: Nick Rupley
	Date Modified: 5/9/2012

	deployChannels
	--------------
		Deploys channels using an array of channel IDs and an optional client info object.

	Arguments
	---------
		Required
		--------
		channelIds: An array of channel IDs, or a string containing a comma-separated list of channel IDs.
		
		Optional
		--------
		clientInfo: An object containing zero or more of the following elements:
			URL: The Mirth Connect Administrator URL to connect to.
			user: Username to connect with.
			pass: Password to connect with.
			version: Version of Mirth Connect to bind to.
			timeout: HTTPS Connection timeout in milliseconds.

	Return Value
	------------
		Returns an object containing a status String element, a message String element, 
		and an overloaded toString method. The status will be "SUCCESS" if the client
		was able to successfully login, and at least one of the channel IDs refers to
		an enabled channel. Otherwise, the status is "FAILURE".
*/

function deployChannels(channelIds,clientInfo) {
	var crlf = '\x0D\x0A', tab = '     ';
	var status = {status:'FAILURE', message:''};
	status.toString = function() {return this.status + ':' + crlf + this.message;};

	try {
		if (!(channelIds instanceof Array)) {
			channelIds = channelIds.replace(/\s/g,'');
			if (channelIds.length > 0)
				channelIds = channelIds.split(',');
			else
				channelIds = [];
		}
		if (channelIds.length <= 0)
			throw('');
	}
	catch(e) {
		status.message = 'Invalid Channel ID List.';
		return status;
	}

	var info = {
		URL: 'https://localhost:8443',
		user: 'admin',
		pass: 'admin',
		version: '2.2.1.5861',
		timeout: 10000
	};
	if (clientInfo) {
		info.URL = clientInfo.URL || info.URL;
		info.user = clientInfo.user || info.user;
		info.pass = clientInfo.pass || info.pass;
		info.version = clientInfo.version || info.version;
		if ((clientInfo.timeout || clientInfo.timeout === 0) && clientInfo.timeout >= 0)	
			info.timeout = clientInfo.timeout;
	}

	try {
		var client = new com.mirth.connect.client.core.Client(info.URL,info.timeout);
		var loginStatus = client.login(info.user,info.pass,info.version);
		status.message += 'Login status: ' + loginStatus.getStatus() + crlf;

		if (!loginStatus.getStatus().toString().startsWith('FAIL')) {
			var list = new java.util.ArrayList();
			var channels = {}, failures = [];
			var successMessage = 'Channels successfully deployed:' + crlf;
			var failureMessage = 'Disabled channels not deployed:' + crlf;

			for each (channel in client.getChannel(null).toArray())
				channels[channel.getId()] = channel.isEnabled();
			for each (channelId in channelIds)
				if (channels[channelId]) {
					list.add(channelId);
					successMessage += tab + channelId + crlf;
				}
				else {
					failures.push(channelId);
					failureMessage += tab + channelId + crlf;
				}

			client.deployChannels(list);
			status.status = list.size() > 0 ? 'SUCCESS' : 'FAILURE';
			status.message += (list.size() > 0 ? successMessage : '') + (failures.length > 0 ? failureMessage : '');
		}
		else
			status.message += loginStatus.getMessage() + crlf;

		try {client.logout();} catch (e) {}
	}
	catch(e) {
		status.status = 'FAILURE';
		status.message += e.toString() + crlf;
	}

	return status;
}
And an example of its use (a channel that accepts a CSV string of channelIds as Delimited Text and outputs the result of deployChannels to the server log):

Code:
logger.info(deployChannels(messageObject.getRawData().split(',')).toString());
Attached Files
File Type: xml deployChannels.xml (3.8 KB, 153 views)

Last edited by narupley; 10-17-2013 at 08:54 AM.
Reply With Quote