Collections getFileContent method renamed to getFileListing.
[arvados.git] / sdk / R / R / Subcollection.R
1 #' Arvados SubCollection Object
2 #'
3 #' Update description
4 #'
5 #' @export Subcollection
6 Subcollection <- R6::R6Class(
7
8     "Subcollection",
9
10     public = list(
11
12         initialize = function(name)
13         {
14             private$name       <- name
15             private$http       <- HttpRequest$new()
16             private$httpParser <- HttpParser$new()
17         },
18         
19         add = function(content)
20         {
21             if("ArvadosFile"   %in% class(content) ||
22                "Subcollection" %in% class(content))
23             {
24                 if(!is.null(content$.__enclos_env__$private$collection))
25                     stop("ArvadosFile/Subcollection already belongs to a collection.")
26
27                 childWithSameName <- private$getChild(content$getName())
28                 if(!is.null(childWithSameName))
29                     stop("Subcollection already contains ArvadosFile
30                           or Subcollection with same name.")
31
32                 if(!is.null(private$collection))
33                 {       
34                     contentPath <- paste0(self$getRelativePath(),
35                                           "/", content$getFileListing())
36
37                     private$collection$.__enclos_env__$private$createFilesOnREST(contentPath)
38                     content$.__enclos_env__$private$addToCollection(private$collection)
39                 }
40
41                 private$children <- c(private$children, content)
42                 content$.__enclos_env__$private$parent = self
43
44                 "Content added successfully."
45             }
46             else
47             {
48                 stop(paste("Expected AravodsFile or Subcollection object, got",
49                            class(content), "."))
50             }
51         },
52
53         removeFromCollection = function()
54         {
55             if(is.null(private$collection))
56                 stop("Subcollection doesn't belong to any collection.")
57
58             if(private$name == "")
59                 stop("Unable to delete root folder.")
60
61             collectionList <- paste0(self$getRelativePath(),
62                                      "/", self$getFileListing(fullpath = FALSE))
63             sapply(collectionList, function(file)
64             {
65                 private$collection$.__enclos_env__$private$deleteFromREST(file)
66             })
67
68             private$addToCollection(NULL)
69             private$dettachFromParent()
70
71             "Content removed successfully."
72         },
73
74         getFileListing = function(fullpath = TRUE)
75         {
76             content <- NULL
77
78             if(fullpath)
79             {
80                 for(child in private$children)
81                     content <- c(content, child$getFileListing())
82
83                 if(private$name != "")
84                     content <- unlist(paste0(private$name, "/", content))
85             }
86             else
87             {
88                 for(child in private$children)
89                     content <- c(content, child$getName())
90             }
91
92             content
93         },
94
95         getSizeInBytes = function()
96         {
97             collectionURL <- URLencode(paste0(private$collection$api$getWebDavHostName(),
98                                               "c=", private$collection$uuid))
99             subcollectionURL <- paste0(collectionURL, "/", self$getRelativePath(), "/");
100
101             headers = list("Authorization" = paste("OAuth2", private$collection$api$getToken()))
102
103             propfindResponse <- private$http$PROPFIND(subcollectionURL, headers)
104
105             sizes <- private$httpParser$extractFileSizeFromWebDAVResponse(propfindResponse, collectionURL)
106             sizes <- as.numeric(sizes[-1])
107
108             sum(sizes)
109         },
110
111         getName = function() private$name,
112
113         getRelativePath = function()
114         {
115             relativePath <- c(private$name)
116             parent <- private$parent
117
118             while(!is.null(parent))
119             {
120                 relativePath <- c(parent$getName(), relativePath)
121                 parent <- parent$getParent()
122             }
123
124             relativePath <- relativePath[relativePath != ""]
125             paste0(relativePath, collapse = "/")
126         },
127
128         move = function(newLocation)
129         {
130             if(is.null(private$collection))
131                 stop("Subcollection doesn't belong to any collection.")
132
133             if(endsWith(newLocation, paste0(private$name, "/")))
134             {
135                 newLocation <- substr(newLocation, 0,
136                                       nchar(newLocation) - nchar(paste0(private$name, "/")))
137             }
138             else if(endsWith(newLocation, private$name))
139             {
140                 newLocation <- substr(newLocation, 0,
141                                       nchar(newLocation) - nchar(private$name))
142             }
143             else
144             {
145                 stop("Destination path is not valid.")
146             }
147
148             newParent <- private$collection$get(newLocation)
149
150             if(is.null(newParent))
151             {
152                 stop("Unable to get destination subcollection.")
153             }
154
155             status <- private$collection$.__enclos_env__$private$moveOnREST(self$getRelativePath(),
156                                                                             paste0(newParent$getRelativePath(), "/", self$getName()))
157
158             private$attachToParent(newParent)
159
160             "Content moved successfully."
161         },
162
163         getParent = function() private$parent
164     ),
165
166     private = list(
167
168         name       = NULL,
169         children   = NULL,
170         parent     = NULL,
171         collection = NULL,
172         http       = NULL,
173         httpParser = NULL,
174
175         getChild = function(name)
176         {
177             for(child in private$children)
178             {
179                 if(child$getName() == name)
180                     return(child)
181             }
182
183             return(NULL)
184         },
185
186         getFirstChild = function()
187         {
188             if(length(private$children) == 0)
189                return(NULL)
190
191             private$children[[1]]
192         },
193
194         removeChild = function(name)
195         {
196             numberOfChildren = length(private$children)
197             if(numberOfChildren > 0)
198             {
199                 for(childIndex in 1:numberOfChildren)
200                 {
201                     if(private$children[[childIndex]]$getName() == name)
202                     {
203                         private$children = private$children[-childIndex]
204                         return()
205                     }
206                 }
207             }
208         },
209
210         addToCollection = function(collection)
211         {
212             for(child in private$children)
213                 child$.__enclos_env__$private$addToCollection(collection)
214
215             private$collection = collection
216         },
217
218         dettachFromParent = function()
219         {
220             if(!is.null(private$parent))
221             {
222                 private$parent$.__enclos_env__$private$removeChild(private$name)
223                 private$parent <- NULL
224             }
225             else
226                 stop("Parent doesn't exists.")
227         },
228
229         attachToParent = function(parent)
230         {
231             if(private$name != "")
232             {
233                 parent$.__enclos_env__$private$children <- c(parent$.__enclos_env__$private$children, self)
234                 private$parent <- parent
235             }
236         }
237     ),
238     
239     cloneable = FALSE
240 )