public void Update(Guid planId, PlanSnapshot.Changes changes)
        {
            lock (_cache)
            {
                if (changes.HasChanges)
                {
                    var applicableChanges = _cache.Update(planId, changes);

                    if (applicableChanges.HasChanges)
                    {
                        _storageProvider.Update(applicableChanges);
                    }
                }
            }
        }
        /**********************************************************************************/

        public PlanSnapshot.Changes Update(Guid planId, PlanSnapshot.Changes changes)
        {
            var validChanges = new PlanSnapshot.Changes();

            lock (_sync)
            {
                CachedPlan plan;

                if (!_plans.TryGetValue(planId, out plan))
                {
                    foreach (var insert in changes.Insert)
                    {
                        var clone = insert.Clone();

                        if (insert is PlanDO)
                        {
                            plan = new CachedPlan((PlanDO)clone, _expirationStrategy.NewExpirationToken());
                            _plans.Add(planId, plan);
                            _planNodesLookup.Add(planId, new CacheItem(clone, plan));
                            clone.RootPlanNode = clone;
                            break;
                        }
                    }
                }

                foreach (var insert in changes.Insert)
                {
                    if (!_planNodesLookup.ContainsKey(insert.Id))
                    {
                        if (insert.ParentPlanNodeId == null || _planNodesLookup.ContainsKey(insert.ParentPlanNodeId.Value))
                        {
                            _planNodesLookup.Add(insert.Id, new CacheItem(insert.Clone(), plan));
                        }
                    }
                }

                foreach (var insert in changes.Insert)
                {
                    CacheItem nodeCacheItem;

                    if (!_planNodesLookup.TryGetValue(insert.Id, out nodeCacheItem))
                    {
                        continue;
                    }

                    validChanges.Insert.Add(insert);

                    var node = nodeCacheItem.Node;

                    if (insert.ParentPlanNodeId != null)
                    {
                        var parent = _planNodesLookup[insert.ParentPlanNodeId.Value].Node;

                        parent.ChildNodes.Add(node);
                        node.ParentPlanNode = parent;
                        node.RootPlanNode   = plan.Root;
                    }
                }

                foreach (var deleted in changes.Delete)
                {
                    CachedPlan cachedPlan;

                    if (_plans.TryGetValue(deleted.Id, out cachedPlan))
                    {
                        PlanTreeHelper.Visit(plan.Root, x =>
                        {
                            _planNodesLookup.Remove(x.Id);
                            validChanges.Delete.Add(x);
                        });

                        _plans.Remove(plan.Root.Id);

                        return(validChanges);
                    }

                    CacheItem node;
                    if (_planNodesLookup.TryGetValue(deleted.Id, out node))
                    {
                        validChanges.Delete.Add(deleted);
                        _planNodesLookup.Remove(deleted.Id);
                        node.Node.RemoveFromParent();
                    }
                }

                foreach (var update in changes.Update)
                {
                    bool approveUpdate = false;

                    foreach (var changedProperty in update.ChangedProperties)
                    {
                        CacheItem originalCacheItem;

                        if (!_planNodesLookup.TryGetValue(update.Node.Id, out originalCacheItem))
                        {
                            continue;
                        }

                        var original = originalCacheItem.Node;

                        // structure was changed
                        if (changedProperty.Name == "ParentPlanNodeId")
                        {
                            CacheItem parentCacheItem;

                            if (update.Node.ParentPlanNodeId == null || !_planNodesLookup.TryGetValue(update.Node.ParentPlanNodeId.Value, out parentCacheItem))
                            {
                                continue;
                            }

                            var parent = parentCacheItem.Node;

                            original.RemoveFromParent();
                            parent.ChildNodes.Add(original);
                            original.ParentPlanNode   = parent;
                            original.ParentPlanNodeId = parent.Id;

                            approveUpdate = true;
                        }
                        else
                        {
                            approveUpdate = true;
                            changedProperty.SetValue(original, changedProperty.GetValue(update.Node));
                        }
                    }

                    if (approveUpdate)
                    {
                        validChanges.Update.Add(update);
                    }
                }
            }

            return(validChanges);
        }
        public virtual void Update(PlanSnapshot.Changes changes)
        {
            var adapter       = (IObjectContextAdapter)Uow.Db;
            var objectContext = adapter.ObjectContext;

            ObjectStateEntry entry;

            foreach (var planNodeDo in changes.Delete)
            {
                var entryStub = planNodeDo.Clone();

                ClearNavigationProperties(entryStub);

                var key = objectContext.CreateEntityKey("PlanNodeDOes", entryStub);

                if (!objectContext.ObjectStateManager.TryGetObjectStateEntry(key, out entry))
                {
                    PlanNodes.Attach(entryStub);
                    entry = objectContext.ObjectStateManager.GetObjectStateEntry(entryStub);
                    entry.Delete();
                }
                else
                {
                    var planNodeFromObjectContext = objectContext.GetObjectByKey(key);
                    PlanNodes.Remove((PlanNodeDO)planNodeFromObjectContext);
                }
            }

            foreach (var planNodeDo in changes.Insert)
            {
                var entity = planNodeDo.Clone();

                ClearNavigationProperties(entity);

                if (entity is ActivityDO)
                {
                    EncryptActivityCrateStorage((ActivityDO)entity);
                }

                PlanNodes.Add(entity);
            }

            foreach (var changedObject in changes.Update)
            {
                var entryStub = changedObject.Node.Clone();

                ClearNavigationProperties(entryStub);

                if (entryStub is ActivityDO)
                {
                    UpdateEncryptedActivityCrateStorage((ActivityDO)entryStub, changedObject);
                }

                var key = objectContext.CreateEntityKey("PlanNodeDOes", entryStub);
                if (!objectContext.ObjectStateManager.TryGetObjectStateEntry(key, out entry))
                {
                    PlanNodes.Attach(entryStub);
                    entry = objectContext.ObjectStateManager.GetObjectStateEntry(entryStub);
                    foreach (var changedProperty in changedObject.ChangedProperties)
                    {
                        entry.SetModifiedProperty(changedProperty.Name);
                    }
                }
                else
                {
                    foreach (var changedProperty in changedObject.ChangedProperties)
                    {
                        changedProperty.SetValue(entry.Entity, changedProperty.GetValue(entryStub));
                    }
                }
            }
        }
Exemple #4
0
        public override void Update(PlanSnapshot.Changes changes)
        {
            foreach (var planNodeDo in changes.Delete)
            {
                PlanNodes.Remove(planNodeDo);

                if (planNodeDo is ActivityDO)
                {
                    ActivityRepository.Remove((ActivityDO)planNodeDo);
                }
                else if (planNodeDo is PlanDO)
                {
                    Plans.Remove((PlanDO)planNodeDo);
                }
                else if (planNodeDo is SubplanDO)
                {
                    SubPlans.Remove((SubplanDO)planNodeDo);
                }
            }

            foreach (var planNodeDo in changes.Insert)
            {
                var entity = planNodeDo.Clone();

                ClearNavigationProperties(entity);

                if (entity is ActivityDO)
                {
                    EncryptActivityCrateStorage((ActivityDO)entity);
                    ActivityRepository.Add((ActivityDO)entity);
                }
                else if (entity is PlanDO)
                {
                    Plans.Add((PlanDO)entity);
                }
                else if (entity is SubplanDO)
                {
                    SubPlans.Add((SubplanDO)entity);
                }
                else
                {
                    PlanNodes.Add(entity);
                }
            }

            foreach (var changedObject in changes.Update)
            {
                var    planNodeDo = changedObject.Node.Clone();
                object entity     = null;

                if (planNodeDo is ActivityDO)
                {
                    entity = ActivityRepository.GetByKey(planNodeDo.Id);
                    UpdateEncryptedActivityCrateStorage((ActivityDO)planNodeDo, changedObject);
                }
                else if (planNodeDo is PlanDO)
                {
                    entity = Plans.GetByKey(planNodeDo.Id);
                }
                else if (planNodeDo is SubplanDO)
                {
                    entity = SubPlans.GetByKey(planNodeDo.Id);
                }

                foreach (var changedProperty in changedObject.ChangedProperties)
                {
                    changedProperty.SetValue(entity, changedProperty.GetValue(planNodeDo));
                }
            }
        }