private void SetEditing(IPropertySource source, PropertyItem item)
        {
            var editorTypeAttr = source.GetAttribute<EditorTypeAttribute>();

            if (editorTypeAttr == null)
            {
                item.EditorType = AutoDetermineEditorType(source.ValueType, source.EnumType, item.EditorParams);
            }
            else
            {
                item.EditorType = editorTypeAttr.EditorType;
                editorTypeAttr.SetParams(item.EditorParams);
            }

            if (source.EnumType != null)
                item.EditorParams["enumKey"] = EnumMapper.GetEnumTypeKey(source.EnumType);

            if (!ReferenceEquals(null, source.BasedOnField))
            {
                if (item.EditorType == "Decimal" &&
                    (source.BasedOnField is DoubleField ||
                     source.BasedOnField is DecimalField) &&
                    source.BasedOnField.Size > 0 &&
                    source.BasedOnField.Scale < source.BasedOnField.Size &&
                    !item.EditorParams.ContainsKey("minValue") &&
                    !item.EditorParams.ContainsKey("maxValue"))
                {
                    string minVal = new String('0', source.BasedOnField.Size - source.BasedOnField.Scale);
                    if (source.BasedOnField.Scale > 0)
                        minVal += "." + new String('0', source.BasedOnField.Scale);
                    string maxVal = minVal.Replace('0', '9');
                    item.EditorParams["minValue"] = minVal;
                    item.EditorParams["maxValue"] = maxVal;
                }
                else if (source.BasedOnField.Size > 0)
                {
                    item.EditorParams["maxLength"] = source.BasedOnField.Size;
                    item.MaxLength = source.BasedOnField.Size;
                }
            }

            var maxLengthAttr = source.GetAttribute<MaxLengthAttribute>();
            if (maxLengthAttr != null)
            {
                item.MaxLength = maxLengthAttr.MaxLength;
                item.EditorParams["maxLength"] = maxLengthAttr.MaxLength;
            }

            foreach (EditorOptionAttribute param in source.GetAttributes<EditorOptionAttribute>())
            {
                var key = param.Key;
                if (key != null &&
                    key.Length >= 1)
                    key = key.Substring(0, 1).ToLowerInvariant() + key.Substring(1);

                item.EditorParams[key] = param.Value;
            }
        }
        private void SetEditing(IPropertySource source, PropertyItem item)
        {
            var editorTypeAttr = source.GetAttribute <EditorTypeAttribute>();

            if (editorTypeAttr == null)
            {
                item.EditorType = AutoDetermineEditorType(source.ValueType, source.EnumType, item.EditorParams);
            }
            else
            {
                item.EditorType = editorTypeAttr.EditorType;
                editorTypeAttr.SetParams(item.EditorParams);
                if (item.EditorType == "Lookup" &&
                    !item.EditorParams.ContainsKey("lookupKey"))
                {
                    var distinct = source.GetAttribute <DistinctValuesEditorAttribute>();
                    if (distinct != null)
                    {
                        string prefix = null;
                        if (distinct.RowType != null)
                        {
                            if (!distinct.RowType.IsInterface &&
                                !distinct.RowType.IsAbstract &&
                                distinct.RowType.IsSubclassOf(typeof(Row)))
                            {
                                prefix = ((Row)Activator.CreateInstance(distinct.RowType))
                                         .GetFields().LocalTextPrefix;
                            }
                        }
                        else
                        {
                            var isRow = source.Property != null &&
                                        source.Property.ReflectedType != null &&
                                        !source.Property.ReflectedType.IsAbstract &&
                                        source.Property.ReflectedType.IsSubclassOf(typeof(Row));

                            if (!isRow)
                            {
                                if (!ReferenceEquals(null, source.BasedOnField))
                                {
                                    prefix = source.BasedOnField.Fields.LocalTextPrefix;
                                }
                            }
                            else
                            {
                                prefix = ((Row)Activator.CreateInstance(source.Property.ReflectedType))
                                         .GetFields().LocalTextPrefix;
                            }
                        }

                        if (prefix != null)
                        {
                            var propertyName = distinct.PropertyName.IsEmptyOrNull() ?
                                               (!ReferenceEquals(null, source.BasedOnField) ?
                                                (source.BasedOnField.PropertyName ?? source.BasedOnField.Name) :
                                                item.Name) : distinct.PropertyName;

                            if (!string.IsNullOrEmpty(propertyName))
                            {
                                item.EditorParams["lookupKey"] = "Distinct." + prefix + "." + propertyName;
                            }
                        }
                    }
                }
            }

            if (source.EnumType != null)
            {
                item.EditorParams["enumKey"] = EnumMapper.GetEnumTypeKey(source.EnumType);
            }

            var dtka = source.GetAttribute <DateTimeKindAttribute>();

            if (dtka != null && dtka.Value != DateTimeKind.Unspecified)
            {
                item.EditorParams["useUtc"] = true;
            }

            if (!ReferenceEquals(null, source.BasedOnField))
            {
                if (dtka == null &&
                    source.BasedOnField is DateTimeField &&
                    ((DateTimeField)source.BasedOnField).DateTimeKind != DateTimeKind.Unspecified)
                {
                    item.EditorParams["useUtc"] = true;
                }

                if (item.EditorType == "Decimal" &&
                    (source.BasedOnField is DoubleField ||
                     source.BasedOnField is SingleField ||
                     source.BasedOnField is DecimalField) &&
                    source.BasedOnField.Size > 0 &&
                    source.BasedOnField.Scale < source.BasedOnField.Size &&
                    !item.EditorParams.ContainsKey("minValue") &&
                    !item.EditorParams.ContainsKey("maxValue"))
                {
                    string minVal = new String('0', source.BasedOnField.Size - source.BasedOnField.Scale);
                    if (source.BasedOnField.Scale > 0)
                    {
                        minVal += "." + new String('0', source.BasedOnField.Scale);
                    }
                    string maxVal = minVal.Replace('0', '9');

                    if ((item.EditorParams.ContainsKey("allowNegatives") &&
                         Convert.ToBoolean(item.EditorParams["allowNegatives"] ?? false) == true) ||
                        (!item.EditorParams.ContainsKey("allowNegatives") &&
                         DecimalEditorAttribute.AllowNegativesByDefault))
                    {
                        minVal = "-" + maxVal;
                    }

                    item.EditorParams["minValue"] = minVal;
                    item.EditorParams["maxValue"] = maxVal;
                }
                else if (item.EditorType == "Integer" &&
                         (source.BasedOnField is Int32Field ||
                          source.BasedOnField is Int16Field ||
                          source.BasedOnField is Int64Field) &&
                         !item.EditorParams.ContainsKey("minValue") &&
                         !item.EditorParams.ContainsKey("maxValue"))
                {
                    item.EditorParams["maxValue"] = source.BasedOnField is Int16Field ? Int16.MaxValue
                        : source.BasedOnField is Int32Field ? Int32.MaxValue :
                                                    source.BasedOnField is Int64Field ? Int64.MaxValue : (object)null;

                    if ((item.EditorParams.ContainsKey("allowNegatives") &&
                         Convert.ToBoolean(item.EditorParams["allowNegatives"] ?? false) == true) ||
                        (!item.EditorParams.ContainsKey("allowNegatives") &&
                         DecimalEditorAttribute.AllowNegativesByDefault) &&
                        item.EditorParams["maxValue"] != null)
                    {
                        item.EditorParams["minValue"] = -Convert.ToInt64(item.EditorParams["maxValue"]) - 1;
                    }
                }
                else if (source.BasedOnField.Size > 0)
                {
                    item.EditorParams["maxLength"] = source.BasedOnField.Size;
                    item.MaxLength = source.BasedOnField.Size;
                }
            }

            var maxLengthAttr = source.GetAttribute <MaxLengthAttribute>();

            if (maxLengthAttr != null)
            {
                item.MaxLength = maxLengthAttr.MaxLength;
                item.EditorParams["maxLength"] = maxLengthAttr.MaxLength;
            }

            foreach (EditorOptionAttribute param in source.GetAttributes <EditorOptionAttribute>())
            {
                var key = param.Key;
                if (key != null &&
                    key.Length >= 1)
                {
                    key = key.Substring(0, 1).ToLowerInvariant() + key.Substring(1);
                }

                item.EditorParams[key] = param.Value;
            }

            SetServiceLookupParams(editorTypeAttr, item.EditorParams);
        }
        private static void SetFormatting(IPropertySource source, PropertyItem item)
        {
            var formatterTypeAttr = source.GetAttribute<FormatterTypeAttribute>();
            var enumType = source.EnumType;
            var valueType = source.ValueType;
            var basedOnField = source.BasedOnField;

            if (formatterTypeAttr == null)
            {
                if (enumType != null)
                {
                    item.FormatterType = "Enum";
                    item.FormatterParams["enumKey"] = EnumMapper.GetEnumTypeKey(enumType);
                }
                else if (valueType == typeof(DateTime) || valueType == typeof(DateTime?))
                {
                    if (!ReferenceEquals(null, basedOnField) && basedOnField is DateTimeField)
                    {
                        switch (((DateTimeField)basedOnField).DateTimeKind)
                        {
                            case DateTimeKind.Unspecified:
                                item.FormatterType = "Date";
                                break;
                            default:
                                item.FormatterType = "DateTime";
                                break;
                        }
                    }
                    else
                        item.FormatterType = "Date";
                }
                else if (valueType == typeof(Boolean))
                    item.FormatterType = "Checkbox";
                else if (valueType == typeof(Decimal) ||
                    valueType == typeof(Double) ||
                    valueType == typeof(Single) ||
                    valueType == typeof(Int32))
                {
                    item.FormatterType = "Number";
                }
            }
            else
            {
                item.FormatterType = formatterTypeAttr.FormatterType;
                formatterTypeAttr.SetParams(item.FormatterParams);
            }

            var displayFormatAttr = source.GetAttribute<DisplayFormatAttribute>();
            if (displayFormatAttr != null)
            {
                item.DisplayFormat = displayFormatAttr.Value;
                item.FormatterParams["displayFormat"] = displayFormatAttr.Value;
            }

            foreach (FormatterOptionAttribute param in source.GetAttributes<FormatterOptionAttribute>())
            {
                var key = param.Key;
                if (key != null &&
                    key.Length >= 1)
                    key = key.Substring(0, 1).ToLowerInvariant() + key.Substring(1);

                item.FormatterParams[key] = param.Value;
            }
        } 
        private static void SetFiltering(IPropertySource source, PropertyItem item)
        {
            var filterOnlyAttr    = source.GetAttribute <FilterOnlyAttribute>();
            var notFilterableAttr = source.GetAttribute <NotFilterableAttribute>();

            if (filterOnlyAttr != null && filterOnlyAttr.Value)
            {
                item.FilterOnly = true;
            }

            if (notFilterableAttr != null && notFilterableAttr.Value)
            {
                item.NotFilterable = true;
            }

            if (item.NotFilterable == true)
            {
                return;
            }

            var quickFilterAttr = source.GetAttribute <QuickFilterAttribute>();

            if (quickFilterAttr != null)
            {
                item.QuickFilter = true;
                if (quickFilterAttr.Separator)
                {
                    item.QuickFilterSeparator = true;
                }

                if (!string.IsNullOrEmpty(quickFilterAttr.CssClass))
                {
                    item.QuickFilterCssClass = quickFilterAttr.CssClass;
                }
            }

            var basedOnField = source.BasedOnField;

            if (!ReferenceEquals(null, basedOnField) &&
                notFilterableAttr == null)
            {
                if (basedOnField.Flags.HasFlag(FieldFlags.DenyFiltering) ||
                    basedOnField.Flags.HasFlag(FieldFlags.NotMapped))
                {
                    item.NotFilterable = true;
                }
            }

            Field  idField;
            string idFieldName;
            var    filteringIdField = source.GetAttribute <FilteringIdFieldAttribute>();

            if (filteringIdField != null)
            {
                idFieldName = filteringIdField.Value;
                idField     = basedOnField.Fields.FindFieldByPropertyName(idFieldName) ?? basedOnField.Fields.FindField(idFieldName);
            }
            else
            {
                idFieldName = AutoDetermineIdField(basedOnField);

                idField = null;
                if (idFieldName != null)
                {
                    idField = basedOnField.Fields.FindFieldByPropertyName(idFieldName) ?? basedOnField.Fields.FindField(idFieldName);
                    if (Object.ReferenceEquals(idField, null) ||
                        (idField.TextualField != basedOnField.PropertyName &&
                         idField.TextualField != basedOnField.Name))
                    {
                        idField     = null;
                        idFieldName = null;
                    }
                }
            }

            var valueType = source.ValueType;

            var filteringTypeAttr = source.GetAttribute <FilteringTypeAttribute>() ??
                                    idField.GetAttribute <FilteringTypeAttribute>();


            if (filteringTypeAttr == null)
            {
                var editorAttr = source.GetAttribute <EditorTypeAttribute>() ??
                                 idField.GetAttribute <EditorTypeAttribute>();

                Action <string[]> copyParamsFromEditor = (keys) =>
                {
                    var prm = new Dictionary <string, object>();
                    editorAttr.SetParams(prm);
                    SetServiceLookupParams(editorAttr, prm);
                    foreach (var key in keys)
                    {
                        if (prm.TryGetValue(key, out object o))
                        {
                            item.FilteringParams[key] = o;
                        }
                    }
                };

                if (idFieldName != null)
                {
                    item.FilteringParams["idField"] = idFieldName;
                    item.FilteringIdField           = idFieldName;
                }

                if (editorAttr != null && !standardFilteringEditors.Contains(editorAttr.EditorType))
                {
                    if (editorAttr is LookupEditorAttribute lea)
                    {
                        item.FilteringType = "Lookup";
                        copyParamsFromEditor(lookupCopyToFilterParams);
                    }
                    else if (editorAttr is ServiceLookupEditorAttribute slea)
                    {
                        item.FilteringType = "ServiceLookup";
                        copyParamsFromEditor(serviceLookupCopyToFilterParams);
                    }
                    else
                    {
                        item.FilteringType = "Editor";
                        item.FilteringParams["editorType"] = editorAttr.EditorType;
                        item.FilteringParams["useLike"]    = source.ValueType == typeof(String);
                        if (editorAttr is LookupEditorBaseAttribute leba &&
                            leba.Async == true)
                        {
                            item.FilteringParams["async"] = true;
                        }
                    }
                }
                else if (source.EnumType != null)
                {
                    item.FilteringType = "Enum";
                    item.FilteringParams["enumKey"] = EnumMapper.GetEnumTypeKey(source.EnumType);
                }
                else if (valueType == typeof(DateTime))
                {
                    if (!ReferenceEquals(null, basedOnField) &&
                        basedOnField is DateTimeField &&
                        !((DateTimeField)basedOnField).DateOnly)
                    {
                        item.FilteringType = "DateTime";
                    }
                    else
                    {
                        item.FilteringType = "Date";
                    }
                }
                else if (valueType == typeof(Boolean))
                {
                    item.FilteringType = "Boolean";
                }
                else if (valueType == typeof(Decimal) ||
                         valueType == typeof(Double) ||
                         valueType == typeof(Single))
                {
                    item.FilteringType = "Decimal";
                }
                else if (valueType == typeof(Int32) ||
                         valueType == typeof(Int16) ||
                         valueType == typeof(Int64))
                {
                    item.FilteringType = "Integer";
                }
                else
                {
                    item.FilteringType = "String";
                }
            }
            else
            {
                item.FilteringType = filteringTypeAttr.FilteringType;
                filteringTypeAttr.SetParams(item.FilteringParams);

                if (item.FilteringType == "Editor")
                {
                    if (!item.FilteringParams.ContainsKey("editorType"))
                    {
                        var editorAttr = source.GetAttribute <EditorTypeAttribute>() ??
                                         idField.GetAttribute <EditorTypeAttribute>();

                        if (editorAttr != null)
                        {
                            item.FilteringParams["editorType"] = editorAttr.EditorType;
                        }
                    }

                    if (!item.FilteringParams.ContainsKey("useLike"))
                    {
                        if (valueType == typeof(String))
                        {
                            item.FilteringParams["useLike"] = true;
                        }
                    }
                }

                object idFieldObj;
                if (item.FilteringParams.TryGetValue("idField", out idFieldObj) && idFieldObj is string)
                {
                    item.FilteringIdField = (idFieldObj as string).TrimToNull();
                }
                else
                {
                    item.FilteringIdField = idFieldName;
                }
            }

            var displayFormatAttr = source.GetAttribute <DisplayFormatAttribute>();

            if (displayFormatAttr != null)
            {
                item.FilteringParams["displayFormat"] = displayFormatAttr.Value;
            }

            foreach (FilteringOptionAttribute param in
                     idField.GetAttributes <FilteringOptionAttribute>().Concat(
                         source.GetAttributes <FilteringOptionAttribute>()))
            {
                var key = param.Key;
                if (key != null &&
                    key.Length >= 1)
                {
                    key = key.Substring(0, 1).ToLowerInvariant() + key.Substring(1);
                }

                if (key == "idField")
                {
                    item.FilteringIdField = (param.Value as string) ?? item.FilteringIdField;
                }

                item.FilteringParams[key] = param.Value;
            }

            foreach (QuickFilterOptionAttribute param in
                     idField.GetAttributes <QuickFilterOptionAttribute>().Concat(
                         source.GetAttributes <QuickFilterOptionAttribute>()))
            {
                var key = param.Key;
                if (key != null &&
                    key.Length >= 1)
                {
                    key = key.Substring(0, 1).ToLowerInvariant() + key.Substring(1);
                }

                item.QuickFilterParams[key] = param.Value;
            }
        }
        private static void SetFormatting(IPropertySource source, PropertyItem item)
        {
            var formatterTypeAttr = source.GetAttribute <FormatterTypeAttribute>();
            var enumType          = source.EnumType;
            var valueType         = source.ValueType;
            var basedOnField      = source.BasedOnField;

            if (formatterTypeAttr == null)
            {
                if (enumType != null)
                {
                    item.FormatterType = "Enum";
                    item.FormatterParams["enumKey"] = EnumMapper.GetEnumTypeKey(enumType);
                }
                else if (valueType == typeof(DateTime) || valueType == typeof(DateTime?))
                {
                    if (!ReferenceEquals(null, basedOnField) &&
                        basedOnField is DateTimeField &&
                        !((DateTimeField)basedOnField).DateOnly)
                    {
                        item.FormatterType = "DateTime";
                    }
                    else
                    {
                        item.FormatterType = "Date";
                    }
                }
                else if (valueType == typeof(Boolean))
                {
                    item.FormatterType = "Checkbox";
                }
                else if (valueType == typeof(Decimal) ||
                         valueType == typeof(Double) ||
                         valueType == typeof(Single) ||
                         valueType == typeof(Int32))
                {
                    item.FormatterType = "Number";
                }
            }
            else
            {
                item.FormatterType = formatterTypeAttr.FormatterType;
                formatterTypeAttr.SetParams(item.FormatterParams);
            }

            var displayFormatAttr = source.GetAttribute <DisplayFormatAttribute>();

            if (displayFormatAttr != null)
            {
                item.DisplayFormat = displayFormatAttr.Value;
                item.FormatterParams["displayFormat"] = displayFormatAttr.Value;
            }

            foreach (FormatterOptionAttribute param in source.GetAttributes <FormatterOptionAttribute>())
            {
                var key = param.Key;
                if (key != null &&
                    key.Length >= 1)
                {
                    key = key.Substring(0, 1).ToLowerInvariant() + key.Substring(1);
                }

                item.FormatterParams[key] = param.Value;
            }
        }
Beispiel #6
0
        private static void SetFiltering(IPropertySource source, PropertyItem item)
        {
            var filterOnlyAttr    = source.GetAttribute <FilterOnlyAttribute>();
            var notFilterableAttr = source.GetAttribute <NotFilterableAttribute>();

            if (filterOnlyAttr != null && filterOnlyAttr.Value)
            {
                item.FilterOnly = true;
            }

            if (notFilterableAttr != null && notFilterableAttr.Value)
            {
                item.NotFilterable = true;
            }

            if (item.NotFilterable == true)
            {
                return;
            }

            var quickFilterAttr = source.GetAttribute <QuickFilterAttribute>();

            if (quickFilterAttr != null)
            {
                item.QuickFilter = true;
                if (quickFilterAttr.Separator)
                {
                    item.QuickFilterSeparator = true;
                }

                if (!string.IsNullOrEmpty(quickFilterAttr.CssClass))
                {
                    item.QuickFilterCssClass = quickFilterAttr.CssClass;
                }
            }

            var basedOnField = source.BasedOnField;

            if (!ReferenceEquals(null, basedOnField) &&
                notFilterableAttr == null)
            {
                if (basedOnField.Flags.HasFlag(FieldFlags.DenyFiltering) ||
                    basedOnField.Flags.HasFlag(FieldFlags.NotMapped))
                {
                    item.NotFilterable = true;
                }
            }

            Field  idField;
            string idFieldName;
            var    filteringIdField = source.GetAttribute <FilteringIdFieldAttribute>();

            if (filteringIdField != null)
            {
                idFieldName = filteringIdField.Value;
                idField     = basedOnField.Fields.FindFieldByPropertyName(idFieldName) ?? basedOnField.Fields.FindField(idFieldName);
            }
            else
            {
                idFieldName = AutoDetermineIdField(basedOnField);

                idField = null;
                if (idFieldName != null)
                {
                    idField = basedOnField.Fields.FindFieldByPropertyName(idFieldName) ?? basedOnField.Fields.FindField(idFieldName);
                    if (Object.ReferenceEquals(idField, null) ||
                        (idField.TextualField != basedOnField.PropertyName &&
                         idField.TextualField != basedOnField.Name))
                    {
                        idField     = null;
                        idFieldName = null;
                    }
                }
            }

            var valueType = source.ValueType;

            var filteringTypeAttr = source.GetAttribute <FilteringTypeAttribute>() ??
                                    idField.GetAttribute <FilteringTypeAttribute>();

            if (filteringTypeAttr == null)
            {
                var editorAttr = source.GetAttribute <EditorTypeAttribute>() ??
                                 idField.GetAttribute <EditorTypeAttribute>();

                if (idFieldName != null)
                {
                    item.FilteringParams["idField"] = idFieldName;
                    item.FilteringIdField           = idFieldName;
                }

                if (editorAttr != null && !standardFilteringEditors.Contains(editorAttr.EditorType))
                {
                    if (editorAttr is LookupEditorAttribute ||
                        editorAttr is AsyncLookupEditorAttribute)
                    {
                        var async = editorAttr as AsyncLookupEditorAttribute;
                        item.FilteringType = async != null ? "AsyncLookup" : "Lookup";
                        item.FilteringParams["lookupKey"] = async != null ? async.LookupKey : ((LookupEditorAttribute)editorAttr).LookupKey;
                    }
                    else
                    {
                        item.FilteringType = "Editor";
                        item.FilteringParams["editorType"] = editorAttr.EditorType;
                        item.FilteringParams["useLike"]    = source.ValueType == typeof(String);
                    }
                }
                else if (source.EnumType != null)
                {
                    item.FilteringType = "Enum";
                    item.FilteringParams["enumKey"] = EnumMapper.GetEnumTypeKey(source.EnumType);
                }
                else if (valueType == typeof(DateTime))
                {
                    if (!ReferenceEquals(null, basedOnField) && basedOnField is DateTimeField)
                    {
                        switch (((DateTimeField)basedOnField).DateTimeKind)
                        {
                        case DateTimeKind.Unspecified:
                            item.FilteringType = "Date";
                            break;

                        default:
                            item.FilteringType = "DateTime";
                            break;
                        }
                    }
                    else
                    {
                        item.FilteringType = "Date";
                    }
                }
                else if (valueType == typeof(Boolean))
                {
                    item.FilteringType = "Boolean";
                }
                else if (valueType == typeof(Decimal) ||
                         valueType == typeof(Double) ||
                         valueType == typeof(Single))
                {
                    item.FilteringType = "Decimal";
                }
                else if (valueType == typeof(Int32) ||
                         valueType == typeof(Int16) ||
                         valueType == typeof(Int64))
                {
                    item.FilteringType = "Integer";
                }
                else
                {
                    item.FilteringType = "String";
                }
            }
            else
            {
                item.FilteringType = filteringTypeAttr.FilteringType;
                filteringTypeAttr.SetParams(item.FilteringParams);

                if (item.FilteringType == "Editor")
                {
                    if (!item.FilteringParams.ContainsKey("editorType"))
                    {
                        var editorAttr = source.GetAttribute <EditorTypeAttribute>() ??
                                         idField.GetAttribute <EditorTypeAttribute>();

                        if (editorAttr != null)
                        {
                            item.FilteringParams["editorType"] = editorAttr.EditorType;
                        }
                    }

                    if (!item.FilteringParams.ContainsKey("useLike"))
                    {
                        if (valueType == typeof(String))
                        {
                            item.FilteringParams["useLike"] = true;
                        }
                    }
                }

                object idFieldObj;
                if (item.FilteringParams.TryGetValue("idField", out idFieldObj) && idFieldObj is string)
                {
                    item.FilteringIdField = (idFieldObj as string).TrimToNull();
                }
                else
                {
                    item.FilteringIdField = idFieldName;
                }
            }

            var displayFormatAttr = source.GetAttribute <DisplayFormatAttribute>();

            if (displayFormatAttr != null)
            {
                item.FilteringParams["displayFormat"] = displayFormatAttr.Value;
            }

            foreach (FilteringOptionAttribute param in
                     idField.GetAttributes <FilteringOptionAttribute>().Concat(
                         source.GetAttributes <FilteringOptionAttribute>()))
            {
                var key = param.Key;
                if (key != null &&
                    key.Length >= 1)
                {
                    key = key.Substring(0, 1).ToLowerInvariant() + key.Substring(1);
                }

                if (key == "idField")
                {
                    item.FilteringIdField = (param.Value as string) ?? item.FilteringIdField;
                }

                item.FilteringParams[key] = param.Value;
            }

            foreach (QuickFilterOptionAttribute param in
                     idField.GetAttributes <QuickFilterOptionAttribute>().Concat(
                         source.GetAttributes <QuickFilterOptionAttribute>()))
            {
                var key = param.Key;
                if (key != null &&
                    key.Length >= 1)
                {
                    key = key.Substring(0, 1).ToLowerInvariant() + key.Substring(1);
                }

                item.QuickFilterParams[key] = param.Value;
            }
        }
        private static void SetFiltering(IPropertySource source, PropertyItem item)
        {
            var filterOnlyAttr = source.GetAttribute<FilterOnlyAttribute>();
            var notFilterableAttr = source.GetAttribute<NotFilterableAttribute>();

            if (filterOnlyAttr != null && filterOnlyAttr.Value)
                item.FilterOnly = true;

            if (notFilterableAttr != null && notFilterableAttr.Value)
                item.NotFilterable = true;

            if (item.NotFilterable == true)
                return;

            var basedOnField = source.BasedOnField;

            Field idField;
            string idFieldName;
            var filteringIdField = source.GetAttribute<FilteringIdFieldAttribute>();
            if (filteringIdField != null)
            {
                idFieldName = filteringIdField.Value;
                idField = basedOnField.Fields.FindFieldByPropertyName(idFieldName) ?? basedOnField.Fields.FindField(idFieldName);
            }
            else
            {
                idFieldName = AutoDetermineIdField(basedOnField);

                idField = null;
                if (idFieldName != null)
                {
                    idField = basedOnField.Fields.FindFieldByPropertyName(idFieldName) ?? basedOnField.Fields.FindField(idFieldName);
                    if (Object.ReferenceEquals(idField, null) ||
                        (idField.TextualField != basedOnField.PropertyName &&
                         idField.TextualField != basedOnField.Name))
                    {
                        idField = null;
                        idFieldName = null;
                    }
                }
            }

            var valueType = source.ValueType;

            var filteringTypeAttr = source.GetAttribute<FilteringTypeAttribute>() ??
                idField.GetAttribute<FilteringTypeAttribute>();

            if (filteringTypeAttr == null)
            {
                var editorAttr = source.GetAttribute<EditorTypeAttribute>() ??
                    idField.GetAttribute<EditorTypeAttribute>();

                if (idFieldName != null)
                {
                    item.FilteringParams["idField"] = idFieldName;
                    item.FilteringIdField = idFieldName;
                }

                if (editorAttr != null && !standardFilteringEditors.Contains(editorAttr.EditorType))
                {
                    if (editorAttr is LookupEditorAttribute ||
                        editorAttr is AsyncLookupEditorAttribute)
                    {
                        var async = editorAttr as AsyncLookupEditorAttribute;
                        item.FilteringType = async != null ? "AsyncLookup" : "Lookup";
                        item.FilteringParams["lookupKey"] = async != null ? async.LookupKey : ((LookupEditorAttribute)editorAttr).LookupKey;
                    }
                    else
                    {
                        item.FilteringType = "Editor";
                        item.FilteringParams["editorType"] = editorAttr.EditorType;
                        item.FilteringParams["useLike"] = source.ValueType == typeof(String);
                    }
                }
                else if (source.EnumType != null)
                {
                    item.FilteringType = "Enum";
                    item.FilteringParams["enumKey"] = EnumMapper.GetEnumTypeKey(source.EnumType);
                }
                else if (valueType == typeof(DateTime))
                {
                    if (!ReferenceEquals(null, basedOnField) && basedOnField is DateTimeField)
                    {
                        switch (((DateTimeField)basedOnField).DateTimeKind)
                        {
                            case DateTimeKind.Unspecified:
                                item.FilteringType = "Date";
                                break;
                            default:
                                item.FilteringType = "DateTime";
                                break;
                        }
                    }
                    else
                        item.FilteringType = "Date";
                }
                else if (valueType == typeof(Boolean))
                    item.FilteringType = "Boolean";
                else if (valueType == typeof(Decimal) ||
                    valueType == typeof(Double) ||
                    valueType == typeof(Single))
                {
                    item.FilteringType = "Decimal";
                }
                else if (valueType == typeof(Int32) ||
                    valueType == typeof(Int16) ||
                    valueType == typeof(Int64))
                {
                    item.FilteringType = "Integer";
                }
                else
                    item.FilteringType = "String";
            }
            else
            {
                item.FilteringType = filteringTypeAttr.FilteringType;
                filteringTypeAttr.SetParams(item.FilteringParams);

                if (item.FilteringType == "Editor")
                {
                    if (!item.FilteringParams.ContainsKey("editorType"))
                    {
                        var editorAttr =source.GetAttribute<EditorTypeAttribute>() ??
                            idField.GetAttribute<EditorTypeAttribute>();

                        if (editorAttr != null)
                            item.FilteringParams["editorType"] = editorAttr.EditorType;
                    }

                    if (!item.FilteringParams.ContainsKey("useLike"))
                    {
                        if (valueType == typeof(String))
                            item.FilteringParams["useLike"] = true;
                    }
                }

                object idFieldObj;
                if (item.FilteringParams.TryGetValue("idField", out idFieldObj) && idFieldObj is string)
                    item.FilteringIdField = (idFieldObj as string).TrimToNull();
                else
                    item.FilteringIdField = idFieldName;
            }

            var displayFormatAttr = source.GetAttribute<DisplayFormatAttribute>();
            if (displayFormatAttr != null)
                item.FilteringParams["displayFormat"] = displayFormatAttr.Value;

            foreach (var param in
                idField.GetAttributes<FilteringOptionAttribute>().Concat(
                source.GetAttributes<FilteringOptionAttribute>()))
            {
                var key = param.Key;
                if (key != null &&
                    key.Length >= 1)
                    key = key.Substring(0, 1).ToLowerInvariant() + key.Substring(1);

                if (key == "idField")
                    item.FilteringIdField = (param.Value as string) ?? item.FilteringIdField;

                item.FilteringParams[key] = param.Value;
            }
        }
        private void SetEditing(IPropertySource source, PropertyItem item)
        {
            var editorTypeAttr = source.GetAttribute <EditorTypeAttribute>();

            if (editorTypeAttr == null)
            {
                item.EditorType = AutoDetermineEditorType(source.ValueType, source.EnumType);
            }
            else
            {
                item.EditorType = editorTypeAttr.EditorType;
                editorTypeAttr.SetParams(item.EditorParams);
            }

            if (source.EnumType != null)
            {
                item.EditorParams["enumKey"] = EnumMapper.GetEnumTypeKey(source.EnumType);
            }

            if (!ReferenceEquals(null, source.BasedOnField))
            {
                if (item.EditorType == "Decimal" &&
                    (source.BasedOnField is DoubleField ||
                     source.BasedOnField is DecimalField) &&
                    source.BasedOnField.Size > 0 &&
                    source.BasedOnField.Scale < source.BasedOnField.Size)
                {
                    string minVal = new String('0', source.BasedOnField.Size - source.BasedOnField.Scale);
                    if (source.BasedOnField.Scale > 0)
                    {
                        minVal += "." + new String('0', source.BasedOnField.Scale);
                    }
                    string maxVal = minVal.Replace('0', '9');
                    item.EditorParams["minValue"] = minVal;
                    item.EditorParams["maxValue"] = maxVal;
                }
                else if (source.BasedOnField.Size > 0)
                {
                    item.EditorParams["maxLength"] = source.BasedOnField.Size;
                    item.MaxLength = source.BasedOnField.Size;
                }
            }

            var maxLengthAttr = source.GetAttribute <MaxLengthAttribute>();

            if (maxLengthAttr != null)
            {
                item.MaxLength = maxLengthAttr.MaxLength;
                item.EditorParams["maxLength"] = maxLengthAttr.MaxLength;
            }

            foreach (EditorOptionAttribute param in source.GetAttributes <EditorOptionAttribute>())
            {
                var key = param.Key;
                if (key != null &&
                    key.Length >= 1)
                {
                    key = key.Substring(0, 1).ToLowerInvariant() + key.Substring(1);
                }

                item.EditorParams[key] = param.Value;
            }
        }
Beispiel #9
0
        private void SetEditing(IPropertySource source, PropertyItem item)
        {
            var editorTypeAttr = source.GetAttribute <EditorTypeAttribute>();

            if (editorTypeAttr == null)
            {
                item.EditorType = AutoDetermineEditorType(source.ValueType, source.EnumType, item.EditorParams);
            }
            else
            {
                item.EditorType = editorTypeAttr.EditorType;
                editorTypeAttr.SetParams(item.EditorParams);
                if (item.EditorType == "Lookup" &&
                    !item.EditorParams.ContainsKey("lookupKey"))
                {
                    var distinct = source.GetAttribute <DistinctValuesEditorAttribute>();
                    if (distinct != null)
                    {
                        string prefix = null;
                        if (distinct.RowType != null)
                        {
                            if (!distinct.RowType.IsInterface &&
                                !distinct.RowType.IsAbstract &&
                                distinct.RowType.IsSubclassOf(typeof(Row)))
                            {
                                prefix = ((Row)Activator.CreateInstance(distinct.RowType))
                                         .GetFields().LocalTextPrefix;
                            }
                        }
                        else
                        {
                            var isRow = source.Property != null &&
                                        source.Property.ReflectedType != null &&
                                        !source.Property.ReflectedType.IsAbstract &&
                                        source.Property.ReflectedType.IsSubclassOf(typeof(Row));

                            if (!isRow)
                            {
                                if (!ReferenceEquals(null, source.BasedOnField))
                                {
                                    prefix = source.BasedOnField.Fields.LocalTextPrefix;
                                }
                            }
                            else
                            {
                                prefix = ((Row)Activator.CreateInstance(source.Property.ReflectedType))
                                         .GetFields().LocalTextPrefix;
                            }
                        }

                        if (prefix != null)
                        {
                            var propertyName = distinct.PropertyName.IsEmptyOrNull() ?
                                               (!ReferenceEquals(null, source.BasedOnField) ?
                                                (source.BasedOnField.PropertyName ?? source.BasedOnField.Name) :
                                                item.Name) : distinct.PropertyName;

                            if (!string.IsNullOrEmpty(propertyName))
                            {
                                item.EditorParams["lookupKey"] = "Distinct." + prefix + "." + propertyName;
                            }
                        }
                    }
                }
            }

            if (source.EnumType != null)
            {
                item.EditorParams["enumKey"] = EnumMapper.GetEnumTypeKey(source.EnumType);
            }

            if (!ReferenceEquals(null, source.BasedOnField))
            {
                if (item.EditorType == "Decimal" &&
                    (source.BasedOnField is DoubleField ||
                     source.BasedOnField is DecimalField) &&
                    source.BasedOnField.Size > 0 &&
                    source.BasedOnField.Scale < source.BasedOnField.Size &&
                    !item.EditorParams.ContainsKey("minValue") &&
                    !item.EditorParams.ContainsKey("maxValue"))
                {
                    string minVal = new String('0', source.BasedOnField.Size - source.BasedOnField.Scale);
                    if (source.BasedOnField.Scale > 0)
                    {
                        minVal += "." + new String('0', source.BasedOnField.Scale);
                    }
                    string maxVal = minVal.Replace('0', '9');
                    item.EditorParams["minValue"] = minVal;
                    item.EditorParams["maxValue"] = maxVal;
                }
                else if (source.BasedOnField.Size > 0)
                {
                    item.EditorParams["maxLength"] = source.BasedOnField.Size;
                    item.MaxLength = source.BasedOnField.Size;
                }
            }

            var maxLengthAttr = source.GetAttribute <MaxLengthAttribute>();

            if (maxLengthAttr != null)
            {
                item.MaxLength = maxLengthAttr.MaxLength;
                item.EditorParams["maxLength"] = maxLengthAttr.MaxLength;
            }

            foreach (EditorOptionAttribute param in source.GetAttributes <EditorOptionAttribute>())
            {
                var key = param.Key;
                if (key != null &&
                    key.Length >= 1)
                {
                    key = key.Substring(0, 1).ToLowerInvariant() + key.Substring(1);
                }

                item.EditorParams[key] = param.Value;
            }
        }