Improve Collections create and move methods and update documentation
[arvados.git] / sdk / R / R / CollectionTree.R
index 2d4af094aa1c96b98b585cba059085c95b537f8c..5f7a29455ae4a58aaae6792f6dd1eb26ae30ae4e 100644 (file)
@@ -15,55 +15,16 @@ CollectionTree <- R6::R6Class(
         initialize = function(fileContent, collection)
         {
             self$pathsList <- fileContent
-
-            treeBranches <- sapply(fileContent, function(filePath)
-            {
-                splitPath <- unlist(strsplit(filePath, "/", fixed = TRUE))
-                branch <- private$createBranch(splitPath)
-            })
-
+            treeBranches <- sapply(fileContent, function(filePath) self$createBranch(filePath))
             root <- Subcollection$new("")
-
-            sapply(treeBranches, function(branch)
-            {
-                private$addBranch(root, branch)
-            })
-
+            sapply(treeBranches, function(branch) self$addBranch(root, branch))
             root$setCollection(collection)
             private$tree <- root
         },
 
-        getElement = function(relativePath)
-        {
-            relativePath <- trimFromStart(relativePath, "./")
-            relativePath <- trimFromEnd(relativePath, "/")
-
-            if(endsWith(relativePath, "/"))
-                relativePath <- substr(relativePath, 0, nchar(relativePath) - 1)
-
-            splitPath <- unlist(strsplit(relativePath, "/", fixed = TRUE))
-            returnElement <- private$tree
-
-            for(pathFragment in splitPath)
-            {
-                returnElement <- returnElement$get(pathFragment)
-
-                if(is.null(returnElement))
-                    return(NULL)
-            }
-
-            returnElement
-        },
-
-        getTree = function() private$tree
-    ),
-
-    private = list(
-
-        tree = NULL,
-
-        createBranch = function(splitPath)
+        createBranch = function(filePath)
         {
+            splitPath <- unlist(strsplit(filePath, "/", fixed = TRUE))
             branch <- NULL
             lastElementIndex <- length(splitPath)
 
@@ -90,7 +51,11 @@ CollectionTree <- R6::R6Class(
 
             if(is.null(child))
             {
+                # Make sure we are don't make any REST call while adding child
+                collection <- container$getCollection()
+                container$setCollection(NULL, setRecursively = FALSE)
                 container$add(node)
+                container$setCollection(collection, setRecursively = FALSE)
             }
             else
             {
@@ -100,14 +65,41 @@ CollectionTree <- R6::R6Class(
                 # If we encounter that same name again we know
                 # it's a folder so we need to replace ArvadosFile with Subcollection.
                 if("ArvadosFile" %in% class(child))
-                {
                     child = private$replaceFileWithSubcollection(child)
-                }
 
-                private$addBranch(child, node$getFirst())
+                self$addBranch(child, node$getFirst())
             }
         },
 
+        getElement = function(relativePath)
+        {
+            relativePath <- trimFromStart(relativePath, "./")
+            relativePath <- trimFromEnd(relativePath, "/")
+
+            if(endsWith(relativePath, "/"))
+                relativePath <- substr(relativePath, 0, nchar(relativePath) - 1)
+
+            splitPath <- unlist(strsplit(relativePath, "/", fixed = TRUE))
+            returnElement <- private$tree
+
+            for(pathFragment in splitPath)
+            {
+                returnElement <- returnElement$get(pathFragment)
+
+                if(is.null(returnElement))
+                    return(NULL)
+            }
+
+            returnElement
+        },
+
+        getTree = function() private$tree
+    ),
+
+    private = list(
+
+        tree = NULL,
+
         replaceFileWithSubcollection = function(arvadosFile)
         {
             subcollection <- Subcollection$new(arvadosFile$getName())
@@ -121,24 +113,3 @@ CollectionTree <- R6::R6Class(
         }
     )
 )
-
-# deepCopyArvadosComposite = function(composite) 
-# {
-    # if("ArvadosFile" %in% class(content))
-    # {
-        # newFile <- ArvadosFile$new(content$name)
-        # newFile$setCollection(content$getCollection())
-
-        # return(newFile)
-    # }
-    # else if("Subcollection" %in% class(content))
-    # {
-        # root <- Subcollection$new(content$name)
-        # root$setCollection(content$getCollection())
-    # }
-    # else
-        # stop("Arvados composite is corrupted. It can contain only ArvadosFile or Subcollection.")
-    
-    
-
-# }