use of edu.harvard.iq.dataverse.Dataset in project dataverse by IQSS.
the class Datasets method createAssignment.
/**
* @todo Make this real. Currently only used for API testing. Copied from
* the equivalent API endpoint for dataverses and simplified with values
* hard coded.
*/
@POST
@Path("{identifier}/assignments")
public Response createAssignment(String userOrGroup, @PathParam("identifier") String id, @QueryParam("key") String apiKey) {
boolean apiTestingOnly = true;
if (apiTestingOnly) {
return error(Response.Status.FORBIDDEN, "This is only for API tests.");
}
try {
Dataset dataset = findDatasetOrDie(id);
RoleAssignee assignee = findAssignee(userOrGroup);
if (assignee == null) {
return error(Response.Status.BAD_REQUEST, "Assignee not found");
}
DataverseRole theRole = rolesSvc.findBuiltinRoleByAlias("admin");
String privateUrlToken = null;
return ok(json(execCommand(new AssignRoleCommand(assignee, theRole, dataset, createDataverseRequest(findUserOrDie()), privateUrlToken))));
} catch (WrappedResponse ex) {
logger.log(Level.WARNING, "Can''t create assignment: {0}", ex.getMessage());
return ex.getResponse();
}
}
use of edu.harvard.iq.dataverse.Dataset in project dataverse by IQSS.
the class Datasets method returnToAuthor.
@POST
@Path("{id}/returnToAuthor")
public Response returnToAuthor(@PathParam("id") String idSupplied, String jsonBody) {
if (jsonBody == null || jsonBody.isEmpty()) {
return error(Response.Status.BAD_REQUEST, "You must supply JSON to this API endpoint and it must contain a reason for returning the dataset.");
}
StringReader rdr = new StringReader(jsonBody);
JsonObject json = Json.createReader(rdr).readObject();
try {
Dataset dataset = findDatasetOrDie(idSupplied);
String reasonForReturn = null;
reasonForReturn = json.getString("reasonForReturn");
// TODO: Once we add a box for the curator to type into, pass the reason for return to the ReturnDatasetToAuthorCommand and delete this check and call to setReturnReason on the API side.
if (reasonForReturn == null || reasonForReturn.isEmpty()) {
return error(Response.Status.BAD_REQUEST, "You must enter a reason for returning a dataset to the author(s).");
}
AuthenticatedUser authenticatedUser = findAuthenticatedUserOrDie();
Dataset updatedDataset = execCommand(new ReturnDatasetToAuthorCommand(createDataverseRequest(authenticatedUser), dataset, reasonForReturn));
boolean inReview = updatedDataset.isLockedFor(DatasetLock.Reason.InReview);
JsonObjectBuilder result = Json.createObjectBuilder();
result.add("inReview", inReview);
result.add("message", "Dataset id " + updatedDataset.getId() + " has been sent back to the author(s).");
return ok(result);
} catch (WrappedResponse wr) {
return wr.getResponse();
}
}
use of edu.harvard.iq.dataverse.Dataset in project dataverse by IQSS.
the class Datasets method submitForReview.
@POST
@Path("{id}/submitForReview")
public Response submitForReview(@PathParam("id") String idSupplied) {
try {
Dataset updatedDataset = execCommand(new SubmitDatasetForReviewCommand(createDataverseRequest(findUserOrDie()), findDatasetOrDie(idSupplied)));
JsonObjectBuilder result = Json.createObjectBuilder();
boolean inReview = updatedDataset.isLockedFor(DatasetLock.Reason.InReview);
result.add("inReview", inReview);
result.add("message", "Dataset id " + updatedDataset.getId() + " has been submitted for review.");
return ok(result);
} catch (WrappedResponse wr) {
return wr.getResponse();
}
}
use of edu.harvard.iq.dataverse.Dataset in project dataverse by IQSS.
the class WorldMapRelatedData method getWorldMapDatafileInfo.
/**
* Retrieve FileMetadata for Use by WorldMap.
* This includes information about the DataFile, Dataset, DatasetVersion, and Dataverse
*
* @param jsonTokenData
* @param request
* @return
*/
@POST
// + "{worldmap_token}")
@Path(GET_WORLDMAP_DATAFILE_API_PATH_FRAGMENT)
public Response getWorldMapDatafileInfo(String jsonTokenData, @Context HttpServletRequest request) {
// , @PathParam("worldmap_token") String worldmapTokenParam) {
if (true) {
// return okResponse("Currently deactivated");
// return okResponse("remote server: " + request.getRemoteAddr());
}
logger.info("API call: getWorldMapDatafileInfo");
// ----------------------------------
// Auth check: Parse the json message and check for a valid GEOCONNECT_TOKEN_KEY and GEOCONNECT_TOKEN_VALUE
// -- For testing, the GEOCONNECT_TOKEN_VALUE will be dynamic, found in the db
// ----------------------------------
logger.info("(1) jsonTokenData: " + jsonTokenData);
// Parse JSON
JsonObject jsonTokenInfo;
try (StringReader rdr = new StringReader(jsonTokenData)) {
jsonTokenInfo = Json.createReader(rdr).readObject();
} catch (JsonParsingException jpe) {
logger.log(Level.SEVERE, "Json: " + jsonTokenData);
return error(Response.Status.BAD_REQUEST, "Error parsing Json: " + jpe.getMessage());
}
logger.info("(1a) jsonTokenInfo: " + jsonTokenInfo);
// Retrieve token string
String worldmapTokenParam = this.retrieveTokenValueFromJson(jsonTokenInfo);
logger.info("(1b) token from JSON: " + worldmapTokenParam);
if (worldmapTokenParam == null) {
return error(Response.Status.BAD_REQUEST, "Token not found in JSON request.");
}
// Retrieve WorldMapToken and make sure it is valid
//
WorldMapToken wmToken = tokenServiceBean.retrieveAndRefreshValidToken(worldmapTokenParam);
logger.info("(2) token retrieved from db: " + wmToken);
if (wmToken == null) {
return error(Response.Status.UNAUTHORIZED, "No access. Invalid token.");
}
// Make sure the token's User still has permissions to access the file
//
logger.info("(3) check permissions");
if (!(tokenServiceBean.canTokenUserEditFile(wmToken))) {
tokenServiceBean.expireToken(wmToken);
return error(Response.Status.UNAUTHORIZED, "No access. Invalid token.");
}
// (1) Retrieve token connected data: DataverseUser, DataFile
//
// Make sure token user and file are still available
//
AuthenticatedUser dvUser = wmToken.getDataverseUser();
if (dvUser == null) {
return error(Response.Status.NOT_FOUND, "DataverseUser not found for token");
}
DataFile dfile = wmToken.getDatafile();
if (dfile == null) {
return error(Response.Status.NOT_FOUND, "DataFile not found for token");
}
// (1a) Retrieve FileMetadata
FileMetadata dfile_meta = dfile.getFileMetadata();
if (dfile_meta == null) {
return error(Response.Status.NOT_FOUND, "FileMetadata not found");
}
// (2) Now get the dataset and the latest DatasetVersion
Dataset dset = dfile.getOwner();
if (dset == null) {
return error(Response.Status.NOT_FOUND, "Owning Dataset for this DataFile not found");
}
// (2a) latest DatasetVersion
// !! How do you check if the lastest version has this specific file?
//
DatasetVersion dset_version = dset.getLatestVersion();
if (dset_version == null) {
return error(Response.Status.NOT_FOUND, "Latest DatasetVersion for this DataFile not found");
}
// (3) get Dataverse
Dataverse dverse = dset.getOwner();
if (dverse == null) {
return error(Response.Status.NOT_FOUND, "Dataverse for this DataFile's Dataset not found");
}
// (4) Roll it all up in a JSON response
final JsonObjectBuilder jsonData = Json.createObjectBuilder();
// ------------------------------------
if (dfile.isShapefileType()) {
jsonData.add("mapping_type", "shapefile");
} else if (dfile.isTabularData()) {
jsonData.add("mapping_type", "tabular");
} else {
logger.log(Level.SEVERE, "This was neither a Shapefile nor a Tabular data file. DataFile id: " + dfile.getId());
return error(Response.Status.BAD_REQUEST, "Sorry! This file does not have mapping data. Please contact the Dataverse administrator. DataFile id: " + dfile.getId());
}
// ------------------------------------
// DataverseUser Info
// ------------------------------------
jsonData.add("dv_user_id", dvUser.getId());
jsonData.add("dv_username", dvUser.getUserIdentifier());
jsonData.add("dv_user_email", dvUser.getEmail());
// ------------------------------------
// Dataverse URLs to this server
// ------------------------------------
String serverName = systemConfig.getDataverseSiteUrl();
jsonData.add("return_to_dataverse_url", dset_version.getReturnToFilePageURL(serverName, dset, dfile));
jsonData.add("datafile_download_url", dfile.getMapItFileDownloadURL(serverName));
// ------------------------------------
// Dataverse
// ------------------------------------
// jsonData.add("dataverse_installation_name", "Harvard Dataverse"); // todo / fix
// is this enough to distinguish a dataverse installation?
jsonData.add("dataverse_installation_name", systemConfig.getDataverseSiteUrl());
jsonData.add("dataverse_id", dverse.getId());
jsonData.add("dataverse_name", dverse.getName());
String dataverseDesc = dverse.getDescription();
if (dataverseDesc == null || dataverseDesc.equalsIgnoreCase("")) {
dataverseDesc = "";
}
jsonData.add("dataverse_description", dataverseDesc);
// ------------------------------------
// Dataset Info
// ------------------------------------
jsonData.add("dataset_id", dset.getId());
// ------------------------------------
// DatasetVersion Info
// ------------------------------------
// database id
jsonData.add("dataset_version_id", dset_version.getId());
// major/minor version number, e.g. 3.1
jsonData.add("dataset_semantic_version", dset_version.getSemanticVersion());
jsonData.add("dataset_name", dset_version.getTitle());
jsonData.add("dataset_citation", dset_version.getCitation(true));
// Need to fix to/do
jsonData.add("dataset_description", "");
jsonData.add("dataset_is_public", dset_version.isReleased());
// ------------------------------------
// DataFile/FileMetaData Info
// ------------------------------------
jsonData.add("datafile_id", dfile.getId());
jsonData.add("datafile_label", dfile_meta.getLabel());
// jsonData.add("filename", dfile_meta.getLabel());
jsonData.add("datafile_expected_md5_checksum", dfile.getChecksumValue());
Long fsize = dfile.getFilesize();
if (fsize == null) {
fsize = new Long(-1);
}
jsonData.add("datafile_filesize", fsize);
jsonData.add("datafile_content_type", dfile.getContentType());
jsonData.add("datafile_create_datetime", dfile.getCreateDate().toString());
// restriction status of the DataFile
jsonData.add("datafile_is_restricted", dfile.isRestricted());
return ok(jsonData);
}
use of edu.harvard.iq.dataverse.Dataset in project dataverse by IQSS.
the class CollectionListManagerImpl method listCollectionContents.
@Override
public Feed listCollectionContents(IRI iri, AuthCredentials authCredentials, SwordConfiguration swordConfiguration) throws SwordServerException, SwordAuthException, SwordError {
AuthenticatedUser user = swordAuth.auth(authCredentials);
DataverseRequest dvReq = new DataverseRequest(user, request);
urlManager.processUrl(iri.toString());
String dvAlias = urlManager.getTargetIdentifier();
if (urlManager.getTargetType().equals("dataverse") && dvAlias != null) {
Dataverse dv = dataverseService.findByAlias(dvAlias);
if (dv != null) {
/**
* We'll say having AddDataset is enough to use this API
* endpoint, which means you are a Contributor to that
* dataverse. If we let just anyone call this endpoint, they
* will be able to see if the supplied dataverse is published or
* not.
*/
if (!permissionService.requestOn(dvReq, dv).has(Permission.AddDataset)) {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "user " + user.getDisplayInfo().getTitle() + " is not authorized to list datasets in dataverse " + dv.getAlias());
}
Abdera abdera = new Abdera();
Feed feed = abdera.newFeed();
feed.setTitle(dv.getName());
String baseUrl = urlManager.getHostnamePlusBaseUrlPath(iri.toString());
List<Dataset> datasets = datasetService.findByOwnerId(dv.getId());
for (Dataset dataset : datasets) {
/**
* @todo Will this be performant enough with production
* data, say in the root dataverse? Remove this todo if
* there are no complaints. :)
*/
if (!permissionService.isUserAllowedOn(user, new UpdateDatasetCommand(dataset, dvReq), dataset)) {
continue;
}
String editUri = baseUrl + "/edit/study/" + dataset.getGlobalId();
String editMediaUri = baseUrl + "/edit-media/study/" + dataset.getGlobalId();
Entry entry = feed.addEntry();
entry.setId(editUri);
entry.setTitle(datasetService.getTitleFromLatestVersion(dataset.getId()));
entry.setBaseUri(new IRI(editUri));
entry.addLink(editMediaUri, "edit-media");
feed.addEntry(entry);
}
Boolean dvHasBeenReleased = dv.isReleased();
feed.addSimpleExtension(new QName(UriRegistry.SWORD_STATE, "dataverseHasBeenReleased"), dvHasBeenReleased.toString());
return feed;
} else {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Could not find dataverse: " + dvAlias);
}
} else {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Couldn't determine target type or identifer from URL: " + iri);
}
}
Aggregations