0

When attempting to use ca.uhn.hl7v2.util.Terser to set empty fields on a given specific subclass of ca.uhn.hl7v2.model.Message (in this case ca.uhn.hl7v2.model.v251.message.ORU_R01), I receive no error messages during the .each{} closure and afterwards the message object has no fields populated.

hl7Map is populated on class instantiation, with values like: def hl7Map= [ "HL7MessageFields":['PID-3-1':"internal order map key", 'PID-3-4':"internal order map key",etc.]]

Code below:

def buildHL7Message(order){

    def date = new Date()
    def format = new SimpleDateFormat(hl7Map["dateFormat"]).format(date)

    //Set date on the Message Header Object
    hl7Map["MSH"]["-7"]= format

    def message = (context.getModelClassFactory().getMessageClass(hl7Map["MessageInstantiationMap"]["messageType"],
                                                                    hl7Map["MessageInstantiationMap"]["version"],
                                                                    true) as Class).newInstance()
    Terser terser = new Terser(message)

    hl7Map["HL7MessageFields"].each{
        terser.set(it.key, order[it.value])
    }

    println message

    return message

}

The end of method results in no output and error logged about encoding, MSH-1 is a required field, pipe delminator but is empty. If the code above uses message.initQuickstart("ORU", "R01", "T"), only the default initQuickstart fields are populated.

If hl7Map["HL7MessageFields"] contains a 'it.key' that is not a valid Group/Segment field, an error is logged by terser that it failed to find the value, the above code with a properly formatted map does not cause an error.

Can anyone help explain why I am not receiving errors yet my message is empty, and help me to populate the message with the appropriate terser.set(params)?

Matt Rkiouak
  • 141
  • 7

1 Answers1

1

Found solution that worked for me after a couple of hours of searching.

The message object's internal representation has a tree like structure where the MSH Segment is the parent, and the segments located after the MSH segment are child segments. Because of this structuring, MSH fields must be set as my original code does, but child segment fields must be set with "/." prepended to the map key devised (i.e. "PID-3-1" must become "/.PID-3-1" in the terser.set() line.

The hl7Map format was updated to better support this terser.set() syntactical requirement.

From the terser documentation, the / indicates search should start at the root of the message, and from an answer on a HAPI mail list link I've now lost, the . indicates search should include child elements of the MSH.

Full code below:

def buildHL7Message(order){

    def date = new Date()
    def format = new SimpleDateFormat(hl7Map["dateFormat"]).format(date)

    //Set date on the Message Header Object
    hl7Map["MSH"]["-7"]= format

    //See http://stackoverflow.com/questions/576955/groovy-way-to-dynamically-invoke-a-static-method
    //And
    //http://stackoverflow.com/questions/7758398/groovy-way-to-dynamically-instantiate-a-class-from-string

    def message = (context.getModelClassFactory().getMessageClass(hl7Map["MessageInstantiationMap"]["messageType"],
                                                                    hl7Map["MessageInstantiationMap"]["version"],
                                                                    true) as Class).newInstance()
    Terser terser = new Terser(message)

    hl7Map["MSH"].each{
        terser.set("MSH"+it.key, it.value)
    }
    hl7Map["HL7MSHChildSegmentMap"].each{
        terser.set(("/."+it.key) as String, order[it.value] as String)
    }

    println message

    return message
}
Matt Rkiouak
  • 141
  • 7