No announcement yet.

dynamic local variable

  • Filter
  • Time
  • Show
Clear All
new posts

  • dynamic local variable

    dear all
    I will need some help on that problem
    I have an ORU message with 3 ORC/3OBR/3OBX segments, and with base64 encapsulated report. (1 report for 3 different exams in 1 ORU message). I need to extract the report into a pdf and put it into a directory. To name the pdf I want to use the accession number in OBR19.1 (for example I will get: RP1652541.pdf). Then I will get 3 identical files with 3 different names.

    With only one ORC/OBR/OBSsegment i use a local variable called ACCESS_NB, doing a mapping with the 19.1 segment, and then as Filewriter destination I put as file name : ${ACCESS_NB}.pdf. This works fine.

    With the 3 segments, I have tried this code, but of course, the ${ACCESS_NB}will take the last accession number, and I have only one pdf file generated instead of 3.

    for (var i = 0; i < getArrayOrXmlLength(msg['OBR']); i++) {
    var ACCESS_NB='';
    //recover accession number as name of pdf file
    var ACCESS_NB = msg['OBR'][i]['OBR.19']['OBR.19.1'].toString();
    channelMap.put('ACCESS_NB', validate(ACCESS_NB, 'NO_ACCESS_NB', new Array()));

    As anyone an idea about a solution to get 3 files ?
    Thanks in advance
    Mirth 3.5.0

  • #2

    try this approach; not the best but the one i just came up!

    // Create array of ACCN ---------------------------------------------
    var arrAccn=[];
    for (var i = 0; i < getArrayOrXmlLength(msg['OBR']); i++) 
    // Generate pdf File-------------------------------------------------
    var reportB64 = msg['OBX'][i]['OBX.5']['OBX.5.1'].toString();
    for(var i=0; i<$c('arrAccn').length; i++)
        FileUtil.write($c('arrAccn')[i].toString() + '.pdf', false, FileUtil.encode(reportB64));
    Best Regards,
    Alex Neiva


    • #3
      OK thanks, I gonna try it !


      • #4
        A file writer can only write a single file per message. It's possible to write the files yourself as Alex suggested in either the transformer or a javascript writer, but then you lose all of the nice things that the file writer does for you (like writing to network locations and allowing you to specify what to do when the file already exists.)

        If you move your File Writer to a second channel, your first channel can receive the messages and create as many new messages as necessary to then route to the second channel.

        To streamline things, you could create attachments for your pdfs in the first channel, then your created messages would be the extended attachment id and can contain the filename in the sourceMap. A trick I learned recently after doing it manually for a long time is to use the strict parser (set on the data type properties) to group orders for you. Accessing the data this way looks a little different than most examples you will find in the forums. The code would look something like this.

        for each (var order in msg['RDO_O01.ORDER']) {
        	var base64pdf = order['OBX']['OBX.5'].toString();
        	var filename = order['OBR']['OBR.19'].toString() + '.pdf';
        	// Create attachment
        	var attachment = addAttachment(base64pdf, 'application/pdf');
        	// Construct expanded attachmentId replacement token
        	var attachmentId = java.lang.String.format('${ATTACH:%s:%d:%s}', channelId,
        		new java.lang.Integer(connectorMessage.getMessageId()), attachment.getId());
        	// Create new message representing this pdf attachment
        	var newMessage = new RawMessage(attachmentId, null, {originalFilename: filename});
        	// Send message to downstream pdf writer
        	var response = router.routeMessage('pdf writer channel', newMessage);
        	// Downstream channel should have the source queue enabled for fastest
        	// processing. Assume no response for successful send. An error likely 
        	// means that the downstream channel isn't started. Handle
        	// appropriately.
        	if (response && response.getStatus() == Status.ERROR) throw 'Oh No!';
        The pdf attachments will be stored with the hl7 message in the first channel. You should be able to view them in the message viewer. The downstream channel should have all data types set to Raw. The File Writer should be in binary mode, have Reattach Attachments set to Yes, and use ${originalFilename} for the file name.

        When creating the RawMessage, you can add additional variables to the sourceMap besides originalFilename. This could allow you to share the downstream channel as a more general purpose File Writer between multiple channels.