Exemplo n.º 1
0
        /// <summary>
        /// Parse the HL7 message in text format, throws HL7Exception if error occurs
        /// </summary>
        /// <returns>boolean</returns>
        public bool ParseMessage()
        {
            bool isValid  = false;
            bool isParsed = false;

            try
            {
                isValid = ValidateMessage();
            }
            catch (HL7Exception ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new HL7Exception("Unhandeled Exceiption in validation - " + ex.Message, HL7Exception.BAD_MESSAGE);
            }

            if (isValid)
            {
                try
                {
                    if (AllSegments == null || AllSegments.Count <= 0)
                    {
                        AllSegments = MessageHelper.SplitString(HL7Message, new Char[1] {
                            DefaultSegmentSeparatorString[msgSegmentSeparatorIndex]
                        });
                    }

                    short SegSeqNo = 0;
                    foreach (String strSegment in AllSegments)
                    {
                        Segment segNew      = new Segment();
                        String  segmentName = strSegment.Substring(0, 3);
                        segNew.FieldDelimiters = new Char[] { FieldDelimiters[0], FieldDelimiters[1], FieldDelimiters[2], FieldDelimiters[4] };
                        segNew.Name            = segmentName;
                        segNew.Value           = strSegment;
                        segNew.SequenceNo      = SegSeqNo++;

                        if (SegmentList.ContainsKey(segmentName))
                        {
                            SegmentList[segmentName].List.Add(segNew);
                        }
                        else
                        {
                            SegmentList.Add(segmentName, segNew);
                            SegmentList[segmentName].List.Add(segNew);
                        }
                    }
                    _SegmentCount = SegSeqNo;

                    String strSerializedMessage = String.Empty;
                    try
                    {
                        strSerializedMessage = SerializeMessageWithoutValidate(); //validation not required here as we haven't changed anything in message
                    }
                    catch (HL7Exception ex)
                    {
                        throw new HL7Exception("Failed to serialize parsed message with error -" + ex.Message, HL7Exception.PARSING_ERROR);
                    }

                    if (!String.IsNullOrEmpty(strSerializedMessage))
                    {
                        if (HL7Message.Equals(strSerializedMessage))
                        {
                            isParsed = true;
                        }
                    }
                    else
                    {
                        throw new HL7Exception("Unable to serialize to origional message -", HL7Exception.PARSING_ERROR);
                    }
                }
                catch (Exception ex)
                {
                    throw new HL7Exception("Failed to parse the message with error -" + ex.Message, HL7Exception.PARSING_ERROR);
                }
            }
            return(isParsed);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Parse the HL7 message in text format, throws HL7Exception if error occurs
        /// </summary>
        /// <returns>boolean</returns>
        public bool ParseMessage()
        {
            bool isValid  = false;
            bool isParsed = false;

            try
            {
                isValid = ValidateMessage();
            }
            catch (HL7Exception ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new HL7Exception("Unhandled Exception in validation - " + ex.Message, HL7Exception.BAD_MESSAGE);
            }

            if (isValid)
            {
                try
                {
                    if (allSegments == null || allSegments.Count <= 0)
                    {
                        allSegments = MessageHelper.SplitString(HL7Message, this.Encoding.SegmentDelimiter);
                    }

                    short SegSeqNo = 0;
                    foreach (string strSegment in allSegments)
                    {
                        Segment newSegment  = new Segment(this.Encoding);
                        string  segmentName = strSegment.Substring(0, 3);
                        newSegment.Name       = segmentName;
                        newSegment.Value      = strSegment;
                        newSegment.SequenceNo = SegSeqNo++;

                        this.AddNewSegment(newSegment);
                    }
                    this.SegmentCount = SegSeqNo;

                    string strSerializedMessage = string.Empty;
                    try
                    {
                        strSerializedMessage = SerializeMessage(false);
                    }
                    catch (HL7Exception ex)
                    {
                        throw new HL7Exception("Failed to serialize parsed message with error - " + ex.Message, HL7Exception.PARSING_ERROR);
                    }

                    if (!string.IsNullOrEmpty(strSerializedMessage))
                    {
                        if (HL7Message.Equals(strSerializedMessage))
                        {
                            isParsed = true;
                        }
                    }
                    else
                    {
                        throw new HL7Exception("Unable to serialize to original message - ", HL7Exception.PARSING_ERROR);
                    }
                }
                catch (Exception ex)
                {
                    throw new HL7Exception("Failed to parse the message with error - " + ex.Message, HL7Exception.PARSING_ERROR);
                }
            }
            return(isParsed);
        }