b95b797b93b0266fc001f9408e10ec14e32f929f
[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$setCollection(collection)
35             private$tree <- root
36         },
37
38         getElement = function(relativePath)
39         {
40             if(startsWith(relativePath, "./"))
41                 relativePath <- substr(relativePath, 3, nchar(relativePath))
42
43             if(endsWith(relativePath, "/"))
44                 relativePath <- substr(relativePath, 0, nchar(relativePath) - 1)
45
46             splitPath <- unlist(strsplit(relativePath, "/", fixed = TRUE))
47             returnElement <- private$tree
48
49             for(pathFragment in splitPath)
50             {
51                 returnElement <- returnElement$get(pathFragment)
52
53                 if(is.null(returnElement))
54                     return(NULL)
55             }
56
57             returnElement
58         },
59
60         getTree = function() private$tree
61     ),
62
63     private = list(
64
65         tree = NULL,
66
67         createBranch = function(splitPath)
68         {
69             branch <- NULL
70             lastElementIndex <- length(splitPath)
71
72             for(elementIndex in lastElementIndex:1)
73             {
74                 if(elementIndex == lastElementIndex)
75                 {
76                     branch <- ArvadosFile$new(splitPath[[elementIndex]])
77                 }
78                 else
79                 {
80                     newFolder <- Subcollection$new(splitPath[[elementIndex]])
81                     newFolder$add(branch)
82                     branch <- newFolder
83                 }
84             }
85             
86             branch
87         },
88
89         addBranch = function(container, node)
90         {
91             child <- container$get(node$getName())
92
93             if(is.null(child))
94             {
95                 container$add(node)
96             }
97             else
98             {
99                 if("ArvadosFile" %in% class(child))
100                 {
101                     child = private$replaceFileWithSubcollection(child)
102                 }
103
104                 private$addBranch(child, node$getFirst())
105             }
106         },
107
108         replaceFileWithSubcollection = function(arvadosFile)
109         {
110             subcollection <- Subcollection$new(arvadosFile$getName())
111             fileParent <- arvadosFile$getParent()
112             fileParent$remove(arvadosFile$getName())
113             fileParent$add(subcollection)
114
115             arvadosFile$setParent(NULL)
116
117             subcollection
118         }
119     )
120 )