Implemented custom print functions for all public classes
[arvados.git] / sdk / R / R / Subcollection.R
index 801cf694fdfc5e2d851ec86c1d3e5d9962560a33..7eb4381edaa151b2ef1053a78c039dfd7af4150d 100644 (file)
@@ -13,7 +13,7 @@ Subcollection <- R6::R6Class(
 
         initialize = function(name)
         {
-            private$name       <- name
+            private$name <- name
         },
 
         getName = function() private$name,
@@ -101,23 +101,9 @@ Subcollection <- R6::R6Class(
 
         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()
@@ -132,35 +118,37 @@ Subcollection <- R6::R6Class(
             return(sum(fileSizes))
         },
 
-        move = function(newLocationInCollection)
+        move = function(newLocation)
         {
             if(is.null(private$collection))
                 stop("Subcollection doesn't belong to any collection")
 
-            newLocationInCollection <- trimFromEnd(newLocationInCollection, "/")
-            newParentLocation <- trimFromEnd(newLocationInCollection, private$name)
+            newLocation <- trimFromEnd(newLocation, "/")
+            nameAndPath <- splitToPathAndName(newLocation)
 
-            newParent <- private$collection$get(newParentLocation)
+            newParent <- private$collection$get(nameAndPath$path)
 
             if(is.null(newParent))
             {
                 stop("Unable to get destination subcollection")
             }
 
-            childWithSameName <- newParent$get(private$name)
+            childWithSameName <- newParent$get(nameAndPath$name)
 
             if(!is.null(childWithSameName))
                 stop("Destination already contains content with same name.")
 
             REST <- private$collection$getRESTService()
             REST$move(self$getRelativePath(),
-                      paste0(newParent$getRelativePath(), "/", self$getName()),
+                      paste0(newParent$getRelativePath(), "/", nameAndPath$name),
                       private$collection$uuid)
 
             private$dettachFromCurrentParent()
             private$attachToNewParent(newParent)
 
-            "Content moved successfully"
+            private$name <- nameAndPath$name
+
+            "Content moved successfully."
         },
 
         get = function(name)
@@ -248,8 +236,50 @@ Subcollection <- R6::R6Class(
             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")
+}