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