Пример #1
0
        /// <summary>
        /// Converts this attribute to the column object.
        /// </summary>
        /// <param name="memberName">Name of the member.</param>
        /// <param name="memberType">Type of the member.</param>
        /// <returns></returns>
        public virtual DextopGridColumn ToGridHeader(String memberName, Type memberType)
        {
            string extType, editorType;

            DextopModelFieldTypeMapper.TryGetFieldTypeName(memberType, out extType, out editorType);
            return(new DextopGridColumn
            {
                align = align,
                dataIndex = dataIndex ?? memberName,
                id = Group == 0 ? id : ((id ?? dataIndex ?? memberName) + Group),
                flex = flex > 0 ? flex : (double?)null,
                text = text ?? memberName,
                readOnly = NullableUtil.DefaultNull(readOnly, false),
                required = NullableUtil.DefaultNull(required, false),
                sortable = NullableUtil.DefaultNull(sortable, true),
                tooltip = tooltip,
                tooltipTpl = tooltipTpl,
                tpl = tpl,
                type = type ?? editorType ?? extType,
                width = NullableUtil.DefaultNull(width, -1),
                renderer = renderer,
                format = format,
                menuDisabled = NullableUtil.DefaultNull(menuDisabled, true),
                filterable = NullableUtil.DefaultNull(filterable, false),
                locked = NullableUtil.DefaultNull(locked, false),
                hasHeaderFilter = NullableUtil.DefaultNull(hasHeaderFilter, false)
            });
        }
Пример #2
0
        /// <summary>
        /// Builds the model for given type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="modelAttribute">The model attribute.</param>
        /// <returns></returns>
        public DextopModel BuildModel(Type type, DextopModelAttribute modelAttribute = null)
        {
            DextopModelTypeMeta meta;

            if (modelAttribute != null)
            {
                meta = new DextopModelTypeMeta
                {
                    IdField               = modelAttribute.Id,
                    ModelName             = modelAttribute.Name,
                    DefaultSerializerType = modelAttribute.DefaultSerializer
                }
            }
            ;
            else
            {
                meta = new DextopModelTypeMeta();
            }

            if (meta.ModelType != null && meta.ModelType != type)
            {
                throw new DextopException("Type mismatch.");
            }

            var excludeFields = new HashSet <String>();

            if (meta.ExcludedFields != null)
            {
                foreach (var f in meta.ExcludedFields)
                {
                    excludeFields.Add(f);
                }
            }

            var properties = type.GetProperties();
            var fields     = type.GetFields();
            var combined   = ((MemberInfo[])fields).Union(properties);
            var model      = new DextopModel
            {
                Meta = meta
            };

            List <String> idCandidates = new List <string>();
            var           dfat         = new DextopModelFieldAttribute();

            foreach (var p in combined)
            {
                Type fieldType;
                if (p is PropertyInfo)
                {
                    fieldType = ((PropertyInfo)p).PropertyType;
                }
                else
                {
                    fieldType = ((FieldInfo)p).FieldType;
                }

                var ea = AttributeHelper.GetCustomAttribute <DextopModelExcludeAttribute>(p, true);
                if (ea != null)
                {
                    excludeFields.Add(p.Name);
                }
                if (!excludeFields.Contains(p.Name))
                {
                    var    fat = AttributeHelper.GetCustomAttribute <DextopModelFieldAttribute>(p, true) ?? dfat;
                    String ft;
                    if (!DextopModelFieldTypeMapper.TryGetFieldTypeName(fieldType, out ft))
                    {
                        excludeFields.Add(p.Name);
                        continue;
                    }
                    var field = new DextopModel.Field
                    {
                        name         = p.Name,
                        type         = fat.type ?? ft,
                        defaultValue = fat.defaultValue,
                        useNull      = fat.useNotNull ? false : true,//fieldType.IsClass || Codaxy.Common.Nullable.IsNullableType(type) || fieldType == typeof(String)
                        dateFormat   = fat.dateFormat,
                        mapping      = fat.mapping,
                        sortDir      = fat.sortDir,
                        persist      = fat.persist,
                        convert      = fat.convert,
                        sortType     = fat.sortType
                    };
                    model.Fields.Add(field);
                    if (meta.IdField == null)
                    {
                        var ida = AttributeHelper.GetCustomAttribute <DextopModelIdAttribute>(p, true);
                        if (ida != null)
                        {
                            meta.IdField  = p.Name;
                            field.useNull = true;
                        }
                        else
                        {
                            if (p.Name.EndsWith("ID") || p.Name.EndsWith("id") || p.Name.EndsWith("Id"))
                            {
                                idCandidates.Add(p.Name);
                            }
                        }
                    }

                    var validations = AttributeHelper.GetCustomAttributes <DextopModelValidationAttribute>(p, true);
                    foreach (var v in validations)
                    {
                        var validation = v.ToValidation(p.Name, fieldType);
                        model.Validations.Add(validation);
                    }
                }
            }

            if (meta.IdField == null)
            {
                if (idCandidates.Count == 1)
                {
                    meta.IdField = idCandidates[0];
                }
                else
                {
                    throw new DextopException("Model for type '{0}' could not be generated as id field could not be resolved.", type);
                }
            }

            model.idProperty = meta.IdField;
            if (meta.ModelName == null)
            {
                meta.ModelName = Application.MapTypeName(type, ".model");
            }
            meta.ExcludedFields = excludeFields.Count == 0 ? null : excludeFields.ToArray();
            meta.Fields         = model.Fields.Select(a => a.name).ToArray();
            meta.ModelType      = type;

            if (!metas.TryAdd(type, meta))
            {
                throw new DextopException("Model for type '{0}' already registered.", type);
            }

            return(model);
        }