예제 #1
0
        /// <summary>Update a page version with another, i.e. save a version of the current item and replace it with the replacement item. Returns a version of the previously published item.</summary>
        /// <param name="currentItem">The item that will be stored as a previous version.</param>
        /// <param name="replacementItem">The item that will take the place of the current item using it's ID. Any saved version of this item will not be modified.</param>
        /// <param name="storeCurrentVersion">Create a copy of the currently published version before overwriting it.</param>
        /// <returns>A version of the previously published item or the current item when storeCurrentVersion is false.</returns>
        public virtual ContentItem ReplaceVersion(ContentItem currentItem, ContentItem replacementItem, bool storeCurrentVersion = true)
        {
            if (currentItem == null)
            {
                throw new ArgumentNullException("currentItem");
            }
            if (replacementItem == null)
            {
                throw new ArgumentNullException("replacementItem");
            }

            CancellableDestinationEventArgs args = new CancellableDestinationEventArgs(currentItem, replacementItem);

            if (ItemReplacingVersion != null)
            {
                ItemReplacingVersion.Invoke(this, args);
            }
            if (!args.Cancel)
            {
                currentItem     = args.AffectedItem;
                replacementItem = args.Destination;

                using (ITransaction transaction = itemRepository.BeginTransaction())
                {
                    if (storeCurrentVersion)
                    {
                        ContentItem versionOfCurrentItem = AddVersion(currentItem);

                        Replace(currentItem, replacementItem);

                        if ((replacementItem.State == ContentState.Draft || replacementItem.State == ContentState.Waiting) && replacementItem.VersionOf.Value == currentItem)
                        {
                            // drafts can be removed once they have been published
                            currentItem.VersionIndex = replacementItem.VersionIndex;
                            itemRepository.SaveOrUpdate(currentItem);

                            Repository.Delete(replacementItem);
                        }

                        transaction.Commit();
                        return(versionOfCurrentItem);
                    }
                    else
                    {
                        Replace(currentItem, replacementItem);

                        if (replacementItem.State == ContentState.Draft && replacementItem.VersionOf.Value == currentItem)
                        {
                            // drafts can be removed once they have been published
                            //itemRepository.Delete(replacementItem);
                            Repository.Delete(replacementItem);
                        }

                        transaction.Commit();
                        return(currentItem);
                    }
                }
            }
            return(currentItem);
        }
예제 #2
0
        /// <summary>Update a page version with another, i.e. save a version of the current item and replace it with the replacement item. Returns a version of the previously published item.</summary>
        /// <param name="currentItem">The item that will be stored as a previous version.</param>
        /// <param name="replacementItem">The item that will take the place of the current item using it's ID. Any saved version of this item will not be modified.</param>
        /// <param name="storeCurrentVersion">Create a copy of the currently published version before overwriting it.</param>
        /// <returns>A version of the previously published item or the current item when storeCurrentVersion is false.</returns>
        public virtual ContentItem ReplaceVersion(ContentItem currentItem, ContentItem replacementItem, bool storeCurrentVersion)
        {
            CancellableDestinationEventArgs args = new CancellableDestinationEventArgs(currentItem, replacementItem);

            if (ItemReplacingVersion != null)
            {
                ItemReplacingVersion.Invoke(this, args);
            }
            if (!args.Cancel)
            {
                currentItem     = args.AffectedItem;
                replacementItem = args.Destination;

                using (ITransaction transaction = itemRepository.BeginTransaction())
                {
                    if (storeCurrentVersion)
                    {
                        ContentItem versionOfCurrentItem = SaveVersion(currentItem); //TODO: remove?

                        Replace(currentItem, replacementItem);

                        if (replacementItem.State == ContentState.Draft && replacementItem.VersionOf == currentItem)
                        {
                            // drafts can be removed once they have been published
                            itemRepository.Delete(replacementItem);
                        }

                        transaction.Commit();
                        return(versionOfCurrentItem);
                    }
                    else
                    {
                        Replace(currentItem, replacementItem);

                        if (replacementItem.State == ContentState.Draft && replacementItem.VersionOf == currentItem)
                        {
                            // drafts can be removed once they have been published
                            itemRepository.Delete(replacementItem);
                        }

                        transaction.Commit();
                        return(currentItem);
                    }
                }
            }
            return(currentItem);
        }