Search code examples
c#.nethl7nhapi

nhapi - Modifiy a segment value


Env :

Visual Studio 2013, Winform / C# / Framework 4.5, nHapi DLL 2.4.0.9, HL7 Version 2.3

I'm building a little windows application that read HL7 messages and send them to an Interface system. Everything is working just fine but I would like to know if it's possible to replace/add/modify a segment value : EVN 5.2 (Operator ID / Family name).

At the moment, I'm reading the content of the HL7 file on the computer, put the content in a string, parse the message, encode the message and return it.

    public static String ParseMessage(String message)
    {
        var parser = new NHapi.Base.Parser.PipeParser();
        var parsedMessage = parser.Parse(message);
        /* I guess it's here that I should do the change for the EVN 5.2 ? But How ;-) */
        var msgType = parsedMessage.GetStructureName();
        var pipeDelimitedMessage = parser.Encode(parsedMessage);

        return pipeDelimitedMessage;
    }

Thanks everyone for you help

Richard


Solution

  • The way the nHapi would have you do this is cast the 'parsed' abstract message down to its concrete type so that you are able to walk the object model and set the properties you'd like.

    As an example, take the case of an ADT A01 admit message:

        [Test]
        public void TestPopulateEVNOperaterID()
        {
            string message = @"MSH|^~\&|SUNS1|OVI02|AZIS|CMD|200606221348||ADT^A01|1049691900|P|2.3
    EVN|A01|200601060800
    PID||8912716038^^^51276|0216128^^^51276||BARDOUN^LEA SACHA||19981201|F|||AVENUE FRANC GOLD 8^^LUXEMBOURGH^^6780^150||053/12456789||N|S|||99120162652||^^^|||||B
    PV1||O|^^|U|||07632^MORTELO^POL^^^DR.|^^^^^|||||N||||||0200001198
    PV2|||^^AZIS||N|||200601060800
    IN1|0001|2|314000|||||||||19800101|||1|BARDOUN^LEA SACHA|1|19981201|AVENUE FRANC GOLD 8^^LUXEMBOURGH^^6780^150|||||||||||||||||ZIN|0164652011399|0164652011399|101|101|45789^Broken bone";
    
            var parser = new PipeParser();
            var abstractMessage = parser.Parse(message);
    
            // this is the normal / expected way of working with NHapi parsed messages
            var typedMessage = abstractMessage as ADT_A01;
            if (typedMessage != null)
            {
                typedMessage.EVN.OperatorID.FamilyName.Value = "Surname";
                typedMessage.EVN.OperatorID.GivenName.Value = "Firstname";
            }
    
            var pipeDelimitedMessage = parser.Encode(typedMessage);
    
            // alternatively, you can apply this modification to any HL7 2.3 message
            // with an EVN segment using this more generic method
            var genericMethod = abstractMessage as AbstractMessage;
            var evn = genericMethod.GetStructure("EVN") as EVN;
            if (evn != null)
            {
                evn.OperatorID.FamilyName.Value = "SurnameGeneric";
                evn.OperatorID.GivenName.Value = "FirstnameGeneric";
            }
    
            pipeDelimitedMessage = parser.Encode(typedMessage);
        }
    

    I believe the second more generic way is probably what you'll be wanting for this case, however I thought I'd just show you as well how to get to parsed / concrete type so that you can work with it that way if you are dealing with a specific message type.