Question
· Nov 15, 2016

Create EnsLib.HL7.Message from %String or file?

For testing purposes, I would like to create an EnsLib.HL7.Message from either a %String or a file on disk and pass it into a function in a unit test (outside of an Ensemble context). EnsLib.HL7.Parser appears to do the necessary parsing, but it also sends the message via Ensemble service rather than returning it as a value. Is there a way to leverage the parsing outside of a Ensemble service? Alternatively, is there another way to create an EnsLib.HL7.Message?

Discussion (4)0
Log in or sign up to continue

Take a look at the EnsLib.HL7.Message class.  It has the following method to import from %String:

classmethod ImportFromString(pString As %String, Output pStatus As %Status, ByRef pConfigItem As %String, pIOFormatClassname As %String) as EnsLib.HL7.Message

It also provides similar methods for importing from streams, which could be a file character stream.  You can view all of these methods using the class reference from the documentation.  Hope that helps.

Here is a code sample that creates the Ensemble representation of an HL7 message from a string and sets it's metadata according to the information found in the MSH segment (using HL7 version 2.5 schema):

/// Create EnsLib.HL7.Message object from HL7 string
Method CreateHL7Message(pHL7String as %String, Output pHL7 As EnsLib.HL7.Message) As %Status
{
    #Dim sc as %Status
    #Dim ex as %Exception.AbstractException
    #Dim tMSH as EnsLib.HL7.Segment    
    
    Try    {
        
        // Create HL7 message object
        set pHL7=##class(EnsLib.HL7.Message).ImportFromString(pHL7String,.sc) $$$ThrowOnError(sc)
        
        // Determine DocType from MSH
        set tMSH=pHL7.getSegmentByIndex(1)
        set tName=tMSH.GetValueAt(9,":_~\&")
        set tDocType=##class(EnsLib.HL7.Schema).ResolveSchemaTypeToDocType("2.5",tName, sc) $$$ThrowOnError(sc)
        
        // Set DocType and build map
        set pHL7.DocType=tDocType
        set sc=pHL7.BuildMap()    $$$ThrowOnError(sc)
        
    
    }
    Catch ex
    {
        // don't return HL7 object in case of an error
        kill pHL7
        set sc=ex.AsStatus()
    }
    Quit sc
}

}

I do this in terminal all the time.  When you do this, you also need to manually set the DocType.  Here's an example:

ENSEMBLE>set msg = ##class(EnsLib.HL7.Message).ImportFromFile("C:\InterSystems\HL7Messages\ADT_A01.txt")

ENSEMBLE>set msg.DocType="2.3.1:ADT_A01"

ENSEMBLE>write msg.GetValueAt("MSH:9")
ADT^A01
ENSEMBLE>write msg.GetValueAt("PID:DateTimeOfBirth.timeofanevent")
19560129
ENSEMBLE>