Merge branch '11876-r-sdk' refs #11876
[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
53                 if(content$getName() == "")
54                     stop("Content has invalid name.")
55
56                 subcollection$add(content)
57                 content
58             }
59             else
60             {
61                 stop(paste0("Expected AravodsFile or Subcollection object, got ",
62                             paste0("(", paste0(class(content), collapse = ", "), ")"),
63                             "."))
64             }
65         },
66
67         create = function(fileNames, relativePath = "")
68         {
69             if(relativePath == ""  ||
70                relativePath == "." ||
71                relativePath == "./")
72             {
73                 subcollection <- private$tree$getTree()
74             }
75             else
76             {
77                 relativePath  <- trimFromEnd(relativePath, "/") 
78                 subcollection <- self$get(relativePath)
79             }
80
81             if(is.null(subcollection))
82                 stop(paste("Subcollection", relativePath, "doesn't exist."))
83
84             if(is.character(fileNames))
85             {
86                 arvadosFiles <- NULL
87                 sapply(fileNames, function(fileName)
88                 {
89                     childWithSameName <- subcollection$get(fileName)
90                     if(!is.null(childWithSameName))
91                         stop("Destination already contains file with same name.")
92
93                     newFile <- ArvadosFile$new(fileName)
94                     subcollection$add(newFile)
95
96                     arvadosFiles <<- c(arvadosFiles, newFile)
97                 })
98
99                 if(length(arvadosFiles) == 1)
100                     return(arvadosFiles[[1]])
101                 else
102                     return(arvadosFiles)
103             }
104             else 
105             {
106                 stop(paste0("Expected character vector, got ",
107                             paste0("(", paste0(class(fileNames), collapse = ", "), ")"),
108                             "."))
109             }
110         },
111
112         remove = function(paths)
113         {
114             if(is.character(paths))
115             {
116                 sapply(paths, function(filePath)
117                 {
118                     filePath <- trimFromEnd(filePath, "/")
119                     file <- self$get(filePath)
120
121                     if(is.null(file))
122                         stop(paste("File", filePath, "doesn't exist."))
123
124                     parent <- file$getParent()
125
126                     if(is.null(parent))
127                         stop("You can't delete root folder.")
128
129                     parent$remove(file$getName())
130                 })
131
132                 "Content removed"
133             }
134             else 
135             {
136                 stop(paste0("Expected character vector, got ",
137                             paste0("(", paste0(class(paths), collapse = ", "), ")"),
138                             "."))
139             }
140         },
141
142         move = function(content, newLocation)
143         {
144             content <- trimFromEnd(content, "/")
145
146             elementToMove <- self$get(content)
147
148             if(is.null(elementToMove))
149                 stop("Content you want to move doesn't exist in the collection.")
150
151             elementToMove$move(newLocation)
152         },
153
154         getFileListing = function()
155         {
156             content <- private$REST$getCollectionContent(self$uuid)
157             content[order(tolower(content))]
158         },
159
160         get = function(relativePath)
161         {
162             private$tree$getElement(relativePath)
163         },
164
165         getRESTService = function() private$REST,
166         setRESTService = function(newRESTService) private$REST <- newRESTService
167     ),
168
169     private = list(
170
171         REST        = NULL,
172         tree        = NULL,
173         fileContent = NULL
174     ),
175
176     cloneable = FALSE
177 )
178
179 #' @export print.Collection
180 print.Collection = function(collection)
181 {
182     cat(paste0("Type: ", "\"", "Arvados Collection", "\""), sep = "\n")
183     cat(paste0("uuid: ", "\"", collection$uuid,      "\""), sep = "\n")
184 }