1 source("./R/Subcollection.R")
3 source("./R/ArvadosFile.R")
5 #' Arvados Collection Object
9 #' @examples arv = Collection$new(api, uuid)
10 #' @export CollectionTree
11 CollectionTree <- R6::R6Class(
17 initialize = function(fileContent, collection)
19 self$pathsList <- fileContent
21 treeBranches <- sapply(fileContent, function(filePath)
23 splitPath <- unlist(strsplit(filePath, "/", fixed = TRUE))
24 branch <- private$createBranch(splitPath)
27 root <- Subcollection$new("")
29 sapply(treeBranches, function(branch)
31 private$addBranch(root, branch)
34 root$setCollection(collection)
38 getElement = function(relativePath)
40 if(startsWith(relativePath, "./"))
41 relativePath <- substr(relativePath, 3, nchar(relativePath))
43 if(endsWith(relativePath, "/"))
44 relativePath <- substr(relativePath, 0, nchar(relativePath) - 1)
46 splitPath <- unlist(strsplit(relativePath, "/", fixed = TRUE))
47 returnElement <- private$tree
49 for(pathFragment in splitPath)
51 returnElement <- returnElement$get(pathFragment)
53 if(is.null(returnElement))
60 getTree = function() private$tree
67 createBranch = function(splitPath)
70 lastElementIndex <- length(splitPath)
72 for(elementIndex in lastElementIndex:1)
74 if(elementIndex == lastElementIndex)
76 branch <- ArvadosFile$new(splitPath[[elementIndex]])
80 newFolder <- Subcollection$new(splitPath[[elementIndex]])
89 addBranch = function(container, node)
91 child <- container$get(node$getName())
99 if("ArvadosFile" %in% class(child))
101 child = private$replaceFileWithSubcollection(child)
104 private$addBranch(child, node$getFirst())
108 replaceFileWithSubcollection = function(arvadosFile)
110 subcollection <- Subcollection$new(arvadosFile$getName())
111 fileParent <- arvadosFile$getParent()
112 fileParent$remove(arvadosFile$getName())
113 fileParent$add(subcollection)
115 arvadosFile$setParent(NULL)