Ejemplo n.º 1
0
 private static void ValidateDisplayColumnAttribute(
     DisplayColumnAttribute displayColumnAttribute,
     PropertyInfo displayColumnProperty,
     Type modelType
     )
 {
     if (displayColumnProperty == null)
     {
         throw new InvalidOperationException(
                   String.Format(
                       CultureInfo.CurrentCulture,
                       MvcResources.DataAnnotationsModelMetadataProvider_UnknownProperty,
                       modelType.FullName,
                       displayColumnAttribute.DisplayColumn
                       )
                   );
     }
     if (displayColumnProperty.GetGetMethod() == null)
     {
         throw new InvalidOperationException(
                   String.Format(
                       CultureInfo.CurrentCulture,
                       MvcResources.DataAnnotationsModelMetadataProvider_UnreadableProperty,
                       modelType.FullName,
                       displayColumnAttribute.DisplayColumn
                       )
                   );
     }
 }
Ejemplo n.º 2
0
        internal static ModelTypeData ReflectClass(System.Type type)
        {
            ModelTypeData modelTypeData = new ModelTypeData();

            System.Reflection.PropertyInfo[] properties             = type.GetProperties();
            DisplayColumnAttribute           displayColumnAttribute = type.GetCustomAttribute(typeof(DisplayColumnAttribute), true) as DisplayColumnAttribute;

            if (displayColumnAttribute != null)
            {
                string displayColumnPropertyName = displayColumnAttribute.DisplayColumn;
                modelTypeData.DisplayColumnProperty = properties.FirstOrDefault((System.Reflection.PropertyInfo x) => x.Name == displayColumnPropertyName);
            }
            System.Reflection.PropertyInfo[] array = properties;
            for (int i = 0; i < array.Length; i++)
            {
                System.Reflection.PropertyInfo propertyInfo = array[i];
                object obj = propertyInfo.GetCustomAttributes(typeof(IdAttribute), true).FirstOrDefault <object>();
                if (obj != null)
                {
                    modelTypeData.IdProperty = propertyInfo;
                    break;
                }
            }
            System.Reflection.PropertyInfo propertyInfo2 = properties.FirstOrDefault((System.Reflection.PropertyInfo x) => x.IsDefined(typeof(SuperordinateIdAttribute), true));
            if (propertyInfo2 != null)
            {
                modelTypeData.SuperordinateIdProperty = propertyInfo2;
            }
            return(modelTypeData);
        }
 public DataAnnotationsModelMetadata(DataAnnotationsModelMetadataProvider provider, Type containerType,
                                     Func <object> modelAccessor, Type modelType, string propertyName,
                                     DisplayColumnAttribute displayColumnAttribute)
     : base(provider, containerType, modelAccessor, modelType, propertyName)
 {
     _displayColumnAttribute = displayColumnAttribute;
 }
Ejemplo n.º 4
0
        public void Ctor_DisplayColumn_SortColumn_SortDescending(string displayColumn, string sortColumn, bool sortDescending)
        {
            var attribute = new DisplayColumnAttribute(displayColumn, sortColumn, sortDescending);

            Assert.Equal(displayColumn, attribute.DisplayColumn);
            Assert.Equal(sortColumn, attribute.SortColumn);
            Assert.Equal(sortDescending, attribute.SortDescending);
        }
Ejemplo n.º 5
0
        public void Ctor_DisplayColumn(string displayColumn)
        {
            var attribute = new DisplayColumnAttribute(displayColumn);

            Assert.Equal(displayColumn, attribute.DisplayColumn);
            Assert.Null(attribute.SortColumn);
            Assert.False(attribute.SortDescending);
        }
Ejemplo n.º 6
0
 private static void ValidateDisplayColumnAttribute(DisplayColumnAttribute displayColumnAttribute, PropertyInfo displayColumnProperty, Type modelType)
 {
     if (displayColumnProperty == null)
     {
         throw Error.InvalidOperation(SRResources.DataAnnotationsModelMetadataProvider_UnknownProperty, modelType, displayColumnAttribute.DisplayColumn);
     }
     if (displayColumnProperty.GetGetMethod() == null)
     {
         throw Error.InvalidOperation(SRResources.DataAnnotationsModelMetadataProvider_UnreadableProperty, modelType, displayColumnAttribute.DisplayColumn);
     }
 }
Ejemplo n.º 7
0
        bool DetermineSortDescending()
        {
            DisplayColumnAttribute attr = null;

            MetaModel.GetDataFieldAttribute <DisplayColumnAttribute> (Attributes, ref attr);
            if (attr == null)
            {
                return(false);
            }

            return(attr.SortDescending);
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Set the metadata of display property.
 /// </summary>
 /// <param name="display">Display attribute.</param>
 protected virtual void SetDisplayColumn(DisplayColumnAttribute display)
 {
     if (display == null)
     {
         throw new ArgumentNullException("display");
     }
     DisplayProperty = GetProperty(display.DisplayColumn);
     if (display.SortColumn != null)
     {
         SortProperty   = GetProperty(display.SortColumn);
         SortDescending = display.SortDescending;
     }
 }
        MetaColumn FindSortColumn()
        {
            if (sortColumnChecked)
            {
                return(sortColumn);
            }

            sortColumnChecked = true;
            DisplayColumnAttribute attr = Attributes [typeof(DisplayColumnAttribute)] as DisplayColumnAttribute;

            if (attr == null)
            {
                return(null);
            }

            string name = attr.SortColumn;

            if (String.IsNullOrEmpty(name))
            {
                return(null);
            }

            MetaColumn ret       = null;
            Exception  exception = null;

            try
            {
                ret = Columns.First <MetaColumn> ((MetaColumn mc) =>
                {
                    if (String.Compare(mc.Name, name, StringComparison.Ordinal) == 0)
                    {
                        return(true);
                    }
                    return(false);
                });
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            if (ret == null)
            {
                throw new InvalidOperationException("The sort column '" + name + "' specified for table '" + Name + "' does not exist.", exception);
            }

            return(ret);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Set the metadata automatic.
        /// </summary>
        protected virtual void SetMetadata()
        {
            DisplayNameAttribute display = Type.GetCustomAttribute <DisplayNameAttribute>();

            if (display != null)
            {
                SetDisplay(display);
            }
            else
            {
                Name = Type.Name;
            }

            DisplayColumnAttribute displayColumn = Type.GetCustomAttribute <DisplayColumnAttribute>();

            if (displayColumn != null)
            {
                SetDisplayColumn(displayColumn);
            }
            else
            {
                DisplayProperty = GetProperty("Index");
            }


            ParentAttribute parent = Type.GetCustomAttribute <ParentAttribute>();

            if (parent != null)
            {
                SetParent(parent);
            }

            EntityAuthenticationAttribute authenticate = Type.GetCustomAttribute <EntityAuthenticationAttribute>();

            if (authenticate == null)
            {
                AllowAnonymous = true;
                AddRoles       = new string[0];
                EditRoles      = new string[0];
                ViewRoles      = new string[0];
                RemoveRoles    = new string[0];
            }
            else
            {
                SetAuthentication(authenticate);
            }
        }
Ejemplo n.º 11
0
        private MetaColumn GetDisplayColumnFromMetadata()
        {
            DisplayColumnAttribute displayColumnAttribute = this.Metadata.DisplayColumnAttribute;

            if (displayColumnAttribute == null)
            {
                return(null);
            }
            MetaColumn column = null;

            if (!this.TryGetColumn(displayColumnAttribute.DisplayColumn, out column))
            {
                object[] args = new object[] { displayColumnAttribute.DisplayColumn, this.Name };
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, DynamicDataResources.MetaTable_CantFindDisplayColumn, args));
            }
            return(column);
        }
Ejemplo n.º 12
0
        public KoobooModelMetadata(DataAnnotationsModelMetadataProvider provider, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName, DisplayColumnAttribute displayColumnAttribute, IEnumerable <Attribute> attributes)
            : base(provider, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute)
        {
            var descAttr = attributes.OfType <DescriptionAttribute>().SingleOrDefault();

            _description = descAttr != null ? descAttr.Description : "";

            DataSourceAttribute = attributes.OfType <DataSourceAttribute>().SingleOrDefault();

            var enumAttribute = attributes.OfType <EnumDataTypeAttribute>().SingleOrDefault();

            if (enumAttribute != null)
            {
                DataSource = new EnumTypeSelectListDataSource(enumAttribute.EnumType);
            }

            Attributes = attributes;

            var defaultValueAttr = attributes.OfType <DefaultValueAttribute>().SingleOrDefault();

            DefaultValue = defaultValueAttr != null ? defaultValueAttr.Value : this.ModelType.GetDefaultValue();

            this.AdditionalValues["DefaultValue"] = DefaultValue;
        }
        /// <summary>
        /// Deploys values according to values found in attributes
        /// </summary>
        /// <param name="propAttributes">The property attributes.</param>
        public void deployAttributes(Object[] propAttributes)
        {
            foreach (Object propAtt in propAttributes)
            {
                descAttribute = propAtt as DescriptionAttribute;
                if (descAttribute != null)
                {
                    description = descAttribute.Description;
                }

                displayNameAttribute = propAtt as DisplayNameAttribute;
                if (displayNameAttribute != null)
                {
                    displayName = displayNameAttribute.DisplayName;
                }

                catAttribute = propAtt as CategoryAttribute;
                if (catAttribute != null)
                {
                    categoryName = catAttribute.Category.ToUpper();

                    if (categoryName.Contains(","))
                    {
                        groups.AddRange(categoryName.getStringTokens());
                    }
                    else
                    {
                        groups.Add(categoryName);
                    }
                }

                if (propAtt is DisplayAttribute)
                {
                    DisplayAttribute displayAttribute_DisplayAttribute = (DisplayAttribute)propAtt;
                    description += displayAttribute_DisplayAttribute.Description.toStringSafe("");
                    displayName  = displayAttribute_DisplayAttribute.Name.toStringSafe(displayName);
                    letter       = displayAttribute_DisplayAttribute.ShortName.toStringSafe(letter);
                    // priority = (int)displayAttribute_DisplayAttribute.Order;
                    categoryName = displayAttribute_DisplayAttribute.GroupName.toStringSafe(categoryName);
                }

                if (propAtt is RangeAttribute)
                {
                    RangeAttribute rng = (RangeAttribute)propAtt;
                    range_defined = true;
                    range_min     = Convert.ToDouble((object)rng.Minimum);
                    range_max     = Convert.ToDouble((object)rng.Maximum);
                }

                if (propAtt is DisplayFormatAttribute)
                {
                    DisplayFormatAttribute dFormat = (DisplayFormatAttribute)propAtt;
                    escapeValueString = dFormat.HtmlEncode;

                    format = dFormat.DataFormatString;
                }

                if (propAtt is DisplayColumnAttribute)
                {
                    DisplayColumnAttribute dColumn = (DisplayColumnAttribute)propAtt;
                    displayName = dColumn.DisplayColumn;
                }

                if (propAtt is XmlIgnoreAttribute)
                {
                    IsXmlIgnore = true;
                }

                if (propAtt is imbAttribute imbAt)
                {
                    switch (imbAt.nameEnum)
                    {
                    case imbAttributeName.DataTableExport:

                        templateFieldDataTable dtc = (templateFieldDataTable)imbAt.objMsg;
                        Object dtc_val             = imbAt.objExtra;
                        deploy(dtc, dtc_val);
                        //propAtt_imbAttribute.objExtra

                        break;

                    case imbAttributeName.reporting_aggregation:
                        aggregation[(dataPointAggregationAspect)imbAt.objExtra] = (dataPointAggregationType)imbAt.objMsg;
                        break;
                    }

                    deploy(imbAt.nameEnum, imbAt.getMessage().toStringSafe());

                    if (!attributes.ContainsKey(imbAt.nameEnum))
                    {
                        attributes.Add(imbAt.nameEnum, imbAt);
                    }
                    else
                    {
                    }
                }
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Initialize entity metadata.
        /// </summary>
        /// <param name="type"></param>
        public EntityMetadata(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            Type    = type;
            KeyType = type.GetProperty("Index").PropertyType;

            PropertyInfo[] properties = type.GetProperties().Where(t => t.GetGetMethod() != null && t.GetSetMethod() != null).ToArray();
            Properties = properties.Select(t => new PropertyMetadata(t)).OrderBy(t => t.Order).ToArray();

            ViewProperties   = Properties.Where(t => !t.IsHiddenOnView).ToArray();
            EditProperties   = Properties.Where(t => !t.IsHiddenOnEdit).ToArray();
            SearchProperties = Properties.Where(t => t.Searchable).ToArray();
            DetailProperties = Properties.Where(t => !t.IsHiddenOnDetail).ToArray();

            _Properties = new Dictionary <string, PropertyMetadata>();
            for (int i = 0; i < Properties.Length; i++)
            {
                _Properties.Add(Properties[i].Property.Name, Properties[i]);
            }

            EntityAuthenticationAttribute authenticate = type.GetCustomAttribute <EntityAuthenticationAttribute>();

            if (authenticate == null)
            {
                AllowAnonymous = true;
                AddRoles       = new string[0];
                EditRoles      = new string[0];
                ViewRoles      = new string[0];
                RemoveRoles    = new string[0];
            }
            else
            {
                AllowAnonymous = authenticate.AllowAnonymous;
                AddRoles       = authenticate.AddRolesRequired;
                EditRoles      = authenticate.EditRolesRequired;
                ViewRoles      = authenticate.ViewRolesRequired;
                RemoveRoles    = authenticate.RemoveRolesRequired;
            }

            DisplayNameAttribute display = type.GetCustomAttribute <DisplayNameAttribute>();

            if (display != null)
            {
                Name = display.DisplayName == null ? type.Name : display.DisplayName;
            }
            else
            {
                Name = type.Name;
            }

            DisplayColumnAttribute displayColumn = type.GetCustomAttribute <DisplayColumnAttribute>();

            if (displayColumn != null)
            {
                DisplayProperty = GetProperty(displayColumn.DisplayColumn);
                if (displayColumn.SortColumn != null)
                {
                    SortProperty   = Properties.SingleOrDefault(t => t.Property.Name == displayColumn.SortColumn);
                    SortDescending = displayColumn.SortDescending;
                }
            }
            else
            {
                DisplayProperty = GetProperty("Index");
            }
            ParentAttribute parent = type.GetCustomAttribute <ParentAttribute>();

            if (parent != null)
            {
                ParentProperty = Properties.SingleOrDefault(t => t.Property.Name == parent.PropertyName);
                ParentLevel    = parent.Level;
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CustomMetadata"/> class.
        /// </summary>
        /// <param name="provider">The provider.</param>
        /// <param name="containerType">Type of the container.</param>
        /// <param name="modelAccessor">The model accessor.</param>
        /// <param name="modelType">Type of the model.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="displayColumnAttribute">The display column attribute.</param>
        /// <param name="attributes">The attributes.</param>
        public CustomMetadata(DataAnnotationsModelMetadataProvider provider, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName, DisplayColumnAttribute displayColumnAttribute, IEnumerable <Attribute> attributes)
            : base(provider, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute)
        {
            var descAttr = attributes.OfType <DescriptionAttribute>().SingleOrDefault();

            _description = descAttr != null ? descAttr.Description : "";
        }
Ejemplo n.º 16
0
        MetaColumn FindDisplayColumn()
        {
            if (displayColumnChecked)
            {
                return(displayColumn);
            }

            displayColumnChecked = true;
            ReadOnlyCollection <MetaColumn> columns = Columns;

            // 1. The column that is specified by using the DisplayColumnAttribute attribute.
            DisplayColumnAttribute attr = Attributes [typeof(DisplayColumnAttribute)] as DisplayColumnAttribute;

            if (attr != null)
            {
                string name = attr.DisplayColumn;
                foreach (MetaColumn mc in columns)
                {
                    if (String.Compare(name, mc.Name, StringComparison.Ordinal) == 0)
                    {
                        return(mc);
                    }
                }

                throw new InvalidOperationException("The display column '" + name + "' specified for the table '" + EntityType.Name + "' does not exist.");
            }

            // 2. The first string column that is not in the primary key.
            // LAMESPEC: also a column which is not a custom one
            ReadOnlyCollection <MetaColumn> pkc = PrimaryKeyColumns;
            bool havePkc = pkc.Count > 0;

            foreach (MetaColumn mc in columns)
            {
                if (mc.IsCustomProperty || (havePkc && pkc.Contains(mc)))
                {
                    continue;
                }
                if (mc.ColumnType == typeof(string))
                {
                    return(mc);
                }
            }

            // 3. The first string column that is in the primary key.
            if (havePkc)
            {
                foreach (MetaColumn mc in pkc)
                {
                    if (mc.ColumnType == typeof(string))
                    {
                        return(mc);
                    }
                }

                // 4. The first non-string column that is in the primary key.
                return(pkc [0]);
            }

            // No check, again, is made whether the columns collection contains enough
            // columns to perform successful lookup, we're emulating that here.
            if (columns.Count == 0)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            // Fallback - return the first column
            return(columns [0]);
        }
        //public override IEnumerable<ModelMetadata> GetMetadataForProperties(object container, Type containerType)
        //{
        //  return base.GetMetadataForProperties(container, containerType);
        //}

        //public override ModelMetadata GetMetadataForType(Func<object> modelAccessor, Type modelType)
        //{
        //  return base.GetMetadataForType(modelAccessor, modelType);
        //}

        //public override ModelMetadata GetMetadataForProperty(Func<object> modelAccessor, Type containerType, string propertyName)
        //{
        //  return base.GetMetadataForProperty(modelAccessor, containerType, propertyName);
        //}

        //protected override ModelMetadata GetMetadataForProperty(Func<object> modelAccessor, Type containerType, System.ComponentModel.PropertyDescriptor propertyDescriptor)
        //{
        //  return base.GetMetadataForProperty(modelAccessor, containerType, propertyDescriptor);
        //}
        protected override ModelMetadata CreateMetadata(IEnumerable <Attribute> attributes, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName)
        {
            //Reflected from base class
            List <Attribute>       list = new List <Attribute>(attributes);
            DisplayColumnAttribute displayColumnAttribute = Enumerable.FirstOrDefault <DisplayColumnAttribute>(Enumerable.OfType <DisplayColumnAttribute>((IEnumerable)list));

            DataAnnotationsModelMetadata annotationsModelMetadata = null;

            //brl additions
            //if this is a BOV backed object
            if (containerType != null && Attribute.IsDefined(containerType, typeof(DryLogicObjectAttribute)) && propertyName != "OI")
            {
                Object         container = null;
                ObjectInstance oi        = null;
                //By having this code here instead of in GetMetadataForProperty, some of the normal features like ui hint will work
                if (modelAccessor != null)
                {
                    var rootModelType = modelAccessor.Target.GetType();
                    var field         = rootModelType.GetField("container");
                    if (field != null)
                    {
                        container = field.GetValue(modelAccessor.Target);
                        //if we don't have a reference to the container yet...
                        if (container.GetType() != containerType)
                        {
                            //...then try to break down the expression to get it
                            //get the expression as text, ie "model.EmployeeViewModel.MyEmployee" and split it
                            var expressionParts = ((LambdaExpression)rootModelType.GetField("expression").GetValue(modelAccessor.Target)).Body.ToString().Split('.');
                            //var expressionParts = new string[] { };

                            //loop thru the parts in the middle
                            for (int i = 1; i < expressionParts.Length - 1; i++)
                            {
                                container = container.GetType().GetProperty(expressionParts[i]).GetValue(container);
                            }
                        }
                        //could use an attribute instead to identify the object instance
                        oi = ObjectInstance.GetObjectInstance(container);

                        if (oi != null)//not really sure how this woudl fail at this point
                        {
                            annotationsModelMetadata           = new PropertyValueMetadata(this, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute);
                            annotationsModelMetadata.Container = container;
                            //internally, setting model wipes out modelAccessor (caching of sorts)
                            annotationsModelMetadata.Model        = oi.PropertyValues[propertyName];
                            annotationsModelMetadata.TemplateHint = "PropertyValue";
                        }
                    }
                }
            }
            if (annotationsModelMetadata == null)
            {
                annotationsModelMetadata = new DataAnnotationsModelMetadata(this, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute);
            }


            HiddenInputAttribute hiddenInputAttribute = Enumerable.FirstOrDefault <HiddenInputAttribute>(Enumerable.OfType <HiddenInputAttribute>((IEnumerable)list));

            if (hiddenInputAttribute != null)
            {
                annotationsModelMetadata.TemplateHint        = "HiddenInput";
                annotationsModelMetadata.HideSurroundingHtml = !hiddenInputAttribute.DisplayValue;
            }
            IEnumerable <UIHintAttribute> source = Enumerable.OfType <UIHintAttribute>((IEnumerable)list);
            UIHintAttribute uiHintAttribute      = Enumerable.FirstOrDefault <UIHintAttribute>(source, (Func <UIHintAttribute, bool>)(a => string.Equals(a.PresentationLayer, "MVC", StringComparison.OrdinalIgnoreCase))) ?? Enumerable.FirstOrDefault <UIHintAttribute>(source, (Func <UIHintAttribute, bool>)(a => string.IsNullOrEmpty(a.PresentationLayer)));

            if (uiHintAttribute != null)
            {
                annotationsModelMetadata.TemplateHint = uiHintAttribute.UIHint;
            }
            DataTypeAttribute attribute = Enumerable.FirstOrDefault <DataTypeAttribute>(Enumerable.OfType <DataTypeAttribute>((IEnumerable)list));

            if (attribute != null)
            {
                annotationsModelMetadata.DataTypeName = DataTypeUtil.ToDataTypeName(attribute, (Func <DataTypeAttribute, bool>)null);
            }
            EditableAttribute editableAttribute = Enumerable.FirstOrDefault <EditableAttribute>(Enumerable.OfType <EditableAttribute>((IEnumerable)attributes));

            if (editableAttribute != null)
            {
                annotationsModelMetadata.IsReadOnly = !editableAttribute.AllowEdit;
            }
            else
            {
                ReadOnlyAttribute readOnlyAttribute = Enumerable.FirstOrDefault <ReadOnlyAttribute>(Enumerable.OfType <ReadOnlyAttribute>((IEnumerable)list));
                if (readOnlyAttribute != null)
                {
                    annotationsModelMetadata.IsReadOnly = readOnlyAttribute.IsReadOnly;
                }
            }
            DisplayFormatAttribute displayFormatAttribute = Enumerable.FirstOrDefault <DisplayFormatAttribute>(Enumerable.OfType <DisplayFormatAttribute>((IEnumerable)list));

            if (displayFormatAttribute == null && attribute != null)
            {
                displayFormatAttribute = attribute.DisplayFormat;
            }
            if (displayFormatAttribute != null)
            {
                annotationsModelMetadata.NullDisplayText          = displayFormatAttribute.NullDisplayText;
                annotationsModelMetadata.DisplayFormatString      = displayFormatAttribute.DataFormatString;
                annotationsModelMetadata.ConvertEmptyStringToNull = displayFormatAttribute.ConvertEmptyStringToNull;
                if (displayFormatAttribute.ApplyFormatInEditMode)
                {
                    annotationsModelMetadata.EditFormatString = displayFormatAttribute.DataFormatString;
                }
                if (!displayFormatAttribute.HtmlEncode && string.IsNullOrWhiteSpace(annotationsModelMetadata.DataTypeName))
                {
                    annotationsModelMetadata.DataTypeName = DataTypeUtil.HtmlTypeName;
                }
            }
            ScaffoldColumnAttribute scaffoldColumnAttribute = Enumerable.FirstOrDefault <ScaffoldColumnAttribute>(Enumerable.OfType <ScaffoldColumnAttribute>((IEnumerable)list));

            if (scaffoldColumnAttribute != null)
            {
                annotationsModelMetadata.ShowForDisplay = annotationsModelMetadata.ShowForEdit = scaffoldColumnAttribute.Scaffold;
            }
            DisplayAttribute displayAttribute = Enumerable.FirstOrDefault <DisplayAttribute>(Enumerable.OfType <DisplayAttribute>((IEnumerable)attributes));
            string           str = (string)null;

            if (displayAttribute != null)
            {
                annotationsModelMetadata.Description      = displayAttribute.GetDescription();
                annotationsModelMetadata.ShortDisplayName = displayAttribute.GetShortName();
                annotationsModelMetadata.Watermark        = displayAttribute.GetPrompt();
                annotationsModelMetadata.Order            = displayAttribute.GetOrder() ?? 10000;
                str = displayAttribute.GetName();
            }
            if (str != null)
            {
                annotationsModelMetadata.DisplayName = str;
            }
            else
            {
                DisplayNameAttribute displayNameAttribute = Enumerable.FirstOrDefault <DisplayNameAttribute>(Enumerable.OfType <DisplayNameAttribute>((IEnumerable)list));
                if (displayNameAttribute != null)
                {
                    annotationsModelMetadata.DisplayName = displayNameAttribute.DisplayName;
                }
            }
            if (Enumerable.FirstOrDefault <RequiredAttribute>(Enumerable.OfType <RequiredAttribute>((IEnumerable)list)) != null)
            {
                annotationsModelMetadata.IsRequired = true;
            }
            return((ModelMetadata)annotationsModelMetadata);
        }
Ejemplo n.º 18
0
 public PropertyUiHintModelMetadata(PropertyUiHintModelMetadataProvider provider, Type containerType,
                                    Func <object> modelAccessor, Type modelType, string propertyName,
                                    DisplayColumnAttribute displayColumnAttribute)
     : base(provider, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute)
 {
 }
        protected override ModelMetadata CreateMetadata(IEnumerable <Attribute> attributes, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName)
        {
            List <Attribute>             attributeList          = new List <Attribute>(attributes);
            DisplayColumnAttribute       displayColumnAttribute = attributeList.OfType <DisplayColumnAttribute>().FirstOrDefault();
            DataAnnotationsModelMetadata result = new DataAnnotationsModelMetadata(this, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute);

            // Do [HiddenInput] before [UIHint], so you can override the template hint
            HiddenInputAttribute hiddenInputAttribute = attributeList.OfType <HiddenInputAttribute>().FirstOrDefault();

            if (hiddenInputAttribute != null)
            {
                result.TemplateHint        = "HiddenInput";
                result.HideSurroundingHtml = !hiddenInputAttribute.DisplayValue;
            }

            // We prefer [UIHint("...", PresentationLayer = "MVC")] but will fall back to [UIHint("...")]
            IEnumerable <UIHintAttribute> uiHintAttributes = attributeList.OfType <UIHintAttribute>();
            UIHintAttribute uiHintAttribute = uiHintAttributes.FirstOrDefault(a => String.Equals(a.PresentationLayer, "MVC", StringComparison.OrdinalIgnoreCase))
                                              ?? uiHintAttributes.FirstOrDefault(a => String.IsNullOrEmpty(a.PresentationLayer));

            if (uiHintAttribute != null)
            {
                result.TemplateHint = uiHintAttribute.UIHint;
            }

            DataTypeAttribute dataTypeAttribute = attributeList.OfType <DataTypeAttribute>().FirstOrDefault();

            if (dataTypeAttribute != null)
            {
                result.DataTypeName = dataTypeAttribute.GetDataTypeName();
            }

            ReadOnlyAttribute readOnlyAttribute = attributeList.OfType <ReadOnlyAttribute>().FirstOrDefault();

            if (readOnlyAttribute != null)
            {
                result.IsReadOnly = readOnlyAttribute.IsReadOnly;
            }

            DisplayFormatAttribute displayFormatAttribute = attributeList.OfType <DisplayFormatAttribute>().FirstOrDefault();

            if (displayFormatAttribute == null && dataTypeAttribute != null)
            {
                displayFormatAttribute = dataTypeAttribute.DisplayFormat;
            }
            if (displayFormatAttribute != null)
            {
                result.NullDisplayText          = displayFormatAttribute.NullDisplayText;
                result.DisplayFormatString      = displayFormatAttribute.DataFormatString;
                result.ConvertEmptyStringToNull = displayFormatAttribute.ConvertEmptyStringToNull;

                if (displayFormatAttribute.ApplyFormatInEditMode)
                {
                    result.EditFormatString = displayFormatAttribute.DataFormatString;
                }
            }

            ScaffoldColumnAttribute scaffoldColumnAttribute = attributeList.OfType <ScaffoldColumnAttribute>().FirstOrDefault();

            if (scaffoldColumnAttribute != null)
            {
                result.ShowForDisplay = result.ShowForEdit = scaffoldColumnAttribute.Scaffold;
            }

            DisplayNameAttribute displayNameAttribute = attributeList.OfType <DisplayNameAttribute>().FirstOrDefault();

            if (displayNameAttribute != null)
            {
                result.DisplayName = displayNameAttribute.DisplayName;
            }

            RequiredAttribute requiredAttribute = attributeList.OfType <RequiredAttribute>().FirstOrDefault();

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

            return(result);
        }
Ejemplo n.º 20
0
 public FieldTemplateMetadata(DataAnnotationsModelMetadataProvider provider, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName, DisplayColumnAttribute displayColumnAttribute, IEnumerable <Attribute> attributes)
     : base(provider, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute)
 {
     Attributes = new List <Attribute>(attributes);
 }
Ejemplo n.º 21
0
        /// <summary>
        /// 返回属性的数据规则和约束条件
        /// </summary>
        /// <param name="prop"></param>
        /// <returns></returns>
        CatalogExtAttribute GetAdvDataRule(PropertyInfo prop)
        {
            var attrs = prop.GetCustomAttributes(true);

            CatalogExtAttribute extAttr = attrs.FirstOrDefault(attr => attr is CatalogExtAttribute) as CatalogExtAttribute;

            if (extAttr == null)
            {
                extAttr = new CatalogExtAttribute();
                if (prop.Name == "Id")
                {
                    extAttr.Editable = false;
                    extAttr.DataType = ExtDataType.SingleLineText;
                }
                if (prop.PropertyType.IsValueType && !prop.PropertyType.IsNullableType())
                {
                    extAttr.AllowNull = false;
                }
            }

            if (extAttr.Name.IsEmpty())
            {
                extAttr.Name = prop.Name;
            }

            RequiredAttribute required = attrs.FirstOrDefault(attr => attr is RequiredAttribute) as RequiredAttribute;

            if (required != null)
            {
                extAttr.AllowNull = false;
            }

            StringLengthAttribute stringLen = attrs.FirstOrDefault(attr => attr is StringLengthAttribute) as StringLengthAttribute;

            if (stringLen != null)
            {
                extAttr.MaxLength = stringLen.MaximumLength;
                extAttr.MinLength = stringLen.MinimumLength;
            }

            DisplayColumnAttribute displayProp = attrs.FirstOrDefault(attr => attr is DisplayColumnAttribute) as DisplayColumnAttribute;

            if (displayProp != null)
            {
                extAttr.DisplayProperty = displayProp.DisplayColumn;
            }

            DisplayFormatAttribute displayFmt = attrs.FirstOrDefault(attr => attr is DisplayFormatAttribute) as DisplayFormatAttribute;

            if (displayFmt != null)
            {
                extAttr.DisplayFormat = displayFmt.DataFormatString;
            }

            RegularExpressionAttribute reg = attrs.FirstOrDefault(attr => attr is RegularExpressionAttribute) as RegularExpressionAttribute;

            if (reg != null)
            {
                extAttr.RegExpr = reg.Pattern;
            }

            RangeAttribute rng = attrs.FirstOrDefault(attr => attr is RangeAttribute) as RangeAttribute;

            if (rng != null)
            {
                extAttr.MinValue = rng.Minimum;
                extAttr.MaxValue = rng.Maximum;
            }

            DisplayAttribute dsp = attrs.FirstOrDefault(attr => attr is DisplayAttribute) as DisplayAttribute;

            if (dsp != null)
            {
                extAttr.Name = dsp.Name;
            }

            BrowsableAttribute brw = attrs.FirstOrDefault(attr => attr is BrowsableAttribute) as BrowsableAttribute;

            if (brw != null)
            {
                extAttr.Browsable = brw.Browsable;
            }
            if (extAttr.DataType == ExtDataType.Auto)
            {
                Type type = prop.PropertyType;
                if (type.IsNullableType())
                {
                    type = type.GetGenericArguments()[0];
                }
                if (type.IsEnum)
                {
                }
                else if (type == typeof(DateTime))
                {
                    extAttr.DataType = ExtDataType.Date;
                }
                else if (type == typeof(bool))
                {
                    extAttr.DataType = ExtDataType.Bool;
                }
                else if (type == typeof(decimal))
                {
                    extAttr.DataType = ExtDataType.Currency;
                }
                else if (type == typeof(float) || type == typeof(double))
                {
                    extAttr.DataType = ExtDataType.FloatNumber;
                }
                else if (type.IsValueType)
                {
                    extAttr.DataType = ExtDataType.SingleNumber;
                }
            }
            if (!extAttr.LinkedProperty.IsEmpty() && extAttr.DisplayProperty.IsEmpty())
            {
                extAttr.DisplayProperty = extAttr.Name;
            }

            if (extAttr.DisplayFormat == null)
            {
                switch (extAttr.DataType)
                {
                case ExtDataType.Date:
                    extAttr.DisplayFormat = "yyyy-MM-dd";
                    break;

                case ExtDataType.DateAndTime:
                    extAttr.DisplayFormat = "yyyy-MM-dd HH:mm:ss";
                    break;

                case ExtDataType.FloatNumber:
                    extAttr.DisplayFormat = "#0.00";
                    //extAttr.MaxValue = int.MaxValue;
                    break;

                case ExtDataType.SingleNumber:
                    extAttr.DisplayFormat = "#0";
                    //extAttr.MaxValue = int.MaxValue;
                    break;

                case ExtDataType.Currency:
                    char currChar = 100.ToString("C")[0];
                    extAttr.DisplayFormat = currChar + "#0.00";
                    //extAttr.MaxValue = int.MaxValue;
                    break;

                case ExtDataType.Percent:
                    extAttr.DisplayFormat = "p2";
                    //extAttr.MaxValue = 1;
                    break;

                case ExtDataType.Time:
                    extAttr.DisplayFormat = "H:mm";
                    break;
                }
            }
            if (extAttr.InputFormat == null)
            {
                extAttr.InputFormat = extAttr.DisplayFormat;
            }
            switch (extAttr.DataType)
            {
            case ExtDataType.FloatNumber:
            case ExtDataType.SingleNumber:
            case ExtDataType.Currency:
            case ExtDataType.Percent:
                if (extAttr.MaxValue == null && extAttr.MinValue == null)
                {
                    extAttr.MinValue = 0;
                    extAttr.MaxValue = int.MaxValue;
                }
                break;
            }
            extAttr.Property = prop;
            return(extAttr);
        }
Ejemplo n.º 22
0
        protected override ModelMetadata CreateMetadata(IEnumerable <Attribute> attributes, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName)
        {
            List <Attribute>             attributeList          = new List <Attribute>(attributes);
            DisplayColumnAttribute       displayColumnAttribute = attributeList.OfType <DisplayColumnAttribute>().FirstOrDefault();
            DataAnnotationsModelMetadata result = new DataAnnotationsModelMetadata(this, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute);

            // Do [HiddenInput] before [UIHint], so you can override the template hint
            HiddenInputAttribute hiddenInputAttribute = attributeList.OfType <HiddenInputAttribute>().FirstOrDefault();

            if (hiddenInputAttribute != null)
            {
                result.TemplateHint        = "HiddenInput";
                result.HideSurroundingHtml = !hiddenInputAttribute.DisplayValue;
            }

            // We prefer [UIHint("...", PresentationLayer = "MVC")] but will fall back to [UIHint("...")]
            IEnumerable <UIHintAttribute> uiHintAttributes = attributeList.OfType <UIHintAttribute>();
            UIHintAttribute uiHintAttribute = uiHintAttributes.FirstOrDefault(a => String.Equals(a.PresentationLayer, "MVC", StringComparison.OrdinalIgnoreCase))
                                              ?? uiHintAttributes.FirstOrDefault(a => String.IsNullOrEmpty(a.PresentationLayer));

            if (uiHintAttribute != null)
            {
                result.TemplateHint = uiHintAttribute.UIHint;
            }

            EditableAttribute editable = attributes.OfType <EditableAttribute>().FirstOrDefault();

            if (editable != null)
            {
                result.IsReadOnly = !editable.AllowEdit;
            }
            else
            {
                ReadOnlyAttribute readOnlyAttribute = attributeList.OfType <ReadOnlyAttribute>().FirstOrDefault();
                if (readOnlyAttribute != null)
                {
                    result.IsReadOnly = readOnlyAttribute.IsReadOnly;
                }
            }

            DataTypeAttribute      dataTypeAttribute      = attributeList.OfType <DataTypeAttribute>().FirstOrDefault();
            DisplayFormatAttribute displayFormatAttribute = attributeList.OfType <DisplayFormatAttribute>().FirstOrDefault();

            SetFromDataTypeAndDisplayAttributes(result, dataTypeAttribute, displayFormatAttribute);

            ScaffoldColumnAttribute scaffoldColumnAttribute = attributeList.OfType <ScaffoldColumnAttribute>().FirstOrDefault();

            if (scaffoldColumnAttribute != null)
            {
                result.ShowForDisplay = result.ShowForEdit = scaffoldColumnAttribute.Scaffold;
            }

            DisplayAttribute display = attributes.OfType <DisplayAttribute>().FirstOrDefault();
            string           name    = null;

            if (display != null)
            {
                result.Description      = display.GetDescription();
                result.ShortDisplayName = display.GetShortName();
                result.Watermark        = display.GetPrompt();
                result.Order            = display.GetOrder() ?? ModelMetadata.DefaultOrder;

                name = display.GetName();
            }

            if (name != null)
            {
                result.DisplayName = name;
            }
            else
            {
                DisplayNameAttribute displayNameAttribute = attributeList.OfType <DisplayNameAttribute>().FirstOrDefault();
                if (displayNameAttribute != null)
                {
                    result.DisplayName = displayNameAttribute.DisplayName;
                }
            }

            RequiredAttribute requiredAttribute = attributeList.OfType <RequiredAttribute>().FirstOrDefault();

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

            return(result);
        }