49a1e81d0767c7b33513a290ecad208f0a7fd216
[arvados.git] / sdk / R / R / Collection.R
1 source("./R/Subcollection.R")
2 source("./R/ArvadosFile.R")
3 source("./R/FileTree.R")
4 source("./R/HttpRequest.R")
5 source("./R/HttpParser.R")
6
7 #' Arvados Collection Object
8 #'
9 #' Update description
10 #'
11 #' @examples arv = Collection$new(api, uuid)
12 #' @export Collection
13 Collection <- R6::R6Class(
14
15     "Collection",
16
17     public = list(
18
19         #Todo(Fudo): Encapsulate this?
20         uuid                     = NULL,
21         etag                     = NULL,
22         owner_uuid               = NULL,
23         created_at               = NULL,
24         modified_by_client_uuid  = NULL,
25         modified_by_user_uuid    = NULL,
26         modified_at              = NULL,
27         portable_data_hash       = NULL,
28         replication_desired      = NULL,
29         replication_confirmed_at = NULL,
30         replication_confirmed    = NULL,
31         updated_at               = NULL,
32         manifest_text            = NULL,
33         name                     = NULL,
34         description              = NULL,
35         properties               = NULL,
36         delete_at                = NULL,
37         file_names               = NULL,
38         trash_at                 = NULL,
39         is_trashed               = NULL,
40
41         initialize = function(api, uuid)
42         {
43             private$api <- api
44             result <- private$api$getCollection(uuid)
45
46             self$uuid                     <- result$uuid                               
47             self$etag                     <- result$etag                               
48             self$owner_uuid               <- result$owner_uuid                         
49             self$created_at               <- result$created_at                         
50             self$modified_by_client_uuid  <- result$modified_by_client_uuid            
51             self$modified_by_user_uuid    <- result$modified_by_user_uuid              
52             self$modified_at              <- result$modified_at                        
53             self$portable_data_hash       <- result$portable_data_hash                 
54             self$replication_desired      <- result$replication_desired                
55             self$replication_confirmed_at <- result$replication_confirmed_at           
56             self$replication_confirmed    <- result$replication_confirmed              
57             self$updated_at               <- result$updated_at                         
58             self$manifest_text            <- result$manifest_text                      
59             self$name                     <- result$name                               
60             self$description              <- result$description                        
61             self$properties               <- result$properties                         
62             self$delete_at                <- result$delete_at                          
63             self$file_names               <- result$file_names                         
64             self$trash_at                 <- result$trash_at                           
65             self$is_trashed               <- result$is_trashed                         
66
67             private$http <- HttpRequest$new()
68             private$httpParser <- HttpParser$new()
69
70             private$fileItems <- private$getCollectionContent()
71             private$fileTree <- FileTree$new(private$fileItems)
72
73         },
74
75         printFileContent = function()
76         {
77             private$fileTree$printContent(private$fileTree$getRoot(), 0)
78         },
79
80         getFileContent = function()
81         {
82             sapply(private$fileItems, function(file)
83             {
84                 file$name
85             })
86         },
87
88         get = function(relativePath)
89         {
90             treeNode <- private$fileTree$traverseInOrder(private$fileTree$getRoot(), function(node)
91             {
92                 if(node$relativePath == relativePath)
93                     return(node)
94                 else
95                     return(NULL)
96             })
97
98             if(!is.null(treeNode))
99             {
100                 return(private$createSubcollectionTree(treeNode))
101             }
102             else
103             {
104                 return(NULL)
105             }
106         },
107
108         createNewFile = function(relativePath, content, contentType)
109         {
110             fileURL <- paste0(private$api$getWebDavHostName(), "c=", self$uuid, "/", relativePath);
111             headers <- list(Authorization = paste("OAuth2", private$api$getToken()), 
112                             "Content-Type" = contentType)
113             body <- content
114
115             serverResponse <- private$http$PUT(fileURL, headers, body)
116
117             if(serverResponse$status_code != 201)
118                 stop(paste("Server code:", serverResponse$status_code))
119
120             fileSize = private$getNewFileSize(relativePath)
121             private$fileTree$addNode(relativePath, fileSize)
122
123             paste0("File created (size = ", fileSize , ")")
124         },
125
126         removeFile = function(relativePath)
127         {
128             node <- private$fileTree$getNode(relativePath)
129
130             if(is.null(node))
131                 stop("File doesn't exists.")
132
133             fileURL <- paste0(private$api$getWebDavHostName(), "c=", self$uuid, "/", relativePath);
134             headers <- list(Authorization = paste("OAuth2", private$api$getToken())) 
135
136             serverResponse <- private$http$DELETE(fileURL, headers)
137
138             if(serverResponse$status_code != 204)
139                 stop(paste("Server code:", serverResponse$status_code))
140
141             "File deleted"
142         },
143
144         update = function(subcollection, event)
145         {
146             #Todo(Fudo): Add some king of check here later on.
147             if(event == "File size changed")
148             {
149                 private$handleFileSizeChange(subcollection$getRelativePath(),
150                                              subcollection$getSizeInBytes())
151             }
152         }
153     ),
154
155     active = list(
156         items = function(value)
157         {
158             if(missing(value))
159                 return(private$fileItems)
160             else
161                 print("Value is read-only.")
162
163             return(NULL)
164         }
165     ),
166     
167     private = list(
168
169         fileItems  = NULL,
170         api        = NULL,
171         fileTree   = NULL,
172         http       = NULL,
173         httpParser = NULL,
174
175         handleFileSizeChange = function(filePath, newSize)
176         {
177             node <- private$fileTree$getNode(filePath)
178
179             if(is.null(node))
180                 stop("File doesn't exits")
181
182             node$size <- newSize
183         },
184
185         createSubcollectionTree = function(treeNode)
186         {
187             if(treeNode$hasChildren())
188             {
189                 children = NULL
190
191                 for(child in treeNode$children)
192                 {
193                     child <- private$createSubcollectionTree(child)
194                     children <- c(children, child)                   
195                 }
196
197                 return(Subcollection$new(treeNode$name, treeNode$relativePath, children))
198             }
199             else
200             {
201                 if(treeNode$type == "file")
202                     return(ArvadosFile$new(treeNode$name, treeNode$relativePath, treeNode$size, private$api, self))
203                 else 
204                     return(Subcollection$new(treeNode$name, treeNode$relativePath, NULL))
205             }
206         },
207
208         getCollectionContent = function()
209         {
210             collectionURL <- URLencode(paste0(private$api$getWebDavHostName(), "c=", self$uuid))
211
212             headers = list("Authorization" = paste("OAuth2", private$api$getToken()))
213
214             response <- private$http$PROPFIND(collectionURL, headers)
215
216             parsedResponse <- private$httpParser$parseWebDAVResponse(response, collectionURL)
217             parsedResponse[-1]
218         },
219
220         getNewFileSize = function(relativePath)
221         {
222             collectionURL <- URLencode(paste0(private$api$getWebDavHostName(), "c=", self$uuid))
223             fileURL = paste0(collectionURL, "/", relativePath);
224             headers = list("Authorization" = paste("OAuth2", private$api$getToken()))
225
226             propfindResponse <- private$http$PROPFIND(fileURL, headers)
227
228             fileInfo <- private$httpParser$parseWebDAVResponse(propfindResponse, collectionURL)
229
230             fileInfo[[1]]$fileSize
231         }
232     ),
233
234     cloneable = FALSE
235 )