Example #1
0
        public virtual void SaveItem(ItemDefinition itemDefinition, ItemChanges changes, CallContext context)
        {
            if (DisableSerialization)
            {
                return;
            }

            Assert.ArgumentNotNull(itemDefinition, "itemDefinition");
            Assert.ArgumentNotNull(changes, "changes");

            // get the item we're saving to evaluate with the predicate
            // NOTE: the item in this state may be incomplete as Sitecore can sometimes send partial item data and rely on changes to do the save
            // e.g. during package installations. So we have to merge the changes with any existing item data if we save it later, to keep it consistent.
            IItemData sourceItem = new ItemData(changes.Item);

            if (!_predicate.Includes(sourceItem).IsIncluded)
            {
                return;
            }

            string oldName = changes.Renamed ? changes.Properties["name"].OriginalValue.ToString() : string.Empty;

            if (changes.Renamed && !oldName.Equals(sourceItem.Name, StringComparison.Ordinal))
            // it's a rename, in which the name actually changed (template builder will cause 'renames' for the same name!!!)
            {
                using (new DatabaseCacheDisabler())
                {
                    // disabling the DB caches while running this ensures that any children of the renamed item are retrieved with their proper post-rename paths and thus are not saved at their old location

                    // this allows us to filter out any excluded children by predicate when the data store moves children
                    var predicatedItem = new PredicateFilteredItemData(sourceItem, _predicate);

                    _targetDataStore.MoveOrRenameItem(predicatedItem, changes.Item.Paths.ParentPath + "/" + oldName);
                }

                _logger.RenamedItem(_targetDataStore.FriendlyName, sourceItem, oldName);
            }
            else if (HasConsequentialChanges(changes))
            // it's a simple update - but we reject it if only inconsequential fields (last updated, revision) were changed - again, template builder FTW
            {
                var existingSerializedItem = _targetDataStore.GetByPathAndId(sourceItem.Path, sourceItem.Id, sourceItem.DatabaseName);

                // generated an IItemData from the item changes we received, and apply those changes to the existing serialized item if any
                if (existingSerializedItem != null)
                {
                    sourceItem = new ItemChangeApplyingItemData(existingSerializedItem, changes);
                }
                else
                {
                    sourceItem = new ItemChangeApplyingItemData(changes);
                }

                _targetDataStore.Save(sourceItem);

                AddBlobsToCache(sourceItem);

                _logger.SavedItem(_targetDataStore.FriendlyName, sourceItem, "Saved");
            }
        }
        public virtual void SaveItem(ItemDefinition itemDefinition, ItemChanges changes, CallContext context)
        {
            if (DisableSerialization) return;

            Assert.ArgumentNotNull(itemDefinition, "itemDefinition");
            Assert.ArgumentNotNull(changes, "changes");

            // get the item we're saving to evaluate with the predicate
            // NOTE: the item in this state may be incomplete as Sitecore can sometimes send partial item data and rely on changes to do the save
            // e.g. during package installations. So we have to merge the changes with any existing item data if we save it later, to keep it consistent.
            IItemData sourceItem = new ItemData(changes.Item);

            if (!_predicate.Includes(sourceItem).IsIncluded) return;

            string oldName = changes.Renamed ? changes.Properties["name"].OriginalValue.ToString() : string.Empty;
            if (changes.Renamed && !oldName.Equals(sourceItem.Name, StringComparison.Ordinal))
            // it's a rename, in which the name actually changed (template builder will cause 'renames' for the same name!!!)
            {
                using (new DatabaseCacheDisabler())
                {
                    // disabling the DB caches while running this ensures that any children of the renamed item are retrieved with their proper post-rename paths and thus are not saved at their old location

                    // this allows us to filter out any excluded children by predicate when the data store moves children
                    var predicatedItem = new PredicateFilteredItemData(sourceItem, _predicate);

                    _targetDataStore.MoveOrRenameItem(predicatedItem, changes.Item.Paths.ParentPath + "/" + oldName);
                }

                _logger.RenamedItem(_targetDataStore.FriendlyName, sourceItem, oldName);
            }
            else if (HasConsequentialChanges(changes))
            // it's a simple update - but we reject it if only inconsequential fields (last updated, revision) were changed - again, template builder FTW
            {
                var existingSerializedItem = _targetDataStore.GetByPathAndId(sourceItem.Path, sourceItem.Id, sourceItem.DatabaseName);

                // generated an IItemData from the item changes we received, and apply those changes to the existing serialized item if any
                if (existingSerializedItem != null) sourceItem = new ItemChangeApplyingItemData(existingSerializedItem, changes);
                else sourceItem = new ItemChangeApplyingItemData(changes);

                _targetDataStore.Save(sourceItem);

                AddBlobsToCache(sourceItem);

                _logger.SavedItem(_targetDataStore.FriendlyName, sourceItem, "Saved");
            }
        }