Ejemplo n.º 1
0
        /// <summary>
        /// Checks the validation of a single property
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="errors">The errors.</param>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public bool TryValidate(IRightDataModel item, string propertyName, List <IModelError> errors, IModelContext context = null)
        {
            var rtn = TryValidateModel(item, Operation.View, errors, context);

            errors.RemoveAll(x => string.Compare(x.Property, propertyName, true) != 0);
            return(rtn);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Create a new item with the properties (state) of an existing IRightDataModel
        /// </summary>
        internal RightEntityDataModel(IRightDataModel model)
        {
            // copy state over

            Name         = model.Name;
            Key          = model.Key;
            IsAssignable = model.IsAssignable;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Validates the model.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="operation">The operation.</param>
        /// <param name="errors">The errors.</param>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        private bool TryValidateModel(IRightDataModel item, Operation operation, List <IModelError> errors, IModelContext context = null)
        {
            if (item == null)
            {
                AddNullModelError(errors);
                return(false);
            }

            return(validator.IsValid(item, operation, errors));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Create a new item with the properties (state) of an existing IRightDataModel
        /// </summary>
        public void Load(IRightDataModel model)
        {
            if (model == null)
            {
                return;
            }
            // copy state from incomming model
            Id = model.Id;

            Name         = model.Name;
            Key          = model.Key;
            IsAssignable = model.IsAssignable;
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Represents a single Right instance as a select list item
 /// </summary>
 public static SelectListItem ToSelectListItem(this IRightDataModel model, int id = -1)
 {
     if (model == null)
     {
         return(new SelectListItem());
     }
     else
     {
         return(new SelectListItem
         {
             Text = string.Format("[{0}] {1}", model.Id, model.IsAssignable.ToString()), // change how the Right appear in drop downs here
             Value = model.Id.ToString(),
             Selected = (id == model.Id)
         });
     }
 }
        /// <summary>
        /// Tries the state of the item.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="trigger">The trigger.</param>
        /// <param name="errors">The errors.</param>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        protected bool TryUpdateItemState(IRightDataModel model, RightTriggers trigger, List <IModelError> errors, IModelContext context = null)
        {
            var state        = ExtractState(model);
            var stateMachine = GetStateMachine(model);

            if (stateMachine.CanFire(trigger))
            {
                stateMachine.Fire(trigger);
                SetModelState(stateMachine.State, model);
                return(service.TrySave(model, errors, context));
            }
            else
            {
                MakeErrorMessage(stateMachine, trigger, errors);
                return(false);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Tries to create a new item
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="errors">The errors.</param>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException">Unable to update IRightDataModel"</exception>
        private bool TryCreate(IRightDataModel item, List <IModelError> errors, IModelContext context = null)
        {
            if (TryValidateModel(item, Operation.Create, errors, context) == false)
            {
                return(false);
            }

            try
            {
                changeHandler.BeforeCreate(item, context);
                dal.Create(item, context);
                changeHandler.AfterCreate(item, context);
            }
            catch (Exception ex)
            {
                log.Exception(LogName, ex);
                throw new InvalidOperationException("Unable to create IRightDataModel", ex);
            }

            return(true);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Returns true if the model is valid
        /// </summary>
        public bool IsValid(IRightDataModel model, Operation op, List <IModelError> errors)
        {
            var e = new List <IModelError>();

            // check for required properties
            if (model.Name.IsPresent() == false)
            {
                e.Add(new ModelError {
                    Property = "Name", ErrorMessage = "right_name_missing"
                });
            }
            if (model.Key.IsPresent() == false)
            {
                e.Add(new ModelError {
                    Property = "Key", ErrorMessage = "right_key_missing"
                });
            }
            // check supplied properties are valid

            errors.CombineOrReplace(e);
            return(e.Any() == false);
        }
 protected override void SetModelState(RightStates state, IRightDataModel model)
 {
     // todo : update model properties to reflect the state
     throw new NotImplementedException();
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Called when [customer update].
 /// </summary>
 /// <param name="item">The item.</param>
 /// <param name="context">The context.</param>
 public virtual void BeforeUpdate(IRightDataModel item, IModelContext context)
 {
 }
 /// <summary>
 /// Sets the state of the model.
 /// </summary>
 abstract protected void SetModelState(RightStates state, IRightDataModel model);
 /// <summary>
 /// Extracts the state.
 /// </summary>
 abstract protected RightStates ExtractState(IRightDataModel model);
 /// <summary>
 /// Gets the state machine.
 /// </summary>
 protected virtual StateMachine <RightStates, RightTriggers> GetStateMachine(IRightDataModel model)
 {
     return(new StateMachine <RightStates, RightTriggers>(ExtractState(model)));
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Gets the single Right by Role id for RoleRight relationship.
 /// </summary>
 public virtual void AfterGetSingleRightByRoleForRoleRight(IRightDataModel result, int roleId, IModelContext context)
 {
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Called when [update].
 /// </summary>
 /// <param name="item">The item.</param>
 /// <param name="context">The context.</param>
 public virtual void AfterUpdate(IRightDataModel item, IModelContext context)
 {
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Tries to save the item.
 /// </summary>
 /// <param name="item"></param>
 /// <param name="errors"></param>
 /// <param name="context"></param>
 /// <returns></returns>
 public bool TrySave(IRightDataModel item, List <IModelError> errors, IModelContext context = null)
 {
     return(item.IsNew ?
            TryCreate(item, errors, context) :
            TryUpdate(item, errors, context));
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Tries the set the model to "complete" state.
 /// </summary>
 public bool TrySetComplete(IRightDataModel model, List <IModelError> errors, IModelContext context = null)
 {
     return(TryUpdateItemState(model, RightTriggers.Complete, errors, context));
 }
Ejemplo n.º 18
0
        // Add new methods to represent other states //

        protected override RightStates ExtractState(IRightDataModel model)
        {
            // todo : extract a RightStates to repersent the current model
            throw new NotImplementedException();
        }
Ejemplo n.º 19
0
 /// <summary>
 /// Represents a collection Right instance as select list items
 /// </summary>
 public static IEnumerable <SelectListItem> ToSelectListItem(this IEnumerable <IRightDataModel> models, IRightDataModel selected = null)
 {
     if (models == null)
     {
         return(Enumerable.Empty <SelectListItem>());
     }
     else
     {
         var rtn = new List <SelectListItem>();
         rtn.Add(new SelectListItem {
             Text = "Select .....", Value = "-1"
         });
         rtn.AddRange(models.Select(x => x.ToSelectListItem(selected == null ? -1 : selected.Id)).OrderBy(x => x.Text));
         return(rtn);
     }
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Called when [create].
 /// </summary>
 /// <param name="item">The item.</param>
 /// <param name="context">The context.</param>
 public virtual void BeforeCreate(IRightDataModel item, IModelContext context = null)
 {
 }