1 source("./R/Subcollection.R")
2 source("./R/ArvadosFile.R")
5 #' Arvados Collection Object
9 #' @examples arv = Collection$new(api, uuid)
10 CollectionTree <- R6::R6Class(
16 initialize = function(fileContent, collection)
18 self$pathsList <- fileContent
20 treeBranches <- sapply(fileContent, function(filePath)
22 splitPath <- unlist(strsplit(filePath, "/", fixed = TRUE))
23 branch <- private$createBranch(splitPath)
26 root <- Subcollection$new("")
28 sapply(treeBranches, function(branch)
30 private$addBranch(root, branch)
33 root$setCollection(collection)
37 getElement = function(relativePath)
39 relativePath <- trimFromStart(relativePath, "./")
40 relativePath <- trimFromEnd(relativePath, "/")
42 if(endsWith(relativePath, "/"))
43 relativePath <- substr(relativePath, 0, nchar(relativePath) - 1)
45 splitPath <- unlist(strsplit(relativePath, "/", fixed = TRUE))
46 returnElement <- private$tree
48 for(pathFragment in splitPath)
50 returnElement <- returnElement$get(pathFragment)
52 if(is.null(returnElement))
59 getTree = function() private$tree
66 createBranch = function(splitPath)
69 lastElementIndex <- length(splitPath)
71 for(elementIndex in lastElementIndex:1)
73 if(elementIndex == lastElementIndex)
75 branch <- ArvadosFile$new(splitPath[[elementIndex]])
79 newFolder <- Subcollection$new(splitPath[[elementIndex]])
88 addBranch = function(container, node)
90 child <- container$get(node$getName())
98 # Note: REST always returns folder name alone before other folder
99 # content, so in first iteration we don't know if it's a file
100 # or folder since its just a name, so we assume it's a file.
101 # If we encounter that same name again we know
102 # it's a folder so we need to replace ArvadosFile with Subcollection.
103 if("ArvadosFile" %in% class(child))
105 child = private$replaceFileWithSubcollection(child)
108 private$addBranch(child, node$getFirst())
112 replaceFileWithSubcollection = function(arvadosFile)
114 subcollection <- Subcollection$new(arvadosFile$getName())
115 fileParent <- arvadosFile$getParent()
116 fileParent$remove(arvadosFile$getName())
117 fileParent$add(subcollection)
119 arvadosFile$setParent(NULL)