/// <summary> /// Moves the descendant items of a serialized parent after it has been moved or renamed. /// </summary> /// <param name="oldReference">Reference to the original path pre-move/rename</param> /// <param name="newItem">The newly renamed or moved parent item</param> /// <param name="sourceItem">The source item representing the renamed/moved item. NOTE that the path of this item is incorrect a lot of the time so we ignore it.</param> /// <param name="renaming">True for moving renamed children, false for moving moved children. For renames, the children already have correct new paths; for moves we have to recalculate it.</param> /// <remarks> /// This method basically gets all descendants of the source item that was moved/renamed, generates an appropriate new serialized item for it, and _if the new child item is in the predicate_ we /// serialize it to its new location. Finally, we delete the old children directory if it existed. /// /// Doing it this way allows handling crazy cases like moving trees of items between included and excluded locations - or even moves or renames causing SOME of the children to be ignored. Wild. /// </remarks> protected virtual void MoveDescendants(ISerializedReference oldReference, ISerializedItem newItem, ISourceItem sourceItem, bool renaming) { // remove the extension from the new item's provider ID string newItemReferencePath = SerializationPathUtility.GetReferenceDirectoryPath(newItem); // if the paths were the same, no moving occurs (this can happen when saving templates, which spuriously can report "renamed" when they are not actually any such thing) if (oldReference.ProviderId.Equals(newItemReferencePath, StringComparison.Ordinal)) { return; } // this is for renaming an item that differs only by case from the original. Because NTFS is case-insensitive the 'new parent' exists // already, but it will use the old name. Not quite what we want. So we need to manually rename the folder. if (oldReference.ProviderId.Equals(newItemReferencePath, StringComparison.OrdinalIgnoreCase) && Directory.Exists(oldReference.ProviderId)) { Directory.Move(oldReference.ProviderId, oldReference.ProviderId + "_tempunicorn"); Directory.Move(oldReference.ProviderId + "_tempunicorn", newItemReferencePath); } var descendantItems = GetDescendants(sourceItem).Cast <SitecoreSourceItem>(); // Go through descendant source items and serialize all that are included by the predicate foreach (var descendant in descendantItems) { var syncItem = ItemSynchronization.BuildSyncItem(descendant.InnerItem); // the newPhysicalPath will point to the OLD physical path pre-move (but for renames we actually get the new path already). // For moves, we re-root the path to point to the new parent item's base path to fix that before we write to disk string newItemPath = (renaming) ? descendant.ItemPath : descendant.ItemPath.Replace(oldReference.ItemPath, newItem.ItemPath); var newPhysicalPath = SerializationPathUtility.GetSerializedItemPath(_rootPath, syncItem.DatabaseName, newItemPath); var newSerializedItem = new SitecoreSerializedItem(syncItem, newPhysicalPath, this); if (!_predicate.Includes(newSerializedItem).IsIncluded) { continue; // if the moved child location is outside the predicate, do not re-serialize } UpdateSerializedItem(newSerializedItem); } // remove the old children folder if it exists - as long as the original name was not a case insensitive version of this item if (Directory.Exists(oldReference.ProviderId) && !oldReference.ProviderId.Equals(newItemReferencePath, StringComparison.OrdinalIgnoreCase)) { Directory.Delete(oldReference.ProviderId, true); } }
public virtual void MoveSerializedItem(ISourceItem sourceItem, ISourceItem newParentItem) { Assert.ArgumentNotNull(sourceItem, "sourceItem"); Assert.ArgumentNotNull(newParentItem, "newParentItem"); var sitecoreSource = sourceItem as SitecoreSourceItem; var sitecoreParent = newParentItem as SitecoreSourceItem; if (sitecoreParent == null) { throw new ArgumentException("newParentItem must be a SitecoreSourceItem", "newParentItem"); } if (sitecoreSource == null) { throw new ArgumentException("sourceItem must be a SitecoreSourceItem", "sourceItem"); } var oldRootDirectory = new SitecoreSerializedReference(SerializationPathUtility.GetSerializedReferencePath(_rootPath, sourceItem), this); var oldRootItemPath = new SitecoreSerializedReference(SerializationPathUtility.GetReferenceItemPath(oldRootDirectory), this); var newRootItemPath = newParentItem.ItemPath + "/" + sourceItem.Name; var newRootSerializedPath = SerializationPathUtility.GetSerializedItemPath(_rootPath, newParentItem.DatabaseName, newRootItemPath); var syncItem = ItemSynchronization.BuildSyncItem(sitecoreSource.InnerItem); // update the path and parent IDs to the new location syncItem.ParentID = newParentItem.Id.ToString(); syncItem.ItemPath = newRootItemPath; // if this occurs we're "moving" an item to the same location it started from. Which means we shouldn't do anything. if (oldRootDirectory.ItemPath.Equals(syncItem.ItemPath)) { return; } var serializedNewItem = new SitecoreSerializedItem(syncItem, newRootSerializedPath, this); // write the moved sync item to its new destination UpdateSerializedItem(serializedNewItem); // move any children to the new destination (and fix their paths) MoveDescendants(oldRootDirectory, serializedNewItem, sourceItem, false); // remove the serialized item in the old location DeleteSerializedItem(oldRootItemPath); }
public virtual ISerializedItem SerializeItem(ISourceItem item) { Assert.ArgumentNotNull(item, "item"); var sitecoreSourceItem = item as SitecoreSourceItem; var sitecoreItem = sitecoreSourceItem != null ? sitecoreSourceItem.InnerItem : Factory.GetDatabase(item.DatabaseName).GetItem(item.Id); Assert.IsNotNull(sitecoreItem, "Item to serialize did not exist!"); var serializedPath = SerializationPathUtility.GetSerializedItemPath(_rootPath, item); var serializedItem = new SitecoreSerializedItem(ItemSynchronization.BuildSyncItem(sitecoreItem), serializedPath, this); UpdateSerializedItem(serializedItem); return(serializedItem); }
/// <summary> /// Moves the descendant items of a serialized parent after it has been moved or renamed. /// </summary> /// <param name="oldReference">Reference to the original path pre-move/rename</param> /// <param name="newItem">The newly renamed or moved parent item</param> /// <param name="sourceItem">The source item representing the renamed/moved item. NOTE that the path of this item is incorrect a lot of the time so we ignore it.</param> /// <remarks> /// This method basically gets all descendants of the source item that was moved/renamed, generates an appropriate new serialized item for it, and _if the new child item is in the predicate_ we /// serialize it to its new location. Finally, we delete the old children directory if it existed. /// /// Doing it this way allows handling crazy cases like moving trees of items between included and excluded locations - or even moves or renames causing SOME of the children to be ignored. Wild. /// </remarks> protected virtual void MoveDescendants(ISerializedReference oldReference, ISerializedItem newItem, ISourceItem sourceItem) { // remove the extension from the new item's provider ID string newItemReferencePath = SerializationPathUtility.GetReferenceDirectoryPath(newItem); // if the paths were the same, no moving occurs (this can happen when saving templates, which spuriously can report "renamed" when they are not actually any such thing) if (oldReference.ProviderId.Equals(newItemReferencePath, StringComparison.Ordinal)) return; // this is for renaming an item that differs only by case from the original. Because NTFS is case-insensitive the 'new parent' exists // already, but it will use the old name. Not quite what we want. So we need to manually rename the folder. if (oldReference.ProviderId.Equals(newItemReferencePath, StringComparison.OrdinalIgnoreCase) && Directory.Exists(oldReference.ProviderId)) { Directory.Move(oldReference.ProviderId, oldReference.ProviderId + "_tempunicorn"); Directory.Move(oldReference.ProviderId + "_tempunicorn", newItemReferencePath); } var descendantItems = GetDescendants(sourceItem).Cast<SitecoreSourceItem>(); // Go through descendant source items and serialize all that are included by the predicate foreach (var descendant in descendantItems) { var syncItem = ItemSynchronization.BuildSyncItem(descendant.InnerItem); // the newPhysicalPath will point to the OLD physical path pre-move/rename. // We re-root the path to point to the new parent item's base path to fix that before we write to disk var newPhysicalPath = SerializationPathUtility.GetSerializedItemPath(_rootPath, syncItem.DatabaseName, syncItem.ItemPath); var newSerializedItem = new SitecoreSerializedItem(syncItem, newPhysicalPath, this); if (!_predicate.Includes(newSerializedItem).IsIncluded) continue; // if the moved child location is outside the predicate, do not re-serialize UpdateSerializedItem(newSerializedItem); } // remove the old children folder if it exists - as long as the original name was not a case insensitive version of this item if (Directory.Exists(oldReference.ProviderId) && !oldReference.ProviderId.Equals(newItemReferencePath, StringComparison.OrdinalIgnoreCase)) { Directory.Delete(oldReference.ProviderId, true); } }
public virtual ISerializedItem SerializeItem(ISourceItem item) { Assert.ArgumentNotNull(item, "item"); var sitecoreSourceItem = item as SitecoreSourceItem; var sitecoreItem = sitecoreSourceItem != null ? sitecoreSourceItem.InnerItem : Factory.GetDatabase(item.DatabaseName).GetItem(item.Id); Assert.IsNotNull(sitecoreItem, "Item to serialize did not exist!"); var serializedPath = SerializationPathUtility.GetSerializedItemPath(_rootPath, item); var serializedItem = new SitecoreSerializedItem(ItemSynchronization.BuildSyncItem(sitecoreItem), serializedPath, this); UpdateSerializedItem(serializedItem); return serializedItem; }
public virtual void MoveSerializedItem(ISourceItem sourceItem, ISourceItem newParentItem) { Assert.ArgumentNotNull(sourceItem, "sourceItem"); Assert.ArgumentNotNull(newParentItem, "newParentItem"); var sitecoreSource = sourceItem as SitecoreSourceItem; var sitecoreParent = newParentItem as SitecoreSourceItem; if (sitecoreParent == null) throw new ArgumentException("newParentItem must be a SitecoreSourceItem", "newParentItem"); if (sitecoreSource == null) throw new ArgumentException("sourceItem must be a SitecoreSourceItem", "sourceItem"); var oldRootDirectory = new SitecoreSerializedReference(SerializationPathUtility.GetSerializedReferencePath(_rootPath, sourceItem), this); var oldRootItemPath = new SitecoreSerializedReference(SerializationPathUtility.GetReferenceItemPath(oldRootDirectory), this); var newRootItemPath = SerializationPathUtility.GetSerializedReferencePath(_rootPath, newParentItem) + Path.DirectorySeparatorChar + sourceItem.Name + PathUtils.Extension; var syncItem = ItemSynchronization.BuildSyncItem(sitecoreSource.InnerItem); // update the path and parent IDs to the new location syncItem.ParentID = newParentItem.Id.ToString(); syncItem.ItemPath = string.Concat(newParentItem.ItemPath, "/", syncItem.Name); var serializedNewItem = new SitecoreSerializedItem(syncItem, newRootItemPath, this); // write the moved sync item to its new destination UpdateSerializedItem(serializedNewItem); // move any children to the new destination (and fix their paths) MoveDescendants(oldRootDirectory, serializedNewItem, sourceItem); // remove the serialized item in the old location DeleteSerializedItem(oldRootItemPath); }