예제 #1
0
        public async Task <ModelConfigurationDto> GetByNameAsync(string name, string @namespace)
        {
            var dto = await _modelConfigurationProvider.GetModelConfigurationOrNullAsync(@namespace, name);

            if (dto == null)
            {
                var exception = new EntityNotFoundException("Model configuration not found");
                exception.MarkExceptionAsLogged();
                throw exception;
            }

            return(dto);
        }
예제 #2
0
        public async Task <List <PropertyMetadataDto> > GetPropertiesAsync(string container)
        {
            if (string.IsNullOrWhiteSpace(container))
            {
                throw new AbpValidationException($"'{nameof(container)}' is mandatory");
            }

            var containerType = await GetContainerTypeAsync(container);

            if (containerType == null)
            {
                return(new List <PropertyMetadataDto>());
            }

            var flags = BindingFlags.Public | BindingFlags.Instance;

            var hardCodedProps = containerType.GetProperties(flags)
                                 .Select(p => _metadataProvider.GetPropertyMetadata(p))
                                 .OrderBy(e => e.Path)
                                 .ToList();

            // try to get data-driven configuration
            var modelConfig = await _modelConfigurationProvider.GetModelConfigurationOrNullAsync(containerType.Namespace, containerType.Name);

            if (modelConfig != null)
            {
                var idx = 0;
                return(modelConfig.Properties
                       .Select(p => {
                    var hardCodedProp = hardCodedProps.FirstOrDefault(pp => pp.Path == p.Name);

                    return new PropertyMetadataDto
                    {
                        IsVisible = hardCodedProp?.IsVisible ?? true,
                        Required = hardCodedProp?.Required ?? false,
                        Readonly = hardCodedProp?.Readonly ?? false,
                        MinLength = hardCodedProp?.MinLength,
                        MaxLength = hardCodedProp?.MaxLength,
                        Min = hardCodedProp?.Min,
                        Max = hardCodedProp?.Max,
                        EnumType = hardCodedProp?.EnumType,
                        OrderIndex = idx++,
                        GroupName = hardCodedProp?.GroupName,

                        Path = p.Name,
                        Label = p.Label,
                        Description = p.Description,
                        DataType = p.DataType,
                        DataFormat = p.DataFormat,
                        EntityTypeShortAlias = p.EntityType,
                        ReferenceListName = p.ReferenceListName,
                        ReferenceListNamespace = p.ReferenceListNamespace,
                        IsFrameworkRelated = p.IsFrameworkRelated,
                        Properties = p.Properties.Select(pp => ObjectMapper.Map <PropertyMetadataDto>(pp)).ToList()
                    };
                })
                       .ToList());
            }
            else
            {
                return(hardCodedProps);
            }
        }
예제 #3
0
        public DataTablesDisplayPropertyColumn GetDisplayPropertyColumn(Type rowType, string propName, string name = null)
        {
            var prop = propName == null
                ? null
                : ReflectionHelper.GetProperty(rowType, propName);

            var displayAttribute = prop != null
                ? prop.GetAttribute <DisplayAttribute>()
                : null;

            var caption = displayAttribute != null && !string.IsNullOrWhiteSpace(displayAttribute.Name)
                ? displayAttribute.Name
                : propName.ToFriendlyName();

            var dataTypeInfo = prop != null
                ? _metadataProvider.GetDataType(prop)
                : new DataTypeInfo(null);

            var column = new DataTablesDisplayPropertyColumn()
            {
                Name             = (propName ?? "").Replace('.', '_'),
                PropertyName     = propName,
                Caption          = caption,
                Description      = prop?.GetDescription(),
                StandardDataType = dataTypeInfo.DataType,
                DataFormat       = dataTypeInfo.DataFormat,

                #region backward compatibility, to be removed
                GeneralDataType = prop != null
                    ? EntityConfigurationLoaderByReflection.GetGeneralDataType(prop)
                    : (GeneralDataType?)null,
                CustomDataType = prop?.GetAttribute <DataTypeAttribute>()?.CustomDataType,
                #endregion
            };
            var entityConfig = prop?.DeclaringType.GetEntityConfiguration();
            var propConfig   = prop != null ? entityConfig?.Properties[prop.Name] : null;

            if (propConfig != null)
            {
                column.ReferenceListName      = propConfig.ReferenceListName;
                column.ReferenceListNamespace = propConfig.ReferenceListNamespace;
                if (propConfig.EntityReferenceType != null)
                {
                    column.EntityReferenceTypeShortAlias = propConfig.EntityReferenceType.GetEntityConfiguration()?.SafeTypeShortAlias;
                    column.AllowInherited = propConfig.PropertyInfo.HasAttribute <AllowInheritedAttribute>();
                }
            }

            if (prop == null)
            {
                var modelConfig    = AsyncHelper.RunSync <ModelConfigurationDto>(() => _modelConfigurationProvider.GetModelConfigurationOrNullAsync(rowType.Namespace, rowType.Name));
                var propertyConfig = modelConfig.Properties.FirstOrDefault(p => p.Name == propName);
                if (propertyConfig != null)
                {
                    column.IsDynamic        = true;
                    column.StandardDataType = propertyConfig.DataType;
                    column.DataFormat       = propertyConfig.DataFormat;
                    column.Description      = propertyConfig.Description;
                    column.IsFilterable     = false;
                    column.IsSortable       = false;
                }
            }

            // Set FilterCaption and FilterPropertyName
            column.FilterCaption ??= column.Caption;
            column.FilterPropertyName ??= column.PropertyName;

            if (column.PropertyName == null)
            {
                column.PropertyName = column.FilterPropertyName;
                column.Name         = (column.PropertyName ?? "").Replace('.', '_');
            }
            column.Caption ??= column.FilterCaption;

            // Check is the property mapped to the DB. If it's not mapped - make the column non sortable and non filterable
            if (column.IsSortable && rowType.IsEntityType() && propName != null && propName != "Id")
            {
                var chain = propName.Split('.').ToList();

                var container = rowType;
                foreach (var chainPropName in chain)
                {
                    if (!container.IsEntityType())
                    {
                        break;
                    }

                    var containerConfig = container.GetEntityConfiguration();
                    var propertyConfig  = containerConfig.Properties.ContainsKey(chainPropName)
                        ? containerConfig.Properties[chainPropName]
                        : null;

                    if (propertyConfig != null && !propertyConfig.IsMapped)
                    {
                        column.IsFilterable = false;
                        column.IsSortable   = false;
                        break;
                    }

                    container = propertyConfig?.PropertyInfo.PropertyType;
                    if (container == null)
                    {
                        break;
                    }
                }
            }

            return(column);
        }