Pointers on creating an operation to file by Message Type

Ensemble

Looking for pointers on how to create a file operation class that would use the HL7 Message Type in the file name

For example if the Message type was ADT^A31 the file name would be ZLOG_FILE_ADTA31_20190927.dat

Thank you for looking.

  • 0
  • 0
  • 58
  • 4
  • 1

Answers

Here is a solution I have knocked up that uses the functionality of the Ensemble File Adapter and the HL7 File Operation class

Class User.ExampleHL7FileOperation Extends (Ens.BusinessOperation, EnsLib.HL7.Operation.FileOperation) [ Language = objectscript ]
{ Parameter ADAPTER = "EnsLib.File.OutboundAdapter"; Property Adapter As EnsLib.File.OutboundAdapter; Parameter INVOCATION = "Queue"; Method WriteMessage(pRequest As EnsLib.HL7.Message, Output pResponse As EnsLib.HL7.Message) As %Status
{
        // Initialise your return status variable and a variable called file
        // I use $ztrap to trap code errors though TRY/CATCH would be the more modern approach
        set tSC=$$$OK,$ztrap="Error",file=""
        // Create your file name and append to the Adapter Attribute 'FilePath'
        // if working with UNIX change the next line of code accordingly
        if $e(..Adapter.FlePath,*)'="\" set ..Adapter.FilePath=..Adapter.FilePath_"\"
        set file=..Adapter.FilePath_"ZLOG_FILE_"_pRequest.GetValueAt("MSH::MessageType.MessageStructure",,tSC)_$tr($zdt($h,3),"-: ","")_".txt" if 'tSC quit tSC
        // You can use the appropriate method in the File Outbound Adapter though I have used the OPEN
        // command
        open file:("WNS"):0
        else  set tSC=$system.Status.Error(5001,"Cannot create File: "_file) goto End
        // Invoke the outputDocument method inherited from the class 'EnsLib.HL7.Operation.FileOperation'
        set tSC=..outputDocument(file,pRequest) if 'tSC goto End
End ;
        // Close the file if it exists
        if file'="" close file
        set pResponse=##class(EnsLib.HL7.Message).%New()
        // Populate the response HL7 message as you see fit. Either send back an HL7 ACK or NACK would be
        // the most appropriate
        quit tSC
Error ;
        set $ztrap="",tSC=$system.Status.Error($$$GeneralError,"Code Error: "_$ze) goto End
} XData MessageMap
        {
        <MapItems>
                 <MapItem MessageType="EnsLib.HL7.Message">
                        <Method>WriteMessage</Method>
                </MapItem>
        </MapItems>
        }

}

I have not trested the code but hopefully my coments will set you on the right path to this solution

1) $replace(pRequest.GetValueAt("MSH::MessageType.MessageType),"^","") instead of pRequest.GetValueAt("MSH::MessageType.MessageStructure",,tSC)

2) $e($zdt($h,8),1,8) instead of $tr($zdt($h,3),"-: ","")

I've found that the MessageStructure component is, more often than not, unvalued.

This should do the trick for all cases where there is a minimally compliant MSH:9 field:

$REPLACE($PIECE(pRequest.GetValueAt("MSH:MessageType"),pRequest.CS,1,2),pRequest.CS,"")

Thank You!!!

Here is what seems to work right now:

Class FCH.HL7.Operation.FileOperation Extends (Ens.BusinessOperation, EnsLib.HL7.Operation.FileOperation) [ Language = cache ]
{ /// From https://community.intersystems.com/post/pointers-creating-operation-file...
/// Nigel Salm, Michel Bruyère, Jeffrey Drumm
Parameter ADAPTER = "EnsLib.File.OutboundAdapter"; Property Adapter As EnsLib.File.OutboundAdapter; Parameter INVOCATION = "Queue"; Method WriteMessage(pRequest As EnsLib.HL7.Message, Output pResponse As EnsLib.HL7.Message) As %Status
{
// Initialise your return status variable and a variable called file
// I use $ZTRAP to trap code errors though TRY/CATCH would be the more modern approach
set tSC=$$$OK,$ZTRAP="Error",file=""
// Create your file name and append to the Adapter Attribute 'FilePath'
// if working with UNIX change the next line of code accordingly
//if $e(..Adapter.FilePath,*)'="\" set ..Adapter.FilePath=..Adapter.FilePath_"\"
if $e(..Adapter.FilePath,*)'="/" set ..Adapter.FilePath=..Adapter.FilePath_"/"
set file=..Adapter.FilePath_"ZLOG_FILE_"_$REPLACE($PIECE(pRequest.GetValueAt("MSH:MessageType"),pRequest.CS,1,2),pRequest.CS,"")_$tr($zdt($h,3),"-: ","")_".txt" if 'tSC quit tSC
$$$LOGINFO("The File Name is="_file)
// You can use the appropriate method in the File Outbound Adapter though I have used the OPEN
// command
open file:("WNS"):0
else  
set tSC=$system.Status.Error(5001,"Cannot create File: "_file) goto End
// Invoke the outputDocument method inherited from the class 'EnsLib.HL7.Operation.FileOperation'
set tSC=..outputDocument(file,pRequest) if 'tSC goto End
End ;
// Close the file if it exists
if file'="" close file
set pResponse=##class(EnsLib.HL7.Message).%New()
// Populate the response HL7 message as you see fit. Either send back an HL7 ACK or NACK would be
// the most appropriate
quit tSC
Error ;
set $ztrap="",tSC=$system.Status.Error($$$GeneralError,"Code Error: "_$ze) goto End
} XData MessageMap
{
<MapItems>
                 <MapItem MessageType="EnsLib.HL7.Message">
                        <Method>WriteMessage</Method>
                </MapItem>
        </MapItems>
} }

result of sending an ORM

ERROR #5001: Cannot create File: /message_archive/inbound_messages/ZLOG_FILE_ORMO0120191003113432.txt
 

Now to work on that error and get the messages in one file.  BUT THANKS