public void OnReturn(IRetrieveRequestHandler handler)
        {
            if (ReferenceEquals(null, Target) ||
                !handler.AllowSelectField(Target) ||
                !handler.ShouldSelectField(Target))
            {
                return;
            }

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

            listRequest.ColumnSelection = this.attr.ColumnSelection;
            listRequest.IncludeColumns  = this.includeColumns;
            listRequest.Criteria        = foreignKeyCriteria == new ValueCriteria(masterKeyField.AsObject(handler.Row)) & filterCriteria;

            IListResponse response = listHandler.Process(handler.Connection, listRequest);

            var list = rowListFactory();

            foreach (var item in response.Entities)
            {
                list.Add(item);
            }

            Target.AsObject(handler.Row, list);
        }
        public void OnReturn(IRetrieveRequestHandler handler)
        {
            if (ReferenceEquals(null, Target) ||
                !handler.AllowSelectField(Target) ||
                !handler.ShouldSelectField(Target))
            {
                return;
            }

            var idField = (Field)((handler.Row as IIdRow).IdField);

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

            listRequest.ColumnSelection = ColumnSelection.KeyOnly;
            listRequest.IncludeColumns  = new HashSet <string>
            {
                itemKeyField.PropertyName ?? itemKeyField.Name
            };
            listRequest.Criteria = thisKeyCriteria == new ValueCriteria(idField.AsObject(handler.Row)) & filterCriteria;

            IListResponse response = listHandler.Process(handler.Connection, listRequest);

            var list = listFactory();

            foreach (Row item in response.Entities)
            {
                list.Add(itemKeyField.AsObject(item));
            }

            Target.AsObject(handler.Row, list);
        }
        public override void OnAfterSave(ISaveRequestHandler handler)
        {
            var newList = Target.AsObject(handler.Row) as IList;

            if (newList == null)
            {
                return;
            }

            var masterId = masterKeyField.AsObject(handler.Row);

            if (handler.IsCreate)
            {
                foreach (Row entity in newList)
                {
                    SaveDetail(handler.UnitOfWork, entity, masterId, null);
                }

                return;
            }

            var oldList = new List <Row>();

            if (!attr.CheckChangesOnUpdate)
            {
                var row        = rowFactory();
                var rowIdField = (row as IIdRow).IdField;

                // if we're not gonna compare old rows with new ones
                // no need to call list request handler

                new SqlQuery()
                .Dialect(handler.Connection.GetDialect())
                .From(row)
                .Select((Field)rowIdField)
                .Where(
                    foreignKeyField == new ValueCriteria(masterKeyField.AsSqlValue(handler.Row)) &
                    queryCriteria)
                .ForEach(handler.Connection, () =>
                {
                    oldList.Add(row.Clone());
                });
            }
            else
            {
                var listHandler = DefaultHandlerFactory.ListHandlerFor(rowType);
                var listRequest = DefaultHandlerFactory.ListRequestFor(rowType);
                listRequest.ColumnSelection = ColumnSelection.List;
                listRequest.Criteria        = foreignKeyCriteria == new ValueCriteria(masterKeyField.AsObject(handler.Row)) & filterCriteria;

                var entities = listHandler.Process(handler.Connection, listRequest).Entities;
                foreach (Row entity in entities)
                {
                    oldList.Add(entity);
                }
            }

            DetailListSave(handler.UnitOfWork, masterId, oldList, newList);
        }
        public void OnReturn(IListRequestHandler handler)
        {
            if (ReferenceEquals(null, Target) ||
                !handler.AllowSelectField(Target) ||
                !handler.ShouldSelectField(Target) ||
                handler.Response.Entities.IsEmptyOrNull())
            {
                return;
            }

            var idField = (Field)((handler.Row as IIdRow).IdField);

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

            listRequest.ColumnSelection = ColumnSelection.KeyOnly;
            listRequest.IncludeColumns  = new HashSet <string>
            {
                itemKeyField.PropertyName ?? itemKeyField.Name,
                thisKeyField.PropertyName ?? thisKeyField.Name
            };

            var enumerator = handler.Response.Entities.Cast <Row>();

            while (true)
            {
                var part = enumerator.Take(1000);
                if (!part.Any())
                {
                    break;
                }

                enumerator = enumerator.Skip(1000);

                listRequest.Criteria = thisKeyCriteria.In(
                    part.Select(x => idField.AsObject(x))) & filterCriteria;

                IListResponse response = listHandler.Process(
                    handler.Connection, listRequest);

                var lookup = response.Entities.Cast <Row>()
                             .ToLookup(x => thisKeyField.AsObject(x).ToString());

                foreach (var row in part)
                {
                    var list     = listFactory();
                    var matching = lookup[idField.AsObject(row).ToString()];
                    foreach (var x in matching)
                    {
                        list.Add(itemKeyField.AsObject(x));
                    }

                    Target.AsObject(row, list);
                }
            }
        }
        public void OnReturn(IListRequestHandler handler)
        {
            if (ReferenceEquals(null, Target) ||
                !handler.AllowSelectField(Target) ||
                !handler.ShouldSelectField(Target) ||
                handler.Response.Entities.IsEmptyOrNull())
            {
                return;
            }

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

            listRequest.ColumnSelection = this.attr.ColumnSelection;
            listRequest.IncludeColumns  = this.includeColumns;

            var enumerator = handler.Response.Entities.Cast <Row>();

            while (true)
            {
                var part = enumerator.Take(1000);
                if (!part.Any())
                {
                    break;
                }

                enumerator = enumerator.Skip(1000);

                listRequest.Criteria = foreignKeyCriteria.In(
                    part.Select(x => masterKeyField.AsObject(x))) & filterCriteria;

                IListResponse response = listHandler.Process(
                    handler.Connection, listRequest);

                var lookup = response.Entities.Cast <Row>()
                             .ToLookup(x => foreignKeyField.AsObject(x).ToString());

                foreach (var row in part)
                {
                    var list     = rowListFactory();
                    var matching = lookup[masterKeyField.AsObject(row).ToString()];
                    foreach (var x in matching)
                    {
                        list.Add(x);
                    }

                    Target.AsObject(row, list);
                }
            }
        }
Esempio n. 6
0
        private object GetExistingID(IDbConnection connection, RelationInfo info,
                                     object thisKey)
        {
            var criteria = new Criteria(info.OtherKeyField.PropertyName ?? info.OtherKeyField.Name) ==
                           new ValueCriteria(thisKey);

            if (!ReferenceEquals(null, info.FilterField))
            {
                var flt = new Criteria(info.FilterField.PropertyName ?? info.FilterField.Name);
                if (info.FilterValue == null)
                {
                    criteria &= flt.IsNull();
                }
                else
                {
                    criteria &= flt == new ValueCriteria(info.FilterValue);
                }
            }

            var listHandler = DefaultHandlerFactory.ListHandlerFor(info.Attr.RowType);
            var listRequest = DefaultHandlerFactory.ListRequestFor(info.Attr.RowType);

            listRequest.ColumnSelection = ColumnSelection.KeyOnly;
            listRequest.Criteria        = criteria;

            var existing = listHandler.Process(connection, listRequest).Entities;

            if (existing.Count > 1)
            {
                throw new Exception(String.Format("Found multiple extension rows for UpdatableExtension '{0}'",
                                                  info.Attr.Alias));
            }

            if (existing.Count == 0)
            {
                return(null);
            }

            return(((Field)((IIdRow)existing[0]).IdField).AsObject((Row)existing[0]));
        }
        public void OnReturn(IRetrieveRequestHandler handler)
        {
            if (handler.Request == null ||
                handler.Request.IncludeColumns == null ||
                !handler.Request.IncludeColumns.Contains("Localizations"))
            {
                return;
            }

            var localIdField = (Field)((handler.Row as IIdRow).IdField);

            var listHandler = DefaultHandlerFactory.ListHandlerFor(localRowType);

            var listRequest = DefaultHandlerFactory.ListRequestFor(localRowType);

            listRequest.ColumnSelection = ColumnSelection.List;
            listRequest.Criteria        = foreignKeyCriteria == new ValueCriteria(localIdField.AsObject(handler.Row));

            IListResponse response = listHandler.Process(handler.Connection, listRequest);

            var row        = rowFactory();
            var rowIdField = (Field)(((IIdRow)row).IdField);
            var fields     = row.GetFields();
            var matches    = new Field[fields.Count];

            for (var i = 0; i < fields.Count; i++)
            {
                var field = fields[i];
                if (ReferenceEquals(field, rowIdField))
                {
                    continue;
                }

                matches[i] = GetLocalizationMatch(field);
            }

            var dictionary = dictionaryFactory();

            foreach (Row localRow in response.Entities)
            {
                row = rowFactory();
                row.TrackAssignments = true;
                rowIdField.AsObject(row, rowIdField.AsObject(row));

                for (var i = 0; i < fields.Count; i++)
                {
                    var match = matches[i];
                    if (!ReferenceEquals(null, match))
                    {
                        var field = fields[i];
                        var value = match.AsObject(localRow);
                        field.AsObject(row, value);
                    }
                }

                var culture = cultureIdField.AsObject(localRow);
                dictionary[culture == null ? "" : culture.ToString()] = row;
            }

            handler.Response.Localizations = dictionary;
        }