/// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="categories">Categories.</param>
 /// <param name="subjects">Elements to be validated.</param>
 public ModelValidationContext(ModelValidationCategories categories, IEnumerable<ModelElement> subjects)
 {
     
     this.subjects = new List<ModelElement>(subjects);
     this.categories = categories;
     this.collectedViolations = new List<IValidationMessage>();
 }
        /// <summary>
        /// Do validation for a single element.
        /// </summary>
        /// <param name="subject">The subject to validate.</param>
        /// <param name="categories">Categories.</param>
        /// <returns>Returns true if no error/warning/message are found.</returns>
        public bool Validate(ModelElement subject, ModelValidationCategories categories)
        {
            List <ModelElement> list = new List <ModelElement>(1);

            list.Add(subject);
            return(Validate(list, categories));
        }
 /// <summary>
 /// Constructor for class ValidationMethodAttribute.
 /// </summary>
 /// <param name="categories">The categories of this method.</param>
 public ModelValidationMethodAttribute(ModelValidationCategories categories)
 {
     this.categories = categories;
 }
 /// <summary>
 /// Constructor for class ValidationMethodAttribute. Specifies Menu, Open and Save by default.
 /// </summary>
 public ModelValidationMethodAttribute()
 {
     categories = ModelValidationCategories.Menu | ModelValidationCategories.Open | ModelValidationCategories.Save;
 }
 /// <summary>
 /// Constructor for class ValidationMethodAttribute.
 /// </summary>
 /// <param name="categories">The categories of this method.</param>
 public ModelValidationMethodAttribute(ModelValidationCategories categories)
 {
     this.categories = categories;
 }
 /// <summary>
 /// Constructor for class ValidationMethodAttribute. Specifies Menu, Open and Save by default.
 /// </summary>
 public ModelValidationMethodAttribute()
 {
     categories = ModelValidationCategories.Menu | ModelValidationCategories.Open | ModelValidationCategories.Save;
 }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="categories">Categories.</param>
 /// <param name="subjects">Elements to be validated.</param>
 public ModelValidationContext(ModelValidationCategories categories, IEnumerable <ModelElement> subjects)
 {
     this.subjects            = new List <ModelElement>(subjects);
     this.categories          = categories;
     this.collectedViolations = new List <IValidationMessage>();
 }
        /*
         * /// <summary>
         * /// Initializes the static Validation map as well as the Validation is enabled fields in the actual instance of this class.
         * /// </summary>
         * public abstract void Initialize();
         *
         * /// <summary>
         * /// Initializes the static Validation map as well as the Validation is enabled fields in the actual instance of this class.
         * /// </summary>
         * /// <param name="controller">Controller to initalize</param>
         * /// <param name="discardController">Validation controllers to discard.</param>
         * public abstract void Initialize(ModelValidationController controller, List<IDomainModelServices> discardController);*/

        /// <summary>
        /// Do validation for a set of elements based on the validation categories
        /// </summary>
        /// <param name="subjects">The list of subjects to validate.</param>
        /// <param name="categories">Categories</param>
        /// <returns>Returns true if no error/warning/message are found.</returns>
        public virtual bool Validate(IEnumerable <ModelElement> subjects, ModelValidationCategories categories)
        {
            ModelValidationContext validationContext = new ModelValidationContext(categories, subjects);

            return(Validate(validationContext));
        }
        private void InternalValidate(ModelElement element, ModelValidationContext context)
        {
            try
            {
                // collect methods on those classes (main class and base classes), where validation is enabled
                List <System.Reflection.MethodInfo> list = new List <System.Reflection.MethodInfo>();

                Type type = element.GetType();
                while (type != null)
                {
                    if (ValidationIsEnabled.Keys.Contains(type))
                    {
                        bool isEnabled = ValidationIsEnabled[type];

                        // collect methods
                        Dictionary <System.Reflection.MethodInfo, ModelValidationCategories> methods = ValidationMap[type];
                        foreach (System.Reflection.MethodInfo methodInfo in methods.Keys)
                        {
                            ModelValidationCategories cat = methods[methodInfo];
                            if ((cat & context.Categories) != 0)
                            {
                                list.Add(methodInfo);
                            }
                        }
                    }
                    type = type.BaseType;
                }

                // invoke methods
                foreach (System.Reflection.MethodInfo methodInfo in list)
                {
                    try
                    {
                        methodInfo.Invoke(element, new object[] { context });
                    }
                    catch (Exception ex)
                    {
                        context.AddMessage(new ValidationMessage(ModelValidationMessageIds.ValidationIvokeMethodExceptionErrorId, ModelValidationViolationType.Error, ex.Message));
                    }
                }
            }
            catch (Exception ex)
            {
                context.AddMessage(new ValidationMessage(ModelValidationMessageIds.ValidationExceptionErrorId, ModelValidationViolationType.Error, ex.Message));
            }


            /*
             *
             * // see if validation is enabled on the class or on any of the base classes.
             * object[] attr = type.GetCustomAttributes(typeof(ModelValidationStateAttribute), true);
             * if (attr.Length == 0)
             *  return;
             *
             * bool isEnabled = false;
             * foreach(ModelValidationStateAttribute attribute in attr)
             *  if (attribute.ValidationState == ModelValidationState.Enabled)
             *  {
             *      isEnabled = true;
             *      break;
             *  }
             * if (!isEnabled)
             *  return;
             *
             * // collect methods on those classes (main class and base classes), where validation is enabled
             * List<System.Reflection.MethodInfo> list = new List<System.Reflection.MethodInfo>();
             *
             * while (type != null)
             * {
             *  // see if validation enabled on the current level
             *  attr = type.GetCustomAttributes(typeof(ModelValidationStateAttribute), false);
             *  if (attr.Length > 0)
             *  {
             *      ModelValidationStateAttribute attribute = attr[0] as ModelValidationStateAttribute;
             *      if( attribute.ValidationState != ModelValidationState.Disabled )
             *      {
             *          // collect methods
             *          System.Reflection.MethodInfo[] methodInfoArr = type.GetMethods(System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
             *          for (int i = 0; i < methodInfoArr.Length; i++)
             *          {
             *              System.Reflection.MethodInfo methodInfo = methodInfoArr[i];
             *              object[] objArr = methodInfo.GetCustomAttributes(typeof(ModelValidationMethodAttribute), false);
             *              if ((objArr != null) && (objArr.Length != 0))
             *              {
             *                  object[] methodAttr = methodInfo.GetCustomAttributes(typeof(ModelValidationMethodAttribute), false);
             *                  if (methodAttr.Length > 0)
             *                  {
             *                      ModelValidationMethodAttribute methodAttribute = methodAttr[0] as ModelValidationMethodAttribute;
             *                      if( (methodAttribute.Categories & context.Categories) != 0 )
             *                          list.Add(methodInfo);
             *                  }
             *              }
             *          }
             *      }
             *  }
             *
             *  type = type.BaseType;
             * }
             *
             * // invoke methods
             * foreach (System.Reflection.MethodInfo methodInfo in list)
             * {
             *  try
             *  {
             *      methodInfo.Invoke(element, new object[] { context });
             *  }
             *  catch (Exception ex)
             *  {
             *      context.AddMessage(new ModelValidationMessage(ModelValidationMessageIds.ValidationExceptionErrorId, ModelValidationViolationType.Error, ex.Message, null));
             *  }
             * }
             */
        }
 /// <summary>
 /// Validate the whole store.
 /// </summary>
 /// <param name="Store">Store to validate.</param>
 /// <param name="categories">Categories.</param>
 /// <returns>Returns true if no error/warning/message are found.</returns>
 public bool Validate(Store Store, ModelValidationCategories categories)
 {
     return(Validate(Store.ElementDirectory.AllElements, categories));
 }