Methods listCollections and listProjects now work properly
[arvados.git] / sdk / R / R / CollectionTree.R
1 source("./R/Subcollection.R")
2
3 source("./R/ArvadosFile.R")
4
5 #' Arvados Collection Object
6 #'
7 #' Update description
8 #'
9 #' @examples arv = Collection$new(api, uuid)
10 #' @export CollectionTree
11 CollectionTree <- R6::R6Class(
12     "CollectionTree",
13     public = list(
14
15         pathsList = NULL,
16
17         initialize = function(fileContent, collection)
18         {
19             self$pathsList <- fileContent
20
21             treeBranches <- sapply(fileContent, function(filePath)
22             {
23                 splitPath <- unlist(strsplit(filePath, "/", fixed = TRUE))
24                 branch = private$createBranch(splitPath)      
25             })
26
27             root <- Subcollection$new("")
28
29             sapply(treeBranches, function(branch)
30             {
31                 private$addBranch(root, branch)
32             })
33
34             root$.__enclos_env__$private$addToCollection(collection)
35             private$tree <- root
36         },
37
38         getElement = function(relativePath)
39         {
40             if(endsWith(relativePath, "/"))
41                 relativePath <- substr(relativePath, 0, nchar(relativePath) - 1)
42
43             splitPath <- unlist(strsplit(relativePath, "/", fixed = TRUE))
44             returnElement = private$tree
45
46             for(pathFragment in splitPath)
47             {
48                 returnElement = returnElement$.__enclos_env__$private$getChild(pathFragment)
49
50                 if(is.null(returnElement))
51                     return(NULL)
52             }
53
54             returnElement
55         }
56     ),
57
58     private = list(
59
60         tree = NULL,
61
62         createBranch = function(splitPath)
63         {
64             branch <- NULL
65             lastElementIndex <- length(splitPath)
66
67             for(elementIndex in lastElementIndex:1)
68             {
69                 if(elementIndex == lastElementIndex)
70                 {
71                     branch = ArvadosFile$new(splitPath[[elementIndex]])
72                 }
73                 else
74                 {
75                     newFolder = Subcollection$new(splitPath[[elementIndex]])
76                     newFolder$add(branch)
77                     branch = newFolder
78                 }
79             }
80             
81             branch
82         },
83
84         addBranch = function(container, node)
85         {
86             child = container$.__enclos_env__$private$getChild(node$getName())
87
88             if(is.null(child))
89             {
90                 container$add(node)
91             }
92             else
93             {
94                 if("ArvadosFile" %in% class(child))
95                 {
96                     child = private$replaceFileWithSubcollection(child)
97                 }
98
99                 private$addBranch(child, node$.__enclos_env__$private$getFirstChild())
100             }
101         },
102
103         replaceFileWithSubcollection = function(arvadosFile)
104         {
105             subcollection <- Subcollection$new(arvadosFile$getName())
106             fileParent <- arvadosFile$.__enclos_env__$private$parent
107             fileParent$.__enclos_env__$private$removeChild(arvadosFile$getName())
108             fileParent$add(subcollection)
109
110             arvadosFile$.__enclos_env__$private$parent <- NULL
111
112             subcollection
113         }
114     )
115 )