1
votes

I need to read a blob field from a database, in a channel reader, then send it through TCP.

I can do this easily with a Database Reader, except when I have Blob fields, so I'm trying to use a JavaScript reader instead:

var dbConn = DatabaseConnectionFactory.createDatabaseConnection('org.postgresql.Driver','jdbc:postgresql://127.0.0.1:5432/ris','postgres','postgres');
// You may access this result below with $('column_name')
var resultQuery = "select mensajehl7, p.ip, p.port from turnohl7 h7 join pacs p on p.idpacs=h7.idpacs where h7.estado='NW'";
var qryResult = dbConn.executeCachedQuery(resultQuery);

var result = new Packages.java.util.ArrayList();

while(qryResult.next())
{ 
  var columna = new Array();
  columna['hl7'] = qryResult.getBytes(1);
  result.add(columna);
}

dbConn.close();
return result;

It seems that the result needs to be an XML, as the stacktrace suggests:

[2012-12-04 12:56:29,227]  ERROR (org.mule.impl.DefaultComponentExceptionStrategy:95): Caught exception in Exception Strategy for: f4a3cccb-902e-4504-86f5-0bfa473ad144: org.mule.umo.routing.RoutingException: Failed to transform message before applying the filter. Failed to route event via endpoint: ImmutableMuleEndpoint{connector=com.mirth.connect.connectors.js.JavaScriptConnector@379cf50c, endpointUri=js://sink, transformer=Transformer{name='ResultMapToXML', returnClass=false, returnClass=false, sourceTypes=[interface java.util.Map, class java.lang.String]}, name='_jsEndpoint#-1389160782', type='receiver', properties={}, transactionConfig=org.mule.impl.MuleTransactionConfig@44adab2d, filter=null, deleteUnacceptedMessages=false, initialised=true, securityFilter=null, synchronous=true, initialState=started, createConnector=0}. Message payload is of type: java.lang.String
org.mule.umo.routing.RoutingException: Failed to transform message before applying the filter. Failed to route event via endpoint: ImmutableMuleEndpoint{connector=com.mirth.connect.connectors.js.JavaScriptConnector@379cf50c, endpointUri=js://sink, transformer=Transformer{name='ResultMapToXML', returnClass=false, returnClass=false, sourceTypes=[interface java.util.Map, class java.lang.String]}, name='_jsEndpoint#-1389160782', type='receiver', properties={}, transactionConfig=org.mule.impl.MuleTransactionConfig@44adab2d, filter=null, deleteUnacceptedMessages=false, initialised=true, securityFilter=null, synchronous=true, initialState=started, createConnector=0}. Message payload is of type: java.lang.String
    at org.mule.routing.inbound.SelectiveConsumer.isMatch(SelectiveConsumer.java:64)
    at org.mule.routing.inbound.InboundMessageRouter.route(InboundMessageRouter.java:79)
    at org.mule.providers.AbstractMessageReceiver$DefaultInternalMessageListener.onMessage(AbstractMessageReceiver.java:487)
    at org.mule.providers.AbstractMessageReceiver.routeMessage(AbstractMessageReceiver.java:266)
    at org.mule.providers.AbstractMessageReceiver.routeMessage(AbstractMessageReceiver.java:225)
    at com.mirth.connect.connectors.js.JavaScriptMessageReceiver.processMessage(JavaScriptMessageReceiver.java:101)
    at com.mirth.connect.connectors.js.JavaScriptMessageReceiver.poll(JavaScriptMessageReceiver.java:82)
    at org.mule.providers.PollingMessageReceiver.run(PollingMessageReceiver.java:97)
    at org.mule.impl.work.WorkerContext.run(WorkerContext.java:290)
    at edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1061)
    at edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:575)
    at java.lang.Thread.run(Unknown Source)Caused by: org.mule.umo.transformer.TransformerException: java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (com.mirth.connect.server.mule.adaptors.AdaptorException)
    at com.mirth.connect.server.mule.transformers.JavaScriptTransformer.transform(JavaScriptTransformer.java:285)
    at org.mule.transformers.AbstractEventAwareTransformer.doTransform(AbstractEventAwareTransformer.java:48)
    at org.mule.transformers.AbstractTransformer.transform(AbstractTransformer.java:197)
    at org.mule.transformers.AbstractTransformer.transform(AbstractTransformer.java:200)
    at org.mule.transformers.AbstractTransformer.transform(AbstractTransformer.java:200)

How can I use my bytea field in a transformer?

2

2 Answers

2
votes

This answer isn't going to be direct, but hopefully it will be helpful.

I'll address two aspects:

1. getting HL7 data from a database

I do something similar, except in my case the HL7 data is stored in SQL Server in a varchar(max), and I call a stored procedure instead of passing a full query.

When the datatable is returned, we extract the hl7 data as a clob (not a blob) as follows

// the hl7 data is returned as a CLOB, not a string
var hl7MsgClob  = dataTable.getClob("HL7");
var clobLength = hl7MsgClob.length();

// clobs have a starting index of 1, and the getSubString is inclusive on both ends of the range
var hl7Msg = hl7MsgClob.getSubString(1,clobLength);

// This is a code template that decodes characters we escaped prior to writing to SQL Server
var cleanHL7 = Util_DecodeTextFromSQLStorage(hl7Msg);

// convert the message to an XML format
var HL7AsXML = SerializerFactory.getHL7Serializer(false,false,true).toXML(cleanHL7);

2. passing data around in Mirth

The datatype you return from your Javascript reader has to match the datatype of the channel source. I don't know about newer Mirth versions, but in version 1.8.2, this is configured on the Summary tab in the drop-down labelled "Incoming Data."

If you have this set to HL7 v2.x, then the required datatype MIGHT actually be the XML variant of HL7 v.2.x - not the more familiar pipe delimited format. If your hl7 is in the pipe delimited format, to experiment, you can convert it to the xML format using the last line of code I posted above.

0
votes

A simple way to get a blob from the database would be simple to use a cast in your select statement.

CAST(<Field Name> AS CHAR(10000) CHARACTER SET utf8) AS '<FieldName>'