5 #' Subcollection class represents a folder inside Arvados collection.
6 #' It is essentially a composite of arvadosFiles and other subcollections.
9 #' \preformatted{subcollection = Subcollection$new(name)}
11 #' @section Arguments:
13 #' \item{name}{Name of the subcollection.}
18 #' \item{getName()}{Returns name of the subcollection.}
19 #' \item{getRelativePath()}{Returns subcollection path relative to the root.}
20 #' \item{add(content)}{Adds ArvadosFile or Subcollection specified by content to the subcollection.}
21 #' \item{remove(name)}{Removes ArvadosFile or Subcollection specified by name from the subcollection.}
22 #' \item{get(relativePath)}{If relativePath is valid, returns ArvadosFile or Subcollection specified by relativePath, else returns NULL.}
23 #' \item{getFileListing()}{Returns subcollections file content as character vector.}
24 #' \item{getSizeInBytes()}{Returns subcollections content size in bytes.}
25 #' \item{move(newLocation)}{Moves subcollection to a new location inside collection.}
28 #' @name Subcollection
31 #' myFolder <- Subcollection$new("myFolder")
32 #' myFile <- ArvadosFile$new("myFile")
34 #' myFolder$add(myFile)
35 #' myFolder$get("myFile")
36 #' myFolder$remove("myFile")
38 #' myFolder$move("newLocation/myFolder")
43 Subcollection <- R6::R6Class(
49 initialize = function(name)
54 getName = function() private$name,
56 getRelativePath = function()
58 relativePath <- c(private$name)
59 parent <- private$parent
61 while(!is.null(parent))
63 relativePath <- c(parent$getName(), relativePath)
64 parent <- parent$getParent()
67 relativePath <- relativePath[relativePath != ""]
68 paste0(relativePath, collapse = "/")
71 add = function(content)
73 if("ArvadosFile" %in% class(content) ||
74 "Subcollection" %in% class(content))
76 if(content$getName() == "")
77 stop("Content has invalid name.")
79 childWithSameName <- self$get(content$getName())
81 if(!is.null(childWithSameName))
82 stop(paste("Subcollection already contains ArvadosFile",
83 "or Subcollection with same name."))
85 if(!is.null(private$collection))
87 if(self$getRelativePath() != "")
88 contentPath <- paste0(self$getRelativePath(),
89 "/", content$getFileListing())
91 contentPath <- content$getFileListing()
93 REST <- private$collection$getRESTService()
94 REST$create(contentPath, private$collection$uuid)
95 content$setCollection(private$collection)
98 private$children <- c(private$children, content)
99 content$setParent(self)
101 "Content added successfully."
105 stop(paste0("Expected AravodsFile or Subcollection object, got ",
106 paste0("(", paste0(class(content), collapse = ", "), ")"),
111 remove = function(name)
113 if(is.character(name))
115 child <- self$get(name)
118 stop(paste("Subcollection doesn't contains ArvadosFile",
119 "or Subcollection with specified name."))
121 if(!is.null(private$collection))
123 REST <- private$collection$getRESTService()
124 REST$delete(child$getRelativePath(), private$collection$uuid)
126 child$setCollection(NULL)
129 private$removeChild(name)
130 child$setParent(NULL)
136 stop(paste0("Expected character, got ",
137 paste0("(", paste0(class(name), collapse = ", "), ")"),
142 getFileListing = function(fullPath = TRUE)
144 content <- private$getContentAsCharVector(fullPath)
145 content[order(tolower(content))]
148 getSizeInBytes = function()
150 if(is.null(private$collection))
153 REST <- private$collection$getRESTService()
155 fileSizes <- REST$getResourceSize(paste0(self$getRelativePath(), "/"),
156 private$collection$uuid)
157 return(sum(fileSizes))
160 move = function(newLocation)
162 if(is.null(private$collection))
163 stop("Subcollection doesn't belong to any collection")
165 newLocation <- trimFromEnd(newLocation, "/")
166 nameAndPath <- splitToPathAndName(newLocation)
168 newParent <- private$collection$get(nameAndPath$path)
170 if(is.null(newParent))
172 stop("Unable to get destination subcollection")
175 childWithSameName <- newParent$get(nameAndPath$name)
177 if(!is.null(childWithSameName))
178 stop("Destination already contains content with same name.")
180 REST <- private$collection$getRESTService()
181 REST$move(self$getRelativePath(),
182 paste0(newParent$getRelativePath(), "/", nameAndPath$name),
183 private$collection$uuid)
185 private$dettachFromCurrentParent()
186 private$attachToNewParent(newParent)
188 private$name <- nameAndPath$name
190 "Content moved successfully."
195 for(child in private$children)
197 if(child$getName() == name)
204 getFirst = function()
206 if(length(private$children) == 0)
209 private$children[[1]]
212 setCollection = function(collection, setRecursively = TRUE)
214 private$collection = collection
218 for(child in private$children)
219 child$setCollection(collection)
223 getCollection = function() private$collection,
225 getParent = function() private$parent,
227 setParent = function(newParent) private$parent <- newParent
237 removeChild = function(name)
239 numberOfChildren = length(private$children)
240 if(numberOfChildren > 0)
242 for(childIndex in 1:numberOfChildren)
244 if(private$children[[childIndex]]$getName() == name)
246 private$children = private$children[-childIndex]
253 attachToNewParent = function(newParent)
255 #Note: We temporary set parents collection to NULL. This will ensure that
256 # add method doesn't post file on REST.
257 parentsCollection <- newParent$getCollection()
258 newParent$setCollection(NULL, setRecursively = FALSE)
262 newParent$setCollection(parentsCollection, setRecursively = FALSE)
264 private$parent <- newParent
267 dettachFromCurrentParent = function()
269 #Note: We temporary set parents collection to NULL. This will ensure that
270 # remove method doesn't remove this subcollection from REST.
271 parent <- private$parent
272 parentsCollection <- parent$getCollection()
273 parent$setCollection(NULL, setRecursively = FALSE)
275 parent$remove(private$name)
277 parent$setCollection(parentsCollection, setRecursively = FALSE)
280 getContentAsCharVector = function(fullPath = TRUE)
286 for(child in private$children)
287 content <- c(content, child$getFileListing())
289 if(private$name != "")
290 content <- unlist(paste0(private$name, "/", content))
294 for(child in private$children)
295 content <- c(content, child$getName())
305 #' print.Subcollection
307 #' Custom print function for Subcollection class
309 #' @param x Instance of Subcollection class
310 #' @param ... Optional arguments.
312 print.Subcollection = function(x, ...)
315 relativePath <- x$getRelativePath()
317 if(!is.null(x$getCollection()))
319 collection <- x$getCollection()$uuid
321 if(!x$getName() == "")
322 relativePath <- paste0("/", relativePath)
325 cat(paste0("Type: ", "\"", "Arvados Subcollection", "\""), sep = "\n")
326 cat(paste0("Name: ", "\"", x$getName(), "\""), sep = "\n")
327 cat(paste0("Relative path: ", "\"", relativePath, "\""), sep = "\n")
328 cat(paste0("Collection: ", "\"", collection, "\""), sep = "\n")