3 #' Arvados SubCollection Object
7 #' @export Subcollection
8 Subcollection <- R6::R6Class(
14 initialize = function(name)
19 getName = function() private$name,
21 getRelativePath = function()
23 relativePath <- c(private$name)
24 parent <- private$parent
26 while(!is.null(parent))
28 relativePath <- c(parent$getName(), relativePath)
29 parent <- parent$getParent()
32 relativePath <- relativePath[relativePath != ""]
33 paste0(relativePath, collapse = "/")
36 add = function(content)
38 if("ArvadosFile" %in% class(content) ||
39 "Subcollection" %in% class(content))
41 childWithSameName <- self$get(content$getName())
42 if(!is.null(childWithSameName))
43 stop(paste("Subcollection already contains ArvadosFile",
44 "or Subcollection with same name."))
46 if(!is.null(private$collection))
48 if(self$getRelativePath() != "")
49 contentPath <- paste0(self$getRelativePath(),
50 "/", content$getFileListing())
52 contentPath <- content$getFileListing()
54 REST <- private$collection$getRESTService()
55 REST$create(contentPath, private$collection$uuid)
56 content$setCollection(private$collection)
59 private$children <- c(private$children, content)
60 content$setParent(self)
62 "Content added successfully."
66 stop(paste0("Expected AravodsFile or Subcollection object, got ",
67 paste0("(", paste0(class(content), collapse = ", "), ")"),
72 remove = function(name)
74 if(is.character(name))
76 child <- self$get(name)
79 stop(paste("Subcollection doesn't contains ArvadosFile",
80 "or Subcollection with specified name."))
82 if(!is.null(private$collection))
84 REST <- private$collection$getRESTService()
85 REST$delete(child$getRelativePath(), private$collection$uuid)
86 child$setCollection(NULL)
89 private$removeChild(name)
96 stop(paste0("Expected character, got ",
97 paste0("(", paste0(class(name), collapse = ", "), ")"),
102 getFileListing = function(fullPath = TRUE)
108 for(child in private$children)
109 content <- c(content, child$getFileListing())
111 if(private$name != "")
112 content <- unlist(paste0(private$name, "/", content))
116 for(child in private$children)
117 content <- c(content, child$getName())
123 getSizeInBytes = function()
125 if(is.null(private$collection))
128 REST <- private$collection$getRESTService()
130 fileSizes <- REST$getResourceSize(paste0(self$getRelativePath(), "/"),
131 private$collection$uuid)
132 return(sum(fileSizes))
135 move = function(newLocation)
137 if(is.null(private$collection))
138 stop("Subcollection doesn't belong to any collection")
140 newLocation <- trimFromEnd(newLocation, "/")
141 nameAndPath <- splitToPathAndName(newLocation)
143 newParent <- private$collection$get(nameAndPath$path)
145 if(is.null(newParent))
147 stop("Unable to get destination subcollection")
150 childWithSameName <- newParent$get(nameAndPath$name)
152 if(!is.null(childWithSameName))
153 stop("Destination already contains content with same name.")
155 REST <- private$collection$getRESTService()
156 REST$move(self$getRelativePath(),
157 paste0(newParent$getRelativePath(), "/", nameAndPath$name),
158 private$collection$uuid)
160 private$dettachFromCurrentParent()
161 private$attachToNewParent(newParent)
163 private$name <- nameAndPath$name
165 "Content moved successfully."
170 for(child in private$children)
172 if(child$getName() == name)
179 getFirst = function()
181 if(length(private$children) == 0)
184 private$children[[1]]
187 setCollection = function(collection, setRecursively = TRUE)
189 private$collection = collection
193 for(child in private$children)
194 child$setCollection(collection)
198 getCollection = function() private$collection,
200 getParent = function() private$parent,
202 setParent = function(newParent) private$parent <- newParent
212 removeChild = function(name)
214 numberOfChildren = length(private$children)
215 if(numberOfChildren > 0)
217 for(childIndex in 1:numberOfChildren)
219 if(private$children[[childIndex]]$getName() == name)
221 private$children = private$children[-childIndex]
228 attachToNewParent = function(newParent)
230 #Note: We temporary set parents collection to NULL. This will ensure that
231 # add method doesn't post file on REST.
232 parentsCollection <- newParent$getCollection()
233 newParent$setCollection(NULL, setRecursively = FALSE)
237 newParent$setCollection(parentsCollection, setRecursively = FALSE)
239 private$parent <- newParent
242 dettachFromCurrentParent = function()
244 #Note: We temporary set parents collection to NULL. This will ensure that
245 # remove method doesn't remove this subcollection from REST.
246 parent <- private$parent
247 parentsCollection <- parent$getCollection()
248 parent$setCollection(NULL, setRecursively = FALSE)
250 parent$remove(private$name)
252 parent$setCollection(parentsCollection, setRecursively = FALSE)