Ejemplo n.º 1
0
        /// <summary>
        /// Validates the HL7 message for basic syntax
        /// </summary>
        /// <returns>A boolean indicating whether the whole message is valid or not</returns>
        private bool validateMessage()
        {
            try
            {
                if (!string.IsNullOrEmpty(HL7Message))
                {
                    //check message length - MSH+Delimeters+12Fields in MSH
                    if (HL7Message.Length < 20)
                    {
                        throw new HL7Exception("Message Length too short: " + HL7Message.Length + " chars.", HL7Exception.BAD_MESSAGE);
                    }

                    //check if message starts with header segment
                    if (!HL7Message.StartsWith("MSH"))
                    {
                        throw new HL7Exception("MSH segment not found at the beggining of the message", HL7Exception.BAD_MESSAGE);
                    }

                    this.Encoding.EvaluateSegmentDelimiter(this.HL7Message);
                    this.HL7Message = string.Join(this.Encoding.SegmentDelimiter, MessageHelper.SplitMessage(this.HL7Message)) + this.Encoding.SegmentDelimiter;

                    //check Segment Name & 4th character of each segment
                    char fourthCharMSH = HL7Message[3];
                    this.allSegments = MessageHelper.SplitMessage(HL7Message);

                    foreach (string strSegment in this.allSegments)
                    {
                        if (string.IsNullOrWhiteSpace(strSegment))
                        {
                            continue;
                        }

                        bool   isValidSegName = false;
                        string segmentName    = strSegment.Substring(0, 3);
                        string segNameRegEx   = "[A-Z][A-Z][A-Z1-9]";
                        isValidSegName = System.Text.RegularExpressions.Regex.IsMatch(segmentName, segNameRegEx);

                        if (!isValidSegName)
                        {
                            throw new HL7Exception("Invalid segment name found: " + strSegment, HL7Exception.BAD_MESSAGE);
                        }

                        char fourthCharSEG = strSegment[3];

                        if (fourthCharMSH != fourthCharSEG)
                        {
                            throw new HL7Exception("Invalid segment found: " + strSegment, HL7Exception.BAD_MESSAGE);
                        }
                    }

                    string _fieldDelimiters_Message = this.allSegments[0].Substring(3, 8 - 3);
                    this.Encoding.EvaluateDelimiters(_fieldDelimiters_Message);

                    // Count field separators, MSH.12 is required so there should be at least 11 field separators in MSH
                    int countFieldSepInMSH = this.allSegments[0].Count(f => f == Encoding.FieldDelimiter);

                    if (countFieldSepInMSH < 11)
                    {
                        throw new HL7Exception("MSH segment doesn't contain all the required fields", HL7Exception.BAD_MESSAGE);
                    }

                    // Find Message Version
                    var MSHFields = MessageHelper.SplitString(this.allSegments[0], Encoding.FieldDelimiter);

                    if (MSHFields.Count >= 12)
                    {
                        this.Version = MessageHelper.SplitString(MSHFields[11], Encoding.ComponentDelimiter)[0];
                    }
                    else
                    {
                        throw new HL7Exception("HL7 version not found in the MSH segment", HL7Exception.REQUIRED_FIELD_MISSING);
                    }

                    //Find Message Type & Trigger Event
                    try
                    {
                        string MSH_9 = MSHFields[8];

                        if (!string.IsNullOrEmpty(MSH_9))
                        {
                            var MSH_9_comps = MessageHelper.SplitString(MSH_9, this.Encoding.ComponentDelimiter);

                            if (MSH_9_comps.Count >= 3)
                            {
                                this.MessageStructure = MSH_9_comps[2];
                            }
                            else if (MSH_9_comps.Count > 0 && MSH_9_comps[0] != null && MSH_9_comps[0].Equals("ACK"))
                            {
                                this.MessageStructure = "ACK";
                            }
                            else if (MSH_9_comps.Count == 2)
                            {
                                this.MessageStructure = MSH_9_comps[0] + "_" + MSH_9_comps[1];
                            }
                            else
                            {
                                throw new HL7Exception("Message Type & Trigger Event value not found in message", HL7Exception.UNSUPPORTED_MESSAGE_TYPE);
                            }
                        }
                        else
                        {
                            throw new HL7Exception("MSH.10 not available", HL7Exception.UNSUPPORTED_MESSAGE_TYPE);
                        }
                    }
                    catch (System.IndexOutOfRangeException e)
                    {
                        throw new HL7Exception("Can't find message structure (MSH.9.3) - " + e.Message, HL7Exception.UNSUPPORTED_MESSAGE_TYPE);
                    }

                    try
                    {
                        this.MessageControlID = MSHFields[9];

                        if (string.IsNullOrEmpty(this.MessageControlID))
                        {
                            throw new HL7Exception("MSH.10 - Message Control ID not found", HL7Exception.REQUIRED_FIELD_MISSING);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new HL7Exception("Error occured while accessing MSH.10 - " + ex.Message, HL7Exception.REQUIRED_FIELD_MISSING);
                    }

                    try
                    {
                        this.ProcessingID = MSHFields[10];

                        if (string.IsNullOrEmpty(this.ProcessingID))
                        {
                            throw new HL7Exception("MSH.11 - Processing ID not found", HL7Exception.REQUIRED_FIELD_MISSING);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new HL7Exception("Error occured while accessing MSH.11 - " + ex.Message, HL7Exception.REQUIRED_FIELD_MISSING);
                    }
                }
                else
                {
                    throw new HL7Exception("No Message Found", HL7Exception.BAD_MESSAGE);
                }
            }
            catch (Exception ex)
            {
                throw new HL7Exception("Failed to validate the message with error - " + ex.Message, HL7Exception.BAD_MESSAGE);
            }

            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// validates the HL7 message for basic syntax
        /// </summary>
        /// <returns>boolean</returns>
        private bool ValidateMessage()
        {
            try
            {
                if (!String.IsNullOrEmpty(HL7Message))
                {
                    HL7Message = HL7Message.Trim(messageTrimChars);

                    //check message length - MSH+Delemeters+12Fields in MSH
                    if (HL7Message.Length < 20)
                    {
                        throw new HL7Exception("Message Length too short" + HL7Message.Length + " fields.", HL7Exception.BAD_MESSAGE);
                    }

                    //check if message starts with header segment
                    if (!HL7Message.StartsWith("MSH"))
                    {
                        throw new HL7Exception("MSH Segment not found at the beggining of the message", HL7Exception.BAD_MESSAGE);
                    }

                    //Find segment separator
                    if (HL7Message.Contains(DefaultSegmentSeparatorString[0]))
                    {
                        msgSegmentSeparatorIndex = 0;
                    }
                    else if (HL7Message.Contains(DefaultSegmentSeparatorString[1]))
                    {
                        msgSegmentSeparatorIndex = 1;
                    }
                    else
                    {
                        msgSegmentSeparatorIndex = 0;
                    }

                    //check Segment Name & 4th character of each segment
                    char fourthCharMSH = HL7Message[3];
                    AllSegments = MessageHelper.SplitString(HL7Message, new Char[1] {
                        DefaultSegmentSeparatorString[msgSegmentSeparatorIndex]
                    });

                    foreach (String strSegment in AllSegments)
                    {
                        bool   isValidSegName = false;
                        String segmentName    = strSegment.Substring(0, 3);
                        String segNameRegEx   = "[A-Z][A-Z][A-Z1-9]";
                        isValidSegName = System.Text.RegularExpressions.Regex.IsMatch(segmentName, segNameRegEx);

                        if (!isValidSegName)
                        {
                            throw new HL7Exception("Invalid Segment Name Found :" + strSegment, HL7Exception.BAD_MESSAGE);
                        }

                        char fourthCharSEG = strSegment[3];

                        if (fourthCharMSH != fourthCharSEG)
                        {
                            throw new HL7Exception("Invalid Segment Found :" + strSegment, HL7Exception.BAD_MESSAGE);
                        }
                    }

                    Char[] _fieldDelimiters_Message = AllSegments[0].Substring(3, 8 - 3).ToArray <Char>();
                    FieldDelimiters = _fieldDelimiters_Message;

                    //count field separators, MSH.12 is required so there should be at least 11 field separators in MSH
                    int countFieldSepInMSH = AllSegments[0].Count(f => f == FieldDelimiters[0]);

                    if (countFieldSepInMSH < 11)
                    {
                        throw new HL7Exception("MSH segment doesn't contain all required fields", HL7Exception.BAD_MESSAGE);
                    }

                    //Find Message Version
                    char[] fieldSeparator = new char[1] {
                        FieldDelimiters[0]
                    };
                    char[] componentSeparator = new char[1] {
                        FieldDelimiters[1]
                    };

                    List <String> MSHFields = AllSegments[0].Split(fieldSeparator, StringSplitOptions.None).ToList <String>();
                    if (MSHFields.Count >= 12)
                    {
                        this._Version = MSHFields[11].Split(componentSeparator, StringSplitOptions.None)[0];
                    }
                    else
                    {
                        throw new HL7Exception("HL7 version not found in MSH Segment", HL7Exception.REQUIRED_FIELD_MISSING);
                    }

                    //Find Message Type & Trigger Event

                    try
                    {
                        String MSH_9 = MSHFields[8];
                        if (!String.IsNullOrEmpty(MSH_9))
                        {
                            System.String[] MSH_9_comps = MSH_9.Split(componentSeparator, StringSplitOptions.None);
                            if (MSH_9_comps.Length >= 3)
                            {
                                this._MessageStructure = MSH_9_comps[2];
                            }
                            else if (MSH_9_comps.Length > 0 && MSH_9_comps[0] != null && MSH_9_comps[0].Equals("ACK"))
                            {
                                this._MessageStructure = "ACK";
                            }
                            else if (MSH_9_comps.Length == 2)
                            {
                                this._MessageStructure = MSH_9_comps[0] + "_" + MSH_9_comps[1];
                            }
                            else
                            {
                                throw new HL7Exception("Message Type & Trigger Event value not found in message", HL7Exception.UNSUPPORTED_MESSAGE_TYPE);
                            }
                        }
                        else
                        {
                            throw new HL7Exception("MSH.10 not available", HL7Exception.UNSUPPORTED_MESSAGE_TYPE);
                        }
                    }
                    catch (System.IndexOutOfRangeException e)
                    {
                        throw new HL7Exception("Can't find message structure (MSH.9.3) - " + e.Message, HL7Exception.UNSUPPORTED_MESSAGE_TYPE);
                    }

                    try
                    {
                        this._MessageControlID = MSHFields[9];

                        if (String.IsNullOrEmpty(this._MessageControlID))
                        {
                            throw new HL7Exception("MSH.10 - Message Control ID not found", HL7Exception.REQUIRED_FIELD_MISSING);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new HL7Exception("Error occured while accessing MSH.10 - " + ex.Message, HL7Exception.REQUIRED_FIELD_MISSING);
                    }

                    try
                    {
                        this._ProcessingID = MSHFields[10];

                        if (String.IsNullOrEmpty(this._ProcessingID))
                        {
                            throw new HL7Exception("MSH.11 - Processing ID not found", HL7Exception.REQUIRED_FIELD_MISSING);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new HL7Exception("Error occured while accessing MSH.11 - " + ex.Message, HL7Exception.REQUIRED_FIELD_MISSING);
                    }
                }
                else
                {
                    throw new HL7Exception("No Message Found", HL7Exception.BAD_MESSAGE);
                }
            }
            catch (Exception ex)
            {
                throw new HL7Exception("Failed to validate the message with error - " + ex.Message, HL7Exception.BAD_MESSAGE);
            }

            return(true);
        }