Perform all File IO operations – Code Templates

In this post, I’m creating the code templates that will do one stop solution for all problems that we face in mirth while doing the File based IO operation.

Imagine a case where you need to move the file from one directory to another via Mirth instead of source or destination connector provided by tool.

In that case:
1. first we need to check if the file exists or not?
2. we have to use FileUtil.write function to write that file to destination location
3. we have to delete the file from the source location.

The performance time required to complete this process will be high. and we have to catch exceptions in correct places. To avoid all these troubles I have developed Mirth code template library that will be one stop solution for all these problem.

This library utilizes apache commons FileUtils library. Download that library from this link here. In that link under binaries select commons-io-2.6-bin.zip if you are using Windows and commons-io-2.6-bin.tar.gz if you are using linux systems.

apache commons fileutils library

Once downloaded navigate to commons-io-2.6 folder and copy the JAR file named commons-io-2.6 alone. You will also find other JAR files along with that, you can ignore them, you have to copy this and place it in your custom-lib folder of Mirth connect installed directory. Once  done, go to Mirth settings tab and click on Reload Resource.

Provide the following codes in your code template library:

Copy Directory To Directory:

function copyDirectoryToDirectory(sourceDirectory, destinationDirectory) {

var srcDirectory = new java.io.File(sourceDirectory);

var destDirectory = new java.io.File(destinationDirectory);

try {

var copyDirectoryToDirectory = new Packages.org.apache.commons.io.FileUtils.copyDirectoryToDirectory(srcDirectory, destDirectory);

} catch (exp) {

logger.debug(exp);

}
}

call from Transformer:

copyDirectoryToDirectory(“C:/Projects/PROJECTS/TEST/Sample Message/PDF-tests/sourcedirectory”,”C:/Projects/PROJECTS/TEST/destinationDirectory”);

Move File To Directory:

function moveFileToDirectory(sourceFileName, destinationDirectoryName) {

var srcFile = new java.io.File(sourceFileName);

var destDir = new java.io.File(destinationDirectoryName);

try {

var moveFileToDirectory = new Packages.org.apache.commons.io.FileUtils.moveFileToDirectory(srcFile, destDir, false);

} catch (exp) {

logger.debug(exp);

}
}

call from Transformer:

moveFileToDirectory(“C:/Projects/PROJECTS/TEST/Sample Message/PDF-tests/test2.pdf”, “C:/Projects/PROJECTS/TEST/Sample Message/”);

Move Directory To Directory:

function moveDirectoryToDirectory(sourceDirectory, destinationDirectory) {

var srcDirectory = new java.io.File(sourceDirectory);

var destDirectory = new java.io.File(destinationDirectory);

try {

var moveDirectoryToDirectory = new Packages.org.apache.commons.io.FileUtils.moveDirectory(srcDirectory, destDirectory);

} catch (exp) {

logger.debug(exp);

}
}

call from Transformer:

moveDirectoryToDirectory(“C:/Projects/PROJECTS/TEST/Sample Message/PDF-tests/sourcedirectory”, “C:/Projects/PROJECTS/TEST/destinationDirectory”);

Copy File To Directory:

function copyFileToDirectory(sourceFileName, destinationDirectoryName) {

var srcFile = new java.io.File(sourceFileName);

var destDir = new java.io.File(destinationDirectoryName);

try {

var copyFileToDirectory = new Packages.org.apache.commons.io.FileUtils.copyFileToDirectory(srcFile, destDir);

} catch (exp) {

logger.debug(exp);

}

}

call from Transformer:

copyFileToDirectory(“C:/Projects/PROJECTS/TEST/Sample Message/PDF-tests/test.pdf”, “C:/Projects/PROJECTS/TEST/Sample Message/”);

Advertisements

Code Templates in Mirth

Mirth is a wonderful tool which has provided options to use generalized functions in the form of code templates. This will improve the performance of the channels in the greater fashion. Code re-usability is a major success of Mirth which is the reason why Code Templates are used for.

Please see the example below.

code templates

Navigate to “Channels” tab in the Left pan of the Mirth navigation bar, create a new Channel or choose any existing channel. When a channel is selected, the options will expand on the left wing of the navigation bar. Select “Edit Code Templates” option there.

You will get a screen like showed below

create library

Note: You will not have any libraries listed at first. Unless you imported a channel from previous version of mirth that might have taken the libraries along with it.

On the right hand side you can see the list of channels that are available in the Mirth tool. You can select the channel that needs this code template to be used. In real time not all channels will use the generalized functions. It depends on the business needs of the channel creation.

Now create a new library to use in your channel by clicking the “New Library” on the left hand side navigation bar. This will list a new library on the dashboard. Please add the description for the library you have created. It is a good practice to write description where ever needed that includes writing the description for the channel even.

Now, be on the same library you have created and select “New Code Template” on the left hand side of the navigation panel. You will be provided with three options on the code template. They are as follows:

  1. Functions
  2. Drag-And-Drop code block
  3. Complied Code Block

I’m going to explain the code templates usage in here by using a simple function. I’m writing a function that will get the segment of the HL7 message and check if the length of the String in the segment is not empty. If the length of the string is 0 or empty then it should alert by saying “Length is irregular”. Below goes the code:

function checkStringLength(data) {
if (data.length != 0) {
data = data;
} else if (data.length == 0) {
data = “Length is irregular”;
}
return data;
}

Navigate to Save Changes on the left hand side of the navigation panel. Now The generalized function that has to be used again and again is successfully written in the code template. The purpose becomes meaningful only we know how to access this code, right?…

  1. To execute this. Navigate to the “Channels” tab, and double click on the channel you have created.
  2. Navigate to the Source Tab.
  3. Click on Edit Transformer on the left hand side of the navigation pan.
  4. Now you will see the source transformer dashboard area. Select the type as “Javascript”
  5. In the Right hand side  there will be three tabs available at the top. Reference, message Trees and Message Template. -> Select Message Template.
    Place the HL7 sample message provided below in the template area.

MSH|^~\&|EPICADT|DH|LABADT|DH|201301011226||ADT^A01|HL7MSG00001|P|2.3| EVN|A01|201301011223||
PID|||MRN12345^5^M11||^JOHN^A^III||19710101|M||C|1 DATICA STREET^^MADISON^WI^53005-1020|GL|(414)379-1212|(414)271-3434||S||MRN12345001^2^M10|123456789|987654^NC|
NK1|1|APPLESEED^BARBARA^J|WIFE||||||NK^NEXT OF KIN
PV1|1|I|2000^2012^01||||004777^GOOD^SIDNEY^J.|||SUR||||ADM|A0|

Whatever function we created in the code template will be available ready-made in the Reference tab. I have named the code template as “Check String Length“. As shown in the picture below

function

So I’m typing “Check String Length” in the Reference tab of the source transformer search area. You will get the function that you have created on the code template area in here directly as shown below

Poin check String length

You can drag and drop the function in your transformer Javascript area. I have just made a printing function that will get the value from the specific segments of the PID 5.1 field of the HL7 message which is basically patient’s first name. And call this function. The code is as below

logger.info(“check String length : “+checkStringLength(msg[‘PID’][‘PID.5’][‘PID.5.2’].toString()));

Now deploy the channel and send the HL7 message pasted above and send it via the dashboard channel by right clicking it. Observe the inferences on both stages when it has a value, it should display the name as it is, When it does not have value it should through the statement in the else part.

Hope it clarifies the question on how to use Code Templates.

Happy Coding & Integration …….

Don’t hesitate to reach out to me if there is any questions on the above part.

 

Blog at WordPress.com.

Up ↑