/// <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); }
public override bool CopyItem(ItemDefinition source, ItemDefinition destination, string copyName, ID copyId, CallContext context) { Assert.ArgumentNotNull(source, "source"); Assert.ArgumentNotNull(destination, "destination"); Assert.ArgumentNotNullOrEmpty(copyName, "copyName"); Assert.ArgumentNotNull(copyId, "copyId"); // we use the Database API instead of our own so we can get a source item from any data provider var existingItem = Database.GetItem(source.ID); Assert.IsNotNull(existingItem, "Could not copy {0} because it did not exist!", source.ID); var destinationItem = _database.GetItem(destination.ID); Assert.IsNotNull(destinationItem, "Source item to copy {0} was in the Rhino provider but the destination parent {1} was not. Copying from Rhino to other providers is not supported.", source.Name, destination.Name); SerializedDatabase.CopyItem(ItemSynchronization.BuildSyncItem(existingItem), destination.ID, copyName, copyId); return(true); }
public override bool SaveItem(ItemDefinition itemDefinition, ItemChanges changes, CallContext context) { Assert.ArgumentNotNull(itemDefinition, "itemDefinition"); Assert.ArgumentNotNull(changes, "changes"); var existingItem = SerializedDatabase.GetItem(itemDefinition.ID); if (existingItem == null) { return(false); // item was not in this data provider, cede control to the next one } var savedItem = ItemSynchronization.BuildSyncItem(changes.Item); if (changes.Renamed) { SerializedDatabase.SaveAndRenameItem(savedItem, changes.Properties["name"].OriginalValue.ToString()); } else { SerializedDatabase.SaveItem(savedItem); } return(true); }
protected override string BuildPackage() { var commands = new List <ICommand>(); foreach (var item in Items) { var syncItem = ItemSynchronization.BuildSyncItem(item); var contentDataItem = new ContentDataItem(string.Empty, item.Paths.ParentPath, item.Name, syncItem); var command = new AddItemCommand(contentDataItem); commands.Add(command); } var rootPath = FileUtil.MapPath("/"); foreach (var fileName in Files) { if (FileUtil.IsFolder(fileName)) { foreach (var file in Directory.GetFiles(FileUtil.MapPath(fileName), "*", SearchOption.AllDirectories)) { var fileInfo = new FileInfo(file); if ((fileInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) { continue; } if ((fileInfo.Attributes & FileAttributes.System) == FileAttributes.System) { continue; } var fileItem = new FileSystemDataItem(rootPath, Path.GetDirectoryName(file), Path.GetFileName(file)); var command = new AddFileCommand(fileItem); commands.Add(command); } } else { var fileItem = new FileSystemDataItem(rootPath, Path.GetDirectoryName(fileName), Path.GetFileName(fileName)); var command = new AddFileCommand(fileItem); commands.Add(command); } } var diff = new DiffInfo(commands) { Author = Author, PostStep = PostStep, Publisher = Publisher, Readme = Readme, Version = Version, Title = PackageName }; var directoryName = Path.GetDirectoryName(FileName) ?? string.Empty; directoryName = directoryName.Replace("\\", "/"); var folderPath = FileUtil.MapPath(directoryName); diff.Serialize(folderPath); var packageFileName = FileUtil.UnmapPath(folderPath, false) + "/" + DiffInfo.OutputFileName; PackageGenerator.GeneratePackage(diff, packageFileName); return(packageFileName); }