Exemplo n.º 1
0
        public bool ActivateFor(IRow row)
        {
            if (Target is null)
            {
                return(false);
            }

            attr = Target.GetAttribute <ImageUploadEditorAttribute>();
            if (attr == null || attr.DisableDefaultBehavior || attr.EditorType != "ImageUpload")
            {
                return(false);
            }

            if (!(Target is StringField))
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
                                                          "Field '{0}' on row type '{1}' has a UploadEditor attribute but it is not a String field!",
                                                          Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }

            if (!(row is IIdRow))
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
                                                          "Field '{0}' on row type '{1}' has a UploadEditor attribute but Row type doesn't implement IIdRow!",
                                                          Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }

            if (!attr.OriginalNameProperty.IsEmptyOrNull())
            {
                var originalNameField = row.FindFieldByPropertyName(attr.OriginalNameProperty) ??
                                        row.FindField(attr.OriginalNameProperty);

                if (originalNameField is null)
                {
                    throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
                                                              "Field '{0}' on row type '{1}' has a UploadEditor attribute but " +
                                                              "a field with OriginalNameProperty '{2}' is not found!",
                                                              Target.PropertyName ?? Target.Name,
                                                              row.GetType().FullName,
                                                              attr.OriginalNameProperty));
                }

                this.originalNameField = (StringField)originalNameField;
            }

            var format = attr.FilenameFormat;

            if (format == null)
            {
                format = row.GetType().Name;
                if (format.EndsWith("Row", StringComparison.Ordinal))
                {
                    format = format[0..^ 3];
Exemplo n.º 2
0
        private Field GetFieldFrom(IRow row)
        {
            Field field = row.FindFieldByPropertyName(propertyName) ??
                          row.FindField(propertyName);

            if (field is null)
            {
                throw new InvalidProgramException(string.Format(CultureInfo.CurrentCulture,
                                                                "Property '{0}' specified in a distinct values script on " +
                                                                "row type {1} is not found!", propertyName, row.GetType().FullName));
            }

            return(field);
        }
Exemplo n.º 3
0
        public bool ActivateFor(IRow row)
        {
            var attrs = row.GetType().GetCustomAttributes <UpdatableExtensionAttribute>();

            if (attrs == null || !attrs.Any())
            {
                return(false);
            }

            var sourceByExpression = row.GetFields().ToLookup(x =>
                                                              BracketLocator.ReplaceBrackets(x.Expression.TrimToEmpty(), BracketRemoverDialect.Instance));

            infoList = attrs.Select(attr =>
            {
                var info = new RelationInfo
                {
                    Attr = attr
                };

                var rowType = attr.RowType;
                if (rowType.IsAbstract ||
                    !typeof(IRow).IsAssignableFrom(rowType) ||
                    rowType.IsInterface)
                {
                    throw new ArgumentException(string.Format(
                                                    "Row type '{1}' has an ExtensionRelation attribute " +
                                                    "but its specified extension row type '{0}' is not a valid row class!",
                                                    rowType.FullName,
                                                    row.GetType().FullName));
                }

                info.RowFactory = () => (IRow)Activator.CreateInstance(rowType);

                var thisKey = attr.ThisKey;
                if (string.IsNullOrEmpty(thisKey))
                {
                    if (!(row is IIdRow))
                    {
                        throw new ArgumentException(string.Format(
                                                        "Row type '{0}' has an ExtensionRelation attribute " +
                                                        "but its ThisKey is not specified!",
                                                        row.GetType().FullName));
                    }

                    info.ThisKeyField = row.IdField;
                }
                else
                {
                    info.ThisKeyField = row.FindFieldByPropertyName(attr.ThisKey) ??
                                        row.FindField(attr.ThisKey);
                    if (info.ThisKeyField is null)
                    {
                        throw new ArgumentException(string.Format("Field '{0}' doesn't exist in row of type '{1}'." +
                                                                  "This field is specified for an ExtensionRelation attribute",
                                                                  attr.ThisKey,
                                                                  row.GetType().FullName));
                    }
                }

                var ext = info.RowFactory();

                var otherKey = attr.OtherKey;
                if (string.IsNullOrEmpty(otherKey))
                {
                    info.OtherKeyField = ext.FindField(info.ThisKeyField.Name);

                    if (info.OtherKeyField is null && ext is IIdRow)
                    {
                        info.OtherKeyField = row.IdField;
                    }

                    if (info.OtherKeyField is null)
                    {
                        throw new ArgumentException(string.Format(
                                                        "Row type '{1}' has an ExtensionRelation attribute " +
                                                        "but its OtherKey is not specified!",
                                                        row.GetType().FullName));
                    }
                }
                else
                {
                    info.OtherKeyField = ext.FindFieldByPropertyName(attr.OtherKey) ?? ext.FindField(attr.OtherKey);
                    if (info.OtherKeyField is null)
                    {
                        throw new ArgumentException(string.Format("Field '{0}' doesn't exist in row of type '{1}'." +
                                                                  "This field is specified for an ExtensionRelation attribute on '{2}'",
                                                                  attr.OtherKey,
                                                                  ext.GetType().FullName,
                                                                  row.GetType().FullName));
                    }
                }

                if (!string.IsNullOrEmpty(attr.FilterField))
                {
                    info.FilterField = ext.FindFieldByPropertyName(attr.FilterField) ?? ext.FindField(attr.FilterField);
                    if (info.FilterField is null)
                    {
                        throw new ArgumentException(string.Format("Field '{0}' doesn't exist in row of type '{1}'." +
                                                                  "This field is specified as FilterField for an ExtensionRelation attribute on '{2}'",
                                                                  attr.OtherKey,
                                                                  ext.GetType().FullName,
                                                                  row.GetType().FullName));
                    }

                    info.FilterValue = info.FilterField.ConvertValue(attr.FilterValue, CultureInfo.InvariantCulture);
                }

                if (!string.IsNullOrEmpty(attr.PresenceField))
                {
                    info.PresenceField = row.FindFieldByPropertyName(attr.PresenceField) ?? row.FindField(attr.PresenceField);
                    if (info.PresenceField is null)
                    {
                        throw new ArgumentException(string.Format("Field '{0}' doesn't exist in row of type '{1}'." +
                                                                  "This field is specified as PresenceField as an ExtensionRelation attribute.",
                                                                  attr.PresenceField,
                                                                  row.GetType().FullName));
                    }

                    info.PresenceValue = attr.PresenceValue;
                }

                var extFields   = ext.GetFields();
                var alias       = attr.Alias;
                var aliasPrefix = attr.Alias + "_";

                var joinByKey = new HashSet <string>(extFields.Joins.Keys, StringComparer.OrdinalIgnoreCase);

                string mapAlias(string x)
                {
                    if (x == "t0" || x == "T0")
                    {
                        return(alias);
                    }

                    if (!joinByKey.Contains(x))
                    {
                        return(x);
                    }

                    return(aliasPrefix + x);
                }

                string mapExpression(string x)
                {
                    if (x == null)
                    {
                        return(null);
                    }

                    return(JoinAliasLocator.ReplaceAliases(x, mapAlias));
                }

                info.Mappings = new List <Tuple <Field, Field> >();
                foreach (var field in extFields)
                {
                    if (ReferenceEquals(info.OtherKeyField, field))
                    {
                        continue;
                    }

                    if (ReferenceEquals(info.FilterField, field))
                    {
                        continue;
                    }

                    var expression = field.Expression.TrimToEmpty();

                    if (string.IsNullOrEmpty(expression))
                    {
                        continue;
                    }

                    expression = mapExpression(expression);
                    expression = BracketLocator.ReplaceBrackets(expression,
                                                                BracketRemoverDialect.Instance);

                    var match = sourceByExpression[expression].FirstOrDefault();
                    if (match is null)
                    {
                        continue;
                    }

                    if (match.IsTableField())
                    {
                        continue;
                    }

                    if (ReferenceEquals(info.ThisKeyField, match))
                    {
                        continue;
                    }

                    if (field.GetType() != match.GetType())
                    {
                        throw new ArgumentException(string.Format(
                                                        "Row type '{0}' has an ExtensionRelation attribute to '{1}'." +
                                                        "Their '{2}' and '{3}' fields are matched but they have different types ({4} and {5})!",
                                                        row.GetType().FullName,
                                                        ext.GetType().FullName,
                                                        field.PropertyName ?? field.Name,
                                                        match.PropertyName ?? match.Name,
                                                        field.GetType().Name,
                                                        match.GetType().Name));
                    }

                    info.Mappings.Add(new Tuple <Field, Field>(match, field));
                }

                if (info.Mappings.Count == 0)
                {
                    throw new ArgumentException(string.Format(
                                                    "Row type '{0}' has an ExtensionRelation attribute " +
                                                    "but no view fields could be matched to extension row '{1}'!",
                                                    row.GetType().FullName,
                                                    ext.GetType().FullName));
                }

                return(info);
            }).ToList();

            return(true);
        }
        public bool ActivateFor(IRow row)
        {
            if (Target is null)
            {
                return(false);
            }

            attr = Target.GetAttribute <MasterDetailRelationAttribute>();
            if (attr == null)
            {
                return(false);
            }

            var rowListType = Target.ValueType;

            if (!rowListType.IsGenericType ||
                rowListType.GetGenericTypeDefinition() != typeof(List <>))
            {
                throw new ArgumentException(string.Format("Field '{0}' in row type '{1}' has a MasterDetailRelationAttribute " +
                                                          "but its property type is not a generic List (e.g. List<IRow>)!",
                                                          Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }

            rowType = rowListType.GetGenericArguments()[0];
            if (rowType.IsAbstract ||
                !typeof(IRow).IsAssignableFrom(rowType) ||
                rowType.IsInterface)
            {
                throw new ArgumentException(string.Format(
                                                "Field '{0}' in row type '{1}' has a MasterDetailRelationAttribute " +
                                                "but its property type is not a generic list of rows (e.g. List<IRow>)!",
                                                Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }

            rowListFactory = () => (IList)Activator.CreateInstance(rowListType);
            rowFactory     = () => (IRow)Activator.CreateInstance(rowType);

            if (attr.MasterKeyField != null)
            {
                // Use field from AltIdField
                masterKeyField = row.FindFieldByPropertyName(attr.MasterKeyField) ??
                                 row.FindField(attr.MasterKeyField);

                if (masterKeyField is null)
                {
                    throw new ArgumentException(string.Format("Field '{0}' doesn't exist in row of type '{1}'." +
                                                              "This field is specified for a master detail relation in field '{2}'.",
                                                              attr.MasterKeyField, row.GetType().FullName,
                                                              Target.PropertyName ?? Target.Name));
                }
            }
            else
            {
                // Default behaviour: use id field
                masterKeyField = row.IdField;
            }

            var detailRow = rowFactory();

            foreignKeyField = detailRow.FindFieldByPropertyName(attr.ForeignKey) ??
                              detailRow.FindField(attr.ForeignKey);

            if (foreignKeyField is null)
            {
                throw new ArgumentException(string.Format("Field '{0}' doesn't exist in row of type '{1}'." +
                                                          "This field is specified for a master detail relation in field '{2}' of row type '{3}'.",
                                                          attr.ForeignKey, detailRow.GetType().FullName,
                                                          Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }

            foreignKeyCriteria = new Criteria(foreignKeyField.PropertyName ?? foreignKeyField.Name);

            if (!string.IsNullOrEmpty(attr.FilterField))
            {
                filterField = detailRow.FindFieldByPropertyName(attr.FilterField) ?? detailRow.FindField(attr.FilterField);
                if (filterField is null)
                {
                    throw new ArgumentException(string.Format("Field '{0}' doesn't exist in row of type '{1}'." +
                                                              "This field is specified for a master detail relation as FilterField in field '{2}' of row type '{3}'.",
                                                              attr.FilterField, detailRow.GetType().FullName,
                                                              Target.PropertyName ?? Target.Name, row.GetType().FullName));
                }

                filterCriteria = new Criteria(filterField.PropertyName ?? filterField.Name);
                filterValue    = filterField.ConvertValue(attr.FilterValue, CultureInfo.InvariantCulture);
                if (filterValue == null)
                {
                    filterCriteria = filterCriteria.IsNull();
                    queryCriteria  = filterField.IsNull();
                }
                else
                {
                    filterCriteria = filterCriteria == new ValueCriteria(filterValue);
                    queryCriteria  = filterField == new ValueCriteria(filterValue);
                }
            }

            queryCriteria &= ServiceQueryHelper.GetNotDeletedCriteria(detailRow);

            includeColumns = new HashSet <string>();

            if (!string.IsNullOrEmpty(attr.IncludeColumns))
            {
                foreach (var s in attr.IncludeColumns.Split(','))
                {
                    var col = s.TrimToNull();
                    if (col != null)
                    {
                        includeColumns.Add(col);
                    }
                }
            }

            return(true);
        }
Exemplo n.º 5
0
        public static List <ReportColumn> GetColumnListFor(Type columnsType,
                                                           IEnumerable <string> columnOrder, IServiceProvider serviceProvider)
        {
            if (columnsType == null)
            {
                throw new ArgumentNullException(nameof(columnsType));
            }

            var list = new List <ReportColumn>();

            if (columnOrder != null && !columnOrder.Any())
            {
                return(list);
            }

            List <PropertyItem> propertyItems = null;
            IDictionary <string, PropertyItem> propertyItemByName = null;
            IDictionary <string, PropertyInfo> propertyInfos      = null;
            IRow basedOnRow = null;

            if (columnsType != null)
            {
                var cache = serviceProvider.GetRequiredService <ITwoLevelCache>();
                propertyItems = cache.GetLocalStoreOnly("DynamicDataReport: Columns:" + columnsType.FullName,
                                                        TimeSpan.Zero, CacheGroupKey, () =>
                {
                    var propertyItemProvider = serviceProvider.GetRequiredService <IPropertyItemProvider>();
                    var items = propertyItemProvider.GetPropertyItemsFor(columnsType).ToList();

                    if (typeof(ICustomizedFormScript).IsAssignableFrom(columnsType))
                    {
                        var instance = ActivatorUtilities.CreateInstance(serviceProvider, columnsType) as ICustomizedFormScript;
                        instance.Customize(items);
                    }

                    return(items);
                });

                propertyItemByName = propertyItems.ToDictionary(x => x.Name);
                propertyInfos      = columnsType.GetProperties().ToDictionary(x => x.Name);

                var basedOnAttr = columnsType.GetCustomAttribute <BasedOnRowAttribute>();
                if (basedOnAttr != null &&
                    basedOnAttr.RowType != null &&
                    !basedOnAttr.RowType.IsInterface &&
                    !basedOnAttr.RowType.IsAbstract &&
                    typeof(IRow).IsAssignableFrom(basedOnAttr.RowType))
                {
                    basedOnRow = (IRow)Activator.CreateInstance(basedOnAttr.RowType);
                }
            }

            if (columnOrder == null)
            {
                columnOrder = propertyItems.Select(x => x.Name).ToList();
            }

            foreach (var columnName in columnOrder)
            {
                if (!propertyItemByName.TryGetValue(columnName, out PropertyItem item))
                {
                    continue;
                }

                var basedOnField = basedOnRow == null ? null :
                                   (basedOnRow.FindField(columnName) ?? basedOnRow.FindFieldByPropertyName(columnName));

                if (propertyInfos == null || !propertyInfos.TryGetValue(columnName, out PropertyInfo p))
                {
                    p = null;
                }

                list.Add(FromPropertyItem(item, basedOnField, p, serviceProvider, serviceProvider.GetRequiredService <ITextLocalizer>()));
            }

            return(list);
        }
Exemplo n.º 6
0
        public static List <ReportColumn> ObjectTypeToList(Type objectType,
                                                           IServiceProvider serviceProvider, ITextLocalizer localizer)
        {
            var list = new List <ReportColumn>();

            IRow basedOnRow     = null;
            var  basedOnRowAttr = objectType.GetCustomAttribute <BasedOnRowAttribute>();

            if (basedOnRowAttr != null)
            {
                basedOnRow = Activator.CreateInstance(basedOnRowAttr.RowType) as IRow;
            }

            foreach (MemberInfo member in objectType.GetMembers(BindingFlags.Instance | BindingFlags.Public))
            {
                var fieldInfo    = member as FieldInfo;
                var propertyInfo = member as PropertyInfo;
                if (fieldInfo == null &&
                    propertyInfo == null)
                {
                    continue;
                }

                if (member.GetCustomAttribute <IgnoreAttribute>() != null)
                {
                    continue;
                }

                Field baseField;
                if (basedOnRow != null)
                {
                    var name = ((MemberInfo)fieldInfo ?? propertyInfo).Name;
                    baseField = basedOnRow.FindFieldByPropertyName(name) ?? basedOnRow.FindField(name);
                }
                else
                {
                    baseField = null;
                }

                ReportColumn column;
                if (fieldInfo != null)
                {
                    column = FromFieldInfo(fieldInfo, localizer, baseField);
                }
                else
                {
                    column = FromPropertyInfo(propertyInfo, localizer, baseField);
                }

                var cellDecorator = member.GetCustomAttribute <CellDecoratorAttribute>();
                if (cellDecorator != null)
                {
                    var decorator = (ICellDecorator)ActivatorUtilities.CreateInstance(
                        serviceProvider, cellDecorator.DecoratorType);
                    column.Decorator = decorator;
                }

                list.Add(column);
            }

            return(list);
        }