use of edu.harvard.iq.dataverse.Dataverse in project dataverse by IQSS.
the class BatchImport method startBatchJob.
private Response startBatchJob(String fileDir, String parentIdtf, String apiKey, ImportType importType, Boolean createDV) {
if (createDV == null) {
createDV = Boolean.FALSE;
}
try {
DataverseRequest dataverseRequest;
try {
dataverseRequest = createDataverseRequest(findAuthenticatedUserOrDie());
} catch (WrappedResponse wr) {
return wr.getResponse();
}
if (parentIdtf == null) {
parentIdtf = "root";
}
Dataverse owner = findDataverse(parentIdtf);
if (owner == null) {
if (createDV) {
owner = importService.createDataverse(parentIdtf, dataverseRequest);
} else {
return error(Response.Status.NOT_FOUND, "Can't find dataverse with identifier='" + parentIdtf + "'");
}
}
batchService.processFilePath(fileDir, parentIdtf, dataverseRequest, owner, importType, createDV);
} catch (ImportException e) {
return this.error(Response.Status.BAD_REQUEST, "Import Exception, " + e.getMessage());
}
return this.accepted();
}
use of edu.harvard.iq.dataverse.Dataverse 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.Dataverse in project dataverse by IQSS.
the class JsonParserTest method testParseNoNameDataverse.
/**
* Test that a dataverse JSON object without name fails to parse.
* @throws JsonParseException if all goes well - this is expected.
* @throws IOException when test file IO goes wrong - this is bad.
*/
@Test(expected = JsonParseException.class)
public void testParseNoNameDataverse() throws JsonParseException, IOException {
JsonObject dvJson;
try (InputStream jsonFile = ClassLoader.getSystemResourceAsStream("json/no-name-dataverse.json")) {
dvJson = Json.createReader(jsonFile).readObject();
Dataverse actual = sut.parseDataverse(dvJson);
}
}
use of edu.harvard.iq.dataverse.Dataverse in project dataverse by IQSS.
the class JsonParserTest method testParseNoContactEmailsDataverse.
/**
* Test that a dataverse JSON object with contacts, but without contact
* email fails to parse.
* @throws JsonParseException if all goes well - this is expected.
* @throws IOException when test file IO goes wrong - this is bad.
*/
@Test(expected = JsonParseException.class)
public void testParseNoContactEmailsDataverse() throws JsonParseException, IOException {
JsonObject dvJson;
try (InputStream jsonFile = ClassLoader.getSystemResourceAsStream("json/no-contacts-dataverse.json")) {
dvJson = Json.createReader(jsonFile).readObject();
Dataverse actual = sut.parseDataverse(dvJson);
}
}
use of edu.harvard.iq.dataverse.Dataverse in project dataverse by IQSS.
the class PrivateUrlUtilTest method testGetUserFromRoleAssignmentNonDataset.
@Test
public void testGetUserFromRoleAssignmentNonDataset() {
DataverseRole aRole = null;
PrivateUrlUser privateUrlUserIn = new PrivateUrlUser(42);
RoleAssignee anAssignee = privateUrlUserIn;
DvObject nonDataset = new Dataverse();
nonDataset.setId(123l);
String privateUrlToken = null;
RoleAssignment ra = new RoleAssignment(aRole, anAssignee, nonDataset, privateUrlToken);
PrivateUrlUser privateUrlUserOut = PrivateUrlUtil.getPrivateUrlUserFromRoleAssignment(ra);
assertNull(privateUrlUserOut);
}
Aggregations