/// <summary> /// Helper Method to traverse a rootcompareobject and all foldercompareobjects and filecompareobjects under it /// </summary> /// <param name="root">RootCompareObject from the previous visitor in order to start the traversal</param> /// <param name="visitor">A particular visit to visit the RCOs. In this case, it will be the PreviewVisitor</param> public static void TraverseFolderHelper(RootCompareObject root, IVisitor visitor) { visitor.Visit(root); Dictionary<string, BaseCompareObject>.ValueCollection values = root.Contents.Values; FolderCompareObject fco; foreach (BaseCompareObject o in values) { if ((fco = o as FolderCompareObject) != null) TraverseFolderHelper(fco, root.Paths.Length, visitor); else visitor.Visit(o as FileCompareObject, root.Paths.Length); } }
private static void LevelOrderTraverseFolder(RootCompareObject root, int numOfPaths, IVisitor visitor, Progress syncProgress) { Queue<BaseCompareObject> levelQueue = new Queue<BaseCompareObject>(); RootCompareObject rt; FolderCompareObject folder = null; levelQueue.Enqueue(root); while (levelQueue.Count > 0) { if (syncProgress != null && syncProgress.State == SyncState.Cancelled) return; BaseCompareObject currObj = levelQueue.Dequeue(); if ((rt = currObj as RootCompareObject) != null) visitor.Visit(rt); else if ((folder = currObj as FolderCompareObject) != null) visitor.Visit(folder, numOfPaths); else visitor.Visit(currObj as FileCompareObject, numOfPaths); Dictionary<string, BaseCompareObject>.ValueCollection values; if (rt != null) { values = rt.Contents.Values; foreach (BaseCompareObject o in values) levelQueue.Enqueue(o); } else if (folder != null) { values = folder.Contents.Values; foreach (BaseCompareObject o in values) levelQueue.Enqueue(o); } } }
/// <summary> /// Visit <see cref="RootCompareObject"/> implementation for BuilderVisitor. /// </summary> /// <param name="root">The <see cref="RootCompareObject"/> to build and process.</param> /// <remarks><see cref="RootCompareObject"/> is a specific type of <see cref="FolderCompareObject"/>, and is thus handled similarly.</remarks> public void Visit(RootCompareObject root) { Visit(root, root.Paths.Length); }
/// <summary> /// Synchronizes a job given a <see cref="ManualSyncRequest"/> and a <see cref="SyncProgress"/>. /// </summary> /// <param name="request">The <see cref="ManualSyncRequest"/> to pass in.</param> /// <param name="progress">The <see cref="SyncProgress"/> to pass in.</param> /// <returns></returns> public static RootCompareObject Sync(ManualSyncRequest request, SyncProgress progress) { ServiceLocator.GetLogger(ServiceLocator.USER_LOG).Write(new LogData(LogEventType.SYNC_STARTED, "Started Manual Sync for " + request.TagName)); //Initialize and add filters conflict and archive filters to it List<Filter> filters = request.Filters.ToList(); filters.Add(FilterFactory.CreateArchiveFilter(request.Config.ArchiveName)); filters.Add(FilterFactory.CreateArchiveFilter(request.Config.ConflictDir)); RootCompareObject rco = new RootCompareObject(request.Paths); // Analyzing progress.ChangeToAnalyzing(); List<string> typeConflicts = new List<string>(); CompareObjectHelper.PreTraverseFolder(rco, new BuilderVisitor(filters, typeConflicts, progress), progress); if (progress.State == SyncState.Cancelled) { ServiceLocator.UIPriorityQueue().Enqueue(new CancelSyncNotification(request.TagName, true)); return null; } CompareObjectHelper.PreTraverseFolder(rco, new XMLMetadataVisitor(), progress); if (progress.State == SyncState.Cancelled) { ServiceLocator.UIPriorityQueue().Enqueue(new CancelSyncNotification(request.TagName, true)); return null; } CompareObjectHelper.PreTraverseFolder(rco, new ProcessMetadataVisitor(), progress); if (progress.State == SyncState.Cancelled) { ServiceLocator.UIPriorityQueue().Enqueue(new CancelSyncNotification(request.TagName, true)); return null; } CompareObjectHelper.LevelOrderTraverseFolder(rco, new FolderRenameVisitor(), progress); if (progress.State == SyncState.Cancelled) { ServiceLocator.UIPriorityQueue().Enqueue(new CancelSyncNotification(request.TagName, true)); return null; } ComparerVisitor comparerVisitor = new ComparerVisitor(); CompareObjectHelper.PostTraverseFolder(rco, comparerVisitor, progress); if (progress.State == SyncState.Cancelled) { ServiceLocator.UIPriorityQueue().Enqueue(new CancelSyncNotification(request.TagName, true)); return null; } if (progress.State != SyncState.Cancelled) { // Syncing progress.ChangeToSyncing(comparerVisitor.TotalNodes); HandleBuildConflicts(typeConflicts, request.Config); CompareObjectHelper.PreTraverseFolder(rco, new ConflictVisitor(request.Config), progress); SyncerVisitor syncerVisitor = new SyncerVisitor(request.Config, progress); CompareObjectHelper.PreTraverseFolder(rco, syncerVisitor, progress); // XML Writer progress.ChangeToFinalizing(syncerVisitor.NodesCount); CompareObjectHelper.PreTraverseFolder(rco, new XMLWriterVisitor(progress), progress); progress.ChangeToFinished(); if (request.Notify) ServiceLocator.LogicLayerNotificationQueue().Enqueue(new MonitorTagNotification(request.TagName)); // Finished ServiceLocator.GetLogger(ServiceLocator.USER_LOG).Write(new LogData(LogEventType.SYNC_STOPPED, "Completed Manual Sync for " + request.TagName)); return rco; } ServiceLocator.GetLogger(ServiceLocator.USER_LOG).Write(new LogData(LogEventType.SYNC_STOPPED, "Cancelled Manual Sync for " + request.TagName)); return null; }
/// <summary> /// Compares/previews a job given a <see cref="ManualCompareRequest"/> and a <see cref="PreviewProgress"/>. /// </summary> /// <param name="request">The <see cref="ManualCompareRequest"/> to pass in.</param> /// <param name="progress">The <see cref="PreviewProgress"/> to pass in.</param> /// <returns></returns> public static RootCompareObject Compare(ManualCompareRequest request, PreviewProgress progress) { List<Filter> filters = request.Filters.ToList(); filters.Add(FilterFactory.CreateArchiveFilter(request.Config.ArchiveName)); filters.Add(FilterFactory.CreateArchiveFilter(request.Config.ConflictDir)); RootCompareObject rco = new RootCompareObject(request.Paths); List<string> typeConflicts = new List<string>(); CompareObjectHelper.PreTraverseFolder(rco, new BuilderVisitor(filters, typeConflicts, progress), progress); CompareObjectHelper.PreTraverseFolder(rco, new XMLMetadataVisitor(), progress); CompareObjectHelper.PreTraverseFolder(rco, new ProcessMetadataVisitor(), progress); CompareObjectHelper.LevelOrderTraverseFolder(rco, new FolderRenameVisitor(), progress); ComparerVisitor comparerVisitor = new ComparerVisitor(); CompareObjectHelper.PostTraverseFolder(rco, comparerVisitor, progress); return rco; }
private void Populate(RootCompareObject rco) { try { _previewSyncData.Rows.Clear(); PreviewVisitor visitor = new PreviewVisitor(_previewSyncData); if (rco != null) { SyncUIHelper.TraverseFolderHelper(rco, visitor); } _previewSyncData.AcceptChanges(); } catch (UnhandledException) { DialogHelper.DisplayUnhandledExceptionMessage(this); } }
/// <summary> /// Traverse the tree in post-order, starting from the <see cref="RootCompareObject"/>. /// </summary> /// <param name="root">The <see cref="RootCompareObject"/> to start level-traversal from.</param> /// <param name="visitor">The <see cref="IVisitor"/> that will be used to visit the tree.</param> /// <param name="syncProgress">The <see cref="Progress"/> object to pass in.</param> public static void PostTraverseFolder(RootCompareObject root, IVisitor visitor, Progress syncProgress) { TraverseFolderHelper(root, visitor, TraverseType.Post, syncProgress); }
// Do nothing for root public void Visit(RootCompareObject root) { }
// For each XMLCompareObjects in the list , create a node and append them to the root node private void AddFileToRoot(List<XMLCompareObject> xmlFileList, RootCompareObject root, int counter, int length) { if (xmlFileList.Count == 0) return; for (int i = 0; i < xmlFileList.Count; i++) { BaseCompareObject o = root.GetChild(xmlFileList[i].Name); FileCompareObject fco; if (o == null) fco = new FileCompareObject(xmlFileList[i].Name, length, root); else fco = (FileCompareObject)o; fco.MetaCreationTimeUtc[counter] = xmlFileList[i].CreatedTimeUtc; fco.MetaHash[counter] = xmlFileList[i].Hash; fco.MetaLastWriteTimeUtc[counter] = xmlFileList[i].LastModifiedTimeUtc; fco.MetaLength[counter] = xmlFileList[i].Size; fco.MetaUpdated[counter] = xmlFileList[i].LastUpdatedTimeUtc; fco.MetaExists[counter] = true; if (o == null) root.AddChild(fco); } }
// Method will return any time the state of the sync progress becomes cancelled. private static void TraverseFolderHelper(RootCompareObject root, IVisitor visitor, TraverseType type, Progress syncProgress) { if (syncProgress != null && syncProgress.State == SyncState.Cancelled) return; if (type == TraverseType.Pre) visitor.Visit(root); Dictionary<string, BaseCompareObject>.ValueCollection values = root.Contents.Values; foreach (BaseCompareObject o in values) { if (syncProgress != null && syncProgress.State == SyncState.Cancelled) return; FolderCompareObject fco; if ((fco = o as FolderCompareObject) != null) TraverseFolderHelper(fco, root.Paths.Length, visitor, type, syncProgress); else visitor.Visit(o as FileCompareObject, root.Paths.Length); } if (type == TraverseType.Post) visitor.Visit(root); }
/// <summary> /// Increases the SyncProgress when it visits the root node /// </summary> /// <param name="root"></param> public void Visit(RootCompareObject root) { _progress.Complete(); }
/// <summary> /// Initialize the Sync Complete Notification /// </summary> /// <param name="tagName">name of the tag</param> /// <param name="rco">the root compare object</param> public SyncCompleteNotification(string tagName,RootCompareObject rco) : base("Sync Complete Notification", NotificationCode.SyncCompleteNotification) { CompareObject = rco; TagName = tagName; }
/// <summary> /// Visits the RootCompareObject and does nothing /// </summary> /// <param name="root">The RootCompareObject to visit</param> public void Visit(RootCompareObject root) { //Do nothing }
/// <summary> /// Creates a sync completed notification /// </summary> /// <param name="tagName">The string value that represents the tag name the sync belongs to</param> /// <param name="rco">The <see cref="RootCompareObject">RootCompareObject</see> object that is used /// for sync</param> /// <returns>the <see cref="SyncCompleteNotification">SyncCompleteNotification</see> /// object</returns> public static AbstractNotification CreateSyncCompleteNotification(string tagName, RootCompareObject rco) { return new SyncCompleteNotification(tagName, rco); }
/// <summary> /// The <see cref="RootCompareObject"/> to visit. /// </summary> /// <param name="root">The <see cref="RootCompareObject"/> to process.</param> public void Visit(RootCompareObject root) { _nodesCount++; _syncProgress.Complete(); //Do nothing }
/// <summary> /// Extracts metadata files and folders and minus them off existing files and folders. /// </summary> /// <param name="root"></param> public void Visit(RootCompareObject root) { XmlDocument xmlDoc = new XmlDocument(); string[] listOfPaths = root.Paths; DirectoryInfo di; List<XMLCompareObject> xmlObjList; List<string> name = new List<string>(); for (int i = 0; i < listOfPaths.Length; i++) { string xmlPath = Path.Combine(listOfPaths[i], CommonXMLConstants.MetadataPath); if (!File.Exists(xmlPath)) continue; CommonMethods.LoadXML(ref xmlDoc, xmlPath); di = new DirectoryInfo(listOfPaths[i]); FileInfo[] fileInfoList = di.GetFiles(); DirectoryInfo[] dirInfoList = di.GetDirectories(); List<string> folderNames = GetAllFoldersInXML(xmlDoc); xmlObjList = GetAllFilesInXML(xmlDoc); RemoveSimilarFiles(xmlObjList, fileInfoList); RemoveSimilarFolders(folderNames, dirInfoList); AddFolderToRoot(folderNames, root, i, root.Paths.Length); AddFileToRoot(xmlObjList, root, i, root.Paths.Length); xmlObjList.Clear(); folderNames.Clear(); } }
/// <summary> /// Traverse the tree in level order, starting from the <see cref="RootCompareObject"/>. /// </summary> /// <param name="root">The <see cref="RootCompareObject"/> to start level-traversal from.</param> /// <param name="visitor">The <see cref="IVisitor"/> that will be used to visit the tree.</param> /// <param name="syncProgress">The <see cref="Progress"/> object to pass in.</param> public static void LevelOrderTraverseFolder(RootCompareObject root, IVisitor visitor, Progress syncProgress) { LevelOrderTraverseFolder(root, root.Paths.Length, visitor, syncProgress); }
// For each list of folder names , create a node and append them to the root node private void AddFolderToRoot(List<string> folderName, RootCompareObject root, int counter, int length) { if (folderName.Count == 0) return; for (int i = 0; i < folderName.Count; i++) { BaseCompareObject o = root.GetChild(folderName[i]); FolderCompareObject fco; if (o == null) fco = new FolderCompareObject(folderName[i], length, root); else fco = (FolderCompareObject)o; fco.MetaExists[counter] = true; if (o == null) root.AddChild(fco); } }
// Simply increase the node count public void Visit(RootCompareObject root) { _totalNodes++; }