Implemented custom print functions for all public classes
[arvados.git] / sdk / R / R / Subcollection.R
index 06df7c48767280f601dcbd30ceec9558adfccdd5..7eb4381edaa151b2ef1053a78c039dfd7af4150d 100644 (file)
@@ -1,3 +1,5 @@
+source("./R/util.R")
+
 #' Arvados SubCollection Object
 #'
 #' Update description
@@ -11,9 +13,7 @@ Subcollection <- R6::R6Class(
 
         initialize = function(name)
         {
-            private$name       <- name
-            private$http       <- HttpRequest$new()
-            private$httpParser <- HttpParser$new()
+            private$name <- name
         },
 
         getName = function() private$name,
@@ -40,8 +40,8 @@ Subcollection <- R6::R6Class(
             {
                 childWithSameName <- self$get(content$getName())
                 if(!is.null(childWithSameName))
-                    stop("Subcollection already contains ArvadosFile
-                          or Subcollection with same name.")
+                    stop(paste("Subcollection already contains ArvadosFile",
+                               "or Subcollection with same name."))
 
                 if(!is.null(private$collection))
                 {       
@@ -51,7 +51,8 @@ Subcollection <- R6::R6Class(
                     else
                         contentPath <- content$getFileListing()
 
-                    private$collection$createFilesOnREST(contentPath)
+                    REST <- private$collection$getRESTService()
+                    REST$create(contentPath, private$collection$uuid)
                     content$setCollection(private$collection)
                 }
 
@@ -62,8 +63,9 @@ Subcollection <- R6::R6Class(
             }
             else
             {
-                stop(paste("Expected AravodsFile or Subcollection object, got",
-                           class(content), "."))
+                stop(paste0("Expected AravodsFile or Subcollection object, got ",
+                            paste0("(", paste0(class(content), collapse = ", "), ")"),
+                            "."))
             }
         },
 
@@ -74,12 +76,13 @@ Subcollection <- R6::R6Class(
                 child <- self$get(name)
 
                 if(is.null(child))
-                    stop("Subcollection doesn't contains ArvadosFile
-                          or Subcollection with same name.")
+                    stop(paste("Subcollection doesn't contains ArvadosFile",
+                               "or Subcollection with specified name."))
 
                 if(!is.null(private$collection))
                 {
-                    private$collection$deleteFromREST(child$getRelativePath())
+                    REST <- private$collection$getRESTService()
+                    REST$delete(child$getRelativePath(), private$collection$uuid)
                     child$setCollection(NULL)
                 }
 
@@ -90,88 +93,60 @@ Subcollection <- R6::R6Class(
             }
             else
             {
-                stop(paste("Expected character, got", class(content), "."))
+                stop(paste0("Expected character, got ",
+                            paste0("(", paste0(class(name), collapse = ", "), ")"),
+                            "."))
             }
         },
 
-        getFileListing = function(fullpath = TRUE)
+        getFileListing = function(fullPath = TRUE)
         {
-            content <- NULL
-
-            if(fullpath)
-            {
-                for(child in private$children)
-                    content <- c(content, child$getFileListing())
-
-                if(private$name != "")
-                    content <- unlist(paste0(private$name, "/", content))
-            }
-            else
-            {
-                for(child in private$children)
-                    content <- c(content, child$getName())
-            }
+            content <- private$getContentAsCharVector(fullPath)
 
-            content
+            content[order(tolower(content))]
         },
 
         getSizeInBytes = function()
         {
-            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()))
-
-            propfindResponse <- private$http$PROPFIND(subcollectionURL, headers)
+            if(is.null(private$collection))
+                return(0)
 
-            sizes <- private$httpParser$extractFileSizeFromWebDAVResponse(propfindResponse, collectionURL)
-            sizes <- as.numeric(sizes[-1])
+            REST <- private$collection$getRESTService()
 
-            sum(sizes)
+            fileSizes <- REST$getResourceSize(paste0(self$getRelativePath(), "/"),
+                                              private$collection$uuid)
+            return(sum(fileSizes))
         },
 
         move = function(newLocation)
         {
             if(is.null(private$collection))
-                stop("Subcollection doesn't belong to any 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.")
-            }
+            newLocation <- trimFromEnd(newLocation, "/")
+            nameAndPath <- splitToPathAndName(newLocation)
 
-            newParent <- private$collection$get(newLocation)
+            newParent <- private$collection$get(nameAndPath$path)
 
             if(is.null(newParent))
             {
-                stop("Unable to get destination subcollection.")
+                stop("Unable to get destination subcollection")
             }
 
-            status <- private$collection$moveOnREST(self$getRelativePath(),
-                                                    paste0(newParent$getRelativePath(),
-                                                           "/", self$getName()))
+            childWithSameName <- newParent$get(nameAndPath$name)
 
-            #Note: We temporary set parents collection to NULL. This will ensure that
-            #      add method doesn't post file on REST server.
-            parentsCollection <- newParent$getCollection()
-            newParent$setCollection(NULL, setRecursively = FALSE)
+            if(!is.null(childWithSameName))
+                stop("Destination already contains content with same name.")
 
-            newParent$add(self)
+            REST <- private$collection$getRESTService()
+            REST$move(self$getRelativePath(),
+                      paste0(newParent$getRelativePath(), "/", nameAndPath$name),
+                      private$collection$uuid)
 
-            newParent$setCollection(parentsCollection, setRecursively = FALSE)
+            private$dettachFromCurrentParent()
+            private$attachToNewParent(newParent)
 
-            private$parent <- newParent
+            private$name <- nameAndPath$name
 
             "Content moved successfully."
         },
@@ -219,8 +194,6 @@ Subcollection <- R6::R6Class(
         children   = NULL,
         parent     = NULL,
         collection = NULL,
-        http       = NULL,
-        httpParser = NULL,
 
         removeChild = function(name)
         {
@@ -236,8 +209,77 @@ Subcollection <- R6::R6Class(
                     }
                 }
             }
+        },
+
+        attachToNewParent = function(newParent)
+        {
+            #Note: We temporary set parents collection to NULL. This will ensure that
+            #      add method doesn't post file on REST.
+            parentsCollection <- newParent$getCollection()
+            newParent$setCollection(NULL, setRecursively = FALSE)
+
+            newParent$add(self)
+
+            newParent$setCollection(parentsCollection, setRecursively = FALSE)
+
+            private$parent <- newParent
+        },
+
+        dettachFromCurrentParent = function()
+        {
+            #Note: 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)
+        },
+
+        getContentAsCharVector = function(fullPath = TRUE)
+        {
+            content <- NULL
+
+            if(fullPath)
+            {
+                for(child in private$children)
+                    content <- c(content, child$getFileListing())
+
+                if(private$name != "")
+                    content <- unlist(paste0(private$name, "/", content))
+            }
+            else
+            {
+                for(child in private$children)
+                    content <- c(content, child$getName())
+            }
+
+            content
+
         }
     ),
     
     cloneable = FALSE
 )
+
+#' @export print.Subcollection
+print.Subcollection = function(subCollection)
+{
+    collection   <- NULL
+    relativePath <- subCollection$getRelativePath()
+
+    if(!is.null(subCollection$getCollection()))
+    {
+        collection <- subCollection$getCollection()$uuid
+
+        if(!subCollection$getName() == "")
+            relativePath <- paste0("/", relativePath)
+    }
+
+    cat(paste0("Type:          ", "\"", "Arvados Subcollection", "\""), sep = "\n")
+    cat(paste0("Name:          ", "\"", subCollection$getName(), "\""), sep = "\n")
+    cat(paste0("Relative path: ", "\"", relativePath, "\"") , sep = "\n")
+    cat(paste0("Collection:    ", "\"", collection, "\""), sep = "\n")
+}