Merge branch 'master' of git.curoverse.com:arvados into 11876-r-sdk
[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                 if(!is.null(private$collection))
28                 {       
29                     contentPath <- paste0(self$getRelativePath(), "/", content$getFileList())
30                     private$collection$.__enclos_env__$private$createFilesOnREST(contentPath)
31                     content$.__enclos_env__$private$addToCollection(private$collection)
32                 }
33
34                 private$children <- c(private$children, content)
35                 content$.__enclos_env__$private$parent = self
36             }
37             else
38             {
39                 stop("Expected AravodsFile or Subcollection object, got ...")
40             }
41         },
42
43         removeFromCollection = function()
44         {
45             if(is.null(private$collection))
46                 stop("Subcollection doesn't belong to any collection.")
47
48             collectionList <- paste0(self$getRelativePath(), "/", self$getFileList(fullpath = FALSE))
49             sapply(collectionList, function(file)
50             {
51                 private$collection$.__enclos_env__$private$deleteFromREST(file)
52             })
53
54             #todo rename this add to a collection
55             private$addToCollection(NULL)
56             private$dettachFromParent()
57
58         },
59
60         getFileList = function(fullpath = TRUE)
61         {
62             content <- NULL
63
64             if(fullpath)
65             {
66                 for(child in private$children)
67                     content <- c(content, child$getFileList())
68
69                 if(private$name != "")
70                     content <- unlist(paste0(private$name, "/", content))
71             }
72             else
73             {
74                 for(child in private$children)
75                     content <- c(content, child$getName())
76             }
77
78             content
79         },
80
81         getSizeInBytes = function()
82         {
83             collectionURL <- URLencode(paste0(private$collection$api$getWebDavHostName(), "c=", private$collection$uuid))
84             subcollectionURL <- paste0(collectionURL, "/", self$getRelativePath(), "/");
85
86             headers = list("Authorization" = paste("OAuth2", private$collection$api$getToken()))
87
88             propfindResponse <- private$http$PROPFIND(subcollectionURL, headers)
89
90             sizes <- private$httpParser$extractFileSizeFromWebDAVResponse(propfindResponse, collectionURL)
91             sizes <- as.numeric(sizes[-1])
92
93             sum(sizes)
94         },
95
96         getName = function() private$name,
97
98         getRelativePath = function()
99         {
100             relativePath <- c(private$name)
101             parent <- private$parent
102
103             #Recurse back to root
104             while(!is.null(parent))
105             {
106                 relativePath <- c(parent$getName(), relativePath)
107                 parent <- parent$getParent()
108             }
109
110             relativePath <- relativePath[relativePath != ""]
111             paste0(relativePath, collapse = "/")
112         },
113
114         move = function(newLocation)
115         {
116             if(endsWith(newLocation, paste0(private$name, "/")))
117             {
118                 newLocation <- substr(newLocation, 0, nchar(newLocation) - nchar(paste0(private$name, "/")))
119             }
120             else if(endsWith(newLocation, private$name))
121             {
122                 newLocation <- substr(newLocation, 0, nchar(newLocation) - nchar(private$name))
123             }
124             else
125             {
126                 stop("Destination path is not valid.")
127             }
128
129             newParent <- private$collection$get(newLocation)
130
131             if(is.null(newParent))
132             {
133                 stop("Unable to get destination subcollectin")
134             }
135
136             status <- private$collection$.__enclos_env__$private$moveOnRest(self$getRelativePath(), paste0(newParent$getRelativePath(), "/", self$getName()))
137
138             private$attachToParent(newParent)
139
140             paste("Status code :", status$status_code)
141         },
142
143         getParent = function() private$parent
144     ),
145
146     private = list(
147
148         name       = NULL,
149         children   = NULL,
150         parent     = NULL,
151         collection = NULL,
152         http       = NULL,
153         httpParser = NULL,
154
155         getChild = function(name)
156         {
157             for(child in private$children)
158             {
159                 if(child$getName() == name)
160                     return(child)
161             }
162
163             return(NULL)
164         },
165
166         getFirstChild = function()
167         {
168             if(length(private$children) == 0)
169                return(NULL)
170
171             private$children[[1]]
172         },
173
174         removeChild = function(name)
175         {
176             numberOfChildren = length(private$children)
177             if(numberOfChildren > 0)
178             {
179                 for(childIndex in 1:numberOfChildren)
180                 {
181                     if(private$children[[childIndex]]$getName() == name)
182                     {
183                         private$children = private$children[-childIndex]
184                         return()
185                     }
186                 }
187             }
188         },
189
190         addToCollection = function(collection)
191         {
192             for(child in private$children)
193                 child$.__enclos_env__$private$addToCollection(collection)
194
195             private$collection = collection
196         },
197
198         dettachFromParent = function()
199         {
200             if(!is.null(private$parent))
201             {
202                 private$parent$.__enclos_env__$private$removeChild(private$name)
203                 private$parent <- NULL
204             }
205             else
206                 stop("Parent doesn't exists.")
207         },
208
209         attachToParent = function(parent)
210         {
211             parent$.__enclos_env__$private$children <- c(parent$.__enclos_env__$private$children, self)
212             private$parent <- parent
213         }
214     ),
215     
216     cloneable = FALSE
217 )