From 3555412f3e8152b7848cf92483257d5d54bf16af Mon Sep 17 00:00:00 2001 From: Adam Reevesman Date: Thu, 17 May 2018 16:59:45 -0700 Subject: [PATCH 1/3] created pid_to_eml_entity function --- NAMESPACE | 1 + R/eml.R | 142 ++++++++++++++++++++++++++++++++- man/pid_to_eml_entity.Rd | 50 ++++++++++++ man/pid_to_eml_other_entity.Rd | 6 +- 4 files changed, 193 insertions(+), 6 deletions(-) create mode 100644 man/pid_to_eml_entity.Rd diff --git a/NAMESPACE b/NAMESPACE index 2c57263..70a142d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -41,6 +41,7 @@ export(new_uuid) export(object_exists) export(parse_resource_map) export(pid_to_eml_datatable) +export(pid_to_eml_entity) export(pid_to_eml_other_entity) export(pid_to_eml_physical) export(publish_object) diff --git a/R/eml.R b/R/eml.R index d6df844..ee6f7a9 100644 --- a/R/eml.R +++ b/R/eml.R @@ -3,6 +3,146 @@ #' Helpers for creating EML. +#' Create EML dataTable or otherEntity objects for a set of PIDs +#' +#' Note this is a useful alternative to pid_to_eml_datatable and +#' pid_to_eml_other_entity because it can create multiple of objects at once. +#' +#' @param mn (MNode) Member Node where the PID is associated with an object. +#' @param pids (character) The PIDs of the objects to create the entities for. Either a vector or list of names of the objects. Note that \code{pids[i]} or \code{pids[[i]]} must be a character vector of length one. +#' @param entityType (character) What kind of objects to create from the input. Either "dataTable" or "otherEntity". +#' @param names (character) Either a vector or list of names of the objects. Note that \code{names[i]} or \code{names[[i]]} must be \code{NA} or a character vector of length one and must correspond to \code{pids[i]} or \code{pids[[i]]}. +#' @param descriptions (character) Either a vector or list of names of the objects. Note that \code{descriptions[i]} or \code{descriptions[[i]]} must be \code{NA} if no entityDescription is needed for a particular pid or a character vector of length one and must correspond to \code{pids[i]} or \code{pids[[i]]}. +#' @param attributes (list) Required if entityType is set to "dataTable". \code{NULL} if entityType is set to "otherEntity" and no attribute tables are needed. A list of attribute tables. Note that \code{attributes[[i]]} must be a dataframe or possibly \code{NA} in the case where \code{entityType} is set to "otherEntity" and must correspond to \code{pids[i]} or \code{pids[[i]]}. +#' @param factors (list) A list of enumerated domain tables. \code{NULL} if no enumerated domains exist. Note that \code{factors[[i]]} must be a dataframe or \code{NA} in the case where enumerated domains are not present in the data. Must correspond to \code{pids[i]} or \code{pids[[i]]}. +#' @param validateAttributes (logical) If set to FALSE or if attributes are not passed into the function, attribute validation test will not run. +#' +#' @return (list) The otherEntity or dataTable object(s) +#' @export +#' +#' @examples +#' \dontrun{ +#' #Generate EML otherEntities for four pids +#' TEST_pid_to_eml_entity(mn, +#' entityType = 'otherEntity', +#' pids = c('pid1', 'pid2', 'pid3', 'pid4'), +#' names = c('name1', 'name2', 'name3', 'name4'), +#' descriptions = c('description1', 'description2', +#' 'description3', 'description4'), +#' attributes = list(atTbl1, atTbl2, atTbl3, atTbl4), +#' factors = rep(factors1, factors2, NA, NA))) +#' } +pid_to_eml_entity <- function(mn, + pids, + entityType, + names, + descriptions = NULL, + attributes = NULL, + factors = NULL, + validateAttributes = TRUE) { + + stopifnot(is(mn, "MNode")) + stopifnot(is.character(pids), + all(nchar(pids)) > 0) + + if ( (length(pids) != length(attributes)) & (!is.null(attributes)) ){ + stop(call. = FALSE, + "'attributes' must be NULL or have same length as pids") + } + if ( (length(pids) != length(factors)) & (!is.null(factors)) ){ + stop(call. = FALSE, + "'factors' must be NULL or have same length as pids") + } + if ( (length(pids) != length(names)) & (!is.null(names)) ){ + stop(call. = FALSE, + "'names' must be NULL or have same length as pids") + } + if ( (length(pids) != length(descriptions)) & (!is.null(descriptions)) ){ + stop(call. = FALSE, + "'descriptions' must be NULL or have same length as pids") + } + + + work <- function(i, some_list){ + + mn <- some_list$mn + entity_type <- some_list$entity_type + pid <- some_list$pid[[i]] + name <- some_list$name[[i]] + + description = some_list$description[[i]] + + + attribute_table <- l$attribute_table[[i]] + factors_table <- l$factors_table[[i]] + + entity <- new(Class = entity_type) + entity@scope <- new("xml_attribute", "document") + entity@physical@.Data[[1]] <- pid_to_eml_physical(mn, pid)[[1]] + if (entityType == "otherEntity"){ + entity@entityType <- "other" + } + + if (is.na(name)) { + stop(call. = FALSE, + paste("'Name' of entity ", i," must be specified in the function call", sep = '')) + } + + entity@entityName <- name + + if (!is.na(description)) { + entity@entityDescription <- description + } + + if(class(attribute_table) == "data.frame") { + stopifnot(is.data.frame(attribute_table)) + + if (class(factors_table) != "data.frame") { + attribute_list <- set_attributes(attribute_table) + } + else { + stopifnot(is.data.frame(factors_table)) + attribute_list <- set_attributes(attribute_table, factors_table) + } + + if (validateAttributes == TRUE) { + stopifnot(eml_validate_attributes(attribute_list)) + } + + entity@attributeList <- attribute_list + } + + else { + if (entity_type == "dataTable"){ + stop(call. = FALSE, + "An attribute table must be provided when creating a dataTable") + } + } + + entity + } + + l <- list(mn = mn, + entity_type = entityType, + pid = as.list(pids), + name = as.list(names), + description = ifelse(rep(is.null(descriptions),length(pids)), + rep(list(NA), + length(pids)), + as.list(descriptions)), + attribute_table = ifelse(rep(is.null(attributes),length(pids)), + rep(list(NA), + length(pids)), + attributes), + factors_table = ifelse(rep(is.null(factors),length(pids)), + rep(list(NA), + length(pids)), + factors)) + + lapply(seq_along(as.list(pids)), work, some_list = l) +} + + #' Create EML otherEntity objects for a set of PIDs #' #' Note this is a wrapper around sysmeta_to_other_entity which handles the task of @@ -660,7 +800,7 @@ eml_project <- function(title, # Title titles <- lapply(title, function(x) { as(x, "title") }) project@title <- as(titles, "ListOftitle") - + # Personnel if(!all(sapply(personnelList, function(x) { is(x, "personnel") }))) { stop(call. = FALSE, diff --git a/man/pid_to_eml_entity.Rd b/man/pid_to_eml_entity.Rd new file mode 100644 index 0000000..8faa7e1 --- /dev/null +++ b/man/pid_to_eml_entity.Rd @@ -0,0 +1,50 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/eml.R +\name{pid_to_eml_entity} +\alias{pid_to_eml_entity} +\title{eml.R} +\usage{ +pid_to_eml_entity(mn, pids, entityType, names, descriptions = NULL, + attributes = NULL, factors = NULL, validateAttributes = TRUE) +} +\arguments{ +\item{mn}{(MNode) Member Node where the PID is associated with an object.} + +\item{pids}{(character) The PIDs of the objects to create the entities for. Either a vector or list of names of the objects. Note that \code{pids[i]} or \code{pids[[i]]} must be a character vector of length one.} + +\item{entityType}{(character) What kind of objects to create from the input. Either "dataTable" or "otherEntity".} + +\item{names}{(character) Either a vector or list of names of the objects. Note that \code{names[i]} or \code{names[[i]]} must be \code{NA} or a character vector of length one and must correspond to \code{pids[i]} or \code{pids[[i]]}.} + +\item{descriptions}{(character) Either a vector or list of names of the objects. Note that \code{descriptions[i]} or \code{descriptions[[i]]} must be \code{NA} if no entityDescription is needed for a particular pid or a character vector of length one and must correspond to \code{pids[i]} or \code{pids[[i]]}.} + +\item{attributes}{(list) Required if entityType is set to "dataTable". \code{NULL} if entityType is set to "otherEntity" and no attribute tables are needed. A list of attribute tables. Note that \code{attributes[[i]]} must be a dataframe or possibly \code{NA} in the case where \code{entityType} is set to "otherEntity" and must correspond to \code{pids[i]} or \code{pids[[i]]}.} + +\item{factors}{(list) A list of enumerated domain tables. \code{NULL} if no enumerated domains exist. Note that \code{factors[[i]]} must be a dataframe or \code{NA} in the case where enumerated domains are not present in the data. Must correspond to \code{pids[i]} or \code{pids[[i]]}.} + +\item{validateAttributes}{(logical) If set to FALSE or if attributes are not passed into the function, attribute validation test will not run.} +} +\value{ +(list) The otherEntity or dataTable object(s) +} +\description{ +Helpers for creating EML. +Create EML dataTable or otherEntity objects for a set of PIDs +} +\details{ +Note this is a useful alternative to pid_to_eml_datatable and +pid_to_eml_other_entity because it can create multiple of objects at once. +} +\examples{ +\dontrun{ +#Generate EML otherEntities for four pids +TEST_pid_to_eml_entity(mn, + entityType = 'otherEntity', + pids = c('pid1', 'pid2', 'pid3', 'pid4'), + names = c('name1', 'name2', 'name3', 'name4'), + descriptions = c('description1', 'description2', + 'description3', 'description4'), + attributes = list(atTbl1, atTbl2, atTbl3, atTbl4), + factors = rep(factors1, factors2, NA, NA))) +} +} diff --git a/man/pid_to_eml_other_entity.Rd b/man/pid_to_eml_other_entity.Rd index d371226..1ec482f 100644 --- a/man/pid_to_eml_other_entity.Rd +++ b/man/pid_to_eml_other_entity.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/eml.R \name{pid_to_eml_other_entity} \alias{pid_to_eml_other_entity} -\title{eml.R} +\title{Create EML otherEntity objects for a set of PIDs} \usage{ pid_to_eml_other_entity(mn, pids) } @@ -15,10 +15,6 @@ pid_to_eml_other_entity(mn, pids) (list of otherEntity) The otherEntity object(s) } \description{ -Helpers for creating EML. -Create EML otherEntity objects for a set of PIDs -} -\details{ Note this is a wrapper around sysmeta_to_other_entity which handles the task of creating the EML otherEntity. } From 879edbcc12f49536855ac888010b9f84daf5ccce Mon Sep 17 00:00:00 2001 From: Adam Reevesman Date: Thu, 17 May 2018 17:15:19 -0700 Subject: [PATCH 2/3] cleaned up whitespace --- R/eml.R | 4 ---- 1 file changed, 4 deletions(-) diff --git a/R/eml.R b/R/eml.R index ee6f7a9..0f93e88 100644 --- a/R/eml.R +++ b/R/eml.R @@ -62,17 +62,13 @@ pid_to_eml_entity <- function(mn, "'descriptions' must be NULL or have same length as pids") } - work <- function(i, some_list){ mn <- some_list$mn entity_type <- some_list$entity_type pid <- some_list$pid[[i]] name <- some_list$name[[i]] - description = some_list$description[[i]] - - attribute_table <- l$attribute_table[[i]] factors_table <- l$factors_table[[i]] From d9b00b20fc6f5df7a06431b9eb7d87f3971fd530 Mon Sep 17 00:00:00 2001 From: maier-m Date: Fri, 25 May 2018 13:04:36 -0700 Subject: [PATCH 3/3] updated example --- NAMESPACE | 4 - R/eml.R | 334 ++++++----------------------- man/pid_to_eml_datatable.Rd | 18 +- man/pid_to_eml_entity.Rd | 42 ++-- man/pid_to_eml_other_entity.Rd | 15 +- man/set_other_entities.Rd | 16 +- man/sysmeta_to_eml_other_entity.Rd | 15 +- tests/testthat/test_eml.R | 65 +++--- 8 files changed, 124 insertions(+), 385 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 70a142d..869a3fa 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -40,9 +40,7 @@ export(mdq_run) export(new_uuid) export(object_exists) export(parse_resource_map) -export(pid_to_eml_datatable) export(pid_to_eml_entity) -export(pid_to_eml_other_entity) export(pid_to_eml_physical) export(publish_object) export(publish_update) @@ -50,12 +48,10 @@ export(remove_public_read) export(set_abstract) export(set_access) export(set_file_name) -export(set_other_entities) export(set_public_read) export(set_rights_and_access) export(set_rights_holder) export(show_indexing_status) -export(sysmeta_to_eml_other_entity) export(sysmeta_to_eml_physical) export(update_object) export(update_resource_map) diff --git a/R/eml.R b/R/eml.R index 0f93e88..212138a 100644 --- a/R/eml.R +++ b/R/eml.R @@ -3,172 +3,88 @@ #' Helpers for creating EML. -#' Create EML dataTable or otherEntity objects for a set of PIDs -#' -#' Note this is a useful alternative to pid_to_eml_datatable and -#' pid_to_eml_other_entity because it can create multiple of objects at once. +#' Create EML entity from a DataONE pid #' #' @param mn (MNode) Member Node where the PID is associated with an object. -#' @param pids (character) The PIDs of the objects to create the entities for. Either a vector or list of names of the objects. Note that \code{pids[i]} or \code{pids[[i]]} must be a character vector of length one. -#' @param entityType (character) What kind of objects to create from the input. Either "dataTable" or "otherEntity". -#' @param names (character) Either a vector or list of names of the objects. Note that \code{names[i]} or \code{names[[i]]} must be \code{NA} or a character vector of length one and must correspond to \code{pids[i]} or \code{pids[[i]]}. -#' @param descriptions (character) Either a vector or list of names of the objects. Note that \code{descriptions[i]} or \code{descriptions[[i]]} must be \code{NA} if no entityDescription is needed for a particular pid or a character vector of length one and must correspond to \code{pids[i]} or \code{pids[[i]]}. -#' @param attributes (list) Required if entityType is set to "dataTable". \code{NULL} if entityType is set to "otherEntity" and no attribute tables are needed. A list of attribute tables. Note that \code{attributes[[i]]} must be a dataframe or possibly \code{NA} in the case where \code{entityType} is set to "otherEntity" and must correspond to \code{pids[i]} or \code{pids[[i]]}. -#' @param factors (list) A list of enumerated domain tables. \code{NULL} if no enumerated domains exist. Note that \code{factors[[i]]} must be a dataframe or \code{NA} in the case where enumerated domains are not present in the data. Must correspond to \code{pids[i]} or \code{pids[[i]]}. -#' @param validateAttributes (logical) If set to FALSE or if attributes are not passed into the function, attribute validation test will not run. -#' -#' @return (list) The otherEntity or dataTable object(s) +#' @param pid (character) The PID of the object to create the sub-tree for. +#' @param entityType (character) What kind of objects to create from the input. Either "dataTable", +#' "spatialRaster", "spatialVector", "storedProcedure", "view", "otherEntity". +#' @param ... (optional) Additional arguments to be passed to \code{new(entityType, ...)}. See example +#' +#' @return (list) The entity object #' @export #' #' @examples #' \dontrun{ -#' #Generate EML otherEntities for four pids -#' TEST_pid_to_eml_entity(mn, -#' entityType = 'otherEntity', -#' pids = c('pid1', 'pid2', 'pid3', 'pid4'), -#' names = c('name1', 'name2', 'name3', 'name4'), -#' descriptions = c('description1', 'description2', -#' 'description3', 'description4'), -#' attributes = list(atTbl1, atTbl2, atTbl3, atTbl4), -#' factors = rep(factors1, factors2, NA, NA))) +#' #Generate EML otherEntity +#' pid_to_eml_entity(mn, +#' pid, +#' entityType = "otherEntity", +#' entityName = "Entity Name", +#' entityDescription = "Description about entity") +#' #' } pid_to_eml_entity <- function(mn, - pids, - entityType, - names, - descriptions = NULL, - attributes = NULL, - factors = NULL, - validateAttributes = TRUE) { + pid, + entityType = "otherEntity", + ...) { stopifnot(is(mn, "MNode")) - stopifnot(is.character(pids), - all(nchar(pids)) > 0) - - if ( (length(pids) != length(attributes)) & (!is.null(attributes)) ){ - stop(call. = FALSE, - "'attributes' must be NULL or have same length as pids") - } - if ( (length(pids) != length(factors)) & (!is.null(factors)) ){ - stop(call. = FALSE, - "'factors' must be NULL or have same length as pids") - } - if ( (length(pids) != length(names)) & (!is.null(names)) ){ - stop(call. = FALSE, - "'names' must be NULL or have same length as pids") - } - if ( (length(pids) != length(descriptions)) & (!is.null(descriptions)) ){ - stop(call. = FALSE, - "'descriptions' must be NULL or have same length as pids") - } - - work <- function(i, some_list){ - - mn <- some_list$mn - entity_type <- some_list$entity_type - pid <- some_list$pid[[i]] - name <- some_list$name[[i]] - description = some_list$description[[i]] - attribute_table <- l$attribute_table[[i]] - factors_table <- l$factors_table[[i]] - - entity <- new(Class = entity_type) - entity@scope <- new("xml_attribute", "document") - entity@physical@.Data[[1]] <- pid_to_eml_physical(mn, pid)[[1]] - if (entityType == "otherEntity"){ - entity@entityType <- "other" - } - - if (is.na(name)) { - stop(call. = FALSE, - paste("'Name' of entity ", i," must be specified in the function call", sep = '')) - } + stopifnot(is.character(pid), + nchar(pid) > 0) - entity@entityName <- name + stopifnot(entityType %in% c("dataTable", + "spatialRaster", + "spatialVector", + "storedProcedure", + "view", + "otherEntity")) - if (!is.na(description)) { - entity@entityDescription <- description - } + systmeta <- getSystemMetadata(mn, pid) + physical <- sysmeta_to_eml_physical(systmeta) - if(class(attribute_table) == "data.frame") { - stopifnot(is.data.frame(attribute_table)) + # Create entity + entity <- new(entityType, + physical = pid_to_eml_physical(mn, pid), + ...) - if (class(factors_table) != "data.frame") { - attribute_list <- set_attributes(attribute_table) - } - else { - stopifnot(is.data.frame(factors_table)) - attribute_list <- set_attributes(attribute_table, factors_table) - } + # Set entity slots + if (length(slot(entity, "id")) == 0) { + entity@id <- new("xml_attribute", systmeta@identifier) + } - if (validateAttributes == TRUE) { - stopifnot(eml_validate_attributes(attribute_list)) - } + if (length(slot(entity, "scope")) == 0) { + entity@scope <- new("xml_attribute", "document") + } - entity@attributeList <- attribute_list - } + if (length(slot(entity, "entityName")) == 0) { - else { - if (entity_type == "dataTable"){ - stop(call. = FALSE, - "An attribute table must be provided when creating a dataTable") - } + if (!is.na(systmeta@fileName)) { + entity@entityName <- new("entityName", systmeta@fileName) } + } - entity + if (entityType == "otherEntity" && length(slot(entity, "entityType")) == 0) { + entity@entityType <- "Other" } - l <- list(mn = mn, - entity_type = entityType, - pid = as.list(pids), - name = as.list(names), - description = ifelse(rep(is.null(descriptions),length(pids)), - rep(list(NA), - length(pids)), - as.list(descriptions)), - attribute_table = ifelse(rep(is.null(attributes),length(pids)), - rep(list(NA), - length(pids)), - attributes), - factors_table = ifelse(rep(is.null(factors),length(pids)), - rep(list(NA), - length(pids)), - factors)) - - lapply(seq_along(as.list(pids)), work, some_list = l) + return(entity) } -#' Create EML otherEntity objects for a set of PIDs -#' -#' Note this is a wrapper around sysmeta_to_other_entity which handles the task of -#' creating the EML otherEntity. +#' This function is deprecated. See \link{pid_to_eml_entity}. #' #' @param mn (MNode) Member Node where the PID is associated with an object. #' @param pids (character) The PID of the object to create the sub-tree for. #' -#' @return (list of otherEntity) The otherEntity object(s) -#' @export -#' -#' @examples -#' \dontrun{ -#' # Generate EML otherEntity objects for all the data in a package -#' pkg <- get_package(mn, pid) -#' pid_to_other_entity(mn, pkg$data) -#' } pid_to_eml_other_entity <- function(mn, pids) { - stopifnot(is(mn, "MNode")) - stopifnot(is.character(pids), - all(nchar(pids)) > 0) - - sysmeta <- lapply(pids, function(pid) { getSystemMetadata(mn, pid) }) - sysmeta_to_eml_other_entity(sysmeta) + .Deprecated(new = "pid_to_eml_entity", + package = "arcticdtautils", + old = "pid_to_eml_other_entity") } -#' Create an EML code\code{dataTable} object for a given PID. -#' -#' This function generates an \code{attributeList} and \code{physical} and constructs a \code{dataTable}. +#' This function is deprecated. See \link{pid_to_eml_entity}. #' #' @param mn (MNode) Member Node where the PID is associated with an object. #' @param pid (character) The PID of the object to create the \code{dataTable} for. @@ -176,19 +92,8 @@ pid_to_eml_other_entity <- function(mn, pids) { #' @param factors (data.frame) Optional data frame of enumerated attribute values (factors). Follows the convention in \link[EML]{set_attributes}. #' @param name (character) Optional field to specify \code{entityName}, otherwise will be extracted from system metadata. #' @param description (character) Optional field to specify \code{entityDescription}, otherwise will match name. -#' @param validateAttributes (logical) If set to FALSE or if attributes are not passed into the function, attribute validation test will not run. +#' @param validateAttributes (logical) If set to FALSE or if attributes are not passed into the function, attribute validatio #' -#' @return (dataTable) The \code{dataTable} object -#' @export -#' -#' @examples -#' \dontrun{ -#' # Generate a dataTable for a given pid -#' attributes <- create_dummy_attributes_dataframe(10) -#' name <- "1234.csv" -#' description <- "A description of this entity." -#' dataTable <- pid_to_eml_datatable(mn, pid, attributes, name=name, description=description) -#' } pid_to_eml_datatable <- function(mn, pid, attributes = NULL, @@ -196,45 +101,9 @@ pid_to_eml_datatable <- function(mn, name = NULL, description = NULL, validateAttributes = TRUE) { - stopifnot(is(mn, "MNode")) - stopifnot(is.character(pid), - nchar(pid) > 0) - - dataTable <- new("dataTable", physical = pid_to_eml_physical(mn, pid)) - - if(!is.null(attributes)) { - stopifnot(is.data.frame(attributes)) - - if (is.null(factors)) { - attributes <- set_attributes(attributes) - } else { - stopifnot(is.data.frame(factors)) - attributes <- set_attributes(attributes, factors) - } - - if (validateAttributes == TRUE) { - stopifnot(eml_validate_attributes(attributes)) - } - - dataTable@attributeList <- attributes - } - - if (is.null(name)) { - name <- getSystemMetadata(mn, pid)@fileName - - if (is.na(name)) { - stop(call. = FALSE, - "'Name' must either be specified in the function call or must exist in the system metadata.") - } - } - - dataTable@entityName <- name - - if (!is.null(description)) { - dataTable@entityDescription <- description - } - - dataTable + .Deprecated(new = "pid_to_eml_entity", + package = "arcticdtautils", + old = "pid_to_eml_other_entity") } @@ -264,45 +133,14 @@ pid_to_eml_physical <- function(mn, pids) { sysmeta_to_eml_physical(sysmeta) } -#' Create an EML otherEntity for the given object from the System Metadata +#' This function is deprecated. See \link{pid_to_eml_entity}. #' #' @param sysmeta (SystemMetadata) One or more System Metadata objects #' -#' @return (list of otherEntity) The otherEntity object(s) -#' @export -#' -#' @examples -#' \dontrun{ -#' # Generate EML otherEntity objects for all the data in a package -#' pkg <- get_package(mn, pid) -#' sm <- lapply(pkg$data, function(pid) { getSystemMetadata(mn, pid) }) -#' sysmeta_to_other_entity(sm) -#'} sysmeta_to_eml_other_entity <- function(sysmeta) { - work <- function(x) { - other_entity <- new("otherEntity") - other_entity@id <- new("xml_attribute", x@identifier) - other_entity@scope <- new("xml_attribute", "document") - - if (is.na(x@fileName)) { - other_entity@entityName <- new("entityName", "NA") - } - else { - other_entity@entityName <- new("entityName", x@fileName) - } - - other_entity@entityType <- "Other" - - phys <- sysmeta_to_eml_physical(x) - other_entity@physical <- new("ListOfphysical", phys) - - other_entity - } - - - if (!is(sysmeta, "list")) sysmeta <- list(sysmeta) - - lapply(sysmeta, work) + .Deprecated(new = "pid_to_eml_entity", + package = "arcticdtautils", + old = "sysmeta_to_other_entity") } @@ -372,56 +210,16 @@ sysmeta_to_eml_physical <- function(sysmeta) { lapply(sysmeta, work) } -#' Creates and sets EML otherEntity elements to an existing EML document, -#' replacing any existing otherEntities -#' -#' This function is slow because it needs get the System Metadata for each -#' element of `pids` in order to get the fileName, checksum, etc. +#' This function is deprecated. See \link{pid_to_eml_entity}. #' #' @param mn (MNode) The Member Node the objects exist on. #' @param path (character) The location on disk of the EML file. #' @param pids (character) One or more PIDs for the objects. #' -#' @return (character) The path to the updated EML file. -#' @export -#' -#' @examples -#' \dontrun{ -#' mn <- MNode(...) # Set up a connection to an MN -#' eml_path <- "/path/to/your/eml.xml" -#' set_other_entities(mn, eml_path, "a_data_pid") -#' } set_other_entities <- function(mn, path, pids) { - stopifnot(is(mn, "MNode")) - stopifnot(file.exists(path)) - stopifnot(all(is.character(pids)), - all(nchar(pids) > 0)) - - if (length(pids) == 0) { - message("Skipped adding EML otherEntity elements because no pids were specified.") - return(path) - } - - # Get the metadata document from the MN and load it as an EML document - doc <- EML::read_eml(path) - stopifnot(is(doc, "eml")) - - message("Setting EML otherEntity elements. This can take a while if there are lots of PIDs...") - - # Generate otherEntity elements - other_entities <- pid_to_eml_other_entity(mn, pids) - - # Concatenate the existing and new otherEntity elements and put back in the - # EML - if (length(other_entities) > 0) { - doc@dataset@otherEntity <- new("ListOfotherEntity", other_entities) - } - - # Write the modified document back to disk and stop - EML::write_eml(doc, path) - stopifnot(EML::eml_validate(path) == TRUE) - - path + .Deprecated(new = "pid_to_eml_entity", + package = "arcticdtautils", + old = "set_other_entities") } #' Get the Metacat docid for the given identifier @@ -739,10 +537,10 @@ eml_individual_name <- function(given_names=NULL, sur_name) { stopifnot(all(sapply(given_names, is.character))) stopifnot(all(lengths(given_names) > 0)) - givens <- lapply(given_names, function(given_name) { - x <- new("givenName") - x@.Data <- given_name - x + givens <- lapply(given_names, function(given_name) { + x <- new("givenName") + x@.Data <- given_name + x }) indiv_name@givenName <- new("ListOfgivenName", givens) diff --git a/man/pid_to_eml_datatable.Rd b/man/pid_to_eml_datatable.Rd index 7aee3b7..55020c1 100644 --- a/man/pid_to_eml_datatable.Rd +++ b/man/pid_to_eml_datatable.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/eml.R \name{pid_to_eml_datatable} \alias{pid_to_eml_datatable} -\title{Create an EML code\code{dataTable} object for a given PID.} +\title{This function is deprecated. See \link{pid_to_eml_entity}.} \usage{ pid_to_eml_datatable(mn, pid, attributes = NULL, factors = NULL, name = NULL, description = NULL, validateAttributes = TRUE) @@ -20,20 +20,8 @@ pid_to_eml_datatable(mn, pid, attributes = NULL, factors = NULL, \item{description}{(character) Optional field to specify \code{entityDescription}, otherwise will match name.} -\item{validateAttributes}{(logical) If set to FALSE or if attributes are not passed into the function, attribute validation test will not run.} -} -\value{ -(dataTable) The \code{dataTable} object +\item{validateAttributes}{(logical) If set to FALSE or if attributes are not passed into the function, attribute validatio} } \description{ -This function generates an \code{attributeList} and \code{physical} and constructs a \code{dataTable}. -} -\examples{ -\dontrun{ -# Generate a dataTable for a given pid -attributes <- create_dummy_attributes_dataframe(10) -name <- "1234.csv" -description <- "A description of this entity." -dataTable <- pid_to_eml_datatable(mn, pid, attributes, name=name, description=description) -} +This function is deprecated. See \link{pid_to_eml_entity}. } diff --git a/man/pid_to_eml_entity.Rd b/man/pid_to_eml_entity.Rd index 8faa7e1..3d9e4e1 100644 --- a/man/pid_to_eml_entity.Rd +++ b/man/pid_to_eml_entity.Rd @@ -4,47 +4,33 @@ \alias{pid_to_eml_entity} \title{eml.R} \usage{ -pid_to_eml_entity(mn, pids, entityType, names, descriptions = NULL, - attributes = NULL, factors = NULL, validateAttributes = TRUE) +pid_to_eml_entity(mn, pid, entityType = "otherEntity", ...) } \arguments{ \item{mn}{(MNode) Member Node where the PID is associated with an object.} -\item{pids}{(character) The PIDs of the objects to create the entities for. Either a vector or list of names of the objects. Note that \code{pids[i]} or \code{pids[[i]]} must be a character vector of length one.} +\item{pid}{(character) The PID of the object to create the sub-tree for.} -\item{entityType}{(character) What kind of objects to create from the input. Either "dataTable" or "otherEntity".} +\item{entityType}{(character) What kind of objects to create from the input. Either "dataTable", +"spatialRaster", "spatialVector", "storedProcedure", "view", "otherEntity".} -\item{names}{(character) Either a vector or list of names of the objects. Note that \code{names[i]} or \code{names[[i]]} must be \code{NA} or a character vector of length one and must correspond to \code{pids[i]} or \code{pids[[i]]}.} - -\item{descriptions}{(character) Either a vector or list of names of the objects. Note that \code{descriptions[i]} or \code{descriptions[[i]]} must be \code{NA} if no entityDescription is needed for a particular pid or a character vector of length one and must correspond to \code{pids[i]} or \code{pids[[i]]}.} - -\item{attributes}{(list) Required if entityType is set to "dataTable". \code{NULL} if entityType is set to "otherEntity" and no attribute tables are needed. A list of attribute tables. Note that \code{attributes[[i]]} must be a dataframe or possibly \code{NA} in the case where \code{entityType} is set to "otherEntity" and must correspond to \code{pids[i]} or \code{pids[[i]]}.} - -\item{factors}{(list) A list of enumerated domain tables. \code{NULL} if no enumerated domains exist. Note that \code{factors[[i]]} must be a dataframe or \code{NA} in the case where enumerated domains are not present in the data. Must correspond to \code{pids[i]} or \code{pids[[i]]}.} - -\item{validateAttributes}{(logical) If set to FALSE or if attributes are not passed into the function, attribute validation test will not run.} +\item{...}{(optional) Additional arguments to be passed to \code{new(entityType, ...)}. See example} } \value{ -(list) The otherEntity or dataTable object(s) +(list) The entity object } \description{ Helpers for creating EML. -Create EML dataTable or otherEntity objects for a set of PIDs -} -\details{ -Note this is a useful alternative to pid_to_eml_datatable and -pid_to_eml_other_entity because it can create multiple of objects at once. +Create EML entity from a DataONE pid } \examples{ \dontrun{ -#Generate EML otherEntities for four pids -TEST_pid_to_eml_entity(mn, - entityType = 'otherEntity', - pids = c('pid1', 'pid2', 'pid3', 'pid4'), - names = c('name1', 'name2', 'name3', 'name4'), - descriptions = c('description1', 'description2', - 'description3', 'description4'), - attributes = list(atTbl1, atTbl2, atTbl3, atTbl4), - factors = rep(factors1, factors2, NA, NA))) +#Generate EML otherEntity +pid_to_eml_entity(mn, + pid, + entityType = "otherEntity", + entityName = "Entity Name", + entityDescription = "Description about entity") + } } diff --git a/man/pid_to_eml_other_entity.Rd b/man/pid_to_eml_other_entity.Rd index 1ec482f..ebc80fa 100644 --- a/man/pid_to_eml_other_entity.Rd +++ b/man/pid_to_eml_other_entity.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/eml.R \name{pid_to_eml_other_entity} \alias{pid_to_eml_other_entity} -\title{Create EML otherEntity objects for a set of PIDs} +\title{This function is deprecated. See \link{pid_to_eml_entity}.} \usage{ pid_to_eml_other_entity(mn, pids) } @@ -11,17 +11,6 @@ pid_to_eml_other_entity(mn, pids) \item{pids}{(character) The PID of the object to create the sub-tree for.} } -\value{ -(list of otherEntity) The otherEntity object(s) -} \description{ -Note this is a wrapper around sysmeta_to_other_entity which handles the task of -creating the EML otherEntity. -} -\examples{ -\dontrun{ -# Generate EML otherEntity objects for all the data in a package -pkg <- get_package(mn, pid) -pid_to_other_entity(mn, pkg$data) -} +This function is deprecated. See \link{pid_to_eml_entity}. } diff --git a/man/set_other_entities.Rd b/man/set_other_entities.Rd index a5c9f40..e1c2ec4 100644 --- a/man/set_other_entities.Rd +++ b/man/set_other_entities.Rd @@ -2,8 +2,7 @@ % Please edit documentation in R/eml.R \name{set_other_entities} \alias{set_other_entities} -\title{Creates and sets EML otherEntity elements to an existing EML document, -replacing any existing otherEntities} +\title{This function is deprecated. See \link{pid_to_eml_entity}.} \usage{ set_other_entities(mn, path, pids) } @@ -14,17 +13,6 @@ set_other_entities(mn, path, pids) \item{pids}{(character) One or more PIDs for the objects.} } -\value{ -(character) The path to the updated EML file. -} \description{ -This function is slow because it needs get the System Metadata for each -element of `pids` in order to get the fileName, checksum, etc. -} -\examples{ -\dontrun{ -mn <- MNode(...) # Set up a connection to an MN -eml_path <- "/path/to/your/eml.xml" -set_other_entities(mn, eml_path, "a_data_pid") -} +This function is deprecated. See \link{pid_to_eml_entity}. } diff --git a/man/sysmeta_to_eml_other_entity.Rd b/man/sysmeta_to_eml_other_entity.Rd index a3d094d..2cde59e 100644 --- a/man/sysmeta_to_eml_other_entity.Rd +++ b/man/sysmeta_to_eml_other_entity.Rd @@ -2,24 +2,13 @@ % Please edit documentation in R/eml.R \name{sysmeta_to_eml_other_entity} \alias{sysmeta_to_eml_other_entity} -\title{Create an EML otherEntity for the given object from the System Metadata} +\title{This function is deprecated. See \link{pid_to_eml_entity}.} \usage{ sysmeta_to_eml_other_entity(sysmeta) } \arguments{ \item{sysmeta}{(SystemMetadata) One or more System Metadata objects} } -\value{ -(list of otherEntity) The otherEntity object(s) -} \description{ -Create an EML otherEntity for the given object from the System Metadata -} -\examples{ -\dontrun{ -# Generate EML otherEntity objects for all the data in a package -pkg <- get_package(mn, pid) -sm <- lapply(pkg$data, function(pid) { getSystemMetadata(mn, pid) }) -sysmeta_to_other_entity(sm) -} +This function is deprecated. See \link{pid_to_eml_entity}. } diff --git a/tests/testthat/test_eml.R b/tests/testthat/test_eml.R index d93fb19..9967a54 100644 --- a/tests/testthat/test_eml.R +++ b/tests/testthat/test_eml.R @@ -97,47 +97,52 @@ test_that("a project can be created with multiple personnel, an abstract can be expect_equal(xml2::xml_text(project@funding@para[[2]]@.Data[[1]]), "I won a second award, wow") }) -test_that("an other entity can be added from a pid", { +test_that("a dataTable and otherEntity can be added from a pid", { if (!is_token_set(mn)) { skip("No token set. Skipping test.") } data_path <- tempfile() writeLines(LETTERS, data_path) - pid <- publish_object(mn, data_path, "text/plain") - - eml_path <- tempfile() - file.copy(file.path(system.file(package = "arcticdatautils"), "example-eml.xml"), eml_path) - doc <- EML::read_eml(eml_path) - doc@dataset@otherEntity <- new("ListOfotherEntity", list()) + pid <- publish_object(mn, data_path, "text/csv") - set_other_entities(mn, eml_path, pid) + eml_path <- file.path(system.file(package = "arcticdatautils"), "example-eml.xml") doc <- EML::read_eml(eml_path) - testthat::expect_length(doc@dataset@otherEntity, 1) -}) + dummy_factors <- c("factor 1", "factor 2") + dummy_attributes <- create_dummy_attributes_dataframe(10, dummy_factors) + dummy_enumeratedDomain <- create_dummy_enumeratedDomain_dataframe(dummy_factors) + + dummy_attributeList <- EML::set_attributes(dummy_attributes, factors = dummy_enumeratedDomain) + dummy_entityName <- "Test_Name" + dummy_entityDescription <- "Test_Description" + + # Create an otherEntity + OE <- pid_to_eml_entity(mn, pid, + entityName = dummy_entityName, + entityDescription = dummy_entityDescription, + attributeList = dummy_attributeList) + expect_s4_class(OE, "otherEntity") + expect_true(slot(OE, "entityName") == dummy_entityName) + expect_true(slot(OE, "entityDescription") == dummy_entityDescription) + + # Create a dataTable + DT <- pid_to_eml_entity(mn, pid, + entityType = "dataTable", + entityName = dummy_entityName, + entityDescription = dummy_entityDescription, + attributeList = dummy_attributeList) + expect_s4_class(DT, "dataTable") + expect_true(slot(DT, "entityName") == dummy_entityName) + expect_true(slot(DT, "entityDescription") == dummy_entityDescription) + + doc@dataset@otherEntity[[1]] <- OE + expect_true(EML::eml_validate(doc)) + + doc@dataset@dataTable[[1]] <- DT + expect_true(EML::eml_validate(doc)) -test_that("a data table can be added from a pid", { - if (!is_token_set(mn)) { - skip("No token set. Skipping test.") - } - - data_path <- tempfile() - writeLines(LETTERS, data_path) - pid <- publish_object(mn, data_path, "text/csv") - - eml_path <- file.path(system.file(package = "arcticdatautils"), "example-eml.xml") - - doc <- EML::read_eml(eml_path) - - factors <- c("factor 1", "factor 2") - dummy_attributes <- create_dummy_attributes_dataframe(10, factors) - dummy_enumeratedDomain <- create_dummy_enumeratedDomain_dataframe(factors) - doc@dataset@dataTable <- as(list(pid_to_eml_datatable(mn, pid, dummy_attributes, dummy_enumeratedDomain)), "ListOfdataTable") - - testthat::expect_length(doc@dataset@dataTable, 1) - unlink(data_path) })