Exemplo n.º 1
0
 private IEnumerable<object> GetArgs(
     ConstructorInfo constructor,
     PropertyValue propertyValue)
 {
     foreach (var parameter in constructor.GetParameters())
     {
         yield return GetArg(parameter.ParameterType, propertyValue);
     }
 }
Exemplo n.º 2
0
        private BaseFilter CreateInstance(
            Type filterType,
            PropertyValue propertyValue)
        {
            var constructor = filterType.GetConstructors().Single();

            var args = GetArgs(constructor, propertyValue);

            return (BaseFilter)constructor.Invoke(args.ToArray());
        }
Exemplo n.º 3
0
        public static string GetFilePath(
            this UrlHelper urlHelper,
            PropertyValue value)
        {
            if (value.AsString.IsNullOrEmpty())
            {
                return null;
            }

            var path = Pather.Combine("~/", value.Property.FileOptions.Path, value.AsString).Replace("\\", "/");

            return urlHelper.Content(path);
        }
Exemplo n.º 4
0
        public static string GetImageBigPath(
            this UrlHelper urlHelper,
            PropertyValue value)
        {
            if (value.AsString.IsNullOrEmpty())
            {
                return null;
            }

            var settings = value.Property.FileOptions.Settings.FirstOrDefault();
            var path = Pather.Combine("~/", value.Property.FileOptions.Path, settings.SubPath, value.AsString).Replace("\\", "/");

            return urlHelper.Content(path);
        }
Exemplo n.º 5
0
        private object GetArg(
            Type type,
            PropertyValue propertyValue)
        {
            if (type == typeof(Property))
                return propertyValue.Property;
            if (type == typeof(Entity))
                return propertyValue.Property.Entity;
            if (type == typeof(IKnowTheTime))
                return new SystemClock();
            if (type == typeof(string))
                return propertyValue.Raw.ToStringSafe();

            return propertyValue.Raw;
        }
Exemplo n.º 6
0
        private static PropertyValue GetPropertyValueForFile(
            Property property,
            IValueProvider valueProvider,
            HttpFileCollectionBase files)
        {
            var propertyValue = new PropertyValue(property);

            var file = files[property.Name];
            propertyValue.Raw = file;
            if (property.TypeInfo.IsFileStoredInDb == false &&
                property.FileOptions.NameCreation == NameCreation.UserInput)
            {
                var providedName = (string)valueProvider.GetValue(property.Name)
                    .ConvertTo(typeof(string), CultureInfo.CurrentCulture);
                propertyValue.Additional = providedName;
            }
            var isDeleted = false;

            if (file == null || file.ContentLength > 0)
            {
                isDeleted = false;
            }
            else
            {
                var isDeletedKey = property.Name + "_delete";
                if (valueProvider.ContainsPrefix(isDeletedKey))
                {
                    isDeleted =
                       ((bool?)
                           valueProvider.GetValue(isDeletedKey)
                               .ConvertTo(typeof(bool), CultureInfo.CurrentCulture)).GetValueOrDefault();
                }
            }

            if (isDeleted)
            {
                propertyValue.DataBehavior = DataBehavior.Clear;
                propertyValue.Additional = null;
            }

            return propertyValue;
        }
Exemplo n.º 7
0
        private static PropertyValue GetPropertyValue(
            Property property,
            IValueProvider valueProvider,
            Func<Property, object> defaultValueResolver = null)
        {
            var propertyValue = new PropertyValue(property);

            var value = valueProvider.GetValue(property.Name);
            if (value != null)
            {
                if (property.IsForeignKey && property.TypeInfo.IsCollection)
                {
                    propertyValue.Values = value.AttemptedValue
                        .Split(',').OfType<object>().ToList();
                }
                else if (property.TypeInfo.DataType == DataType.DateTime)
                {
                    propertyValue.Raw = ParseDateTime(property, (string)value.ConvertTo(typeof(string)));
                }
                else
                {
                    propertyValue.Raw = value.ConvertTo(
                        property.TypeInfo.OriginalType,
                        CultureInfo.CurrentCulture);
                }
            }

            if (defaultValueResolver != null)
            {
                var defaultValue = defaultValueResolver(property);

                if (defaultValue is ValueBehavior ||
                    (propertyValue.Raw == null && defaultValue != null))
                {
                    propertyValue.Raw = defaultValue;
                }
            }

            return propertyValue;
        }
Exemplo n.º 8
0
        public bool Validate(PropertyValue propertyValue)
        {
            var file = (HttpPostedFile)propertyValue.Raw;
            if (file == null)
            {
                _notificator.AddModelError(propertyValue.Property.Name, "Missing file.");
                return false;
            }

            var maxFileSize = propertyValue.Property.FileOptions.MaxFileSize.GetValueOrDefault(_configuration.MaxFileSize);
            if (file.ContentLength > maxFileSize)
            {
                _notificator.AddModelError(propertyValue.Property.Name, "File is too big.");
                return false;
            }

            if (propertyValue.Property.TypeInfo.IsImage && IsImage(file) == false)
            {
                _notificator.AddModelError(propertyValue.Property.Name, "File is not a image.");
                return false;
            }

            var allowedFileExtensions = propertyValue.Property.FileOptions.AllowedFileExtensions;
            if (allowedFileExtensions == null || allowedFileExtensions.Length == 0)
            {
                allowedFileExtensions = _configuration.AllowedFileExtensions;
            }
            var ext = Path.GetExtension(file.FileName).ToLower();
            if (allowedFileExtensions.Contains(ext))
            {
                _notificator.AddModelError(propertyValue.Property.Name, "Wrong file extension.");
                return false;
            }

            return true;
        }
Exemplo n.º 9
0
        public Property(Entity entity, PropertyInfo property)
        {
            if (property == null)
                throw new ArgumentNullException("property");
            if (entity == null)
                throw new ArgumentNullException("entity");

            Entity = entity;
            PropertyInfo = property;

            Name = property.Name;
            ColumnName = property.Name;
            ControlsAttributes = new Dictionary<string, object>();
            // TODO: determine ColumnName

            TypeInfo = new PropertyTypeInfo(property.PropertyType, Attributes);
            ImageOptions = new ImageOptions(Attributes, Entity.Name);
            Value = new PropertyValue(TypeInfo);
            Template = new PropertyTemplate(Attributes, TypeInfo, IsForeignKey);

            if (TypeInfo.DataType == DataType.Numeric)
            {
                if (TypeInfo.IsFloatingPoint)
                {
                    ControlsAttributes.Add("data-number-number-of-decimals", "4");
                }
                if (TypeInfo.IsNullable)
                {
                    ControlsAttributes.Add("data-number-value", "");
                }
            }

            SetForeignKey(Attributes);

            SetDeleteOption(Attributes);

            IsKey = Attributes.OfType<KeyAttribute>().Any();
            IsLinkKey = Attributes.OfType<LinkKeyAttribute>().Any();

            var columnAttribute =
                Attributes.OfType<ColumnAttribute>().FirstOrDefault();
            if (columnAttribute != null)
            {
                ColumnName = columnAttribute.Name;
            }

            var requiredAttribute =
                Attributes.OfType<RequiredAttribute>().FirstOrDefault();
            if (requiredAttribute != null)
            {
                IsRequired = true;
                RequiredErrorMessage = requiredAttribute.ErrorMessage;
            }

            var displayAttribute =
                Attributes.OfType<DisplayAttribute>().FirstOrDefault();
            if (displayAttribute != null)
            {
                DisplayName = displayAttribute.Name ?? Name.SplitCamelCase();
                Description = displayAttribute.Description;
                GroupName =
                    displayAttribute.GroupName ?? IlaroAdminResources.Others;
            }
            else
            {
                DisplayName = Name.SplitCamelCase();
                GroupName = IlaroAdminResources.Others;
            }
        }
Exemplo n.º 10
0
        public void Fill(
            FormCollection collection,
            HttpFileCollectionBase files,
            Func<Property, object> defaultValueResolver = null)
        {
            foreach (var property in Entity.Properties)
            {
                var propertyValue = new PropertyValue(property);
                Values.Add(propertyValue);
                if (property.TypeInfo.IsFile)
                {
                    var file = files[property.Name];
                    propertyValue.Raw = file;
                    if (property.TypeInfo.IsFileStoredInDb == false &&
                        property.FileOptions.NameCreation == NameCreation.UserInput)
                    {
                        var providedName = (string)collection.GetValue(property.Name)
                            .ConvertTo(typeof(string), CultureInfo.CurrentCulture);
                        propertyValue.Additional = providedName;
                    }
                    var isDeleted =
                        ((bool?)
                            collection.GetValue(property.Name + "_delete")
                                .ConvertTo(typeof(bool), CultureInfo.CurrentCulture)).GetValueOrDefault();

                    if (file.ContentLength > 0)
                        isDeleted = false;

                    if (isDeleted)
                    {
                        propertyValue.DataBehavior = DataBehavior.Clear;
                        propertyValue.Additional = null;
                    }
                }
                else
                {
                    var value = collection.GetValue(property.Name);
                    if (value != null)
                    {
                        if (property.IsForeignKey && property.TypeInfo.IsCollection)
                        {
                            propertyValue.Values = value.AttemptedValue
                                .Split(',').OfType<object>().ToList();
                        }
                        else
                        {
                            propertyValue.Raw = value.ConvertTo(
                                property.TypeInfo.Type,
                                CultureInfo.CurrentCulture);
                        }
                    }

                    if (defaultValueResolver != null)
                    {
                        var defaultValue = defaultValueResolver(property);

                        if (defaultValue is ValueBehavior ||
                            (propertyValue.Raw == null && defaultValue != null))
                        {
                            propertyValue.Raw = defaultValue;
                        }
                    }
                }
            }
        }
Exemplo n.º 11
0
        public Property(Entity entity, PropertyInfo property)
        {
            if (property == null)
            {
                throw new ArgumentNullException("property");
            }
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            Entity       = entity;
            PropertyInfo = property;

            Name               = property.Name;
            ColumnName         = property.Name;
            ControlsAttributes = new Dictionary <string, object>();
            // TODO: determine ColumnName

            TypeInfo    = new PropertyTypeInfo(property.PropertyType, Attributes);
            FileOptions = new FileOptions(Attributes);
            Value       = new PropertyValue(Attributes, TypeInfo);
            Template    = new PropertyTemplate(Attributes, TypeInfo, IsForeignKey);

            if (TypeInfo.DataType == DataType.Numeric)
            {
                if (TypeInfo.IsFloatingPoint)
                {
                    ControlsAttributes.Add("data-number-number-of-decimals", "4");
                }
                if (TypeInfo.IsNullable)
                {
                    ControlsAttributes.Add("data-number-value", "");
                }
            }

            SetForeignKey(Attributes);

            SetDeleteOption(Attributes);

            IsKey     = Attributes.OfType <KeyAttribute>().Any();
            IsLinkKey = Attributes.OfType <LinkKeyAttribute>().Any();

            var columnAttribute =
                Attributes.OfType <ColumnAttribute>().FirstOrDefault();

            if (columnAttribute != null)
            {
                ColumnName = columnAttribute.Name;
            }

            var requiredAttribute =
                Attributes.OfType <RequiredAttribute>().FirstOrDefault();

            if (requiredAttribute != null)
            {
                IsRequired           = true;
                RequiredErrorMessage = requiredAttribute.ErrorMessage;
            }

            var displayAttribute =
                Attributes.OfType <DisplayAttribute>().FirstOrDefault();

            if (displayAttribute != null)
            {
                DisplayName = displayAttribute.Name ?? Name.SplitCamelCase();
                Description = displayAttribute.Description;
                GroupName   =
                    displayAttribute.GroupName ?? IlaroAdminResources.Others;
            }
            else
            {
                DisplayName = Name.SplitCamelCase();
                GroupName   = IlaroAdminResources.Others;
            }

            SetFormat(Attributes);
        }