Search code examples
hl7nhapi

How to tell if a segment actually exists in a HL7 message via NHAPI?


I have an SIU S12 message that does not contain a PV2 segment. However, when I get the parsed message from NHAPI, the parent group for PV2, the SIU_S12_PATIENT group, return 1 for currentReps ("PV2"), which means the PV2 is present.

var parser = new NHapi.Base.Parser.PipeParser();
var parsedMessage = parser.Parse(message) as NHapi.Model.V231.Message.SIU_S12;
var patientGroup=parsedMessage.GetPATIENT(0);
// This call should not create the segment if it does not exist
int pv2Count=patientGroup.currentReps("PV2");
//pv2Count is 1 here despite no PV2 segment exists in the message
//also Both GetAll("PV2") and SegmentFinder say the PV2 segment is present
//DG1RepetitionsUsed is also 1 despite no DG1 segment is present in the message

I am trying to avoid writing code to evaluate every field in the segment. PV2 is just an example - there are a lot more segments that could be missing from the message source.

I am using NHAPI v 2.4, the latest version.

Update: following Tyson's suggestion I come up with this method;

var parser = new NHapi.Base.Parser.PipeParser();
var parsedMessage = parser.Parse(message) as NHapi.Model.V231.Message.SIU_S12;
var encodingChars = new NHapi.Base.Parser.EncodingCharacters('|', null);
var patientGroup = parsedMessage.GetPATIENT(0);
var dg1 = (NHapi.Model.V231.Segment.DG1) (patientGroup.GetStructure("DG1"));
string encodedDg1 = NHapi.Base.Parser.PipeParser.Encode(dg1, encodingChars);
bool dg1Exists = string.Compare(encodedDg1, 
    "DG1", StringComparison.InvariantCultureIgnoreCase)==0;

Solution

  • easiest thing that I have found to do is to determine if a segment is in a message is to search the actual string of the message for the segment name plus a pipe. So, for example

        if(message.Contains("PV2|"))
        {
            //do something neat
    
        }
    

    From my experience, it is either that, or examining every sub-field under the segment to see if there is a value.

    EDIT

    I found another way to check that might work a little better. The PipeParser class has a couple of static methods on it that takes in ISegment, IGroup, and IType objects that will return a string representation of the object NHapi reference.

    Sample code:

            string validTestMessages =
          "MSH|^~\\&|ADT1|MCM|LABADT|MCM|198808181126|SECURITY|ADT^A01|MSG00001|P|2.6\r" +
          "EVN|A01-|198808181123\r" +
          "PID|||PID1234^5^M11^HBOC^CPI^HV||JONES^WILLIAM^A^III||19610615000000|M||2106-3|1200 N ELM STREET^^GREENSBORO^NC^27401-1020|GL||||S||S|123456789|9-87654^NC\r" +
          "PV1|1|I|||||TEST^TEST^TEST||||||||||||||||||||||||||||||||||||||||||||\r";
    
            var encodingChars = new EncodingCharacters('|', null);
    
            PipeParser parser = new PipeParser();
    
            var message = parser.Parse(validTestMessages);
    
            PV1 pv1 = (PV1)message.GetStructure("PV1");
            var doctor = pv1.GetAttendingDoctor(0);
    
    
            string encodedMessage = PipeParser.Encode(pv1, encodingChars);
            Console.WriteLine(encodedMessage);
    
            encodedMessage = PipeParser.Encode(doctor, encodingChars);
            Console.WriteLine(encodedMessage);
    

    Output:

    PV1|1|I|||||TEST^TEST^TEST
    TEST^TEST^TEST
    

    if there is no segment or the item is empty, then the PiperParser will return an empty string.