4

In NHapi, how can we parse a message if we don't know what messageType (MSH#9) it is?

var parser = new NHapi.Base.Parser.PipeParser();

IMessage parsedMessage = parser.Parse(SampleMessage);

parsedMessage is a NHapi.Base.Model.GenericMessage.V25 at runtime and I can't seem to read in the MSH header to read the MessageType field and then re-parse(?) the message as that message type.

I'm frustrated by the lack of documentation and examples. Perhaps I'm very far off base. I am very new to HL7, but I thought I was doing well understanding the HL7 spec until I tried using NHapi.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
Jason Kleban
  • 20,024
  • 18
  • 75
  • 125

1 Answers1

8

parsedMessage.GetStructureName() will give you the message type and trigger event. parser.Encode(parsedMessage) will give you the message back in pipe-delimited format.

The following code shows how to get the message type and also how to get the original message in pipe format.

public static String ParseMessage(String message)
{
    var parser = new NHapi.Base.Parser.PipeParser();
    var parsedMessage = parser.Parse(message);

    //Get the message type and trigger event
    var msgType = parsedMessage.GetStructureName();

    //Get the message in raw, pipe-delimited format
    var pipeDelimitedMessage = parser.Encode(parsedMessage);

    return pipeDelimitedMessage;
}

Some good starter code can be found at the hapi examples site.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
Mike Stonis
  • 2,184
  • 14
  • 24
  • Hi Mike. Thanks for responding. When I use `parser.Parse(SampleMessage).GetStructureName();` I only get "GenericMessage+V25" even though I'm expecting to see something related to ORMO01 based on the content of SampleMessage. – Jason Kleban Dec 20 '11 at 14:14
  • 1
    Can you provide a sample deidentified message (e.g. a message with no real patient data)? It could be that the inbound message has parsing errors and it not coming in properly. Also, do you have the NHapi.Model.V25 assembly referenced in your project? I noticed that if I remove the reference to the V25 assembly, I get the same message type as you have. – Mike Stonis Dec 20 '11 at 15:32
  • Hi Mike - it was the missing reference. I didn't understand how the models work - I had 2.51, but not 2.5. I thought they were backward-compatible. Adding the additional model dlls makes it work. Thanks. – Jason Kleban Dec 20 '11 at 17:14
  • Ideally, new versions of HL7 are backwards with older messages. If you want to use one canonical model/version, you might want to consider creating a pre-processor before sending it to nHapi that changes all messages version to 2.5.1. This way, you can process more generically. Best of luck. – Mike Stonis Dec 20 '11 at 17:41
  • @MikeStonis: Can you Please provide "good starter code" for .NET users/testers. The link which you were provided above `Hapi Examples site` is for Java not for C#.NET. – venkat Jan 27 '16 at 12:17