+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: Apache-2.0
+
source("./R/Subcollection.R")
source("./R/ArvadosFile.R")
source("./R/RESTService.R")
source("./R/util.R")
#' Collection
-#'
+#'
#' Collection class provides interface for working with Arvados collections.
-#'
+#'
#' @section Usage:
#' \preformatted{collection = Collection$new(arv, uuid)}
#'
#' \item{arv}{Arvados object.}
#' \item{uuid}{UUID of a collection.}
#' }
-#'
+#'
#' @section Methods:
#' \describe{
#' \item{add(content)}{Adds ArvadosFile or Subcollection specified by content to the collection.}
public = list(
- api = NULL,
- uuid = NULL,
+ uuid = NULL,
+ # api = NULL,
- initialize = function(api, uuid)
+ initialize = function(api, uuid)
{
- self$api <- api
+ # self$api <- api
private$REST <- api$getRESTService()
self$uuid <- uuid
add = function(content, relativePath = "")
{
+ if(is.null(private$tree))
+ private$genereateCollectionTreeStructure()
+
if(relativePath == "" ||
relativePath == "." ||
relativePath == "./")
if("ArvadosFile" %in% class(content) ||
"Subcollection" %in% class(content))
{
-
if(content$getName() == "")
stop("Content has invalid name.")
create = function(fileNames, relativePath = "")
{
+ if(is.null(private$tree))
+ private$genereateCollectionTreeStructure()
+
if(relativePath == "" ||
relativePath == "." ||
relativePath == "./")
}
else
{
- relativePath <- trimFromEnd(relativePath, "/")
+ relativePath <- trimFromEnd(relativePath, "/")
subcollection <- self$get(relativePath)
}
else
return(arvadosFiles)
}
- else
+ else
{
stop(paste0("Expected character vector, got ",
paste0("(", paste0(class(fileNames), collapse = ", "), ")"),
remove = function(paths)
{
+ if(is.null(private$tree))
+ private$genereateCollectionTreeStructure()
+
if(is.character(paths))
{
sapply(paths, function(filePath)
"Content removed"
}
- else
+ else
{
stop(paste0("Expected character vector, got ",
paste0("(", paste0(class(paths), collapse = ", "), ")"),
move = function(content, newLocation)
{
+ if(is.null(private$tree))
+ private$genereateCollectionTreeStructure()
+
content <- trimFromEnd(content, "/")
elementToMove <- self$get(content)
getFileListing = function()
{
+ if(is.null(private$tree))
+ private$genereateCollectionTreeStructure()
+
content <- private$REST$getCollectionContent(self$uuid)
content[order(tolower(content))]
},
get = function(relativePath)
{
+ if(is.null(private$tree))
+ private$genereateCollectionTreeStructure()
+
private$tree$getElement(relativePath)
},
+ toJSON = function()
+ {
+ fields <- sapply(private$classFields, function(field)
+ {
+ self[[field]]
+ }, USE.NAMES = TRUE)
+
+ jsonlite::toJSON(list("collection" =
+ Filter(Negate(is.null), fields)), auto_unbox = TRUE)
+ },
+
+ isEmpty = function() {
+ fields <- sapply(private$classFields,
+ function(field) self[[field]])
+
+ if(any(sapply(fields, function(field) !is.null(field) && field != "")))
+ FALSE
+ else
+ TRUE
+ },
+
getRESTService = function() private$REST,
setRESTService = function(newRESTService) private$REST <- newRESTService
),
REST = NULL,
tree = NULL,
- fileContent = NULL
+ fileContent = NULL,
+ classFields = NULL,
+
+ genereateCollectionTreeStructure = function()
+ {
+ if(is.null(self$uuid))
+ stop("Collection uuid is not defined.")
+
+ if(is.null(private$REST))
+ stop("REST service is not defined.")
+
+ private$fileContent <- private$REST$getCollectionContent(self$uuid)
+ private$tree <- CollectionTree$new(private$fileContent, self)
+ }
),
cloneable = FALSE
#'
#' @param x Instance of Collection class
#' @param ... Optional arguments.
-#' @export
+#' @export
print.Collection = function(x, ...)
{
cat(paste0("Type: ", "\"", "Arvados Collection", "\""), sep = "\n")