コード例 #1
0
        protected override List <TRow> GetItems()
        {
            var items = base.GetItems();

            if (items.Any())
            {
                var attr = items.First().GetType().GetCustomAttribute <LocalizationRowAttribute>();
                if (attr == null)
                {
                    return(items);
                }

                var localRowType        = attr.LocalizationRow;
                var localRowFactory     = FastReflection.DelegateForConstructor <Row>(localRowType);
                var localRow            = localRowFactory();
                var foreignKeyFieldName = attr.MappedIdField ?? ((Field)((IIdRow)items.First()).IdField).PropertyName;
                var foreignKeyField     = localRow.FindFieldByPropertyName(foreignKeyFieldName) ??
                                          localRow.FindField(foreignKeyFieldName);
                var foreignKeyCriteria = new Criteria(foreignKeyField.PropertyName ?? foreignKeyField.Name);

                var languageIdKeyField = ((Field)((ILocalizationRow)localRow).CultureIdField).PropertyName;
                var languageIdField    = localRow.FindFieldByPropertyName(languageIdKeyField) ??
                                         localRow.FindField(languageIdKeyField);
                var languageIdCriteria = new Criteria(languageIdField.PropertyName ?? languageIdField.Name);

                var culture = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;

                var userLanguage = new LanguageRow();
                using (var connectionLanguage = SqlConnections.NewFor <LanguageRow>())
                {
                    userLanguage = connectionLanguage.TryFirst <LanguageRow>(new Criteria(LanguageRow.Fields.LanguageId).Like($"%{culture}%"));
                    if (userLanguage == null)
                    {
                        return(items);
                    }
                }

                var listHandler = DefaultHandlerFactory.ListHandlerFor(localRowType);
                var listRequest = DefaultHandlerFactory.ListRequestFor(localRowType);
                listRequest.ColumnSelection = ColumnSelection.List;
                listRequest.Criteria        = (foreignKeyCriteria.In(items.Select(i => i.IdField[i])) && languageIdCriteria == userLanguage.Id.Value);

                var translationsResponse = listHandler.Process(SqlConnections.NewFor <TRow>(), listRequest);
                if (translationsResponse.Entities.Count == 0)
                {
                    return(items);
                }
                foreach (var item in items)
                {
                    var translated = translationsResponse.Entities.Cast <IOMPLocalizationLangRow>().FirstOrDefault(
                        t => (Int32)foreignKeyField.AsObject(t as Row) == item.IdField[item]);
                    if (translated != null && !string.IsNullOrEmpty(translated.NameField[translated as Row]))
                    {
                        item.NameField[item] = translated.NameField[translated as Row];
                    }
                }
            }

            return(items);
        }
コード例 #2
0
        public bool ActivateFor(Row row)
        {
            var loc = row as IOMPLocalizationRow;

            if (loc == null)
            {
                return(false);
            }

            attr = row.GetType().GetCustomAttribute <LocalizationRowAttribute>();
            if (attr == null)
            {
                return(false);
            }

            localRowType = attr.LocalizationRow;
            if (!typeof(ILocalizationRow).IsAssignableFrom(localRowType))
            {
                throw new ArgumentException(String.Format(
                                                "Row type '{0}' has a LocalizationRowAttribute, " +
                                                "but its localization row type ('{1}') doesn't implement ILocalizationRow interface!",
                                                row.GetType().FullName, localRowType.FullName));
            }

            localRowFactory = FastReflection.DelegateForConstructor <Row>(localRowType);
            var localRow = localRowFactory();

            var foreignKeyFieldName = attr.MappedIdField ?? ((Field)((IIdRow)row).IdField).PropertyName;

            foreignKeyField = localRow.FindFieldByPropertyName(foreignKeyFieldName) ??
                              localRow.FindField(foreignKeyFieldName);

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


            var languageIdKeyField = ((Field)((ILocalizationRow)localRow).CultureIdField).PropertyName;

            languageIdField = localRow.FindFieldByPropertyName(languageIdKeyField) ??
                              localRow.FindField(languageIdKeyField);

            this.languageIdCriteria = new Criteria(languageIdField.PropertyName ?? languageIdField.Name);

            return(true);
        }
コード例 #3
0
        public bool ActivateFor(Row row)
        {
            if (ReferenceEquals(null, Target))
            {
                return(false);
            }

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

            if (!(row is IIdRow))
            {
                throw new ArgumentException(String.Format("Field '{0}' in row type '{1}' has a LinkingSetRelationBehavior " +
                                                          "but it doesn't implement IIdRow!",
                                                          Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }


            var listType = Target.ValueType;

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

            var rowType = attr.RowType;

            if (rowType.IsAbstract ||
                !typeof(Row).IsAssignableFrom(rowType))
            {
                throw new ArgumentException(String.Format(
                                                "Field '{0}' in row type '{1}' has a LinkingSetRelationBehavior " +
                                                "but specified row type is not valid row class!",
                                                Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }

            if (!typeof(IIdRow).IsAssignableFrom(rowType))
            {
                throw new ArgumentException(String.Format(
                                                "Field '{0}' in row type '{1}' has a LinkingSetRelationBehavior " +
                                                "but specified row type doesn't implement IIdRow!",
                                                Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }

            listFactory = FastReflection.DelegateForConstructor <IList>(listType);
            rowFactory  = FastReflection.DelegateForConstructor <Row>(rowType);

            listHandlerFactory = FastReflection.DelegateForConstructor <IListRequestProcessor>(
                typeof(ListRequestHandler <>).MakeGenericType(rowType));

            saveHandlerFactory = FastReflection.DelegateForConstructor <ISaveRequestProcessor>(
                typeof(SaveRequestHandler <>).MakeGenericType(rowType));

            saveRequestFactory = FastReflection.DelegateForConstructor <ISaveRequest>(
                typeof(SaveRequest <>).MakeGenericType(rowType));

            deleteHandlerFactory = FastReflection.DelegateForConstructor <IDeleteRequestProcessor>(
                typeof(DeleteRequestHandler <>).MakeGenericType(rowType));

            var detailRow = rowFactory();

            thisKeyField = detailRow.FindFieldByPropertyName(attr.ThisKey) ??
                           detailRow.FindField(attr.ThisKey);

            if (ReferenceEquals(thisKeyField, null))
            {
                throw new ArgumentException(String.Format("Field '{0}' doesn't exist in row of type '{1}'." +
                                                          "This field is specified for a linking set relation in field '{2}' of row type '{3}'.",
                                                          attr.ThisKey, detailRow.GetType().FullName,
                                                          Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }

            itemKeyField = detailRow.FindFieldByPropertyName(attr.ItemKey) ??
                           detailRow.FindField(attr.ItemKey);

            if (ReferenceEquals(itemKeyField, null))
            {
                throw new ArgumentException(String.Format("Field '{0}' doesn't exist in row of type '{1}'." +
                                                          "This field is specified for a linking set relation in field '{2}' of row type '{3}'.",
                                                          attr.ItemKey, detailRow.GetType().FullName,
                                                          Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }

            return(true);
        }
コード例 #4
0
        public bool ActivateFor(Row 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));

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

                var rowType = attr.RowType;
                if (rowType.IsAbstract ||
                    !typeof(Row).IsAssignableFrom(rowType))
                {
                    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 = FastReflection.DelegateForConstructor <Row>(rowType);

                info.ListHandlerFactory = FastReflection.DelegateForConstructor <IListRequestProcessor>(
                    typeof(ListRequestHandler <>).MakeGenericType(rowType));

                info.SaveHandlerFactory = FastReflection.DelegateForConstructor <ISaveRequestProcessor>(
                    typeof(SaveRequestHandler <>).MakeGenericType(rowType));

                info.SaveRequestFactory = FastReflection.DelegateForConstructor <ISaveRequest>(
                    typeof(SaveRequest <>).MakeGenericType(rowType));

                info.DeleteHandlerFactory = FastReflection.DelegateForConstructor <IDeleteRequestProcessor>(
                    typeof(DeleteRequestHandler <>).MakeGenericType(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 = (Field)(((IIdRow)row).IdField);
                }
                else
                {
                    info.ThisKeyField = row.FindFieldByPropertyName(attr.ThisKey) ?? row.FindField(attr.ThisKey);
                    if (ReferenceEquals(info.ThisKeyField, 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 (ReferenceEquals(info.OtherKeyField, null) && ext is IIdRow)
                    {
                        info.OtherKeyField = (Field)(((IIdRow)row).IdField);
                    }

                    if (ReferenceEquals(info.OtherKeyField, 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 (ReferenceEquals(info.OtherKeyField, 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 (ReferenceEquals(info.FilterField, 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 = ext.FindFieldByPropertyName(attr.PresenceField) ?? ext.FindField(attr.PresenceField);
                    if (ReferenceEquals(info.PresenceField, 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);

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

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

                    return(aliasPrefix + x);
                };

                Func <string, string> mapExpression = 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 (ReferenceEquals(null, match))
                    {
                        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);
        }
コード例 #5
0
        public bool ActivateFor(Row row)
        {
            if (ReferenceEquals(null, Target))
                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<Row>)!",
                    Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }

            var rowType = rowListType.GetGenericArguments()[0];
            if (rowType.IsAbstract ||
                !typeof(Row).IsAssignableFrom(rowType))
            {
                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<Row>)!",
                        Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }

            rowListFactory = FastReflection.DelegateForConstructor<IList>(rowListType);
            rowFactory = FastReflection.DelegateForConstructor<Row>(rowType);

            listHandlerFactory = FastReflection.DelegateForConstructor<IListRequestProcessor>(
                typeof(ListRequestHandler<>).MakeGenericType(rowType));

            saveHandlerFactory = FastReflection.DelegateForConstructor<ISaveRequestProcessor>(
                typeof(SaveRequestHandler<>).MakeGenericType(rowType));

            saveRequestFactory = FastReflection.DelegateForConstructor<ISaveRequest>(
                typeof(SaveRequest<>).MakeGenericType(rowType));

            deleteHandlerFactory = FastReflection.DelegateForConstructor<IDeleteRequestProcessor>(
                typeof(DeleteRequestHandler<>).MakeGenericType(rowType));

            if (attr.MasterKeyField != null)
            {
                // Use field from AltIdField
                masterKeyField = row.FindFieldByPropertyName(attr.MasterKeyField) ??
                    row.FindField(attr.MasterKeyField);
                if (ReferenceEquals(masterKeyField, 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 = (Field)((row as IIdRow).IdField);
            }

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

            if (ReferenceEquals(foreignKeyField, 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));

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

            if (!string.IsNullOrEmpty(attr.FilterField))
            {
                this.filterField = detailRow.FindFieldByPropertyName(attr.FilterField) ?? detailRow.FindField(attr.FilterField);
                if (ReferenceEquals(null, this.filterField))
                    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));

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

            queryCriteria = queryCriteria & ServiceQueryHelper.GetNotDeletedCriteria(detailRow);

            this.includeColumns = new HashSet<string>();

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

            return true;
        }
コード例 #6
0
        public bool ActivateFor(Row row)
        {
            if (ReferenceEquals(null, Target))
            {
                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<Row>)!",
                                                          Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }

            var rowType = rowListType.GetGenericArguments()[0];

            if (rowType.IsAbstract ||
                !typeof(Row).IsAssignableFrom(rowType))
            {
                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<Row>)!",
                                                Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }

            rowListFactory = FastReflection.DelegateForConstructor <IList>(rowListType);
            rowFactory     = FastReflection.DelegateForConstructor <Row>(rowType);

            listHandlerFactory = FastReflection.DelegateForConstructor <IListRequestProcessor>(
                typeof(ListRequestHandler <>).MakeGenericType(rowType));

            saveHandlerFactory = FastReflection.DelegateForConstructor <ISaveRequestProcessor>(
                typeof(SaveRequestHandler <>).MakeGenericType(rowType));

            saveRequestFactory = FastReflection.DelegateForConstructor <ISaveRequest>(
                typeof(SaveRequest <>).MakeGenericType(rowType));

            deleteHandlerFactory = FastReflection.DelegateForConstructor <IDeleteRequestProcessor>(
                typeof(DeleteRequestHandler <>).MakeGenericType(rowType));

            var detailRow = rowFactory();

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

            if (ReferenceEquals(foreignKeyField, 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));
            }

            return(true);
        }
コード例 #7
0
        public bool ActivateFor(Row row)
        {
            if (ReferenceEquals(null, Target))
            {
                return(false);
            }

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

            if (!(row is IIdRow))
            {
                throw new ArgumentException(String.Format("Field '{0}' in row type '{1}' has a LinkingSetRelationBehavior " +
                                                          "but it doesn't implement IIdRow!",
                                                          Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }


            var listType = Target.ValueType;

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

            rowType = attr.RowType;
            if (rowType.IsAbstract ||
                !typeof(Row).IsAssignableFrom(rowType))
            {
                throw new ArgumentException(String.Format(
                                                "Field '{0}' in row type '{1}' has a LinkingSetRelationBehavior " +
                                                "but specified row type is not valid row class!",
                                                Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }

            if (!typeof(IIdRow).IsAssignableFrom(rowType))
            {
                throw new ArgumentException(String.Format(
                                                "Field '{0}' in row type '{1}' has a LinkingSetRelationBehavior " +
                                                "but specified row type doesn't implement IIdRow!",
                                                Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }

            listFactory = FastReflection.DelegateForConstructor <IList>(listType);
            rowFactory  = FastReflection.DelegateForConstructor <Row>(rowType);

            var detailRow = rowFactory();

            thisKeyField = detailRow.FindFieldByPropertyName(attr.ThisKey) ??
                           detailRow.FindField(attr.ThisKey);

            if (ReferenceEquals(thisKeyField, null))
            {
                throw new ArgumentException(String.Format("Field '{0}' doesn't exist in row of type '{1}'." +
                                                          "This field is specified for a linking set relation in field '{2}' of row type '{3}'.",
                                                          attr.ThisKey, detailRow.GetType().FullName,
                                                          Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }

            this.thisKeyCriteria = new Criteria(thisKeyField.PropertyName ?? thisKeyField.Name);

            itemKeyField = detailRow.FindFieldByPropertyName(attr.ItemKey) ??
                           detailRow.FindField(attr.ItemKey);

            if (ReferenceEquals(itemKeyField, null))
            {
                throw new ArgumentException(String.Format("Field '{0}' doesn't exist in row of type '{1}'." +
                                                          "This field is specified for a linking set relation in field '{2}' of row type '{3}'.",
                                                          attr.ItemKey, detailRow.GetType().FullName,
                                                          Target.PropertyName ?? Target.Name, row.GetType().FullName));
            }

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

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

            queryCriteria = queryCriteria & ServiceQueryHelper.GetNotDeletedCriteria(detailRow);

            return(true);
        }
コード例 #8
0
        public bool ActivateFor(Row row)
        {
            attr = row.GetType().GetCustomAttribute <LocalizationRowAttribute>();
            if (attr == null)
            {
                return(false);
            }

            localRowType = attr.LocalizationRow;
            if (!typeof(ILocalizationRow).IsAssignableFrom(localRowType))
            {
                throw new ArgumentException(String.Format(
                                                "Row type '{0}' has a LocalizationRowAttribute, " +
                                                "but its localization row type ('{1}') doesn't implement ILocalizationRow interface!",
                                                row.GetType().FullName, localRowType.FullName));
            }

            if (!typeof(IIdRow).IsAssignableFrom(localRowType))
            {
                throw new ArgumentException(String.Format(
                                                "Row type '{0}' has a LocalizationRowAttribute, " +
                                                "but its localization row type ('{1}') doesn't implement IIdRow interface!",
                                                row.GetType().FullName, localRowType.FullName));
            }

            if (!(row is IIdRow))
            {
                throw new ArgumentException(String.Format(
                                                "Row type '{0}' has a LocalizationRowAttribute, " +
                                                "but row type itself doesn't implement IIdRow interface!",
                                                row.GetType().FullName));
            }

            rowFactory      = FastReflection.DelegateForConstructor <Row>(row.GetType());
            localRowFactory = FastReflection.DelegateForConstructor <Row>(localRowType);

            var localRow = localRowFactory();

            localRowInstance = localRow;

            rowPrefixLength      = PrefixHelper.DeterminePrefixLength(row.EnumerateTableFields(), x => x.Name);
            localRowPrefixLength = PrefixHelper.DeterminePrefixLength(localRow.EnumerateTableFields(), x => x.Name);
            localRowIdField      = (Field)(((IIdRow)localRow).IdField);
            cultureIdField       = ((ILocalizationRow)localRow).CultureIdField;

            var foreignKeyFieldName = attr.MappedIdField ?? ((Field)((IIdRow)row).IdField).PropertyName;

            foreignKeyField = localRow.FindFieldByPropertyName(foreignKeyFieldName) ??
                              localRow.FindField(foreignKeyFieldName);

            if (ReferenceEquals(null, foreignKeyField))
            {
                throw new ArgumentException(String.Format(
                                                "Row type '{0}' has a LocalizationRowAttribute, " +
                                                "but its localization row type ('{1}') doesn't have a field with name '{2}'!",
                                                row.GetType().FullName, localRowType.FullName, foreignKeyFieldName));
            }

            dictionaryFactory = FastReflection.DelegateForConstructor <IDictionary>(
                typeof(Dictionary <,>).MakeGenericType(typeof(string), row.GetType()));

            this.foreignKeyCriteria = new Criteria(foreignKeyField.PropertyName ?? foreignKeyField.Name);
            return(true);
        }
コード例 #9
0
        private static Factories GetFactoriesFor(Type rowType)
        {
            Factories factories;

            if (factoriesByType.TryGetValue(rowType, out factories))
            {
                return(factories);
            }

            factories = new Factories();

            var registry = Dependency.TryResolve <IDefaultHandlerRegistry>() ??
                           DefaultHandlerRegistry.Instance;

            foreach (var handlerType in registry.GetTypes())
            {
                Type[] genericArguments;
                if (typeof(IListRequestProcessor).IsAssignableFrom(handlerType))
                {
                    genericArguments = IsSubclassOfRawGeneric(typeof(ListRequestHandler <, ,>), handlerType);
                    if (genericArguments == null || genericArguments[0] != rowType)
                    {
                        continue;
                    }

                    if (factories.listRequestFactory != null)
                    {
                        throw new InvalidProgramException("There are multiple ListRequestHandler types " +
                                                          "with [DefaultHandler] attribute for row type " + rowType.FullName);
                    }

                    factories.listHandlerFactory = FastReflection
                                                   .DelegateForConstructor <IListRequestProcessor>(handlerType);
                    factories.listRequestFactory = FastReflection
                                                   .DelegateForConstructor <ListRequest>(genericArguments[1]);
                    continue;
                }

                if (typeof(IRetrieveRequestProcessor).IsAssignableFrom(handlerType))
                {
                    genericArguments = IsSubclassOfRawGeneric(typeof(RetrieveRequestHandler <, ,>), handlerType);
                    if (genericArguments == null || genericArguments[0] != rowType)
                    {
                        continue;
                    }

                    if (factories.retrieveRequestFactory != null)
                    {
                        throw new InvalidProgramException("There are multiple RetrieveRequestHandler types " +
                                                          "with [DefaultHandler] attribute for row type " + rowType.FullName);
                    }

                    factories.retrieveHandlerFactory = FastReflection
                                                       .DelegateForConstructor <IRetrieveRequestProcessor>(handlerType);
                    factories.retrieveRequestFactory = FastReflection
                                                       .DelegateForConstructor <RetrieveRequest>(genericArguments[1]);
                    continue;
                }

                if (typeof(ISaveRequestProcessor).IsAssignableFrom(handlerType))
                {
                    genericArguments = IsSubclassOfRawGeneric(typeof(SaveRequestHandler <, ,>), handlerType);
                    if (genericArguments == null || genericArguments[0] != rowType)
                    {
                        continue;
                    }

                    if (factories.saveRequestFactory != null)
                    {
                        throw new InvalidProgramException("There are multiple SaveRequestHandler types " +
                                                          "with [DefaultHandler] attribute for row type " + rowType.FullName);
                    }

                    factories.saveHandlerFactory = FastReflection
                                                   .DelegateForConstructor <ISaveRequestProcessor>(handlerType);
                    factories.saveRequestFactory = FastReflection
                                                   .DelegateForConstructor <ISaveRequest>(genericArguments[1]);
                    continue;
                }

                if (typeof(IDeleteRequestProcessor).IsAssignableFrom(handlerType))
                {
                    genericArguments = IsSubclassOfRawGeneric(typeof(DeleteRequestHandler <, ,>), handlerType);
                    if (genericArguments == null || genericArguments[0] != rowType)
                    {
                        continue;
                    }

                    if (factories.deleteRequestFactory != null)
                    {
                        throw new InvalidProgramException("There are multiple DeleteRequestHandler types " +
                                                          "with [DefaultHandler] attribute for row type " + rowType.FullName);
                    }

                    factories.deleteHandlerFactory = FastReflection
                                                     .DelegateForConstructor <IDeleteRequestProcessor>(handlerType);
                    factories.deleteRequestFactory = FastReflection
                                                     .DelegateForConstructor <DeleteRequest>(genericArguments[1]);
                    continue;
                }
            }

            factories.listHandlerFactory = factories.listHandlerFactory ?? FastReflection
                                           .DelegateForConstructor <IListRequestProcessor>(typeof(ListRequestHandler <>).MakeGenericType(rowType));
            factories.listRequestFactory = factories.listRequestFactory ?? new Func <ListRequest>(() => new ListRequest());

            factories.retrieveHandlerFactory = factories.retrieveHandlerFactory ?? FastReflection
                                               .DelegateForConstructor <IRetrieveRequestProcessor>(typeof(RetrieveRequestHandler <>).MakeGenericType(rowType));
            factories.retrieveRequestFactory = factories.retrieveRequestFactory ?? new Func <RetrieveRequest>(() => new RetrieveRequest());

            factories.saveHandlerFactory = factories.saveHandlerFactory ?? FastReflection
                                           .DelegateForConstructor <ISaveRequestProcessor>(typeof(SaveRequestHandler <>).MakeGenericType(rowType));
            factories.saveRequestFactory = factories.saveRequestFactory ?? FastReflection
                                           .DelegateForConstructor <ISaveRequest>(typeof(SaveRequest <>).MakeGenericType(rowType));

            factories.deleteHandlerFactory = factories.deleteHandlerFactory ?? FastReflection
                                             .DelegateForConstructor <IDeleteRequestProcessor>(typeof(DeleteRequestHandler <>).MakeGenericType(rowType));
            factories.deleteRequestFactory = factories.deleteRequestFactory ?? new Func <DeleteRequest>(() => new DeleteRequest());

            var newDict = new Dictionary <Type, Factories>(factoriesByType);

            newDict[rowType] = factories;
            factoriesByType  = newDict;

            return(factories);
        }
コード例 #10
0
        public void OnReturn(IListRequestHandler handler)
        {
            if (handler.Response.Entities.Count == 0)
            {
                return;
            }

            // Lets Get the main row and translate it
            var culture      = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
            var userLanguage = new LanguageRow();

            using (var connectionLanguage = SqlConnections.NewFor <LanguageRow>())
            {
                userLanguage = connectionLanguage.TryFirst <LanguageRow>(new Criteria(LanguageRow.Fields.LanguageId).Like($"%{culture}%"));
                if (userLanguage == null)
                {
                    return;
                }
            }

            var listHandler = DefaultHandlerFactory.ListHandlerFor(localRowType);
            var listRequest = DefaultHandlerFactory.ListRequestFor(localRowType);

            listRequest.ColumnSelection = ColumnSelection.List;

            List <IOMPLocalizationRow> localList = handler.Response.Entities.Cast <IOMPLocalizationRow>().ToList();

            listRequest.Criteria = (foreignKeyCriteria.In(localList.Select(s => s.IdField[s as Row])) && languageIdCriteria == userLanguage.Id.Value);

            var translationsResponse = listHandler.Process(handler.Connection, listRequest);

            if (translationsResponse.Entities.Count > 0)
            {
                var responseLang = translationsResponse.Entities.Cast <IOMPLocalizationLangRow>();
                // Validate They have IName row
                foreach (IOMPLocalizationRow responseEntity in handler.Response.Entities)
                {
                    var entityLang = responseLang
                                     .FirstOrDefault(s =>
                                                     (Int32)foreignKeyField.AsObject(s as Row) ==
                                                     responseEntity.IdField[responseEntity as Row]);

                    if (entityLang != null && !string.IsNullOrEmpty(entityLang?.NameField[entityLang as Row]))
                    {
                        responseEntity.NameField[responseEntity as Row] = entityLang?.NameField[entityLang as Row];
                    }
                }
            }

            // How about the joined tables, lets translate them too
            var localizationRowFields = handler.Row.GetFields();

            foreach (var localizationRowField in localizationRowFields.Where(f =>
                                                                             !String.IsNullOrEmpty(f.ForeignTable)))
            {
                var foreignKeyAttr   = localizationRowField.GetAttribute <ForeignKeyAttribute>();
                var textualFieldAttr = localizationRowField.GetAttribute <TextualFieldAttribute>();
                if (foreignKeyAttr.RowType == null || textualFieldAttr == null || string.IsNullOrEmpty(textualFieldAttr.Value))
                {
                    continue;
                }

                var foreignRowFactory = FastReflection.DelegateForConstructor <Row>(foreignKeyAttr.RowType);
                var foreignRow        = foreignRowFactory();
                var foreignAttr       = foreignRow.GetType().GetCustomAttribute <LocalizationRowAttribute>();

                if (foreignAttr == null)
                {
                    continue;
                }

                var foreignLangType = foreignAttr.LocalizationRow;

                var foreignLangRowFactory = FastReflection.DelegateForConstructor <Row>(foreignLangType);
                var foreignLangRow        = foreignLangRowFactory();

                var externalForeignKeyFieldName =
                    foreignAttr.MappedIdField ?? ((Field)((IIdRow)foreignRow).IdField).PropertyName;
                var externalForeignKeyField = foreignLangRow.FindFieldByPropertyName(externalForeignKeyFieldName) ??
                                              foreignLangRow.FindField(externalForeignKeyFieldName);

                var listForeignHandler = DefaultHandlerFactory.ListHandlerFor(foreignLangType);
                var listForeignRequest = DefaultHandlerFactory.ListRequestFor(foreignLangType);
                listRequest.ColumnSelection = ColumnSelection.List;

                var externalForeignCriteria =
                    new Criteria(externalForeignKeyField.PropertyName ?? externalForeignKeyField.Name);

                var idList = new List <Int32>();
                foreach (IOMPLocalizationRow responseEntity in handler.Response.Entities)
                {
                    var idToAdd = (Int32?)localizationRowField.AsObject(responseEntity as Row);
                    if (idToAdd.HasValue)
                    {
                        idList.Add(idToAdd.Value);
                    }
                }
                if (!idList.Any())
                {
                    continue;
                }
                listForeignRequest.Criteria =
                    externalForeignCriteria.In(idList.Distinct()) &&
                    languageIdCriteria == userLanguage.Id.Value;

                var translationsForeignResponse =
                    listForeignHandler.Process(handler.Connection, listForeignRequest);

                if (translationsForeignResponse.TotalCount > 0)
                {
                    foreach (IOMPLocalizationRow responseEntity in handler.Response.Entities)
                    {
                        var localId = (Int32?)localizationRowField.AsObject(responseEntity as Row);

                        var entityLang = translationsForeignResponse.Entities.Cast <IOMPLocalizationLangRow>()
                                         .FirstOrDefault(s =>
                                                         (Int32?)externalForeignKeyField.AsObject(s as Row) == localId);

                        if (entityLang != null)
                        {
                            var foreignEntityName = entityLang?.NameField[entityLang as Row];

                            var textualFieldExternal = (responseEntity as Row).FindField(textualFieldAttr.Value);
                            if (!string.IsNullOrEmpty(foreignEntityName))
                            {
                                textualFieldExternal.AsObject(responseEntity as Row, foreignEntityName);
                            }
                        }
                    }
                }
            }
        }