1 source("./R/Subcollection.R")
2 source("./R/ArvadosFile.R")
3 source("./R/RESTService.R")
6 #' Arvados Collection Object
10 #' @examples arv = Collection$new(api, uuid)
12 Collection <- R6::R6Class(
21 initialize = function(api, uuid)
24 private$REST <- api$getRESTService()
28 private$fileContent <- private$REST$getCollectionContent(uuid)
29 private$tree <- CollectionTree$new(private$fileContent, self)
32 add = function(content, relativePath = "")
34 if(relativePath == "" ||
35 relativePath == "." ||
38 subcollection <- private$tree$getTree()
42 relativePath <- trimFromEnd(relativePath, "/")
43 subcollection <- self$get(relativePath)
46 if(is.null(subcollection))
47 stop(paste("Subcollection", relativePath, "doesn't exist."))
49 if("ArvadosFile" %in% class(content) ||
50 "Subcollection" %in% class(content))
53 if(content$getName() == "")
54 stop("Content has invalid name.")
56 subcollection$add(content)
61 stop(paste0("Expected AravodsFile or Subcollection object, got ",
62 paste0("(", paste0(class(content), collapse = ", "), ")"),
67 create = function(fileNames, relativePath = "")
69 if(relativePath == "" ||
70 relativePath == "." ||
73 subcollection <- private$tree$getTree()
77 relativePath <- trimFromEnd(relativePath, "/")
78 subcollection <- self$get(relativePath)
81 if(is.null(subcollection))
82 stop(paste("Subcollection", relativePath, "doesn't exist."))
84 if(is.character(fileNames))
87 sapply(fileNames, function(fileName)
89 childWithSameName <- subcollection$get(fileName)
90 if(!is.null(childWithSameName))
91 stop("Destination already contains file with same name.")
93 newFile <- ArvadosFile$new(fileName)
94 subcollection$add(newFile)
96 arvadosFiles <<- c(arvadosFiles, newFile)
99 if(length(arvadosFiles) == 1)
100 return(arvadosFiles[[1]])
106 stop(paste0("Expected character vector, got ",
107 paste0("(", paste0(class(fileNames), collapse = ", "), ")"),
112 remove = function(paths)
114 if(is.character(paths))
116 sapply(paths, function(filePath)
118 filePath <- trimFromEnd(filePath, "/")
119 file <- self$get(filePath)
122 stop(paste("File", filePath, "doesn't exist."))
124 parent <- file$getParent()
127 stop("You can't delete root folder.")
129 parent$remove(file$getName())
136 stop(paste0("Expected character vector, got ",
137 paste0("(", paste0(class(paths), collapse = ", "), ")"),
142 move = function(content, newLocation)
144 content <- trimFromEnd(content, "/")
146 elementToMove <- self$get(content)
148 if(is.null(elementToMove))
149 stop("Content you want to move doesn't exist in the collection.")
151 elementToMove$move(newLocation)
154 getFileListing = function()
156 content <- private$REST$getCollectionContent(self$uuid)
157 content[order(tolower(content))]
160 get = function(relativePath)
162 private$tree$getElement(relativePath)
165 getRESTService = function() private$REST,
166 setRESTService = function(newRESTService) private$REST <- newRESTService
179 #' @export print.Collection
180 print.Collection = function(collection)
182 cat(paste0("Type: ", "\"", "Arvados Collection", "\""), sep = "\n")
183 cat(paste0("uuid: ", "\"", collection$uuid, "\""), sep = "\n")