Merge branch '13453-r-sdk-incorrect-rest-call-fix'
[arvados.git] / sdk / R / R / CollectionTree.R
index 82c6eb8f9e16011a2b82c753e2e672ed15387d0e..91e4ec86459dc8e4ad8891d59cbdb80d771a4013 100644 (file)
@@ -1,13 +1,7 @@
 source("./R/Subcollection.R")
-
 source("./R/ArvadosFile.R")
+source("./R/util.R")
 
-#' Arvados Collection Object
-#'
-#' Update description
-#'
-#' @examples arv = Collection$new(api, uuid)
-#' @export CollectionTree
 CollectionTree <- R6::R6Class(
     "CollectionTree",
     public = list(
@@ -21,7 +15,7 @@ CollectionTree <- R6::R6Class(
             treeBranches <- sapply(fileContent, function(filePath)
             {
                 splitPath <- unlist(strsplit(filePath, "/", fixed = TRUE))
-                branch = private$createBranch(splitPath)      
+                branch <- private$createBranch(splitPath)      
             })
 
             root <- Subcollection$new("")
@@ -31,28 +25,33 @@ CollectionTree <- R6::R6Class(
                 private$addBranch(root, branch)
             })
 
-            root$.__enclos_env__$private$addToCollection(collection)
+            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
+            returnElement <- private$tree
 
             for(pathFragment in splitPath)
             {
-                returnElement = returnElement$.__enclos_env__$private$getChild(pathFragment)
+                returnElement <- returnElement$get(pathFragment)
 
                 if(is.null(returnElement))
                     return(NULL)
             }
 
             returnElement
-        }
+        },
+
+        getTree = function() private$tree
     ),
 
     private = list(
@@ -68,13 +67,13 @@ CollectionTree <- R6::R6Class(
             {
                 if(elementIndex == lastElementIndex)
                 {
-                    branch = ArvadosFile$new(splitPath[[elementIndex]])
+                    branch <- ArvadosFile$new(splitPath[[elementIndex]])
                 }
                 else
                 {
-                    newFolder = Subcollection$new(splitPath[[elementIndex]])
+                    newFolder <- Subcollection$new(splitPath[[elementIndex]])
                     newFolder$add(branch)
-                    branch = newFolder
+                    branch <- newFolder
                 }
             }
             
@@ -83,32 +82,36 @@ CollectionTree <- R6::R6Class(
 
         addBranch = function(container, node)
         {
-            child = container$.__enclos_env__$private$getChild(node$getName())
+            child <- container$get(node$getName())
 
             if(is.null(child))
             {
                 container$add(node)
-                #todo add it to collection
             }
             else
             {
+                # Note: REST always returns folder name alone before other folder 
+                # content, so in first iteration we don't know if it's a file
+                # or folder since its just a name, so we assume it's a file. 
+                # 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$.__enclos_env__$private$getFirstChild())
+                private$addBranch(child, node$getFirst())
             }
         },
 
         replaceFileWithSubcollection = function(arvadosFile)
         {
             subcollection <- Subcollection$new(arvadosFile$getName())
-            fileParent <- arvadosFile$.__enclos_env__$private$parent
-            fileParent$.__enclos_env__$private$removeChild(arvadosFile$getName())
+            fileParent <- arvadosFile$getParent()
+            fileParent$remove(arvadosFile$getName())
             fileParent$add(subcollection)
 
-            arvadosFile$.__enclos_env__$private$parent <- NULL
+            arvadosFile$setParent(NULL)
 
             subcollection
         }