/// <param name="message">a parsed message to validate (note that MSH-9-1 and MSH-9-2 must be valued)
        /// </param>
        /// <returns> true if the message is OK
        /// </returns>
        /// <throws>  HL7Exception if there is at least one error and this validator is set to fail on errors </throws>
        public virtual bool validate(IMessage message)
        {
            Terser t = new Terser(message);

            IMessageRule[] rules = myContext.getMessageRules(message.Version, t.Get("MSH-9-1"), t.Get("MSH-9-2"));

            ValidationException toThrow = null;
            bool result = true;

            for (int i = 0; i < rules.Length; i++)
            {
                ValidationException[] ex = rules[i].test(message);
                for (int j = 0; j < ex.Length; j++)
                {
                    result = false;
                    ourLog.Error("Invalid message", ex[j]);
                    if (failOnError && toThrow == null)
                    {
                        toThrow = ex[j];
                    }
                }
            }

            if (toThrow != null)
            {
                throw new HL7Exception("Invalid message", toThrow);
            }

            return(result);
        }
Пример #2
0
        /// <summary>
        /// Get message rules
        /// </summary>
        /// <param name="theVersion"></param>
        /// <param name="theMessageType"></param>
        /// <param name="theTriggerEvent"></param>
        /// <returns></returns>
        public IMessageRule[] getMessageRules(string theVersion, string theMessageType, string theTriggerEvent)
        {
            IMessageRule[] result = new IMessageRule[0];
            if (originalContext != null)
            {
                result = originalContext.getMessageRules(theVersion, theMessageType, theTriggerEvent);
            }

            foreach (ISpecificMessageRule rule in messageRules)
            {
                // Add the rule if it applies
                bool flag1 = rule.GetVersions().Contains("*") || rule.GetVersions().Contains(theVersion);
                bool flag2 = rule.GetMessageTypes().Contains("*") || rule.GetMessageTypes().Contains(theMessageType);
                bool flag3 = rule.GetTriggerEvents().Contains("*") || rule.GetTriggerEvents().Contains(theTriggerEvent);
                if (flag1 && flag2 && flag3)
                {
                    List <IMessageRule> rules = new List <IMessageRule>(result);
                    rules.Add(rule);
                    result = rules.ToArray();
                }
            }

            return(result);
        }