1 source("./R/Subcollection.R")
2 source("./R/ArvadosFile.R")
5 CollectionTree <- R6::R6Class(
11 initialize = function(fileContent, collection)
13 self$pathsList <- fileContent
15 treeBranches <- sapply(fileContent, function(filePath)
17 splitPath <- unlist(strsplit(filePath, "/", fixed = TRUE))
18 branch <- private$createBranch(splitPath)
21 root <- Subcollection$new("")
23 sapply(treeBranches, function(branch)
25 private$addBranch(root, branch)
28 root$setCollection(collection)
32 getElement = function(relativePath)
34 relativePath <- trimFromStart(relativePath, "./")
35 relativePath <- trimFromEnd(relativePath, "/")
37 if(endsWith(relativePath, "/"))
38 relativePath <- substr(relativePath, 0, nchar(relativePath) - 1)
40 splitPath <- unlist(strsplit(relativePath, "/", fixed = TRUE))
41 returnElement <- private$tree
43 for(pathFragment in splitPath)
45 returnElement <- returnElement$get(pathFragment)
47 if(is.null(returnElement))
54 getTree = function() private$tree
61 createBranch = function(splitPath)
64 lastElementIndex <- length(splitPath)
66 for(elementIndex in lastElementIndex:1)
68 if(elementIndex == lastElementIndex)
70 branch <- ArvadosFile$new(splitPath[[elementIndex]])
74 newFolder <- Subcollection$new(splitPath[[elementIndex]])
83 addBranch = function(container, node)
85 child <- container$get(node$getName())
93 # Note: REST always returns folder name alone before other folder
94 # content, so in first iteration we don't know if it's a file
95 # or folder since its just a name, so we assume it's a file.
96 # If we encounter that same name again we know
97 # it's a folder so we need to replace ArvadosFile with Subcollection.
98 if("ArvadosFile" %in% class(child))
100 child = private$replaceFileWithSubcollection(child)
103 private$addBranch(child, node$getFirst())
107 replaceFileWithSubcollection = function(arvadosFile)
109 subcollection <- Subcollection$new(arvadosFile$getName())
110 fileParent <- arvadosFile$getParent()
111 fileParent$remove(arvadosFile$getName())
112 fileParent$add(subcollection)
114 arvadosFile$setParent(NULL)