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$.__enclos_env__$private$addToCollection(collection)
38 getElement = function(relativePath)
40 if(endsWith(relativePath, "/"))
41 relativePath <- substr(relativePath, 0, nchar(relativePath) - 1)
43 splitPath <- unlist(strsplit(relativePath, "/", fixed = TRUE))
44 returnElement = private$tree
46 for(pathFragment in splitPath)
48 returnElement = returnElement$.__enclos_env__$private$getChild(pathFragment)
50 if(is.null(returnElement))
62 createBranch = function(splitPath)
65 lastElementIndex <- length(splitPath)
67 for(elementIndex in lastElementIndex:1)
69 if(elementIndex == lastElementIndex)
71 branch = ArvadosFile$new(splitPath[[elementIndex]])
75 newFolder = Subcollection$new(splitPath[[elementIndex]])
84 addBranch = function(container, node)
86 child = container$.__enclos_env__$private$getChild(node$getName())
91 #todo add it to collection
95 if("ArvadosFile" %in% class(child))
97 child = private$replaceFileWithSubcollection(child)
100 private$addBranch(child, node$.__enclos_env__$private$getFirstChild())
104 replaceFileWithSubcollection = function(arvadosFile)
106 subcollection <- Subcollection$new(arvadosFile$getName())
107 fileParent <- arvadosFile$.__enclos_env__$private$parent
108 fileParent$.__enclos_env__$private$removeChild(arvadosFile$getName())
109 fileParent$add(subcollection)
111 arvadosFile$.__enclos_env__$private$parent <- NULL