use of edu.harvard.iq.dataverse.DatasetVersion in project dataverse by IQSS.
the class ContainerManagerImpl method replaceMetadata.
@Override
public DepositReceipt replaceMetadata(String uri, Deposit deposit, AuthCredentials authCredentials, SwordConfiguration swordConfiguration) throws SwordError, SwordServerException, SwordAuthException {
AuthenticatedUser user = swordAuth.auth(authCredentials);
DataverseRequest dvReq = new DataverseRequest(user, httpRequest);
logger.fine("replaceMetadata called with url: " + uri);
urlManager.processUrl(uri);
String targetType = urlManager.getTargetType();
if (!targetType.isEmpty()) {
logger.fine("operating on target type: " + urlManager.getTargetType());
if ("dataverse".equals(targetType)) {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Metadata replace of dataverse is not supported.");
} else if ("study".equals(targetType)) {
logger.fine("replacing metadata for dataset");
// do a sanity check on the XML received
try {
SwordEntry swordEntry = deposit.getSwordEntry();
logger.fine("deposit XML received by replaceMetadata():\n" + swordEntry);
} catch (ParseException ex) {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Can not replace dataset metadata due to malformed Atom entry: " + ex);
}
String globalId = urlManager.getTargetIdentifier();
Dataset dataset = datasetService.findByGlobalId(globalId);
if (dataset != null) {
Dataverse dvThatOwnsDataset = dataset.getOwner();
UpdateDatasetCommand updateDatasetCommand = new UpdateDatasetCommand(dataset, dvReq);
if (!permissionService.isUserAllowedOn(user, updateDatasetCommand, dataset)) {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "User " + user.getDisplayInfo().getTitle() + " is not authorized to modify dataverse " + dvThatOwnsDataset.getAlias());
}
DatasetVersion datasetVersion = dataset.getEditVersion();
// erase all metadata before creating populating dataset version
List<DatasetField> emptyDatasetFields = new ArrayList<>();
datasetVersion.setDatasetFields(emptyDatasetFields);
String foreignFormat = SwordUtil.DCTERMS;
try {
importGenericService.importXML(deposit.getSwordEntry().toString(), foreignFormat, datasetVersion);
} catch (Exception ex) {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "problem calling importXML: " + ex);
}
swordService.addDatasetContact(datasetVersion, user);
swordService.addDatasetDepositor(datasetVersion, user);
swordService.addDatasetSubjectIfMissing(datasetVersion);
swordService.setDatasetLicenseAndTermsOfUse(datasetVersion, deposit.getSwordEntry());
try {
engineSvc.submit(updateDatasetCommand);
} catch (CommandException ex) {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "problem updating dataset: " + ex);
}
ReceiptGenerator receiptGenerator = new ReceiptGenerator();
String baseUrl = urlManager.getHostnamePlusBaseUrlPath(uri);
DepositReceipt depositReceipt = receiptGenerator.createDatasetReceipt(baseUrl, dataset);
return depositReceipt;
} else {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Could not find dataset based on global id (" + globalId + ") in URL: " + uri);
}
} else {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Unknown target type specified on which to replace metadata: " + uri);
}
} else {
throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "No target specified on which to replace metadata: " + uri);
}
}
use of edu.harvard.iq.dataverse.DatasetVersion in project dataverse by IQSS.
the class JsonPrinterTest method testGetFileCategories.
@Test
public void testGetFileCategories() {
FileMetadata fmd = new FileMetadata();
DatasetVersion dsVersion = new DatasetVersion();
DataFile dataFile = new DataFile();
List<DataFileTag> dataFileTags = new ArrayList<>();
DataFileTag tag = new DataFileTag();
tag.setTypeByLabel("Survey");
dataFileTags.add(tag);
dataFile.setTags(dataFileTags);
fmd.setDatasetVersion(dsVersion);
fmd.setDataFile(dataFile);
List<DataFileCategory> fileCategories = new ArrayList<>();
DataFileCategory dataFileCategory = new DataFileCategory();
dataFileCategory.setName("Data");
fileCategories.add(dataFileCategory);
fmd.setCategories(fileCategories);
JsonObjectBuilder job = JsonPrinter.json(fmd);
assertNotNull(job);
JsonObject jsonObject = job.build();
System.out.println("json: " + jsonObject);
assertEquals("", jsonObject.getString("description"));
assertEquals("", jsonObject.getString("label"));
assertEquals("Data", jsonObject.getJsonArray("categories").getString(0));
assertEquals("", jsonObject.getJsonObject("dataFile").getString("filename"));
assertEquals(-1, jsonObject.getJsonObject("dataFile").getInt("filesize"));
assertEquals("UNKNOWN", jsonObject.getJsonObject("dataFile").getString("originalFormatLabel"));
assertEquals(-1, jsonObject.getJsonObject("dataFile").getInt("rootDataFileId"));
assertEquals("Survey", jsonObject.getJsonObject("dataFile").getJsonArray("tabularTags").getString(0));
}
use of edu.harvard.iq.dataverse.DatasetVersion in project dataverse by IQSS.
the class JsonParserTest method testparseFiles.
@Test
public void testparseFiles() throws JsonParseException {
JsonArrayBuilder metadatasJsonBuilder = Json.createArrayBuilder();
JsonObjectBuilder fileMetadataGood = Json.createObjectBuilder();
fileMetadataGood.add("label", "myLabel");
JsonObjectBuilder fileGood = Json.createObjectBuilder();
fileMetadataGood.add("dataFile", fileGood);
fileMetadataGood.add("categories", Json.createArrayBuilder().add("Documentation"));
JsonObjectBuilder fileMetadataBad = Json.createObjectBuilder();
fileMetadataBad.add("label", "bad");
JsonObjectBuilder fileBad = Json.createObjectBuilder();
fileMetadataBad.add("dataFile", fileBad);
fileMetadataBad.add("categories", Json.createArrayBuilder().add(BigDecimal.ONE));
metadatasJsonBuilder.add(fileMetadataGood);
metadatasJsonBuilder.add(fileMetadataBad);
JsonArray metadatasJson = metadatasJsonBuilder.build();
DatasetVersion dsv = new DatasetVersion();
Dataset dataset = new Dataset();
dsv.setDataset(dataset);
List<FileMetadata> fileMetadatas = new JsonParser().parseFiles(metadatasJson, dsv);
System.out.println("fileMetadatas: " + fileMetadatas);
assertEquals("myLabel", fileMetadatas.get(0).getLabel());
assertEquals("Documentation", fileMetadatas.get(0).getCategories().get(0).getName());
assertEquals(null, fileMetadatas.get(1).getCategories());
List<FileMetadata> codeCoverage = new JsonParser().parseFiles(Json.createArrayBuilder().add(Json.createObjectBuilder().add("label", "myLabel").add("dataFile", Json.createObjectBuilder().add("categories", JsonValue.NULL))).build(), dsv);
assertEquals(null, codeCoverage.get(0).getCategories());
}
use of edu.harvard.iq.dataverse.DatasetVersion in project dataverse by IQSS.
the class PrivateUrlUtilTest method testGetDraftDatasetVersionFromRoleAssignmentNullDataset.
@Test
public void testGetDraftDatasetVersionFromRoleAssignmentNullDataset() {
DataverseRole aRole = null;
PrivateUrlUser privateUrlUser = new PrivateUrlUser(42);
RoleAssignee anAssignee = privateUrlUser;
DvObject dataset = null;
String privateUrlToken = null;
RoleAssignment ra = new RoleAssignment(aRole, anAssignee, dataset, privateUrlToken);
DatasetVersion datasetVersion = PrivateUrlUtil.getDraftDatasetVersionFromRoleAssignment(ra);
assertNull(datasetVersion);
}
use of edu.harvard.iq.dataverse.DatasetVersion in project dataverse by IQSS.
the class PrivateUrlUtilTest method testGetDraftUrlSuccess.
@Test
public void testGetDraftUrlSuccess() throws Exception {
DatasetVersion draft = new DatasetVersion();
Dataset dataset = new Dataset();
dataset.setProtocol("doi");
dataset.setAuthority("10.5072/FK2");
dataset.setIdentifier("3L33T");
draft.setDataset(dataset);
assertEquals("/dataset.xhtml?persistentId=doi:10.5072/FK2/3L33T&version=DRAFT", PrivateUrlUtil.getDraftUrl(draft));
}
Aggregations