Showing posts with label Code Templates. Show all posts
Showing posts with label Code Templates. Show all posts

Monday, June 20, 2016

A Few Miscellaneous Scripts and Snippets for Mirth

These are some scripts and snippets I have found useful in Mirth Connect.

---

Quite often when I am developing a new script for a channel, I've found it to be particularly helpful to have something akin to a break-point within the script that sends output to the console.
Using the logger.info() method is great when you are just inserting a single point here and there, but occasionally there are times when I need to know where exactly things stopped working, and to do that I want to be able to trace the script's execution much more easily.
To accomplish this without having to spend much more time going back and forth from the code to the channel, I wrote a quick script which I then added to the Code Templates section of Mirth.

function brk(tag) {
logger.info("Test Point "+tag);
}

That's it. Now all I have to do when I want to keep track of each time the code hits a certain point in the code is to insert brk(1); and the console output shows "Test Point 1" whenever it goes off. Sure it's a bit lazy, but then again, I am all about making my job easier.

---

Something that really qualifies as more of a trick and less of a script: If you want to add multiple segments to an object for easy passing back and forth, instead of trying to come up with some crazy complicated object structure, just create an array and add the objects as items in the array. You can still call and pass like a regular object, but the number of items is purely dynamic and is quite easy to reference. (Pro-Tip: The simplest way to do this is to create a temporary object and then assign a spot in the array to it)
For example:

var objectArray = [];
var tempObject = {};
    //get the next spot in the array and set it as your new index
var index = objectArray.length; // lets call it '3'
    //assign properties to the temp object
tempObject.patientId = msg["PID"]["PID.2"]["PID.2.1"].toString(); // we'll say it's "1234"
    // etc...
    //now assign the object to the next index in the array
objectArray[index] = tempObject;
    //make sure to clear the temp object before each new round
tempObject = {};

...

    //now you can call the properties using the index of the array
    //eg -
logger.info(objectArray[3].patientId); // "1234"

Friday, June 17, 2016

Order/Observation Parsing

Here is a quick script I wrote to parse order and observation groups in Mirth Connect. This is very generic and requires tweaking for each application, but it's a good place to start if you're trying to keep track of multiple orders within a message.
The script iterates through the message and can easily be set to gather the message segments into an object or array for further processing.
Feel free to use this script as you like. 

/*
Script Purpose: Organize and compile order and observation segment groups

::Possible Group Organizations::
[orc]-->obr --> (obx) --> (nte) --> (obx)... [spm]
|  |       |-> (obx) --> (nte)... [spm]
|  |-> (nte) --> (obx) --> (nte)... [spm]
|->obr ...
[orc] ...
*/

//get number of ORC messages
var orcmsg = msg["ORC"].length();
var ordGroup = 0; // track order groups
var obvGroup = 0; // track observation groups
var keeper = false; // help prevent grabbing unassociated NTE segments

if(orcmsg==1){ //only one ORC segment exists in the message
//begin looping through the message
var orcFound = false;
for each (seg in msg.children()){
if(seg.name().toString()=="ORC"){
//load ORC info 
orcFound = true;
}
else if(seg.name().toString()=="OBR" && orcFound){
ordGroup = seg["OBR.1"]["OBR.1.1"].toString();
obvGroup = 0; // reset observation group number
keeper = true; // make sure we don't include stray NTE segments
//load OBR info
}
else if(seg.name().toString()=="OBX" && orcFound){
obvGroup++;
keeper = true;
//load OBX info
}
else if(seg.name().toString()=="NTE" && orcFound && keeper){
//load NTE info
}
else if(seg.name().toString()=="SPM" && orcFound){
//Load SPM info
}
else{ //unaccounted segment handling
keeper = false;
}
}
}
else if(orcmsg>=2){ 
//begin looping through the message
var orcFound = false;
for each (seg in msg.children()){
if(seg.name().toString()=="ORC"){
//load ORC info 
orcFound = true;
keeper = false;
}
else if(seg.name().toString()=="OBR" && orcFound){
ordGroup = seg["OBR.1"]["OBR.1.1"].toString();
obvGroup = 0; // reset observation group number
keeper = true; // make sure we ignore stray NTE segments
//load OBR info
}
else if(seg.name().toString()=="OBX" && orcFound){
obvGroup++;
keeper = true;
//load OBX info
}
else if(seg.name().toString()=="NTE" && orcFound && keeper){
//load NTE info
}
else if(seg.name().toString()=="SPM" && orcFound){
//Load SPM info
}
else{ //unaccounted segment handling
keeper = false;
}
}
}
else{/* Message contains no Common Order segment (ORC) */}

I take no responsibility for any damage caused by using this script. This is meant to be used as a tool to assist in development of Mirth Connect Channels, and comes with no warranty of any kind. 

Thursday, June 16, 2016

Simplify Database Connection Updates

To simplify changing database connection strings I've found it's quite useful to create a function in the Code Templates. This way I can just use that for each time I need to put a database call and whenever I need to change the connection string I have one single spot to modify instead of dozens.

function ODBConn(state) {
  if(state=="open"){
    importPackage(java.sql);
    var dbConn = DriverManager.getConnection('URL','Username','Password');
  }
return dbConn;
}

This example is specific to using prepared statements for database interaction, but the principal is the same for any type or method of connection. 

To use the function for connecting, simply use something like this:

var conn = ODBConn('open');
...
var preparedQuery = conn.prepareStatement("some prepared query string");
  preparedQuery.setString(1,firstParameter);
  preparedQuery.setString(2,secondParameter);
  preparedQuery.executeQuery();

One thing to note about this script is that it does not close the connection when finished as shown. Fortunately closing the connection is very simple, it should just be done at once the need for a connection is complete.

Just append this to the end of your script to close :

conn.close();

Voila! You have a reusable database connection function and whenever you need to change a database password or connection url! It will now be in one convenient place instead of having to search through every channel destination and transformer script to update your information.