1 #' Arvados SubCollection Object
5 #' @export Subcollection
6 Subcollection <- R6::R6Class(
12 initialize = function(name)
17 getName = function() private$name,
19 getRelativePath = function()
21 relativePath <- c(private$name)
22 parent <- private$parent
24 while(!is.null(parent))
26 relativePath <- c(parent$getName(), relativePath)
27 parent <- parent$getParent()
30 relativePath <- relativePath[relativePath != ""]
31 paste0(relativePath, collapse = "/")
34 add = function(content)
36 if("ArvadosFile" %in% class(content) ||
37 "Subcollection" %in% class(content))
39 childWithSameName <- self$get(content$getName())
40 if(!is.null(childWithSameName))
41 stop(paste("Subcollection already contains ArvadosFile",
42 "or Subcollection with same name."))
44 if(!is.null(private$collection))
46 if(self$getRelativePath() != "")
47 contentPath <- paste0(self$getRelativePath(),
48 "/", content$getFileListing())
50 contentPath <- content$getFileListing()
52 REST <- private$collection$getRESTService()
53 REST$create(contentPath, private$collection$uuid)
54 content$setCollection(private$collection)
57 private$children <- c(private$children, content)
58 content$setParent(self)
60 "Content added successfully."
64 stop(paste0("Expected AravodsFile or Subcollection object, got ",
65 paste0("(", paste0(class(content), collapse = ", "), ")"),
70 remove = function(name)
72 if(is.character(name))
74 child <- self$get(name)
77 stop(paste("Subcollection doesn't contains ArvadosFile",
78 "or Subcollection with specified name."))
80 if(!is.null(private$collection))
82 REST <- private$collection$getRESTService()
83 REST$delete(child$getRelativePath(), private$collection$uuid)
84 child$setCollection(NULL)
87 private$removeChild(name)
94 stop(paste0("Expected character, got ",
95 paste0("(", paste0(class(name), collapse = ", "), ")"),
100 getFileListing = function(fullPath = TRUE)
106 for(child in private$children)
107 content <- c(content, child$getFileListing())
109 if(private$name != "")
110 content <- unlist(paste0(private$name, "/", content))
114 for(child in private$children)
115 content <- c(content, child$getName())
121 getSizeInBytes = function()
123 if(!is.null(private$collection))
125 REST <- private$collection$getRESTService()
126 subcollectionSize <- REST$getResourceSize(private$collection$uuid,
127 self$getRelativePath())
128 return(subcollectionSize)
136 move = function(newLocationInCollection)
138 if(is.null(private$collection))
139 stop("Subcollection doesn't belong to any collection")
141 newLocationInCollection <- trimFromEnd(newLocationInCollection, "/")
142 newParentLocation <- trimFromEnd(newLocationInCollection, private$name)
144 newParent <- private$collection$get(newParentLocation)
146 if(is.null(newParent))
148 stop("Unable to get destination subcollection")
151 REST <- private$collection$getRESTService()
152 REST$move(self$getRelativePath(),
153 paste0(newParent$getRelativePath(), "/", self$getName()),
154 private$collection$uuid)
156 private$dettachFromCurrentParent()
157 private$attachToNewParent(newParent)
159 "Content moved successfully"
164 for(child in private$children)
166 if(child$getName() == name)
173 getFirst = function()
175 if(length(private$children) == 0)
178 private$children[[1]]
181 setCollection = function(collection, setRecursively = TRUE)
183 private$collection = collection
187 for(child in private$children)
188 child$setCollection(collection)
192 getCollection = function() private$collection,
194 getParent = function() private$parent,
196 setParent = function(newParent) private$parent <- newParent
206 removeChild = function(name)
208 numberOfChildren = length(private$children)
209 if(numberOfChildren > 0)
211 for(childIndex in 1:numberOfChildren)
213 if(private$children[[childIndex]]$getName() == name)
215 private$children = private$children[-childIndex]
222 attachToNewParent = function(newParent)
224 #Note: We temporary set parents collection to NULL. This will ensure that
225 # add method doesn't post file on REST.
226 parentsCollection <- newParent$getCollection()
227 newParent$setCollection(NULL, setRecursively = FALSE)
231 newParent$setCollection(parentsCollection, setRecursively = FALSE)
233 private$parent <- newParent
236 dettachFromCurrentParent = function()
238 #Note: We temporary set parents collection to NULL. This will ensure that
239 # remove method doesn't remove this subcollection from REST.
240 parent <- private$parent
241 parentsCollection <- parent$getCollection()
242 parent$setCollection(NULL, setRecursively = FALSE)
244 parent$remove(private$name)
246 parent$setCollection(parentsCollection, setRecursively = FALSE)