Improve Collections create and move methods and update documentation
[arvados.git] / sdk / R / R / Subcollection.R
index a1fba1a4adf912f896fedbf26f152ca6b9ee269c..17a9ef3ee3ba6180546763da637a8824905d66dc 100644 (file)
@@ -1,8 +1,51 @@
-#' Arvados SubCollection Object
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: Apache-2.0
+
+source("./R/util.R")
+
+#' Subcollection
+#'
+#' Subcollection class represents a folder inside Arvados collection.
+#' It is essentially a composite of arvadosFiles and other subcollections.
+#'
+#' @section Usage:
+#' \preformatted{subcollection = Subcollection$new(name)}
+#'
+#' @section Arguments:
+#' \describe{
+#'   \item{name}{Name of the subcollection.}
+#' }
 #'
-#' Update description
+#' @section Methods:
+#' \describe{
+#'   \item{getName()}{Returns name of the subcollection.}
+#'   \item{getRelativePath()}{Returns subcollection path relative to the root.}
+#'   \item{add(content)}{Adds ArvadosFile or Subcollection specified by content to the subcollection.}
+#'   \item{remove(name)}{Removes ArvadosFile or Subcollection specified by name from the subcollection.}
+#'   \item{get(relativePath)}{If relativePath is valid, returns ArvadosFile or Subcollection specified by relativePath, else returns NULL.}
+#'   \item{getFileListing()}{Returns subcollections file content as character vector.}
+#'   \item{getSizeInBytes()}{Returns subcollections content size in bytes.}
+#'   \item{move(destination)}{Moves subcollection to a new location inside collection.}
+#'   \item{copy(destination)}{Copies subcollection to a new location inside collection.}
+#' }
 #'
-#' @export Subcollection
+#' @name Subcollection
+#' @examples
+#' \dontrun{
+#' myFolder <- Subcollection$new("myFolder")
+#' myFile   <- ArvadosFile$new("myFile")
+#'
+#' myFolder$add(myFile)
+#' myFolder$get("myFile")
+#' myFolder$remove("myFile")
+#'
+#' myFolder$move("newLocation/myFolder")
+#' myFolder$copy("newLocation/myFolder")
+#' }
+NULL
+
+#' @export
 Subcollection <- R6::R6Class(
 
     "Subcollection",
@@ -11,149 +54,192 @@ Subcollection <- R6::R6Class(
 
         initialize = function(name)
         {
-            private$name       <- name
-            private$http       <- HttpRequest$new()
-            private$httpParser <- HttpParser$new()
+            private$name <- name
         },
-        
+
+        getName = function() private$name,
+
+        getRelativePath = function()
+        {
+            relativePath <- c(private$name)
+            parent <- private$parent
+
+            while(!is.null(parent))
+            {
+                relativePath <- c(parent$getName(), relativePath)
+                parent <- parent$getParent()
+            }
+
+            relativePath <- relativePath[relativePath != ""]
+            paste0(relativePath, collapse = "/")
+        },
+
         add = function(content)
         {
             if("ArvadosFile"   %in% class(content) ||
                "Subcollection" %in% class(content))
             {
-                if(!is.null(content$.__enclos_env__$private$collection))
-                    stop("ArvadosFile/Subcollection already belongs to a collection.")
+                if(!is.null(content$getCollection()))
+                    stop("Content already belongs to a collection.")
+
+                if(content$getName() == "")
+                    stop("Content has invalid name.")
+
+                childWithSameName <- self$get(content$getName())
+
+                if(!is.null(childWithSameName))
+                    stop(paste("Subcollection already contains ArvadosFile",
+                               "or Subcollection with same name."))
 
                 if(!is.null(private$collection))
-                {       
-                    contentPath <- paste0(self$getRelativePath(), "/", content$getFileList())
-                    private$collection$.__enclos_env__$private$createFilesOnREST(contentPath)
-                    content$.__enclos_env__$private$addToCollection(private$collection)
+                {
+                    if(self$getRelativePath() != "")
+                        contentPath <- paste0(self$getRelativePath(),
+                                              "/", content$getFileListing())
+                    else
+                        contentPath <- content$getFileListing()
+
+                    REST <- private$collection$getRESTService()
+                    REST$create(contentPath, private$collection$uuid)
+                    content$setCollection(private$collection)
                 }
 
                 private$children <- c(private$children, content)
-                content$.__enclos_env__$private$parent = self
+                content$setParent(self)
+
+                "Content added successfully."
             }
             else
             {
-                stop("Expected AravodsFile or Subcollection object, got ...")
+                stop(paste0("Expected AravodsFile or Subcollection object, got ",
+                            paste0("(", paste0(class(content), collapse = ", "), ")"),
+                            "."))
             }
         },
 
-        removeFromCollection = function()
+        remove = function(name)
         {
-            if(is.null(private$collection))
-                stop("Subcollection doesn't belong to any collection.")
-
-            if(self$name == "")
-                stop("Unable to delete root folder.")
-
-            collectionList <- paste0(self$getRelativePath(), "/", self$getFileList(fullpath = FALSE))
-            sapply(collectionList, function(file)
+            if(is.character(name))
             {
-                private$collection$.__enclos_env__$private$deleteFromREST(file)
-            })
+                child <- self$get(name)
 
-            private$addToCollection(NULL)
-            private$dettachFromParent()
-        },
+                if(is.null(child))
+                    stop(paste("Subcollection doesn't contains ArvadosFile",
+                               "or Subcollection with specified name."))
 
-        getFileList = function(fullpath = TRUE)
-        {
-            content <- NULL
+                if(!is.null(private$collection))
+                {
+                    REST <- private$collection$getRESTService()
+                    REST$delete(child$getRelativePath(), private$collection$uuid)
 
-            if(fullpath)
-            {
-                for(child in private$children)
-                    content <- c(content, child$getFileList())
+                    child$setCollection(NULL)
+                }
 
-                if(private$name != "")
-                    content <- unlist(paste0(private$name, "/", content))
+                private$removeChild(name)
+                child$setParent(NULL)
+
+                "Content removed"
             }
             else
             {
-                for(child in private$children)
-                    content <- c(content, child$getName())
+                stop(paste0("Expected character, got ",
+                            paste0("(", paste0(class(name), collapse = ", "), ")"),
+                            "."))
             }
+        },
 
-            content
+        getFileListing = function(fullPath = TRUE)
+        {
+            content <- private$getContentAsCharVector(fullPath)
+            content[order(tolower(content))]
         },
 
         getSizeInBytes = function()
         {
-            collectionURL <- URLencode(paste0(private$collection$api$getWebDavHostName(), "c=", private$collection$uuid))
-            subcollectionURL <- paste0(collectionURL, "/", self$getRelativePath(), "/");
+            if(is.null(private$collection))
+                return(0)
 
-            headers = list("Authorization" = paste("OAuth2", private$collection$api$getToken()))
+            REST <- private$collection$getRESTService()
 
-            propfindResponse <- private$http$PROPFIND(subcollectionURL, headers)
+            fileSizes <- REST$getResourceSize(paste0(self$getRelativePath(), "/"),
+                                              private$collection$uuid)
+            return(sum(fileSizes))
+        },
 
-            sizes <- private$httpParser$extractFileSizeFromWebDAVResponse(propfindResponse, collectionURL)
-            sizes <- as.numeric(sizes[-1])
+        move = function(destination)
+        {
+            if(is.null(private$collection))
+                stop("Subcollection doesn't belong to any collection.")
 
-            sum(sizes)
-        },
+            destination <- trimFromEnd(destination, "/")
+            nameAndPath <- splitToPathAndName(destination)
 
-        getName = function() private$name,
+            newParent <- private$collection$get(nameAndPath$path)
 
-        getRelativePath = function()
-        {
-            relativePath <- c(private$name)
-            parent <- private$parent
+            if(is.null(newParent))
+                stop("Unable to get destination subcollection.")
 
-            #Recurse back to root
-            while(!is.null(parent))
-            {
-                relativePath <- c(parent$getName(), relativePath)
-                parent <- parent$getParent()
-            }
+            childWithSameName <- newParent$get(nameAndPath$name)
 
-            relativePath <- relativePath[relativePath != ""]
-            paste0(relativePath, collapse = "/")
+            if(!is.null(childWithSameName))
+                stop("Destination already contains content with same name.")
+
+            REST <- private$collection$getRESTService()
+            REST$move(self$getRelativePath(),
+                      paste0(newParent$getRelativePath(), "/", nameAndPath$name),
+                      private$collection$uuid)
+
+            private$dettachFromCurrentParent()
+            private$attachToNewParent(self, newParent)
+
+            private$parent <- newParent
+            private$name <- nameAndPath$name
+
+            self
         },
 
-        move = function(newLocation)
+        copy = function(destination)
         {
-            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.")
-            }
+            if(is.null(private$collection))
+                stop("Subcollection doesn't belong to any collection.")
 
-            newParent <- private$collection$get(newLocation)
+            destination <- trimFromEnd(destination, "/")
+            nameAndPath <- splitToPathAndName(destination)
 
-            if(is.null(newParent))
-            {
-                stop("Unable to get destination subcollectin")
-            }
+            newParent <- private$collection$get(nameAndPath$path)
 
-            status <- private$collection$.__enclos_env__$private$moveOnRest(self$getRelativePath(), paste0(newParent$getRelativePath(), "/", self$getName()))
+            if(is.null(newParent) || !("Subcollection" %in% class(newParent)))
+                stop("Unable to get destination subcollection.")
 
-            private$attachToParent(newParent)
+            childWithSameName <- newParent$get(nameAndPath$name)
 
-            paste("Status code :", status$status_code)
-        },
+            if(!is.null(childWithSameName))
+                stop("Destination already contains content with same name.")
 
-        getParent = function() private$parent
-    ),
+            REST <- private$collection$getRESTService()
+            REST$copy(self$getRelativePath(),
+                      paste0(newParent$getRelativePath(), "/", nameAndPath$name),
+                      private$collection$uuid)
 
-    private = list(
+            newContent <- self$duplicate(nameAndPath$name)
+            newContent$setCollection(self$getCollection(), setRecursively = TRUE)
+            newContent$setParent(newParent)
+            private$attachToNewParent(newContent, newParent)
 
-        name       = NULL,
-        children   = NULL,
-        parent     = NULL,
-        collection = NULL,
-        http       = NULL,
-        httpParser = NULL,
+            newContent
+        },
 
-        getChild = function(name)
+        duplicate = function(newName = NULL)
+        {
+            name <- if(!is.null(newName)) newName else private$name
+            root <- Subcollection$new(name)
+            for(child in private$children)
+                root$add(child$duplicate())
+
+            root
+        },
+
+        get = function(name)
         {
             for(child in private$children)
             {
@@ -164,7 +250,7 @@ Subcollection <- R6::R6Class(
             return(NULL)
         },
 
-        getFirstChild = function()
+        getFirst = function()
         {
             if(length(private$children) == 0)
                return(NULL)
@@ -172,6 +258,31 @@ Subcollection <- R6::R6Class(
             private$children[[1]]
         },
 
+        setCollection = function(collection, setRecursively = TRUE)
+        {
+            private$collection = collection
+
+            if(setRecursively)
+            {
+                for(child in private$children)
+                    child$setCollection(collection)
+            }
+        },
+
+        getCollection = function() private$collection,
+
+        getParent = function() private$parent,
+
+        setParent = function(newParent) private$parent <- newParent
+    ),
+
+    private = list(
+
+        name       = NULL,
+        children   = NULL,
+        parent     = NULL,
+        collection = NULL,
+
         removeChild = function(name)
         {
             numberOfChildren = length(private$children)
@@ -188,31 +299,79 @@ Subcollection <- R6::R6Class(
             }
         },
 
-        addToCollection = function(collection)
+        attachToNewParent = function(content, newParent)
         {
-            for(child in private$children)
-                child$.__enclos_env__$private$addToCollection(collection)
+            # We temporary set parents collection to NULL. This will ensure that
+            # add method doesn't post this subcollection to REST.
+            # We also need to set content's collection to NULL because
+            # add method throws exception if we try to add content that already
+            # belongs to a collection.
+            parentsCollection <- newParent$getCollection()
+            content$setCollection(NULL, setRecursively = FALSE)
+            newParent$setCollection(NULL, setRecursively = FALSE)
+            newParent$add(content)
+            content$setCollection(parentsCollection, setRecursively = FALSE)
+            newParent$setCollection(parentsCollection, setRecursively = FALSE)
+        },
 
-            private$collection = collection
+        dettachFromCurrentParent = function()
+        {
+            # We temporary set parents collection to NULL. This will ensure that
+            # remove method doesn't remove this subcollection from REST.
+            parent <- private$parent
+            parentsCollection <- parent$getCollection()
+            parent$setCollection(NULL, setRecursively = FALSE)
+            parent$remove(private$name)
+            parent$setCollection(parentsCollection, setRecursively = FALSE)
         },
 
-        dettachFromParent = function()
+        getContentAsCharVector = function(fullPath = TRUE)
         {
-            if(!is.null(private$parent))
+            content <- NULL
+
+            if(fullPath)
             {
-                private$parent$.__enclos_env__$private$removeChild(private$name)
-                private$parent <- NULL
+                for(child in private$children)
+                    content <- c(content, child$getFileListing())
+
+                if(private$name != "")
+                    content <- unlist(paste0(private$name, "/", content))
             }
             else
-                stop("Parent doesn't exists.")
-        },
+            {
+                for(child in private$children)
+                    content <- c(content, child$getName())
+            }
 
-        attachToParent = function(parent)
-        {
-            parent$.__enclos_env__$private$children <- c(parent$.__enclos_env__$private$children, self)
-            private$parent <- parent
+            content
         }
     ),
-    
+
     cloneable = FALSE
 )
+
+#' print.Subcollection
+#'
+#' Custom print function for Subcollection class
+#'
+#' @param x Instance of Subcollection class
+#' @param ... Optional arguments.
+#' @export
+print.Subcollection = function(x, ...)
+{
+    collection   <- NULL
+    relativePath <- x$getRelativePath()
+
+    if(!is.null(x$getCollection()))
+    {
+        collection <- x$getCollection()$uuid
+
+        if(!x$getName() == "")
+            relativePath <- paste0("/", relativePath)
+    }
+
+    cat(paste0("Type:          ", "\"", "Arvados Subcollection", "\""), sep = "\n")
+    cat(paste0("Name:          ", "\"", x$getName(),             "\""), sep = "\n")
+    cat(paste0("Relative path: ", "\"", relativePath,            "\""), sep = "\n")
+    cat(paste0("Collection:    ", "\"", collection,              "\""), sep = "\n")
+}