Merge branch 'master' of git.curoverse.com:arvados into 11876-r-sdk
[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(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$get(pathFragment)
49
50                 if(is.null(returnElement))
51                     return(NULL)
52             }
53
54             returnElement
55         },
56
57         getTree = function() private$tree
58     ),
59
60     private = list(
61
62         tree = NULL,
63
64         createBranch = function(splitPath)
65         {
66             branch <- NULL
67             lastElementIndex <- length(splitPath)
68
69             for(elementIndex in lastElementIndex:1)
70             {
71                 if(elementIndex == lastElementIndex)
72                 {
73                     branch <- ArvadosFile$new(splitPath[[elementIndex]])
74                 }
75                 else
76                 {
77                     newFolder <- Subcollection$new(splitPath[[elementIndex]])
78                     newFolder$add(branch)
79                     branch <- newFolder
80                 }
81             }
82             
83             branch
84         },
85
86         addBranch = function(container, node)
87         {
88             child <- container$get(node$getName())
89
90             if(is.null(child))
91             {
92                 container$add(node)
93             }
94             else
95             {
96                 if("ArvadosFile" %in% class(child))
97                 {
98                     child = private$replaceFileWithSubcollection(child)
99                 }
100
101                 private$addBranch(child, node$getFirst())
102             }
103         },
104
105         replaceFileWithSubcollection = function(arvadosFile)
106         {
107             subcollection <- Subcollection$new(arvadosFile$getName())
108             fileParent <- arvadosFile$getParent()
109             fileParent$remove(arvadosFile$getName())
110             fileParent$add(subcollection)
111
112             arvadosFile$setParent(NULL)
113
114             subcollection
115         }
116     )
117 )