getFileListing now returns sorted list
[arvados.git] / sdk / R / R / Collection.R
1 source("./R/Subcollection.R")
2 source("./R/ArvadosFile.R")
3 source("./R/RESTService.R")
4 source("./R/util.R")
5
6 #' Arvados Collection Object
7 #'
8 #' Update description
9 #'
10 #' @examples arv = Collection$new(api, uuid)
11 #' @export Collection
12 Collection <- R6::R6Class(
13
14     "Collection",
15
16     public = list(
17
18         api  = NULL,
19         uuid = NULL,
20
21         initialize = function(api, uuid)
22         {
23             self$api <- api
24             private$REST <- api$getRESTService()
25
26             self$uuid <- uuid
27
28             private$fileContent <- private$REST$getCollectionContent(uuid)
29             private$tree <- CollectionTree$new(private$fileContent, self)
30         },
31
32         add = function(content, relativePath = "")
33         {
34             if(relativePath == ""  ||
35                relativePath == "." ||
36                relativePath == "./")
37             {
38                 subcollection <- private$tree$getTree()
39             }
40             else
41             {
42                 relativePath <- trimFromEnd(relativePath, "/")
43                 subcollection <- self$get(relativePath)
44             }
45
46             if(is.null(subcollection))
47                 stop(paste("Subcollection", relativePath, "doesn't exist."))
48
49             if("ArvadosFile"   %in% class(content) ||
50                "Subcollection" %in% class(content))
51             {
52                 subcollection$add(content)
53
54                 content
55             }
56             else
57             {
58                 stop(paste0("Expected AravodsFile or Subcollection object, got ",
59                             paste0("(", paste0(class(content), collapse = ", "), ")"),
60                             "."))
61             }
62         },
63
64         create = function(fileNames, relativePath = "")
65         {
66             if(relativePath == ""  ||
67                relativePath == "." ||
68                relativePath == "./")
69             {
70                 subcollection <- private$tree$getTree()
71             }
72             else
73             {
74                 relativePath  <- trimFromEnd(relativePath, "/") 
75                 subcollection <- self$get(relativePath)
76             }
77
78             if(is.null(subcollection))
79                 stop(paste("Subcollection", relativePath, "doesn't exist."))
80
81             if(is.character(fileNames))
82             {
83                 arvadosFiles <- NULL
84                 sapply(fileNames, function(fileName)
85                 {
86                     childWithSameName <- subcollection$get(fileName)
87                     if(!is.null(childWithSameName))
88                         stop("Destination already contains file with same name.")
89
90                     newFile <- ArvadosFile$new(fileName)
91                     subcollection$add(newFile)
92
93                     arvadosFiles <<- c(arvadosFiles, newFile)
94                 })
95
96                 if(length(arvadosFiles) == 1)
97                     return(arvadosFiles[[1]])
98                 else
99                     return(arvadosFiles)
100             }
101             else 
102             {
103                 stop(paste0("Expected character vector, got ",
104                             paste0("(", paste0(class(fileNames), collapse = ", "), ")"),
105                             "."))
106             }
107         },
108
109         remove = function(paths)
110         {
111             if(is.character(paths))
112             {
113                 sapply(paths, function(filePath)
114                 {
115                     filePath <- trimFromEnd(filePath, "/")
116                     file <- self$get(filePath)
117
118                     if(is.null(file))
119                         stop(paste("File", filePath, "doesn't exist."))
120
121                     parent <- file$getParent()
122                     parent$remove(file$getName())
123                 })
124             }
125             else 
126             {
127                 stop(paste0("Expected character vector, got ",
128                             paste0("(", paste0(class(paths), collapse = ", "), ")"),
129                             "."))
130             }
131         },
132
133         move = function(content, newLocation)
134         {
135             content <- trimFromEnd(content, "/")
136
137             elementToMove <- self$get(content)
138
139             if(is.null(elementToMove))
140                 stop("Element you want to move doesn't exist in the collection.")
141
142             elementToMove$move(newLocation)
143         },
144
145         getFileListing = function()
146         {
147             content <- private$REST$getCollectionContent(self$uuid)
148             content[order(tolower(content))]
149         },
150
151         get = function(relativePath)
152         {
153             private$tree$getElement(relativePath)
154         },
155
156         getRESTService = function() private$REST,
157         setRESTService = function(newRESTService) private$REST <- newRESTService
158     ),
159
160     private = list(
161
162         REST        = NULL,
163         tree        = NULL,
164         fileContent = NULL,
165
166         generateTree = function(content)
167         {
168             treeBranches <- sapply(collectionContent, function(filePath)
169             {
170                 splitPath <- unlist(strsplit(filePath$name, "/", fixed = TRUE))
171
172                 branch = private$createBranch(splitPath, filePath$fileSize)      
173             })
174         }
175     ),
176
177     cloneable = FALSE
178 )