Implement copy method, update move method and remove trailing
[arvados.git] / sdk / R / R / Collection.R
index ed5b4f4b968d0b9a9b1b570682af587942c29ea2..f88eb0e7cdfd36a097cf79494a4d298b5d199d35 100644 (file)
@@ -1,12 +1,16 @@
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: Apache-2.0
+
 source("./R/Subcollection.R")
 source("./R/ArvadosFile.R")
 source("./R/RESTService.R")
 source("./R/util.R")
 
 #' Collection
-#' 
+#'
 #' Collection class provides interface for working with Arvados collections.
-#' 
+#'
 #' @section Usage:
 #' \preformatted{collection = Collection$new(arv, uuid)}
 #'
@@ -15,7 +19,7 @@ source("./R/util.R")
 #'   \item{arv}{Arvados object.}
 #'   \item{uuid}{UUID of a collection.}
 #' }
-#' 
+#'
 #' @section Methods:
 #' \describe{
 #'   \item{add(content)}{Adds ArvadosFile or Subcollection specified by content to the collection.}
@@ -53,22 +57,19 @@ Collection <- R6::R6Class(
 
     public = list(
 
-        api  = NULL,
-        uuid = NULL,
+               uuid = NULL,
 
-        initialize = function(api, uuid)
+               initialize = function(api, uuid)
         {
-            self$api <- api
             private$REST <- api$getRESTService()
-
             self$uuid <- uuid
-
-            private$fileContent <- private$REST$getCollectionContent(uuid)
-            private$tree <- CollectionTree$new(private$fileContent, self)
         },
 
         add = function(content, relativePath = "")
         {
+            if(is.null(private$tree))
+                private$generateCollectionTreeStructure()
+
             if(relativePath == ""  ||
                relativePath == "." ||
                relativePath == "./")
@@ -87,7 +88,6 @@ Collection <- R6::R6Class(
             if("ArvadosFile"   %in% class(content) ||
                "Subcollection" %in% class(content))
             {
-
                 if(content$getName() == "")
                     stop("Content has invalid name.")
 
@@ -104,6 +104,9 @@ Collection <- R6::R6Class(
 
         create = function(fileNames, relativePath = "")
         {
+            if(is.null(private$tree))
+                private$generateCollectionTreeStructure()
+
             if(relativePath == ""  ||
                relativePath == "." ||
                relativePath == "./")
@@ -112,7 +115,7 @@ Collection <- R6::R6Class(
             }
             else
             {
-                relativePath  <- trimFromEnd(relativePath, "/") 
+                relativePath  <- trimFromEnd(relativePath, "/")
                 subcollection <- self$get(relativePath)
             }
 
@@ -139,7 +142,7 @@ Collection <- R6::R6Class(
                 else
                     return(arvadosFiles)
             }
-            else 
+            else
             {
                 stop(paste0("Expected character vector, got ",
                             paste0("(", paste0(class(fileNames), collapse = ", "), ")"),
@@ -149,6 +152,9 @@ Collection <- R6::R6Class(
 
         remove = function(paths)
         {
+            if(is.null(private$tree))
+                private$generateCollectionTreeStructure()
+
             if(is.character(paths))
             {
                 sapply(paths, function(filePath)
@@ -169,7 +175,7 @@ Collection <- R6::R6Class(
 
                 "Content removed"
             }
-            else 
+            else
             {
                 stop(paste0("Expected character vector, got ",
                             paste0("(", paste0(class(paths), collapse = ", "), ")"),
@@ -177,8 +183,11 @@ Collection <- R6::R6Class(
             }
         },
 
-        move = function(content, newLocation)
+        move = function(content, destination)
         {
+            if(is.null(private$tree))
+                private$generateCollectionTreeStructure()
+
             content <- trimFromEnd(content, "/")
 
             elementToMove <- self$get(content)
@@ -186,17 +195,44 @@ Collection <- R6::R6Class(
             if(is.null(elementToMove))
                 stop("Content you want to move doesn't exist in the collection.")
 
-            elementToMove$move(newLocation)
+            elementToMove$move(destination)
+        },
+
+        copy = function(content, destination)
+        {
+            if(is.null(private$tree))
+                private$generateCollectionTreeStructure()
+
+            content <- trimFromEnd(content, "/")
+
+            elementToCopy <- self$get(content)
+
+            if(is.null(elementToCopy))
+                stop("Content you want to copy doesn't exist in the collection.")
+
+            elementToCopy$copy(destination)
+        },
+
+        refresh = function()
+        {
+            private$tree$getTree()$setCollection(NULL, setRecursively = TRUE)
+            private$tree <- NULL
         },
 
         getFileListing = function()
         {
+            if(is.null(private$tree))
+                private$generateCollectionTreeStructure()
+
             content <- private$REST$getCollectionContent(self$uuid)
             content[order(tolower(content))]
         },
 
         get = function(relativePath)
         {
+            if(is.null(private$tree))
+                private$generateCollectionTreeStructure()
+
             private$tree$getElement(relativePath)
         },
 
@@ -208,7 +244,19 @@ Collection <- R6::R6Class(
 
         REST        = NULL,
         tree        = NULL,
-        fileContent = NULL
+        fileContent = NULL,
+
+        generateCollectionTreeStructure = function()
+        {
+            if(is.null(self$uuid))
+                stop("Collection uuid is not defined.")
+
+            if(is.null(private$REST))
+                stop("REST service is not defined.")
+
+            private$fileContent <- private$REST$getCollectionContent(self$uuid)
+            private$tree <- CollectionTree$new(private$fileContent, self)
+        }
     ),
 
     cloneable = FALSE
@@ -220,7 +268,7 @@ Collection <- R6::R6Class(
 #'
 #' @param x Instance of Collection class
 #' @param ... Optional arguments.
-#' @export 
+#' @export
 print.Collection = function(x, ...)
 {
     cat(paste0("Type: ", "\"", "Arvados Collection", "\""), sep = "\n")