예제 #1
0
 public FormConfig()
 {
     FieldConfig     = new FieldConfig();
     FieldConditions = new FieldConditions();
     FieldSetConfig  = new FieldSetConfig();
     FormActions     = new Dictionary <string, AdminAction>();
 }
예제 #2
0
        public FormPropertyBuilder(IFormConfig formConfig, LambdaExpression fieldExpression)
            : base(formConfig.AllFields, fieldExpression)
        {
            _fieldConditions = formConfig.FieldConditions;
            _fieldExpression = fieldExpression;
            var fieldName = ReflectionExtensions.GetMemberName(fieldExpression);

            _field = formConfig.AllFields.FirstOrDefault(f => f.FieldName == fieldName);
        }
예제 #3
0
        private void PopulateFieldOptions(Field field, FieldConditions fieldConditions, EntityConfig entityConfig = null)
        {
            var attributes = (field.FieldExpression.Body as MemberExpression).Member.GetCustomAttributes(true);
            var efProperty = entityConfig?.EntityType?.GetProperties().FirstOrDefault(p => p.Name == field.FieldName);
            var navigation = entityConfig?.Navigations?.FirstOrDefault(n => n.Name == field.FieldName);

            if (efProperty == null && entityConfig?.EntityType != null && navigation != null)
            {
                var releatedField = GetReleatedFields(navigation.ForeignKey, entityConfig.EntityType.ClrType).FirstOrDefault();
                efProperty = entityConfig.EntityType.GetProperties().FirstOrDefault(p => p.Name == releatedField.FieldName);
            }

            if (field.FieldOption == null)
            {
                field.FieldOption = new FieldOption();
            }


            //field.FieldOption.Metadata = _modelMetadataProvider.GetMetadataForType(field.FieldClrType);



            //var displayAttribute = attributes.OfType<DisplayAttribute>().FirstOrDefault();
            //var displayFormatAttribute = attributes.OfType<DisplayFormatAttribute>().FirstOrDefault();
            //var displayNameAttribute = attributes.OfType<DisplayNameAttribute>().FirstOrDefault();

            if (field.FieldOption.FieldType == FieldType.Unknown)
            {
                var fieldTypeAttribute = attributes.OfType <FieldInfoAttribute>().FirstOrDefault();
                field.FieldType = fieldTypeAttribute?.FieldType ?? FieldType.Unknown;
            }
            else
            {
                field.FieldType = field.FieldOption.FieldType;
            }

            //TODO: Localization in future
            //IStringLocalizer localizer = null;
            //if (_stringLocalizerFactory != null && _localizationOptions.DataAnnotationLocalizerProvider != null)
            //{
            //    localizer = _localizationOptions.DataAnnotationLocalizerProvider(containerType, _stringLocalizerFactory);
            //}

            // DisplayName
            // DisplayAttribute has precedence over DisplayNameAttribute.

            if (string.IsNullOrEmpty(field.FieldOption.DisplayName))
            {
                //if (displayAttribute?.GetName() != null)
                //{
                //    field.FieldOption.DisplayName = displayAttribute.GetName();
                //}
                //else if (displayNameAttribute != null)
                //{
                //    //if (localizer != null &&
                //    //    !string.IsNullOrEmpty(displayNameAttribute.DisplayName))
                //    //{
                //    //    displayMetadata.DisplayName = () => localizer[displayNameAttribute.DisplayName];
                //    //}
                //    //else
                //    //{
                //    field.FieldOption.DisplayName = displayNameAttribute.DisplayName;
                //    //}
                //}
                //if (!string.IsNullOrEmpty(metadata.DisplayName))
                //{
                //    field.FieldOption.DisplayName = metadata.DisplayName;
                //}
                //else
                //{
                field.FieldOption.DisplayName = field.FieldName;
                //}
            }


            if (string.IsNullOrEmpty(field.FieldOption.Format) /*&& displayFormatAttribute != null*/)
            {
                //field.FieldOption.Format = metadata.DisplayFormatString; //displayFormatAttribute.DataFormatString;

                //if (string.IsNullOrEmpty(field.FieldOption.Format))
                //{
                field.FieldOption.Format = _defaultDateFormat;
                //}
            }

            //if (string.IsNullOrEmpty(field.FieldOption.Description) /*&& displayAttribute != null*/)
            //{
            //    //if (localizer != null &&
            //    //    !string.IsNullOrEmpty(displayAttribute.Description) &&
            //    //    displayAttribute.ResourceType == null)
            //    //{
            //    //    displayMetadata.Description = () => localizer[displayAttribute.Description];
            //    //}
            //    //else
            //    //{
            //    field.FieldOption.Description = metadata.Description; //displayAttribute.GetDescription();
            //    //}
            //}

            //// NullDisplayText
            //if (string.IsNullOrEmpty(field.FieldOption.NullDisplayText) /*&& displayFormatAttribute != null*/)
            //{
            //    field.FieldOption.NullDisplayText = metadata.NullDisplayText; //displayFormatAttribute.NullDisplayText;
            //}

            if (field.FieldOption.MaxLength == 0)
            {
                field.FieldOption.MaxLength = efProperty?.GetMaxLength() ?? 0;
            }

            if (field.FieldType == FieldType.Unknown)
            {
                field.FieldType = GetFieldType(field);
            }

            if (field.FieldOption.IsRequired == null)
            {
                field.FieldOption.IsRequired = (!efProperty?.IsColumnNullable() ?? false) || !field.FieldClrType.IsNullable();
            }

            //FieldConditions
            var showCondition = fieldConditions?.ShowOnConditions?.FirstOrDefault(f => f.FieldName == field.FieldName);

            if (showCondition != null)
            {
                field.FieldOption.ShowOn = showCondition.ConditionExpression;
            }

            var enableCondition = fieldConditions?.EnableOnConditions?.FirstOrDefault(f => f.FieldName == field.FieldName);

            if (enableCondition != null)
            {
                field.FieldOption.EnableOn = enableCondition.ConditionExpression;
            }

            var validateCondition = fieldConditions?.ValidateOnConditions?.FirstOrDefault(f => f.FieldName == field.FieldName);

            if (validateCondition != null)
            {
                field.FieldOption.ValidateOn = validateCondition.ConditionExpression;
            }

            //if (entityConfig != null)
            //{
            if (field.FieldOption.RelationType != RelationType.None)
            {
                //var navigation = entityConfig.Navigations.FirstOrDefault(n => n.Name == field.FieldName);
                if (field.FieldOption.RelationType == RelationType.ManyToMany)
                {
                    field.FieldType = FieldType.MultiSelect;
                    //var fieldClrType = navigation.FieldInfo.FieldType.GenericTypeArguments[0];
                    //var fieldEntityType = _dbContext.Model.FindEntityType(fieldClrType);
                    //var fieldNavigations = fieldEntityType.GetNavigations();

                    //field.FieldOption.ReleatedFields = new List<ReleatedField>();

                    //foreach (var fieldNav in fieldNavigations)
                    //{
                    //    var releatedFields = GetReleatedFields(fieldNav.ForeignKey, entityType.ClrType);
                    //    field.FieldOption.ReleatedFields.AddRange(releatedFields);
                    //}

                    //TODO: Validate ManyToMay relationship using the hints from following commented code
                }
                else if (field.FieldOption.RelationType == RelationType.ManyToOne)
                {
                    field.FieldType = FieldType.Select;

                    //var fKField = GetReleatedFields(navigation.ForeignKey, entityType.ClrType);
                    //field.FieldOption.ReleatedFields = fKField;

                    //TODO: Validate ManyToOne relationship using the hints from following commented code
                }
            }
            //}



            //Check wheter the field is one to many or many to many
            //var isMultiple = field.FieldClrType.GetInterfaces().Any(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(ICollection<>));
            //var navigation = adminConfig.EntityConfig.Navigations.FirstOrDefault(n => n.Name == field.FieldName);
            //if (navigation != null)
            //{
            //    if (isMultiple && navigation.FieldInfo.FieldType.IsGenericType)
            //    {
            //        var releatedClrType = navigation.FieldInfo.FieldType.GenericTypeArguments[0];
            //        var releatedEntityType = _dbContext.Model.FindEntityType(releatedClrType);
            //        var releatedEntityNavigations = releatedEntityType.GetNavigations();
            //        var grandChildEntity = releatedEntityNavigations.FirstOrDefault(n => n.ForeignKey != navigation.ForeignKey);
            //        field.FieldOption.RelationType = grandChildEntity != null ? RelationType.ManyToMany : RelationType.OneToMany;
            //    }
            //    else
            //    {
            //        var releatedClrType = navigation.FieldInfo.FieldType;
            //        var releatedEntityType = _dbContext.Model.FindEntityType(releatedClrType);
            //    }

            //}
        }