use of edu.harvard.iq.dataverse.engine.command.exception.CommandException in project dataverse by IQSS.
the class DatasetPage method createPrivateUrl.
public void createPrivateUrl() {
try {
PrivateUrl createdPrivateUrl = commandEngine.submit(new CreatePrivateUrlCommand(dvRequestService.getDataverseRequest(), dataset));
privateUrl = createdPrivateUrl;
JH.addMessage(FacesMessage.SEVERITY_INFO, BundleUtil.getStringFromBundle("dataset.privateurl.infoMessageAuthor", Arrays.asList(getPrivateUrlLink(privateUrl))));
privateUrlWasJustCreated = true;
} catch (CommandException ex) {
String msg = BundleUtil.getStringFromBundle("dataset.privateurl.noPermToCreate", PrivateUrlUtil.getRequiredPermissions(ex));
logger.info("Unable to create a Private URL for dataset id " + dataset.getId() + ". Message to user: " + msg + " Exception: " + ex);
JH.addErrorMessage(msg);
}
}
use of edu.harvard.iq.dataverse.engine.command.exception.CommandException in project dataverse by IQSS.
the class UpdateDatasetThumbnailCommand method execute.
@Override
public DatasetThumbnail execute(CommandContext ctxt) throws CommandException {
if (dataset == null) {
String message = "Can't update dataset thumbnail. Dataset is null.";
logger.info(message);
throw new IllegalCommandException(message, this);
}
// }
if (userIntent == null) {
throw new IllegalCommandException("No changes to save.", this);
}
switch(userIntent) {
case setDatasetFileAsThumbnail:
if (dataFileIdSupplied == null) {
throw new CommandException("A file was not selected to be the new dataset thumbnail.", this);
}
DataFile datasetFileThumbnailToSwitchTo = ctxt.files().find(dataFileIdSupplied);
if (datasetFileThumbnailToSwitchTo == null) {
throw new CommandException("Could not find file based on id supplied: " + dataFileIdSupplied + ".", this);
}
Dataset ds1 = ctxt.datasets().setDatasetFileAsThumbnail(dataset, datasetFileThumbnailToSwitchTo);
DatasetThumbnail datasetThumbnail = ds1.getDatasetThumbnail();
if (datasetThumbnail != null) {
DataFile dataFile = datasetThumbnail.getDataFile();
if (dataFile != null) {
if (dataFile.getId().equals(dataFileIdSupplied)) {
return datasetThumbnail;
} else {
throw new CommandException("Dataset thumbnail is should be based on file id " + dataFile.getId() + " but instead it is " + dataFileIdSupplied + ".", this);
}
}
} else {
throw new CommandException("Dataset thumbnail is unexpectedly absent.", this);
}
case setNonDatasetFileAsThumbnail:
File uploadedFile;
try {
uploadedFile = FileUtil.inputStreamToFile(inputStream);
} catch (IOException ex) {
throw new CommandException("In setNonDatasetFileAsThumbnail caught exception calling inputStreamToFile: " + ex, this);
}
if (uploadedFile == null) {
throw new CommandException("In setNonDatasetFileAsThumbnail uploadedFile was null.", this);
}
long uploadLogoSizeLimit = ctxt.systemConfig().getUploadLogoSizeLimit();
if (uploadedFile.length() > uploadLogoSizeLimit) {
throw new IllegalCommandException("File is larger than maximum size: " + uploadLogoSizeLimit + ".", this);
}
FileInputStream fileAsStream = null;
try {
fileAsStream = new FileInputStream(uploadedFile);
} catch (FileNotFoundException ex) {
Logger.getLogger(UpdateDatasetThumbnailCommand.class.getName()).log(Level.SEVERE, null, ex);
}
Dataset datasetWithNewThumbnail = ctxt.datasets().setNonDatasetFileAsThumbnail(dataset, fileAsStream);
if (datasetWithNewThumbnail != null) {
return datasetWithNewThumbnail.getDatasetThumbnail();
} else {
return null;
}
case removeThumbnail:
Dataset ds2 = ctxt.datasets().removeDatasetThumbnail(dataset);
DatasetThumbnail datasetThumbnail2 = ds2.getDatasetThumbnail();
if (datasetThumbnail2 == null) {
return null;
} else {
throw new CommandException("User wanted to remove the thumbnail it still has one!", this);
}
default:
throw new IllegalCommandException("Whatever you are trying to do to the dataset thumbnail is not supported.", this);
}
}
use of edu.harvard.iq.dataverse.engine.command.exception.CommandException in project dataverse by IQSS.
the class Files method restrictFileInDataset.
/**
* Restrict or Unrestrict an Existing File
* @author sarahferry
*
* @param fileToRestrictId
* @param restrictStr
* @return
*/
@PUT
@Path("{id}/restrict")
public Response restrictFileInDataset(@PathParam("id") Long fileToRestrictId, String restrictStr) {
// create request
DataverseRequest dataverseRequest = null;
// get the datafile
DataFile dataFile = fileService.find(fileToRestrictId);
if (dataFile == null) {
return error(BAD_REQUEST, "Could not find datafile with id " + fileToRestrictId);
}
boolean restrict = Boolean.valueOf(restrictStr);
try {
dataverseRequest = createDataverseRequest(findUserOrDie());
} catch (WrappedResponse wr) {
return error(BAD_REQUEST, "Couldn't find user to execute command: " + wr.getLocalizedMessage());
}
// try to restrict the datafile
try {
engineSvc.submit(new RestrictFileCommand(dataFile, dataverseRequest, restrict));
} catch (CommandException ex) {
return error(BAD_REQUEST, "Problem trying to update restriction status on " + dataFile.getDisplayName() + ": " + ex.getLocalizedMessage());
}
// update the dataset
try {
engineSvc.submit(new UpdateDatasetCommand(dataFile.getOwner(), dataverseRequest));
} catch (CommandException ex) {
return error(BAD_REQUEST, "Problem saving datafile " + dataFile.getDisplayName() + ": " + ex.getLocalizedMessage());
}
String text = restrict ? "restricted." : "unrestricted.";
return ok("File " + dataFile.getDisplayName() + " " + text);
}
use of edu.harvard.iq.dataverse.engine.command.exception.CommandException in project dataverse by IQSS.
the class Files method getMapLayerMetadatas.
// end: replaceFileInDataset
@DELETE
@Path("{id}/map")
public Response getMapLayerMetadatas(@PathParam("id") Long idSupplied) {
DataverseRequest dataverseRequest = null;
try {
dataverseRequest = createDataverseRequest(findUserOrDie());
} catch (WrappedResponse wr) {
return error(BAD_REQUEST, "Couldn't find user to execute command: " + wr.getLocalizedMessage());
}
DataFile dataFile = fileService.find(idSupplied);
try {
boolean deleted = engineSvc.submit(new DeleteMapLayerMetadataCommand(dataverseRequest, dataFile));
if (deleted) {
return ok("Map deleted from file id " + dataFile.getId());
} else {
return error(BAD_REQUEST, "Could not delete map from file id " + dataFile.getId());
}
} catch (CommandException ex) {
return error(BAD_REQUEST, "Problem trying to delete map from file id " + dataFile.getId() + ": " + ex.getLocalizedMessage());
}
}
use of edu.harvard.iq.dataverse.engine.command.exception.CommandException in project dataverse by IQSS.
the class ImportServiceBean method createDataverse.
/**
* This is just a convenience method, for testing migration. It creates
* a dummy dataverse with the directory name as dataverse name & alias.
* @param dvName
* @param dataverseRequest
* @return
* @throws ImportException
*/
@TransactionAttribute(REQUIRES_NEW)
public Dataverse createDataverse(String dvName, DataverseRequest dataverseRequest) throws ImportException {
Dataverse d = new Dataverse();
Dataverse root = dataverseService.findByAlias("root");
d.setOwner(root);
d.setAlias(dvName);
d.setName(dvName);
d.setAffiliation("affiliation");
d.setPermissionRoot(false);
d.setDescription("description");
d.setDataverseType(Dataverse.DataverseType.RESEARCHERS);
DataverseContact dc = new DataverseContact();
dc.setContactEmail("pete@mailinator.com");
ArrayList<DataverseContact> dcList = new ArrayList<>();
dcList.add(dc);
d.setDataverseContacts(dcList);
try {
d = engineSvc.submit(new CreateDataverseCommand(d, dataverseRequest, null, null));
} catch (EJBException ex) {
Throwable cause = ex;
StringBuilder sb = new StringBuilder();
sb.append("Error creating dataverse.");
while (cause.getCause() != null) {
cause = cause.getCause();
if (cause instanceof ConstraintViolationException) {
ConstraintViolationException constraintViolationException = (ConstraintViolationException) cause;
for (ConstraintViolation<?> violation : constraintViolationException.getConstraintViolations()) {
sb.append(" Invalid value: <<<").append(violation.getInvalidValue()).append(">>> for ").append(violation.getPropertyPath()).append(" at ").append(violation.getLeafBean()).append(" - ").append(violation.getMessage());
}
}
}
logger.log(Level.SEVERE, sb.toString());
System.out.println("Error creating dataverse: " + sb.toString());
throw new ImportException(sb.toString());
} catch (CommandException e) {
throw new ImportException(e.getMessage());
}
return d;
}
Aggregations