public void Add(XtraBinding XtraBinding, object DataSourceItem, XtraBindingStoredItemState State)
        {
            object underlyingItem = XtraBinding.ItemAdapter.GetUnderlyingDataSourceItem(DataSourceItem);

            XtraBindingUpdateSetEntity enty = _entyties.FirstOrDefault(p => p.xtraBinding == XtraBinding);

            if (enty == null)
            {
                enty = new XtraBindingUpdateSetEntity(XtraBinding);

                _entyties.Add(enty);
            }

            XtraBindingStoredItem item = GetStoredItem(enty, underlyingItem);

            if (item == null)
            {
                item = new XtraBindingStoredItem();

                enty.StoredItems.Add(item);
            }

            item.Update(GetFieldsValues(XtraBinding, underlyingItem), State);

            item.UpdateDataSourceItem(DataSourceItem);
        }
        private static XtraBinding CreateXtraBindingSource(XOTableContext TableContext, IControllerProvider ControllerProvider, XtraBindingChildProperties ChildProperties)
        {
            object dataSource = ControllerProvider.GetDataSource(TableContext.XOTable);

            DataAvail.Data.DataAdapter.IDataAdapter dataAdapter = ControllerProvider.GetDataAdapter(TableContext.XOTable);

            DataAvail.XtraBindings.XtraBindingOperation oper = new DataAvail.XtraBindings.XtraBindingOperation(dataSource, dataAdapter, ControllerProvider.GetObjectCalculator(TableContext.XOTable));

            XtraBinding xtraBinding = null;

            if (TableContext.ShownChildrenRelations.Count() > 0)
            {
                xtraBinding = new DataAvail.XtraBindings.XtraBindingContainer(dataSource, oper);

                foreach (XORelation rel in TableContext.ShownChildrenRelations)
                {
                    DataAvail.XtraBindings.XtraBinding childBinding = CreateXtraBindingSource(rel.ChildTable.CreateTableChildContext(TableContext.Fields.First(p=>p.Name == rel.ParentField.Name)), ControllerProvider, new XtraBindingChildProperties(
                        xtraBinding, rel.ParentTable.Name, rel.ParentField.Name, rel.ChildTable.Name, rel.ChildField.Name));

                    ((XtraBindingContainer)xtraBinding).Children.Add((XtraBindingChild)childBinding);
                }
            }
            else if (ChildProperties != null)
            {
                xtraBinding = new DataAvail.XtraBindings.XtraBindingChild(dataSource, oper, ChildProperties);
            }
            else
            {
                xtraBinding = new XtraBinding(dataSource, oper);
            }

            return xtraBinding;
        }
        internal ItemSelectorController(XtraBinding XtraBinding, IControllerDynamicContext ControllerDynamicContext) :
            base(XtraBinding, ControllerDynamicContext)
        {
            XORelation rel = this.TableContext.GetChildRelation(this.ChildFieldContext.XOField);

            _valueFieldName = rel.ParentField.Name;
        }
        public XtraBindingUpdateSetRejectResult(XtraBinding XtraBinding, string PkFieldName, IEnumerable<XtraBindingStoredItem> Items)
        {
            xtraBinding = XtraBinding;

            pkFieldName = PkFieldName;

            items = Items;
        }
        public XtraBindingChildProperties(XtraBinding ParentBinding, string ParentObjectName, string ParentFieldName, string ChildObjectName, string FkFieldName)
        {
            parentBinding = ParentBinding;

            parentFieldName = ParentFieldName;

            fkFieldName = FkFieldName;

            childObjectName = ChildObjectName;

            parentObjectName = ParentObjectName;
        }
        public void Clear(XtraBinding XtraBinding, IEnumerable<object> PKs)
        {
            XtraBindingUpdateSetEntity enty = _entyties.FirstOrDefault(p => p.xtraBinding == XtraBinding);

            if (enty != null)
            { 
                string key = GetPkFieldName(XtraBinding);

                foreach (XtraBindingStoredItem item in enty.StoredItems.Where(p => PKs.Contains(p.FieldsValues[key])).ToArray())
                {
                    enty.StoredItems.Remove(item);
                }

                if (enty.StoredItems.Count == 0)
                {
                    _entyties.Remove(enty);
                }
            }
        }
        public IEnumerable<XtraBindingUpdateSetUpdateResult> Update(XtraBinding XtraBinding, IEnumerable<object> PKs)
        {
            XtraBinding.xtraBindingOperation.BeginTransaction();

            _updateSet.Clear();

            try
            {
                OnUpdate(XtraBinding, PKs, null);
            }
            catch (System.Exception e)
            {
                XtraBinding.xtraBindingOperation.RollbackTransaction();

                throw e;
            }

            XtraBinding.xtraBindingOperation.CommitTransaction();

            return _updateSet;
        }
        public Controller(XtraBinding XtraBinding, IControllerDynamicContext ControllerDynamicContext)
        {
            _xtraBinding = XtraBinding;

            _dynamicContext = ControllerDynamicContext;

            _dataSources = new ControllerDataSourcesCollection(this);

            _commands = new ControllerCommands(this);

            _itemSelector = new ItemSelector(this);

            _xtraBinding.Calculator.SetAppItemContext(TableContext);

            CreateUI();

            XtraBinding.xtraBindingOperation.EndFill += new XtraBindingOperationEndFillHandler(xtraBindingOperation_EndFill);

            XtraBinding.CalulatorCalculated += new EventHandler(XtraBinding_CalulatorCalculated);

            DynamicContext.TableContextChanged += new EventHandler(DynamicContext_TableContextChanged);
        }
        public IEnumerable<XtraBindingUpdateSetRejectResult> Reject(XtraBinding XtraBinding, IEnumerable<object> PKs)
        {
            _rejectSet.Clear();

            OnReject(XtraBinding, PKs);

            return _rejectSet;
        }
        /// <summary>
        /// Update values of the items which contain XtraBinding's data source by the corresonding values from the stored items.
        /// </summary>
        internal void RefreshItems(XtraBinding XtraBinding)
        { 
            string key = GetPkFieldName(XtraBinding);

            IEnumerable<XtraBindingStoredItem> storedItems = GetStoredItems(XtraBinding);

            if (XtraBinding is XtraBindingChild)
            {
                string fk = ((XtraBindingChild)XtraBinding).ChildProperties.fkFieldName;

                _updateSetDataConverter.GetUnderlyingObjects(XtraBinding.DataSource,
                    storedItems.Where(p => p.FieldsValues[fk].Equals(((XtraBindingChild)XtraBinding).ParentPKValue) 
                        && p.State == XtraBindingStoredItemState.Added)
                    , true);
            }

            var r = storedItems.Select(p => new { i = XtraBinding.GetDataSourceItemsByValue(key, p.FieldsValues[key]).SingleOrDefault(), s = p }).Where(p => p.i != null);

            foreach (var i in r.Where(p=>p.s.State == XtraBindingStoredItemState.Modifyed))
            {
                foreach (string fieldName in _updateSetDataConverter.GetFieldsNames(XtraBinding.DataSource))
                {
                    XtraBinding.SetDataSourceItemValue(i.i, fieldName, i.s.FieldsValues[fieldName]);
                }

                i.s.UpdateDataSourceItem(i.i);
            }

            foreach (var i in r.Where(p => p.s.State == XtraBindingStoredItemState.Removed))
            {
                XtraBinding.BindingSource.Remove(i.i);

                i.s.UpdateDataSourceItem(i.i);
            }

            foreach (var i in r.Where(p => p.s.State == XtraBindingStoredItemState.Added))
            {
                i.s.UpdateDataSourceItem(i.i);
            }
        }
        private void OnReject(XtraBinding XtraBinding, IEnumerable<object> PKs)
        {
            if (XtraBinding is XtraBindingContainer)
            {
                foreach (XtraBindingChild childBinding in ((XtraBindingContainer)XtraBinding).Children)
                {
                    IEnumerable<object> childrenPKs = PKs == null ? null : GetChildrenPKS(childBinding, PKs);

                    OnReject(childBinding, childrenPKs);
                }
            }

            XtraBindingUpdateSetEntity enty = _entyties.FirstOrDefault(p => p.xtraBinding == XtraBinding);

            if (enty != null)
            {
                string key = GetPkFieldName(XtraBinding);

                IEnumerable<XtraBindingStoredItem> items = PKs == null ? enty.StoredItems : enty.StoredItems.Where(p => PKs.Contains(p.FieldsValues[key]));

                _rejectSet.Add(new XtraBindingUpdateSetRejectResult(XtraBinding, key, items.ToArray()));

                foreach (XtraBindingStoredItem item in items.ToArray())
                {
                    enty.StoredItems.Remove(item);
                }

                if (enty.StoredItems.Count == 0)
                {
                    _entyties.Remove(enty);
                }
            }
        }
 internal XtraBindingUpdateSetEntity(XtraBinding XtraBinding)
 {
     xtraBinding = XtraBinding;
 }
        internal IEnumerable<object> GetStoredItemsPKS(XtraBinding XtraBinding, IEnumerable<XtraBindingStoredItem> StoredItems)
        {
            string key = GetPkFieldName(XtraBinding);

            return StoredItems.Select(p => p.FieldsValues[key]);
        }
        private void OnUpdate(XtraBinding XtraBinding, IEnumerable<object> PKs, IDictionary<object, object> ParentOldNewPKS)
        {
            XtraBindingUpdateSetEntity entry = _entyties.FirstOrDefault(p => p.xtraBinding == XtraBinding);

            IDictionary<object, object> oldNewPks = PKs == null ? null : PKs.ToDictionary(k => k, v => v);

            if (entry != null)
            {
                IEnumerable<object> pks = PKs ?? entry.StoredItems.Select(p => p.FieldsValues[GetPkFieldName(XtraBinding)]);

                IDictionary<XtraBindingStoredItem, object> items = GetUnderlyingObjects(entry, pks);

                if (ParentOldNewPKS != null)
                    SubstituteOldNewFKS(XtraBinding, items.Where(p=>p.Key.State != XtraBindingStoredItemState.Removed).Select(p=>p.Value), ParentOldNewPKS);

                XtraBinding.xtraBindingOperation.Update(items.Values);

                IEnumerable<object> newPKs = GetObjectsPKS(XtraBinding, items.Select(p=>p.Key.State != XtraBindingStoredItemState.Removed ? p.Value : null));

                oldNewPks = pks.Select((p, k) => new { p, k }).ToDictionary(k => k.p, v => newPKs.ElementAt(v.k));

                _updateSet.Add(new XtraBindingUpdateSetUpdateResult(XtraBinding, GetPkFieldName(XtraBinding), items.Keys, newPKs));

            }

            if (XtraBinding is XtraBindingContainer)
            {
                foreach (XtraBindingChild childBinding in ((XtraBindingContainer)XtraBinding).Children)
                {
                    IEnumerable<object> childrenPKs = PKs == null ? null : GetChildrenPKS(childBinding, PKs);

                    OnUpdate(childBinding, childrenPKs, oldNewPks);
                }
            }
        }
 private string GetPkFieldName(XtraBinding XtraBinding)
 {
     return _updateSetDataConverter.GetPrimaryKeyFieldName(XtraBinding.DataSource);
 }
 private IDictionary<string, object> GetFieldsValues(XtraBinding XtraBinding, object UnderlyingItem)
 {
     return GetFieldsValues(XtraBinding.DataSource, UnderlyingItem);
 }
        /// <summary>
        /// Get stored items for XtraBinding and his descendants
        /// </summary>
        internal IEnumerable<XtraBindingStoredItem> GetStoredItemsBatch(XtraBinding XtraBinding, IEnumerable<object> PKs)
        {
            List<XtraBindingStoredItem> items = new List<XtraBindingStoredItem>();

            IEnumerable<XtraBindingStoredItem> ims = _entyties.Where(p => p.xtraBinding == XtraBinding).SelectMany(p => p.StoredItems);

            if (PKs != null)
                ims = ims.Where(p => PKs.Contains(p.FieldsValues[GetPkFieldName(XtraBinding)]));

            items.AddRange(ims);

            if (XtraBinding is XtraBindingContainer)
            {
                foreach(XtraBindingChild childBinding in ((XtraBindingContainer)XtraBinding).Children)
                {
                    IEnumerable<object> childrenPKs = PKs == null ? null : GetChildrenPKS(childBinding, PKs);

                    items.AddRange(GetStoredItemsBatch(childBinding, childrenPKs));
                }
            }

            return items;
        }
 /// <summary>
 /// Get stored items only for XtraBinding
 /// </summary>
 internal IEnumerable<XtraBindingStoredItem> GetStoredItems(XtraBinding XtraBinding)
 {
     return _entyties.Where(p => p.xtraBinding == XtraBinding).SelectMany(p => p.StoredItems);
 }
 public Controller(XtraBinding XtraBinding, XOTableContext XOTableContext)
     : this(XtraBinding, new ControllerDynamicContext(XOTableContext))
 { }
        private void SubstituteOldNewFKS(XtraBinding XtraBinding, IEnumerable<object> UnderlyingObjects, IDictionary<object, object> ParentOldNewPKS)
        {
            if (XtraBinding is XtraBindingChild)
            {
                string fk = ((XtraBindingChild)XtraBinding).ChildProperties.fkFieldName;

                string pk = GetPkFieldName(XtraBinding);

                foreach (object underlyingItem in UnderlyingObjects)
                {
                    object oldVal = _updateSetDataConverter.GetValue(underlyingItem, fk);

                    if (ParentOldNewPKS.Keys.Contains(oldVal))
                    {
                        object newVal = ParentOldNewPKS[oldVal];

                        if (newVal != null && !newVal.Equals(oldVal))
                            _updateSetDataConverter.SetValue(underlyingItem, fk, newVal);
                    }
                }
            }
        }
        internal IEnumerable<object> GetObjectsPKS(XtraBinding XtraBinding, IEnumerable<object> UnderlyingObjects)
        {
            string key = GetPkFieldName(XtraBinding);

            return UnderlyingObjects.Select(p => p != null ? _updateSetDataConverter.GetValue(p, key) : null);
        }
 public XtraBindingUpdateSetUpdateResult(XtraBinding XtraBinding, string PkFieldName, IEnumerable<XtraBindingStoredItem> Items, IEnumerable<object> PKsAfterUpdate)
     : base(XtraBinding, PkFieldName, Items)
 {
     pksAfterUpdate = PKsAfterUpdate;
 }