1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 source("./R/Subcollection.R")
6 source("./R/ArvadosFile.R")
7 source("./R/RESTService.R")
12 #' Collection class provides interface for working with Arvados collections.
15 #' \preformatted{collection = Collection$new(arv, uuid)}
17 #' @section Arguments:
19 #' \item{arv}{Arvados object.}
20 #' \item{uuid}{UUID of a collection.}
25 #' \item{add(content)}{Adds ArvadosFile or Subcollection specified by content to the collection.}
26 #' \item{create(files)}{Creates one or more ArvadosFiles and adds them to the collection at specified path.}
27 #' \item{remove(fileNames)}{Remove one or more files from the collection.}
28 #' \item{move(content, destination)}{Moves ArvadosFile or Subcollection to another location in the collection.}
29 #' \item{copy(content, destination)}{Copies ArvadosFile or Subcollection to another location in the collection.}
30 #' \item{getFileListing()}{Returns collections file content as character vector.}
31 #' \item{get(relativePath)}{If relativePath is valid, returns ArvadosFile or Subcollection specified by relativePath, else returns NULL.}
37 #' arv <- Arvados$new("your Arvados token", "example.arvadosapi.com")
38 #' collection <- Collection$new(arv, "uuid")
40 #' createdFiles <- collection$create(c("main.cpp", lib.dll), "cpp/src/")
42 #' collection$remove("location/to/my/file.cpp")
44 #' collection$move("folder/file.cpp", "file.cpp")
46 #' arvadosFile <- collection$get("location/to/my/file.cpp")
47 #' arvadosSubcollection <- collection$get("location/to/my/directory/")
52 Collection <- R6::R6Class(
60 initialize = function(api, uuid)
62 private$REST <- api$getRESTService()
66 add = function(content, relativePath = "")
68 if(is.null(private$tree))
69 private$generateCollectionTreeStructure()
71 if(relativePath == "" ||
72 relativePath == "." ||
75 subcollection <- private$tree$getTree()
79 relativePath <- trimFromEnd(relativePath, "/")
80 subcollection <- self$get(relativePath)
83 if(is.null(subcollection))
84 stop(paste("Subcollection", relativePath, "doesn't exist."))
86 if("ArvadosFile" %in% class(content) ||
87 "Subcollection" %in% class(content))
89 if(!is.null(content$getCollection()))
90 stop("Content already belongs to a collection.")
92 if(content$getName() == "")
93 stop("Content has invalid name.")
95 subcollection$add(content)
100 stop(paste0("Expected AravodsFile or Subcollection object, got ",
101 paste0("(", paste0(class(content), collapse = ", "), ")"),
106 create = function(files)
108 if(is.null(private$tree))
109 private$generateCollectionTreeStructure()
111 if(is.character(files))
113 sapply(files, function(file)
115 childWithSameName <- self$get(file)
116 if(!is.null(childWithSameName))
117 stop("Destination already contains file with same name.")
119 newTreeBranch <- private$tree$createBranch(file)
120 private$tree$addBranch(private$tree$getTree(), newTreeBranch)
122 private$REST$create(file, self$uuid)
123 newTreeBranch$setCollection(self)
130 stop(paste0("Expected character vector, got ",
131 paste0("(", paste0(class(files), collapse = ", "), ")"),
136 remove = function(paths)
138 if(is.null(private$tree))
139 private$generateCollectionTreeStructure()
141 if(is.character(paths))
143 sapply(paths, function(filePath)
145 filePath <- trimFromEnd(filePath, "/")
146 file <- self$get(filePath)
149 stop(paste("File", filePath, "doesn't exist."))
151 parent <- file$getParent()
154 stop("You can't delete root folder.")
156 parent$remove(file$getName())
163 stop(paste0("Expected character vector, got ",
164 paste0("(", paste0(class(paths), collapse = ", "), ")"),
169 move = function(content, destination)
171 if(is.null(private$tree))
172 private$generateCollectionTreeStructure()
174 content <- trimFromEnd(content, "/")
176 elementToMove <- self$get(content)
178 if(is.null(elementToMove))
179 stop("Content you want to move doesn't exist in the collection.")
181 elementToMove$move(destination)
184 copy = function(content, destination)
186 if(is.null(private$tree))
187 private$generateCollectionTreeStructure()
189 content <- trimFromEnd(content, "/")
191 elementToCopy <- self$get(content)
193 if(is.null(elementToCopy))
194 stop("Content you want to copy doesn't exist in the collection.")
196 elementToCopy$copy(destination)
201 if(!is.null(private$tree))
203 private$tree$getTree()$setCollection(NULL, setRecursively = TRUE)
208 getFileListing = function()
210 if(is.null(private$tree))
211 private$generateCollectionTreeStructure()
213 content <- private$REST$getCollectionContent(self$uuid)
214 content[order(tolower(content))]
217 get = function(relativePath)
219 if(is.null(private$tree))
220 private$generateCollectionTreeStructure()
222 private$tree$getElement(relativePath)
225 getRESTService = function() private$REST,
226 setRESTService = function(newRESTService) private$REST <- newRESTService
235 generateCollectionTreeStructure = function()
237 if(is.null(self$uuid))
238 stop("Collection uuid is not defined.")
240 if(is.null(private$REST))
241 stop("REST service is not defined.")
243 private$fileContent <- private$REST$getCollectionContent(self$uuid)
244 private$tree <- CollectionTree$new(private$fileContent, self)
253 #' Custom print function for Collection class
255 #' @param x Instance of Collection class
256 #' @param ... Optional arguments.
258 print.Collection = function(x, ...)
260 cat(paste0("Type: ", "\"", "Arvados Collection", "\""), sep = "\n")
261 cat(paste0("uuid: ", "\"", x$uuid, "\""), sep = "\n")