/// <summary>
 /// Raises the HasBrokenRules event.
 /// </summary>
 /// <param name="e">Event arguments.</param>
 protected virtual void OnHasBrokenRules(HasBrokenRulesEventArgs e)
 {
     if (HasBrokenRules != null)
     {
         HasBrokenRules(this, e);
     }
 }
        private bool ExecuteSaveAction(ISavable savableObject, ITrackStatus trackableObject, CslaActionExtenderProperties props)
        {
            var  result       = true;
            bool okToContinue = true;

            BusinessBase businessObject = null;
            bool         savableObjectIsBusinessBase = savableObject is BusinessBase;

            if (savableObjectIsBusinessBase)
            {
                businessObject = savableObject as BusinessBase;
            }

            if (savableObjectIsBusinessBase)
            {
                if (!businessObject.IsValid)
                {
                    HasBrokenRulesEventArgs argsHasBrokenRules = new HasBrokenRulesEventArgs(
                        props.CommandName,
                        businessObject.GetBrokenRules().ErrorCount > 0,
                        businessObject.GetBrokenRules().WarningCount > 0,
                        businessObject.GetBrokenRules().InformationCount > 0,
                        _autoShowBrokenRules);

                    OnHasBrokenRules(argsHasBrokenRules);

                    okToContinue = !argsHasBrokenRules.Cancel;
                    //in case the client changed it
                    _autoShowBrokenRules = argsHasBrokenRules.AutoShowBrokenRules;
                }
            }

            if (okToContinue)
            {
                if (savableObjectIsBusinessBase)
                {
                    if (_autoShowBrokenRules && !businessObject.IsValid)
                    {
                        // todo: add child broken rules
                        string brokenRules = string.Empty;
                        foreach (var brokenRule in businessObject.GetBrokenRules())
                        {
                            var lambdaBrokenRule = brokenRule;
                            var friendlyName     =
                                PropertyInfoManager.GetRegisteredProperties(businessObject.GetType()).Find(
                                    c => c.Name == lambdaBrokenRule.Property).FriendlyName;
                            brokenRules += string.Format("{0}: {1}{2}", friendlyName, brokenRule, Environment.NewLine);
                        }
                        MessageBox.Show(brokenRules, Resources.ActionExtenderErrorCaption,
                                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                if (trackableObject.IsValid)
                {
                    CslaActionCancelEventArgs savingArgs = new CslaActionCancelEventArgs(false, props.CommandName);
                    OnObjectSaving(savingArgs);

                    if (!savingArgs.Cancel)
                    {
                        _bindingSourceTree.Apply();
                        ISavable objectToSave;

                        if (Csla.ApplicationContext.AutoCloneOnUpdate == false)
                        {
                            objectToSave = ((ICloneable)savableObject).Clone() as ISavable;// if not AutoClone, clone manually
                        }
                        else
                        {
                            objectToSave = savableObject;
                        }

                        if (objectToSave != null)
                        {
                            try
                            {
                                RemoveEventHooks(savableObject);
                                savableObject = savableObject.Save() as ISavable;

                                OnObjectSaved(new CslaActionEventArgs(props.CommandName));

                                switch (props.PostSaveAction)
                                {
                                case PostSaveActionType.None:

                                    if (props.RebindAfterSave)
                                    {
                                        _bindingSourceTree.Bind(savableObject);
                                        AddEventHooks(savableObject);
                                    }
                                    break;

                                case PostSaveActionType.AndClose:

                                    CloseForm();
                                    break;

                                case PostSaveActionType.AndNew:

                                    OnSetForNew(new CslaActionEventArgs(props.CommandName));
                                    AddEventHooks(savableObject);
                                    break;
                                }
                            }
                            catch (Exception ex)
                            {
                                _bindingSourceTree.Bind(objectToSave);
                                AddEventHooks(objectToSave);
                                OnErrorEncountered(new ErrorEncounteredEventArgs(props.CommandName, new ObjectSaveException(ex)));
                                // there was some problem
                                result = false;
                            }
                        }
                        else
                        {
                            // did not find bound object so don't bother raising the Clicked event
                            result = false;
                        }

                        _bindingSourceTree.SetEvents(true);
                    }
                }
                else
                {
                    OnBusinessObjectInvalid(new CslaActionEventArgs(props.CommandName));
                    // object not valid or has broken rules set to invalidate it due to this control's properties
                    result = false;
                }
            }
            else
            {
                // process was canceled from the HasBrokenRules event (okToContinue = false)
                result = false;
            }

            return(result);
        }