Methods listCollections and listProjects now work properly
[arvados.git] / sdk / R / R / Subcollection.R
index b453c535ada4ebf786bb770dc6c5f11017923fab..62c62d457e519434a7aa32543a9267ae6172ad59 100644 (file)
@@ -24,19 +24,29 @@ Subcollection <- R6::R6Class(
                 if(!is.null(content$.__enclos_env__$private$collection))
                     stop("ArvadosFile/Subcollection already belongs to a collection.")
 
+                childWithSameName <- private$getChild(content$getName())
+                if(!is.null(childWithSameName))
+                    stop("Subcollection already contains ArvadosFile
+                          or Subcollection with same name.")
+
                 if(!is.null(private$collection))
                 {       
-                    contentPath <- paste0(self$getRelativePath(), "/", content$getFileList())
+                    contentPath <- paste0(self$getRelativePath(),
+                                          "/", content$getFileList())
+
                     private$collection$.__enclos_env__$private$createFilesOnREST(contentPath)
                     content$.__enclos_env__$private$addToCollection(private$collection)
                 }
 
                 private$children <- c(private$children, content)
                 content$.__enclos_env__$private$parent = self
+
+                "Content added successfully."
             }
             else
             {
-                stop("Expected AravodsFile or Subcollection object, got ...")
+                stop(paste("Expected AravodsFile or Subcollection object, got",
+                           class(content), "."))
             }
         },
 
@@ -45,16 +55,20 @@ Subcollection <- R6::R6Class(
             if(is.null(private$collection))
                 stop("Subcollection doesn't belong to any collection.")
 
-            collectionList <- paste0(self$getRelativePath(), "/", self$getFileList(fullpath = FALSE))
+            if(private$name == "")
+                stop("Unable to delete root folder.")
+
+            collectionList <- paste0(self$getRelativePath(),
+                                     "/", self$getFileList(fullpath = FALSE))
             sapply(collectionList, function(file)
             {
                 private$collection$.__enclos_env__$private$deleteFromREST(file)
             })
 
-            #todo rename this add to a collection
             private$addToCollection(NULL)
-            private$detachFromParent()
+            private$dettachFromParent()
 
+            "Content removed successfully."
         },
 
         getFileList = function(fullpath = TRUE)
@@ -80,7 +94,8 @@ Subcollection <- R6::R6Class(
 
         getSizeInBytes = function()
         {
-            collectionURL <- URLencode(paste0(private$collection$api$getWebDavHostName(), "c=", private$collection$uuid))
+            collectionURL <- URLencode(paste0(private$collection$api$getWebDavHostName(),
+                                              "c=", private$collection$uuid))
             subcollectionURL <- paste0(collectionURL, "/", self$getRelativePath(), "/");
 
             headers = list("Authorization" = paste("OAuth2", private$collection$api$getToken()))
@@ -100,7 +115,6 @@ Subcollection <- R6::R6Class(
             relativePath <- c(private$name)
             parent <- private$parent
 
-            #Recurse back to root
             while(!is.null(parent))
             {
                 relativePath <- c(parent$getName(), relativePath)
@@ -111,6 +125,41 @@ Subcollection <- R6::R6Class(
             paste0(relativePath, collapse = "/")
         },
 
+        move = function(newLocation)
+        {
+            if(is.null(private$collection))
+                stop("Subcollection doesn't belong to any collection.")
+
+            if(endsWith(newLocation, paste0(private$name, "/")))
+            {
+                newLocation <- substr(newLocation, 0,
+                                      nchar(newLocation) - nchar(paste0(private$name, "/")))
+            }
+            else if(endsWith(newLocation, private$name))
+            {
+                newLocation <- substr(newLocation, 0,
+                                      nchar(newLocation) - nchar(private$name))
+            }
+            else
+            {
+                stop("Destination path is not valid.")
+            }
+
+            newParent <- private$collection$get(newLocation)
+
+            if(is.null(newParent))
+            {
+                stop("Unable to get destination subcollection.")
+            }
+
+            status <- private$collection$.__enclos_env__$private$moveOnREST(self$getRelativePath(),
+                                                                            paste0(newParent$getRelativePath(), "/", self$getName()))
+
+            private$attachToParent(newParent)
+
+            "Content moved successfully."
+        },
+
         getParent = function() private$parent
     ),
 
@@ -166,13 +215,24 @@ Subcollection <- R6::R6Class(
             private$collection = collection
         },
 
-        detachFromParent = function()
+        dettachFromParent = function()
         {
             if(!is.null(private$parent))
             {
                 private$parent$.__enclos_env__$private$removeChild(private$name)
                 private$parent <- NULL
             }
+            else
+                stop("Parent doesn't exists.")
+        },
+
+        attachToParent = function(parent)
+        {
+            if(private$name != "")
+            {
+                parent$.__enclos_env__$private$children <- c(parent$.__enclos_env__$private$children, self)
+                private$parent <- parent
+            }
         }
     ),