Showing posts with label Mirth Project. Show all posts
Showing posts with label Mirth Project. Show all posts

Wednesday, August 13, 2008

Accessing a Message in Different Parts of Mirth

Depending on which part of Mirth you are writing JavaScript code, you need to use different objects to access the same data. It's pretty annoying.

Objects
messageObject.getRawData() - gets raw data - works in transformer, filter, and connector javascript
messageObject.getEncodedData() - works in transformer, filter, and connector javascript
messageObject.getTransformedData() - works in transformer, filter and connector javascript IFF there is a transformer, otherwise returns null
msg - gets array representing XML - works only in transformer and filter, no equivalent in connector

In other scripting situations (like SQL) you can use ${message.rawData} and the like to get rawData, encodedData, and transformedData.

To get a msg object in a connector, check out this posting by ChrisL on the Mirth forum:

If you are using this on the DB Writer Javascript, then the error is occurring because MSG isn't valid there. msg is only available in the transformer and filter.

You can store msg in the transformer with

channelMap.put('msg', msg)

then in your destination DB Writer do:
Code:

var msg = channelMap.get('msg');

Monday, August 11, 2008

Common Mirth Functions

Inside a transformer

Convert XML to HL7
SerializerFactory.getHL7Serializer(useStrictParser, useStrictValidation, handleRepetitions).fromXML(message);


Access parts of an XML message
msg['MSH']['MSH.4']['MSH.4.1'] = 'TEST12345';

Set the transformedData to a new value
msg = ""

Inside a Message Template
Get raw message data: ${message.rawData}
Get transformed data (if a transformer exists-- you can create an empty javascript transformer): ${message.transformedData}
Get encoded data, which has special characters encoded: ${message.encodedData}

Common Mirth Error Messages

org.mule.umo.transformer.TransformerException: java.lang.ArrayIndexOutOfBoundsException: 2 (com.webreach.mirth.model.converters.SerializerException)

Mirth is trying to parse an XML document as HL7.
Make sure the channel is being sent rawdata and the message templates in the channel transformers are set to the right formats.

Mirth Project: Step by Step how to apply XSL to incoming HL7, etc

I needed to apply XSL transformations to incoming HL7 data. NOTE: unfortunately there was a bug in the code below and after fixing it, looks like Xalan won't parse non XML source documents.

  • I create an LLP listener
  • I added a destination file writer
  • I set the template to: ${message.transformedData}
  • I clicked 'edit transformer'
  • Click "Add New Step"
  • Change the type to JavaScript
  • Under the message templates tab, choose XML (or whichever format you wish to output) as the Outbound Message Template (DON'T FORGET THIS! I'd call this is a gotcha)Here is the code to load a stylesheet, apply it, and then save the results back to the message's transformedData member (mostly lifted from another post on this forum):

importPackage(Packages.javax.xml.transform);
importPackage(Packages.javax.xml.transform.stream);
// Create the transformer factory
var tFactory = TransformerFactory.newInstance();

// Load XSL file
// XSL files are loaded based on the directory Mirth is running in
logger.info('Message type is ' + messageObject.getType());
var
xslSource = new StreamSource(messageObject.getType()+".xsl");

if (
xslSource==null)
logger.error('error loading XSL');

// Create a new transformer using this transformation
var transformer = tFactory.newTransformer( xslSource );

var
bos = new java.io.ByteArrayOutputStream();

// Create an inputstream using the raw message
var msg_sbis = new java.io.StringBufferInputStream(messageObject.getRawData()); // use msg to get internal Mirth XML representation

// Apply the XSL to the message
transformer.transform( new StreamSource(msg_sbis),new StreamResult( bos ));


// Set transformedData to be the new document
msg = new XML(bos.toString());


Note that you can come up with other types of combinations. For example, to forward several HL7 feeds to the same destination, create some channels with channel writer destinations and set message output type to 'HL7' in the message templates. Create a new channel with a channel reader source. Configure your destination and set the transformer to output the correct type via the message templates tab.

Labels

Blog Archive

Contributors