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(fileNames, relativePath = "")}{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, newLocation)}{Moves ArvadosFile or Subcollection to another location in the collection.}
29 #' \item{getFileListing()}{Returns collections file content as character vector.}
30 #' \item{get(relativePath)}{If relativePath is valid, returns ArvadosFile or Subcollection specified by relativePath, else returns NULL.}
36 #' arv <- Arvados$new("your Arvados token", "example.arvadosapi.com")
37 #' collection <- Collection$new(arv, "uuid")
39 #' newFile <- ArvadosFile$new("myFile")
40 #' collection$add(newFile, "myFolder")
42 #' createdFiles <- collection$create(c("main.cpp", lib.dll), "cpp/src/")
44 #' collection$remove("location/to/my/file.cpp")
46 #' collection$move("folder/file.cpp", "file.cpp")
48 #' arvadosFile <- collection$get("location/to/my/file.cpp")
49 #' arvadosSubcollection <- collection$get("location/to/my/directory/")
54 Collection <- R6::R6Class(
63 initialize = function(api, uuid)
66 private$REST <- api$getRESTService()
70 private$fileContent <- private$REST$getCollectionContent(uuid)
71 private$tree <- CollectionTree$new(private$fileContent, self)
74 add = function(content, relativePath = "")
76 if(is.null(private$tree))
77 private$genereateCollectionTreeStructure()
79 if(relativePath == "" ||
80 relativePath == "." ||
83 subcollection <- private$tree$getTree()
87 relativePath <- trimFromEnd(relativePath, "/")
88 subcollection <- self$get(relativePath)
91 if(is.null(subcollection))
92 stop(paste("Subcollection", relativePath, "doesn't exist."))
94 if("ArvadosFile" %in% class(content) ||
95 "Subcollection" %in% class(content))
97 if(content$getName() == "")
98 stop("Content has invalid name.")
100 subcollection$add(content)
105 stop(paste0("Expected AravodsFile or Subcollection object, got ",
106 paste0("(", paste0(class(content), collapse = ", "), ")"),
111 create = function(fileNames, relativePath = "")
113 if(is.null(private$tree))
114 private$genereateCollectionTreeStructure()
116 if(relativePath == "" ||
117 relativePath == "." ||
118 relativePath == "./")
120 subcollection <- private$tree$getTree()
124 relativePath <- trimFromEnd(relativePath, "/")
125 subcollection <- self$get(relativePath)
128 if(is.null(subcollection))
129 stop(paste("Subcollection", relativePath, "doesn't exist."))
131 if(is.character(fileNames))
134 sapply(fileNames, function(fileName)
136 childWithSameName <- subcollection$get(fileName)
137 if(!is.null(childWithSameName))
138 stop("Destination already contains file with same name.")
140 newFile <- ArvadosFile$new(fileName)
141 subcollection$add(newFile)
143 arvadosFiles <<- c(arvadosFiles, newFile)
146 if(length(arvadosFiles) == 1)
147 return(arvadosFiles[[1]])
153 stop(paste0("Expected character vector, got ",
154 paste0("(", paste0(class(fileNames), collapse = ", "), ")"),
159 remove = function(paths)
161 if(is.null(private$tree))
162 private$genereateCollectionTreeStructure()
164 if(is.character(paths))
166 sapply(paths, function(filePath)
168 filePath <- trimFromEnd(filePath, "/")
169 file <- self$get(filePath)
172 stop(paste("File", filePath, "doesn't exist."))
174 parent <- file$getParent()
177 stop("You can't delete root folder.")
179 parent$remove(file$getName())
186 stop(paste0("Expected character vector, got ",
187 paste0("(", paste0(class(paths), collapse = ", "), ")"),
192 move = function(content, newLocation)
194 if(is.null(private$tree))
195 private$genereateCollectionTreeStructure()
197 content <- trimFromEnd(content, "/")
199 elementToMove <- self$get(content)
201 if(is.null(elementToMove))
202 stop("Content you want to move doesn't exist in the collection.")
204 elementToMove$move(newLocation)
207 getFileListing = function()
209 if(is.null(private$tree))
210 private$genereateCollectionTreeStructure()
212 content <- private$REST$getCollectionContent(self$uuid)
213 content[order(tolower(content))]
216 get = function(relativePath)
218 if(is.null(private$tree))
219 private$genereateCollectionTreeStructure()
221 private$tree$getElement(relativePath)
226 fields <- sapply(private$classFields, function(field)
231 jsonlite::toJSON(list("collection" =
232 Filter(Negate(is.null), fields)), auto_unbox = TRUE)
235 isEmpty = function() {
236 fields <- sapply(private$classFields,
237 function(field) self[[field]])
239 if(any(sapply(fields, function(field) !is.null(field) && field != "")))
245 getRESTService = function() private$REST,
246 setRESTService = function(newRESTService) private$REST <- newRESTService
256 genereateCollectionTreeStructure = function()
258 if(is.null(self$uuid))
259 stop("Collection uuid is not defined.")
261 if(is.null(private$REST))
262 stop("REST service is not defined.")
264 private$fileContent <- private$REST$getCollectionContent(self$uuid)
265 private$tree <- CollectionTree$new(private$fileContent, self)
274 #' Custom print function for Collection class
276 #' @param x Instance of Collection class
277 #' @param ... Optional arguments.
279 print.Collection = function(x, ...)
281 cat(paste0("Type: ", "\"", "Arvados Collection", "\""), sep = "\n")
282 cat(paste0("uuid: ", "\"", x$uuid, "\""), sep = "\n")