2

Closed

Bug with OBX-5 repetitions in Varies.fixOBX5()

description

The fixOBX5() method in the Varies class of NHapi.Base.Model only 'fixes' the first repetition in the OBX-5 field. The code is currently this:
    /// <summary> Sets the data type of field 5 in the given OBX segment to the value of OBX-2.  The argument 
    /// is a Segment as opposed to a particular OBX because it is meant to work with any version.  
    /// </summary>
    public static void fixOBX5(ISegment segment, IModelClassFactory factory)
    {
        try
        {
            //get unqualified class name
            IPrimitive obx2 = (IPrimitive)segment.GetField(2, 0);
            Varies v = (Varies)segment.GetField(5, 0);

            if (obx2.Value == null)
            {
                if (v.Data != null)
                {
                    if (!(v.Data is IPrimitive) || ((IPrimitive)v.Data).Value != null)
                    {
                        throw new HL7Exception("OBX-5 is valued, but OBX-2 is not.  A datatype for OBX-5 must be specified using OBX-2.", HL7Exception.REQUIRED_FIELD_MISSING);
                    }
                }
            }
            else
            {
                //set class
                System.Type c = factory.GetTypeClass(obx2.Value, segment.Message.Version);
                //                Class c = NHapi.Base.Parser.ParserBase.findClass(obx2.getValue(), 
                //                                                segment.getMessage().getVersion(), 
                //                                                "datatype");
                v.Data = (IType)c.GetConstructor(new System.Type[] { typeof(IMessage) }).Invoke(new System.Object[] { v.Message });
            }
        }
        catch (HL7Exception e)
        {
            throw e;
        }
        catch (System.Exception e)
        {
            throw new HL7Exception(e.GetType().FullName + " trying to set data type of OBX-5", HL7Exception.APPLICATION_INTERNAL_ERROR, e);
        }
    }
Note the statement:
Varies v = (Varies)segment.GetField(5, 0);
This means that only the 0 (first) repetition of 5th field (OBX-5) is processed.

When processing an OBX segment with repetitions in OBX-5, the first repetition's data property will be correctly set to an instance of the type specified in OBX-2 but the remaining repetitions will not be correctly set.

I think the most straightforward solution is to iterate through the repetition values returned by calling GetField(5) (i.e. return all repetitions) like this:
    public static void fixOBX5(ISegment segment, IModelClassFactory factory)
    {
        try
        {
            //get unqualified class name
            IPrimitive obx2 = (IPrimitive)segment.GetField(2, 0);

            // Note that a local fix has been applied here to iterate through all repetitions in the OBX-5 field.
            // The original code only processed the first repetition by calling GetField(5, 0).
            foreach (IType repetition in segment.GetField(5))
            {
                Varies v = (Varies)repetition;

                if (obx2.Value == null)
                {
                    if (v.Data != null)
                    {
                        if (!(v.Data is IPrimitive) || ((IPrimitive)v.Data).Value != null)
                        {
                            throw new HL7Exception("OBX-5 is valued, but OBX-2 is not.  A datatype for OBX-5 must be specified using OBX-2.", HL7Exception.REQUIRED_FIELD_MISSING);
                        }
                    }
                }
                else
                {
                    //set class
                    System.Type c = factory.GetTypeClass(obx2.Value, segment.Message.Version);
                    //                Class c = NHapi.Base.Parser.ParserBase.findClass(obx2.getValue(), 
                    //                                                segment.getMessage().getVersion(), 
                    //                                                "datatype");
                    v.Data = (IType)c.GetConstructor(new System.Type[] { typeof(IMessage) }).Invoke(new System.Object[] { v.Message });
                }
            }
        }
        catch (HL7Exception e)
        {
            throw e;
        }
        catch (System.Exception e)
        {
            throw new HL7Exception(e.GetType().FullName + " trying to set data type of OBX-5", HL7Exception.APPLICATION_INTERNAL_ERROR, e);
        }
    }
Closed Jan 28, 2015 at 7:40 AM by djedwards

comments

jrbateman wrote Nov 27, 2014 at 2:38 AM

I too, ran into this problem parsing multiple OBX elements. I downloaded the source code for the "fix" , compiled it and it worked beautifully. Will look forward to this fix being incorporated into the master download. My thanks to you for your work.

James

djedwards wrote Jan 28, 2015 at 7:40 AM

Thanks steveshort, merged and released as part of version 2.4.0.3