Search in sources :

Example 51 with CommandException

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);
    }
}
Also used : CreatePrivateUrlCommand(edu.harvard.iq.dataverse.engine.command.impl.CreatePrivateUrlCommand) PrivateUrl(edu.harvard.iq.dataverse.privateurl.PrivateUrl) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) IllegalCommandException(edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException)

Example 52 with CommandException

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);
    }
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) DatasetThumbnail(edu.harvard.iq.dataverse.dataset.DatasetThumbnail) Dataset(edu.harvard.iq.dataverse.Dataset) IllegalCommandException(edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException) FileNotFoundException(java.io.FileNotFoundException) IllegalCommandException(edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) IOException(java.io.IOException) DataFile(edu.harvard.iq.dataverse.DataFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 53 with CommandException

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);
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) DataFile(edu.harvard.iq.dataverse.DataFile) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) RestrictFileCommand(edu.harvard.iq.dataverse.engine.command.impl.RestrictFileCommand) UpdateDatasetCommand(edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetCommand) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT)

Example 54 with CommandException

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());
    }
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) DataFile(edu.harvard.iq.dataverse.DataFile) DeleteMapLayerMetadataCommand(edu.harvard.iq.dataverse.engine.command.impl.DeleteMapLayerMetadataCommand) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 55 with CommandException

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;
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) ArrayList(java.util.ArrayList) DataverseContact(edu.harvard.iq.dataverse.DataverseContact) ConstraintViolationException(javax.validation.ConstraintViolationException) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) CreateDataverseCommand(edu.harvard.iq.dataverse.engine.command.impl.CreateDataverseCommand) EJBException(javax.ejb.EJBException) Dataverse(edu.harvard.iq.dataverse.Dataverse) TransactionAttribute(javax.ejb.TransactionAttribute)

Aggregations

CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)86 Dataset (edu.harvard.iq.dataverse.Dataset)21 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)20 IllegalCommandException (edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException)19 Test (org.junit.Test)16 PermissionException (edu.harvard.iq.dataverse.engine.command.exception.PermissionException)15 EJBException (javax.ejb.EJBException)13 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)12 DataFile (edu.harvard.iq.dataverse.DataFile)11 Dataverse (edu.harvard.iq.dataverse.Dataverse)9 UpdateDatasetCommand (edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetCommand)9 ConstraintViolation (javax.validation.ConstraintViolation)9 DatasetThumbnail (edu.harvard.iq.dataverse.dataset.DatasetThumbnail)8 IOException (java.io.IOException)8 Timestamp (java.sql.Timestamp)8 ArrayList (java.util.ArrayList)8 Date (java.util.Date)8 FacesMessage (javax.faces.application.FacesMessage)7 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)6 SwordError (org.swordapp.server.SwordError)6