/// <summary>
        /// Checks rules for a property, validating the property against all shared and instance rules for the property.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="value">The value.</param>
        /// <param name="calledByCheckAllRules">The called by check all rules.</param>
        /// <returns><c>true</c> if this property is valid; otherwise, <c>false</c>.</returns>
        Boolean CheckRulesForProperty(String propertyName, Object value, CalledByCheckAllRules calledByCheckAllRules = CalledByCheckAllRules.No)
        {
            Boolean foundBrokenRule    = false;
            Boolean removedBrokenRules = false;

            //CheckAllRules clears all BrokenRules before calling, so no reason to run this code.
            if (calledByCheckAllRules == CalledByCheckAllRules.No)
            {
                foreach (var item in (from r in BrokenRules where r.PropertyName == propertyName select r).ToList())
                {
                    BrokenRules.Remove(item);
                    removedBrokenRules = true;
                }
            }

            foreach (ValidationRule vr in ValidationRulesManager.GetRulesForProperty(propertyName))
            {
                if (!vr.IsValid(value))
                {
                    BrokenRules.Add(new BrokenRule(vr.PropertyName, vr.ErrorMessage));
                    foundBrokenRule = true;
                }
            }

            //if CheckAllRules called this method, there is no reason to send out change
            //  notification for every property since these properties are scoped to the entity.
            //  CheckAllRules will raise the notificaitons when completed.
            if (calledByCheckAllRules == CalledByCheckAllRules.No && (foundBrokenRule || removedBrokenRules))
            {
                RaiseBrokenRuleFountNotification(foundBrokenRule);
            }
            return(foundBrokenRule);
        }
Exemplo n.º 2
0
        private void UpdateState()
        {
            Popup popup = (Popup)FindName("popup");

            if (popup != null)
            {
                popup.IsOpen = false;
            }

            BusinessBase businessObject = Source as BusinessBase;

            if (businessObject != null)
            {
                // for some reason Linq does not work against BrokenRulesCollection...
                List <BrokenRule> allRules = new List <BrokenRule>();
                foreach (var r in businessObject.BrokenRulesCollection)
                {
                    if (r.Property == Property)
                    {
                        allRules.Add(r);
                    }
                }

                var removeRules = (from r in BrokenRules
                                   where !allRules.Contains(r)
                                   select r).ToArray();

                var addRules = (from r in allRules
                                where !BrokenRules.Contains(r)
                                select r).ToArray();

                foreach (var rule in removeRules)
                {
                    BrokenRules.Remove(rule);
                }
                foreach (var rule in addRules)
                {
                    BrokenRules.Add(rule);
                }

                BrokenRule worst = (from r in BrokenRules
                                    orderby r.Severity
                                    select r).FirstOrDefault();

                if (worst != null)
                {
                    _worst = worst.Severity;
                }

                _isValid = BrokenRules.Count == 0;
                GoToState(true);
            }
            else
            {
                BrokenRules.Clear();
                _isValid = true;
                GoToState(true);
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Validate the rules list
 /// </summary>
 /// <param name="list">list of rules</param>
 private void ValidateRulesList(List <ValidateRuleBase> list)
 {
     foreach (ValidateRuleBase rule in list)
     {
         PropertyInfo info = _parentObject.GetType().GetProperty(rule.PropertyName);
         if (info != null)
         {
             object value = info.GetValue(_parentObject, null);
             if (rule.Validate(value))
             {
                 BrokenRules.Remove(rule);
             }
             else
             {
                 BrokenRules.Add(rule);
             }
         }
         else
         {
             throw new ArgumentException(string.Format("Property \"{0}\" not found on object \"{1}\"", rule.PropertyName, _parentObject.GetType().ToString()));
         }
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Updates the state on control Property.
        /// </summary>
        protected virtual void UpdateState()
        {
            if (_loading)
            {
                return;
            }

            Popup popup = (Popup)FindChild(this, "popup");

            if (popup != null)
            {
                popup.IsOpen = false;
            }

            if (Source == null || string.IsNullOrEmpty(PropertyName))
            {
                BrokenRules.Clear();
                RuleDescription = string.Empty;
                IsValid         = true;
                CanWrite        = false;
                CanRead         = false;
            }
            else
            {
                var iarw = Source as Csla.Security.IAuthorizeReadWrite;
                if (iarw != null)
                {
                    CanWrite = iarw.CanWriteProperty(PropertyName);
                    CanRead  = iarw.CanReadProperty(PropertyName);
                }

                BusinessBase businessObject = Source as BusinessBase;
                if (businessObject != null)
                {
                    var allRules = (from r in businessObject.BrokenRulesCollection
                                    where r.Property == PropertyName
                                    select r).ToArray();

                    var removeRules = (from r in BrokenRules
                                       where !allRules.Contains(r)
                                       select r).ToArray();

                    var addRules = (from r in allRules
                                    where !BrokenRules.Contains(r)
                                    select r).ToArray();

                    foreach (var rule in removeRules)
                    {
                        BrokenRules.Remove(rule);
                    }
                    foreach (var rule in addRules)
                    {
                        BrokenRules.Add(rule);
                    }

                    IsValid = BrokenRules.Count == 0;

                    if (!IsValid)
                    {
                        BrokenRule worst = (from r in BrokenRules
                                            orderby r.Severity
                                            select r).FirstOrDefault();

                        if (worst != null)
                        {
                            RuleSeverity    = worst.Severity;
                            RuleDescription = worst.Description;
                        }
                        else
                        {
                            RuleDescription = string.Empty;
                        }
                    }
                    else
                    {
                        RuleDescription = string.Empty;
                    }
                }
                else
                {
                    BrokenRules.Clear();
                    RuleDescription = string.Empty;
                    IsValid         = true;
                }
            }
            GoToState(true);
        }