web stats
Wrting to a file - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 03-22-2012, 05:01 AM
Dushi Dushi is offline
OBX.1 Kenobi
 
Join Date: Feb 2012
Posts: 26
Dushi is on a distinguished road
Default Wrting to a file

Hi All,

I am trying to write a simple java script to get the encoded message and writ it to a file. This script will be in the postprocessor.

But it doesn't work

var log1file="D:\TEST\log1.txt";
var ReportBody=(messageObject.getEncodedData());
FileUtil.write(log1file, true, "ReportBody");

Any help much appreciated.

Thank you.
Reply With Quote
  #2  
Old 03-22-2012, 05:10 AM
narupley's Avatar
narupley narupley is offline
Mirth Employee
 
Join Date: Oct 2010
Posts: 3,529
narupley is on a distinguished road
Default

Quote:
Originally Posted by Dushi View Post
Hi All,

I am trying to write a simple java script to get the encoded message and writ it to a file. This script will be in the postprocessor.

But it doesn't work

var log1file="D:\TEST\log1.txt";
var ReportBody=(messageObject.getEncodedData());
FileUtil.write(log1file, true, "ReportBody");

Any help much appreciated.

Thank you.
One problem is that you aren't escaping the backslashes in your filename, so it's actually "D:TESTlog1.txt" (or it might throw an exception.. I forget how Rhino handles that). Really the only other problem is that ReportBody is in quotes, when it shouldn't be. You should also be using Apache Commons, because FileUtil is deprecated...

Code:
org.apache.commons.io.FileUtils.write(new java.io.File('D:/TEST/log1.txt'),true,messageObject.getEncodedData());
EDIT: Fixed code:

Code:
org.apache.commons.io.FileUtils.write(new java.io.File('D:/TEST/log1.txt'),messageObject.getEncodedData(),true);

Last edited by narupley; 03-22-2012 at 06:32 AM.
Reply With Quote
  #3  
Old 03-22-2012, 06:07 AM
Dushi Dushi is offline
OBX.1 Kenobi
 
Join Date: Feb 2012
Posts: 26
Dushi is on a distinguished road
Default

Thank you for the prompt reply.
It's still not working:

sorry.. dont' have much knowledge in scripting.

[2012-03-22 13:03:48,386] ERROR (com.mirth.connect.server.util.JavaScriptUtil:264) : Error executing Postprocessor script from channel: f3c28251-0a18-4bcd-b2cf-6430387f3d9d
com.mirth.connect.server.MirthJavascriptTransforme rException: SOURCE CODE:
10: 11: //var log1file='D:/log1.txt';12: //var ReportBody=(messageObject.getEncodedData());13: //FileUtil.write(log1file, true, "ReportBody");14: 15: org.apache.commons.io.FileUtils.write(new java.io.File('D:/TEST/log1.txt'),true,messageObject.getEncodedData());16 : 17: //FileUtil.write('D:\TEST\log.txt',true,getEncodedDa ta());18: 19: // Create an instance of the channelStatisticsController objectLINE NUMBER:
15DETAILS:
Can't find method org.apache.commons.io.FileUtils.write(java.io.File ,boolean,java.lang.String).
Reply With Quote
  #4  
Old 03-22-2012, 06:16 AM
Dushi Dushi is offline
OBX.1 Kenobi
 
Join Date: Feb 2012
Posts: 26
Dushi is on a distinguished road
Default

It's OK. It worked!!! Thank you so much. :-)
Reply With Quote
  #5  
Old 03-22-2012, 06:30 AM
narupley's Avatar
narupley narupley is offline
Mirth Employee
 
Join Date: Oct 2010
Posts: 3,529
narupley is on a distinguished road
Default

Quote:
Originally Posted by Dushi View Post
It's OK. It worked!!! Thank you so much. :-)
Yeah sorry, I got the parameters mixed up: FileUtils.write
Reply With Quote
  #6  
Old 03-22-2012, 07:13 AM
Dushi Dushi is offline
OBX.1 Kenobi
 
Join Date: Feb 2012
Posts: 26
Dushi is on a distinguished road
Default

I just realised that it's sending the raw messages not the encoded message, where the script clearing saying encoded.

any ideas please?
Reply With Quote
  #7  
Old 03-22-2012, 07:27 AM
narupley's Avatar
narupley narupley is offline
Mirth Employee
 
Join Date: Oct 2010
Posts: 3,529
narupley is on a distinguished road
Default

Quote:
Originally Posted by Dushi View Post
I just realised that it's sending the raw messages not the encoded message, where the script clearing saying encoded.

any ideas please?
That's because you're doing that code in your postprocessor, where you don't natively have access to the message objects of your destinations. The easiest way around that is to place whatever you need in the channel map, and then using it in the postprocessor.
Reply With Quote
  #8  
Old 03-26-2012, 05:20 AM
Dushi Dushi is offline
OBX.1 Kenobi
 
Join Date: Feb 2012
Posts: 26
Dushi is on a distinguished road
Default

Hi

I am still struggling with this..

I have created couple of mappers and JavaScript, all in the destination transformer.


Variable: log1file
Mapping: 'D:/TEST/log1.txt'

Variable: messagetext
Mapping: messageObject.getEncodedData()

Then the JS :

FileUtil.write($('log1file'), true, $('messagetext'));

This way I get an empty file created!!! I am not sure where I am going wrong here.

Thanks.
Reply With Quote
  #9  
Old 03-26-2012, 05:26 AM
narupley's Avatar
narupley narupley is offline
Mirth Employee
 
Join Date: Oct 2010
Posts: 3,529
narupley is on a distinguished road
Default

Quote:
Originally Posted by Dushi View Post
Hi

I am still struggling with this..

I have created couple of mappers and JavaScript, all in the destination transformer.


Variable: log1file
Mapping: 'D:/TEST/log1.txt'

Variable: messagetext
Mapping: messageObject.getEncodedData()

Then the JS :

FileUtil.write($('log1file'), true, $('messagetext'));

This way I get an empty file created!!! I am not sure where I am going wrong here.

Thanks.
Well, it looks like you're using the com.mirth version again. Not that it's inherently bad, but it is deprecated. Try this:

Code:
org.apache.commons.io.FileUtils.write(new java.io.File($('log1file').toString()),$('messagetext').toString(),true);
Also, you put those variables in the channel map right (not the connector map)?
Reply With Quote
  #10  
Old 03-26-2012, 05:50 AM
Dushi Dushi is offline
OBX.1 Kenobi
 
Join Date: Feb 2012
Posts: 26
Dushi is on a distinguished road
Default

Yes the variables are in channel map.

I copied your code and tried again, getting the same result. Empty file. :-(
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 06:21 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Mirth Corporation