1 #' Arvados SubCollection Object
5 #' @export Subcollection
6 Subcollection <- R6::R6Class(
12 initialize = function(name)
15 private$http <- HttpRequest$new()
16 private$httpParser <- HttpParser$new()
19 add = function(content)
21 if("ArvadosFile" %in% class(content) ||
22 "Subcollection" %in% class(content))
24 if(!is.null(content$.__enclos_env__$private$collection))
25 stop("ArvadosFile/Subcollection already belongs to a collection.")
27 if(!is.null(private$collection))
29 contentPath <- paste0(self$getRelativePath(), "/", content$getFileList())
30 private$collection$.__enclos_env__$private$createFilesOnREST(contentPath)
31 content$.__enclos_env__$private$addToCollection(private$collection)
34 private$children <- c(private$children, content)
35 content$.__enclos_env__$private$parent = self
39 stop("Expected AravodsFile or Subcollection object, got ...")
43 removeFromCollection = function()
45 if(is.null(private$collection))
46 stop("Subcollection doesn't belong to any collection.")
48 collectionList <- paste0(self$getRelativePath(), "/", self$getFileList(fullpath = FALSE))
49 sapply(collectionList, function(file)
51 private$collection$.__enclos_env__$private$deleteFromREST(file)
54 #todo rename this add to a collection
55 private$addToCollection(NULL)
56 private$dettachFromParent()
60 getFileList = function(fullpath = TRUE)
66 for(child in private$children)
67 content <- c(content, child$getFileList())
69 if(private$name != "")
70 content <- unlist(paste0(private$name, "/", content))
74 for(child in private$children)
75 content <- c(content, child$getName())
81 getSizeInBytes = function()
83 collectionURL <- URLencode(paste0(private$collection$api$getWebDavHostName(), "c=", private$collection$uuid))
84 subcollectionURL <- paste0(collectionURL, "/", self$getRelativePath(), "/");
86 headers = list("Authorization" = paste("OAuth2", private$collection$api$getToken()))
88 propfindResponse <- private$http$PROPFIND(subcollectionURL, headers)
90 sizes <- private$httpParser$extractFileSizeFromWebDAVResponse(propfindResponse, collectionURL)
91 sizes <- as.numeric(sizes[-1])
96 getName = function() private$name,
98 getRelativePath = function()
100 relativePath <- c(private$name)
101 parent <- private$parent
103 #Recurse back to root
104 while(!is.null(parent))
106 relativePath <- c(parent$getName(), relativePath)
107 parent <- parent$getParent()
110 relativePath <- relativePath[relativePath != ""]
111 paste0(relativePath, collapse = "/")
114 move = function(newLocation)
116 if(endsWith(newLocation, paste0(private$name, "/")))
118 newLocation <- substr(newLocation, 0, nchar(newLocation) - nchar(paste0(private$name, "/")))
120 else if(endsWith(newLocation, private$name))
122 newLocation <- substr(newLocation, 0, nchar(newLocation) - nchar(private$name))
126 stop("Destination path is not valid.")
129 newParent <- private$collection$get(newLocation)
131 if(is.null(newParent))
133 stop("Unable to get destination subcollectin")
136 status <- private$collection$.__enclos_env__$private$moveOnRest(self$getRelativePath(), paste0(newParent$getRelativePath(), "/", self$getName()))
138 private$attachToParent(newParent)
140 paste("Status code :", status$status_code)
143 getParent = function() private$parent
155 getChild = function(name)
157 for(child in private$children)
159 if(child$getName() == name)
166 getFirstChild = function()
168 if(length(private$children) == 0)
171 private$children[[1]]
174 removeChild = function(name)
176 numberOfChildren = length(private$children)
177 if(numberOfChildren > 0)
179 for(childIndex in 1:numberOfChildren)
181 if(private$children[[childIndex]]$getName() == name)
183 private$children = private$children[-childIndex]
190 addToCollection = function(collection)
192 for(child in private$children)
193 child$.__enclos_env__$private$addToCollection(collection)
195 private$collection = collection
198 dettachFromParent = function()
200 if(!is.null(private$parent))
202 private$parent$.__enclos_env__$private$removeChild(private$name)
203 private$parent <- NULL
206 stop("Parent doesn't exists.")
209 attachToParent = function(parent)
211 parent$.__enclos_env__$private$children <- c(parent$.__enclos_env__$private$children, self)
212 private$parent <- parent