Added code to delete/move child objects
authorPanagiotis Kanavos <pkanavos@gmail.com>
Tue, 24 Jan 2012 07:28:02 +0000 (09:28 +0200)
committerPanagiotis Kanavos <pkanavos@gmail.com>
Tue, 24 Jan 2012 07:28:02 +0000 (09:28 +0200)
trunk/Pithos.Core/Agents/WorkflowAgent.cs

index 02155e9..9ef6844 100644 (file)
@@ -126,17 +126,7 @@ namespace Pithos.Core.Agents
                                                                         accountInfo.BlockHash));
                                 break;
                             case FileStatus.Deleted:
-                                if (fileState != null)
-                                {
-                                    var children = StatusKeeper.GetChildren(fileState);
-                                    foreach (var child in children)
-                                    {
-                                        var childInfo = child.IsFolder
-                                                            ? (FileSystemInfo) new DirectoryInfo(child.FilePath)
-                                                            : new FileInfo(child.FilePath);
-                                        NetworkAgent.Post(new CloudDeleteAction(accountInfo, childInfo, child));
-                                    }
-                                }
+                                DeleteChildObjects(state, fileState);
                                 NetworkAgent.Post(new CloudDeleteAction(accountInfo, info, fileState));
                                 break;
                             case FileStatus.Renamed:
@@ -148,7 +138,10 @@ namespace Pithos.Core.Agents
                                                              : new FileInfo(state.Path);
                                 NetworkAgent.Post(new CloudMoveAction(accountInfo, CloudActionType.RenameCloud,
                                                                       oldInfo,
-                                                                      newInfo));
+                                                                      newInfo));                                
+                                //TODO: Do I have to move children as well or will Pithos handle this?
+                               //Need to find all children of the OLD filepath
+                                MoveChildObjects(state);
                                 break;
                         }
                     }
@@ -163,6 +156,46 @@ namespace Pithos.Core.Agents
             }
         }
 
+        private void DeleteChildObjects(WorkflowState state, FileState fileState)
+        {
+            if (fileState != null)
+            {
+                var children = StatusKeeper.GetChildren(fileState);
+                foreach (var child in children)
+                {
+                    var childInfo = child.IsFolder
+                                        ? (FileSystemInfo) new DirectoryInfo(child.FilePath)
+                                        : new FileInfo(child.FilePath);
+                    NetworkAgent.Post(new CloudDeleteAction(state.AccountInfo, childInfo, child));
+                }
+            }
+        }
+
+        private void MoveChildObjects(WorkflowState state)
+        {
+            var oldFileState = StatusKeeper.GetStateByFilePath(state.OldPath);
+            if (oldFileState != null)
+            {
+                var children = StatusKeeper.GetChildren(oldFileState);
+                foreach (var child in children)
+                {
+                    var newPath = Path.Combine(state.Path, child.FilePath.Substring(state.OldPath.Length+1));
+
+                    var oldMoveInfo = child.IsFolder
+                                          ? (FileSystemInfo) new DirectoryInfo(child.FilePath)
+                                          : new FileInfo(child.FilePath);
+                    var newMoveInfo = child.IsFolder
+                                          ? (FileSystemInfo) new DirectoryInfo(newPath)
+                                          : new FileInfo(newPath);
+                    //The new file path will be created by trimming the old root path
+                    //and substituting the new root path
+
+                    NetworkAgent.Post(new CloudMoveAction(state.AccountInfo, CloudActionType.RenameCloud,
+                                                          oldMoveInfo, newMoveInfo));
+                }
+            }
+        }
+
 
         //Starts interrupted files for a specific account
         public void RestartInterruptedFiles(AccountInfo accountInfo)