Esempio n. 1
0
        private static UpdatePropertyResultViewModel createResultForConversionFailure(
            UpdatePropertyModel <TEntity> updatePropertyModel, Exception ex)
        {
            var message      = ex.Message;
            var accessor     = PropertyUtility.FindPropertyByName <TEntity>(updatePropertyModel.PropertyName);
            var propertyName = LocalizationManager.GetHeader(accessor.InnerProperty);

            var error = new ValidationError(propertyName, message);

            return(new UpdatePropertyResultViewModel
            {
                success = false,
                errors = new[] { error }
            });
        }
        private static UpdatePropertyResultViewModel createResultForConversionFailure(
            UpdatePropertyModel <TEntity> updatePropertyModel, Exception ex)
        {
            var message      = ex.Message;
            var accessor     = PropertyUtility.FindPropertyByName <TEntity>(updatePropertyModel.PropertyName);
            var propertyName = LocalizationManager.GetHeader(accessor.InnerProperty);

            var error = new AjaxError {
                field = propertyName, message = message
            };
            var updatePropertyResultViewModel = new UpdatePropertyResultViewModel
            {
                Success = false,
            };

            updatePropertyResultViewModel.Errors.Add(error);
            return(updatePropertyResultViewModel);
        }
        public virtual EditPropertyResult EditProperty(UpdatePropertyModel <T> update, T entity)
        {
            var    accessor       = getAccessor(update);
            var    oldValue       = accessor.GetValue(entity);
            var    newValueString = update.PropertyValue ?? string.Empty;
            object newValue       = null;
            var    targetType     = accessor.PropertyType;

            try
            {
                newValue = _converter.FromString(newValueString, targetType);

                if (!PropertyUtility.IsChanged(targetType, newValue, oldValue))
                {
                    return(EditPropertyResult.NotChangedResult());
                }
            }
            catch (Exception ex)
            {
                throw InvalidPropertyConversionException
                      .For(FastPackKeys.INVALID_TYPE_CONVERSION.ToFormat(newValueString,
                                                                         LocalizationManager.GetTextForType(
                                                                             targetType.Name.ToUpper())), ex);
            }

            accessor.SetValue(entity, newValue);
            var prevValue = oldValue == null ? string.Empty : oldValue.ToString();
            var result    = new EditPropertyResult(accessor, typeof(T), prevValue, newValueString);

            if (!result.IsListAccessor())
            {
                //Used to be UpdatePropertyModel.formatter.GetDisplay.  We had an unused _flattener variable.  Why?
                result.PreviousValue = _formatter.GetDisplay(accessor, oldValue);
                result.NewValue      = _formatter.GetDisplayForProperty(accessor, entity);
            }

            return(result);
        }
 protected virtual Accessor getAccessor(UpdatePropertyModel <T> update)
 {
     return(PropertyUtility.FindPropertyByName <T>(update.PropertyName));
 }
 public PropertyToUpdate FindProperty(UpdatePropertyModel <T> update)
 {
     return(PropertyToUpdate.For <T>(getAccessor(update)));
 }
        public virtual bool CanEdit(UpdatePropertyModel <T> update)
        {
            var accessor = getAccessor(update);

            return(accessor != null);
        }
        public UpdatePropertyResultViewModel EditProperty(UpdatePropertyModel <TEntity> updatePropertyModel)
        {
            var model = _repository.Find <TEntity>(updatePropertyModel.Id);
            // TODO -- what if it isn't found?
            // " ? "
            //updatePropertyModel.Formatter = _displayFormatter;

            EditPropertyResult editResult;

            try
            {
                var handler = _handlers.FirstOrDefault(x => x.CanEdit(updatePropertyModel));

                if (handler == null)
                {
                    throw new ApplicationException(
                              "No handler for property {0} on type {1}".ToFormat(updatePropertyModel.PropertyName,
                                                                                 typeof(Entity).FullName));
                }

                var property = handler.FindProperty(updatePropertyModel);

                var rights = _fieldAccess.RightsFor(model, property.Property);
                if (!rights.Write)
                {
                    var updatePropertyViewModel = new UpdatePropertyResultViewModel
                    {
                        Message       = FastPackKeys.NOT_AUTHORIZED.ToString(),
                        ShouldRefresh = false,
                        Success       = false
                    };
                    updatePropertyViewModel.AddError(new AjaxError {
                        field = property.Property.Name, message = FastPackKeys.NOT_AUTHORIZED.ToString()
                    });
                    return(updatePropertyViewModel);
                }


                editResult = handler.EditProperty(updatePropertyModel, model);
            }
            catch (FormatException ex)
            {
                return(createResultForConversionFailure(updatePropertyModel, ex));
            }
            catch (InvalidPropertyConversionException ex)
            {
                return(createResultForConversionFailure(updatePropertyModel, ex));
            }

            if (editResult.WasNotApplied)
            {
                var updatePropertyResultViewModel = new UpdatePropertyResultViewModel
                {
                    Success = false
                };
                updatePropertyResultViewModel.AddErrors(editResult.ToValidationErrors());
                return(updatePropertyResultViewModel);
            }

            var returnValue = propertySaveResult(model, editResult);

            if (returnValue.Success)
            {
                _logger.Log(model, editResult);
            }

            return(returnValue);
        }