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 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);
        }
Пример #3
0
        public override void OnBeforeDelete(IDeleteRequestHandler handler)
        {
            foreach (var info in infoList)
            {
                if (!info.Attr.CascadeDelete)
                {
                    continue;
                }

                var thisKey = info.ThisKeyField.AsObject(handler.Row);
                if (ReferenceEquals(null, thisKey))
                {
                    continue;
                }

                var oldID = GetExistingID(handler.Connection, info, thisKey);
                if (oldID == null)
                {
                    continue;
                }

                var deleteHandler = DefaultHandlerFactory.DeleteHandlerFor(info.Attr.RowType);
                var deleteRequest = DefaultHandlerFactory.DeleteRequestFor(info.Attr.RowType);
                deleteRequest.EntityId = oldID;
                deleteHandler.Process(handler.UnitOfWork, deleteRequest);
            }
        }
        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);
        }
        private void DeleteDetail(IUnitOfWork uow, object detailId)
        {
            var deleteHandler = DefaultHandlerFactory.DeleteHandlerFor(rowType);
            var deleteRequest = DefaultHandlerFactory.DeleteRequestFor(rowType);

            deleteRequest.EntityId = detailId;
            deleteHandler.Process(uow, deleteRequest);
        }
        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);
                }
            }
        }
Пример #7
0
        public override void OnAfterSave(ISaveRequestHandler handler)
        {
            foreach (var info in infoList)
            {
                object mappingsObj;
                if (!handler.StateBag.TryGetValue("UpdatableExtensionBehavior_Assignments_" + info.Attr.Alias, out mappingsObj))
                {
                    continue;
                }

                var mappings = (IEnumerable <Tuple <Field, Field> >)mappingsObj;
                if (mappings == null || !mappings.Any())
                {
                    continue;
                }

                var thisKey = info.ThisKeyField.AsObject(handler.Row);
                if (ReferenceEquals(null, thisKey))
                {
                    continue;
                }

                object oldID = GetExistingID(handler.Connection, info, thisKey);
                if (oldID == null && !CheckPresenceValue(info, handler.Row))
                {
                    continue;
                }

                var extension = info.RowFactory();

                if (oldID != null)
                {
                    ((Field)((IIdRow)extension).IdField).AsObject(extension, oldID);
                }

                info.OtherKeyField.AsObject(extension, thisKey);
                if (!ReferenceEquals(null, info.FilterField))
                {
                    info.FilterField.AsObject(extension, info.FilterValue);
                }

                var request = DefaultHandlerFactory.SaveRequestFor(info.Attr.RowType);
                request.Entity   = extension;
                request.EntityId = oldID;

                foreach (var mapping in mappings)
                {
                    mapping.Item2.AsObject(extension, mapping.Item1.AsObject(handler.Row));
                }

                DefaultHandlerFactory.SaveHandlerFor(info.Attr.RowType)
                .Process(handler.UnitOfWork, request, oldID == null ? SaveRequestType.Create : SaveRequestType.Update);
            }
        }
Пример #8
0
        private void SaveLocalRow(IUnitOfWork uow, Row localRow, object masterId, object localRowId)
        {
            localRow = localRow.Clone();

            foreignKeyField.AsObject(localRow, masterId);
            ((Field)((IIdRow)localRow).IdField).AsObject(localRow, localRowId);

            var saveHandler = DefaultHandlerFactory.SaveHandlerFor(localRowType);
            var saveRequest = DefaultHandlerFactory.SaveRequestFor(localRowType);

            saveRequest.Entity = localRow;
            saveHandler.Process(uow, saveRequest, localRowId == null ? SaveRequestType.Create : SaveRequestType.Update);
        }
        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);
                }
            }
        }
        private void InsertDetail(IUnitOfWork uow, object masterId, object itemKey)
        {
            var detail = rowFactory();

            thisKeyField.AsObject(detail, masterId);
            itemKeyField.AsObject(detail, itemKeyField.ConvertValue(itemKey, CultureInfo.InvariantCulture));
            if (!ReferenceEquals(null, filterField))
            {
                filterField.AsObject(detail, filterValue);
            }

            var saveHandler = DefaultHandlerFactory.SaveHandlerFor(rowType);
            var saveRequest = DefaultHandlerFactory.SaveRequestFor(rowType);

            saveRequest.Entity = detail;
            saveHandler.Process(uow, saveRequest, SaveRequestType.Create);
        }
        private void SaveDetail(IUnitOfWork uow, Row detail, object masterId, object detailId)
        {
            detail = detail.Clone();

            foreignKeyField.AsObject(detail, masterId);
            if (!ReferenceEquals(null, filterField))
            {
                filterField.AsObject(detail, filterValue);
            }

            ((Field)((IIdRow)detail).IdField).AsObject(detail, detailId);

            var saveHandler = DefaultHandlerFactory.SaveHandlerFor(rowType);
            var saveRequest = DefaultHandlerFactory.SaveRequestFor(rowType);

            saveRequest.Entity = detail;
            saveHandler.Process(uow, saveRequest, detailId == null ? SaveRequestType.Create : SaveRequestType.Update);
        }
Пример #12
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]));
        }
Пример #13
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;
        }