2411: add copyright headers to our R files.
[arvados.git] / sdk / R / R / CollectionTree.R
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 source("./R/Subcollection.R")
6 source("./R/ArvadosFile.R")
7 source("./R/util.R")
8
9 CollectionTree <- R6::R6Class(
10     "CollectionTree",
11     public = list(
12
13         pathsList = NULL,
14
15         initialize = function(fileContent, collection)
16         {
17             self$pathsList <- fileContent
18
19             treeBranches <- sapply(fileContent, function(filePath)
20             {
21                 splitPath <- unlist(strsplit(filePath, "/", fixed = TRUE))
22                 branch <- private$createBranch(splitPath)      
23             })
24
25             root <- Subcollection$new("")
26
27             sapply(treeBranches, function(branch)
28             {
29                 private$addBranch(root, branch)
30             })
31
32             root$setCollection(collection)
33             private$tree <- root
34         },
35
36         getElement = function(relativePath)
37         {
38             relativePath <- trimFromStart(relativePath, "./")
39             relativePath <- trimFromEnd(relativePath, "/")
40
41             if(endsWith(relativePath, "/"))
42                 relativePath <- substr(relativePath, 0, nchar(relativePath) - 1)
43
44             splitPath <- unlist(strsplit(relativePath, "/", fixed = TRUE))
45             returnElement <- private$tree
46
47             for(pathFragment in splitPath)
48             {
49                 returnElement <- returnElement$get(pathFragment)
50
51                 if(is.null(returnElement))
52                     return(NULL)
53             }
54
55             returnElement
56         },
57
58         getTree = function() private$tree
59     ),
60
61     private = list(
62
63         tree = NULL,
64
65         createBranch = function(splitPath)
66         {
67             branch <- NULL
68             lastElementIndex <- length(splitPath)
69
70             for(elementIndex in lastElementIndex:1)
71             {
72                 if(elementIndex == lastElementIndex)
73                 {
74                     branch <- ArvadosFile$new(splitPath[[elementIndex]])
75                 }
76                 else
77                 {
78                     newFolder <- Subcollection$new(splitPath[[elementIndex]])
79                     newFolder$add(branch)
80                     branch <- newFolder
81                 }
82             }
83             
84             branch
85         },
86
87         addBranch = function(container, node)
88         {
89             child <- container$get(node$getName())
90
91             if(is.null(child))
92             {
93                 container$add(node)
94             }
95             else
96             {
97                 # Note: REST always returns folder name alone before other folder 
98                 # content, so in first iteration we don't know if it's a file
99                 # or folder since its just a name, so we assume it's a file. 
100                 # If we encounter that same name again we know 
101                 # it's a folder so we need to replace ArvadosFile with Subcollection.
102                 if("ArvadosFile" %in% class(child))
103                 {
104                     child = private$replaceFileWithSubcollection(child)
105                 }
106
107                 private$addBranch(child, node$getFirst())
108             }
109         },
110
111         replaceFileWithSubcollection = function(arvadosFile)
112         {
113             subcollection <- Subcollection$new(arvadosFile$getName())
114             fileParent <- arvadosFile$getParent()
115             fileParent$remove(arvadosFile$getName())
116             fileParent$add(subcollection)
117
118             arvadosFile$setParent(NULL)
119
120             subcollection
121         }
122     )
123 )