Example #1
0
        /// <summary> Parses a segment string and populates the given Segment object.  Unexpected fields are
        /// added as Varies' at the end of the segment.  
        /// 
        /// </summary>
        /// <throws>  HL7Exception if the given string does not contain the </throws>
        /// <summary>      given segment or if the string is not encoded properly
        /// </summary>
        public virtual void Parse(ISegment destination, String segment, EncodingCharacters encodingChars)
        {
            int fieldOffset = 0;
            if (IsDelimDefSegment(destination.GetStructureName()))
            {
                fieldOffset = 1;
                //set field 1 to fourth character of string
                Terser.Set(destination, 1, 0, 1, 1, Convert.ToString(encodingChars.FieldSeparator));
            }

            String[] fields = Split(segment, Convert.ToString(encodingChars.FieldSeparator));

            for (int i = 1; i < fields.Length; i++)
            {
                String[] reps = Split(fields[i], Convert.ToString(encodingChars.RepetitionSeparator));
                if (log.DebugEnabled)
                {
                    log.Debug(reps.Length + "reps delimited by: " + encodingChars.RepetitionSeparator);
                }

                //MSH-2 will get split incorrectly so we have to fudge it ...
                bool isMSH2 = IsDelimDefSegment(destination.GetStructureName()) && i + fieldOffset == 2;
                if (isMSH2)
                {
                    reps = new String[1];
                    reps[0] = fields[i];
                }

                for (int j = 0; j < reps.Length; j++)
                {
                    try
                    {
                        StringBuilder statusMessage = new StringBuilder("Parsing field ");
                        statusMessage.Append(i + fieldOffset);
                        statusMessage.Append(" repetition ");
                        statusMessage.Append(j);
                        log.Debug(statusMessage.ToString());

                        if (i > 0 && i <= destination.NumFields() && j < destination.GetMaxCardinality(i))
                        {
                            IType field = destination.GetField(i + fieldOffset, j);
                            if (isMSH2)
                            {
                                Terser.getPrimitive(field, 1, 1).Value = reps[j];
                            }
                            else
                            {
                                Parse(field, reps[j], encodingChars);
                            }
                        }
                        else
                        {
                            StringBuilder errorMessage = new StringBuilder("Unexpected repitition for field ");
                            statusMessage.Append(i + fieldOffset);
                            statusMessage.Append(" repetition ");
                            statusMessage.Append(j);
                            statusMessage.Append(", ignoring extra repititions.");
                            log.Debug(statusMessage.ToString());
                        }
                    }
                    catch (HL7Exception e)
                    {
                        //set the field location and throw again ...
                        e.FieldPosition = i + fieldOffset;
                        e.SegmentRepetition = MessageIterator.getIndex(destination.ParentStructure, destination).rep;
                        e.SegmentName = destination.GetStructureName();
                        throw;
                    }
                }
            }

            //set data type of OBX-5
            if (destination.GetType().FullName.IndexOf("OBX") >= 0)
            {
                Varies.fixOBX5(destination, Factory);
            }
        }