Search in sources :

Example 11 with UpdateDatasetCommand

use of edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetCommand 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 12 with UpdateDatasetCommand

use of edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetCommand in project dataverse by IQSS.

the class FilePage method save.

public String save() {
    // Validate
    Set<ConstraintViolation> constraintViolations = this.fileMetadata.getDatasetVersion().validate();
    if (!constraintViolations.isEmpty()) {
        // JsfHelper.addFlashMessage(JH.localize("dataset.message.validationError"));
        fileDeleteInProgress = false;
        JH.addMessage(FacesMessage.SEVERITY_ERROR, JH.localize("dataset.message.validationError"));
        // FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Validation Error", "See below for details."));
        return "";
    }
    Command<Dataset> cmd;
    try {
        cmd = new UpdateDatasetCommand(editDataset, dvRequestService.getDataverseRequest(), filesToBeDeleted);
        commandEngine.submit(cmd);
    } catch (EJBException ex) {
        StringBuilder error = new StringBuilder();
        error.append(ex).append(" ");
        error.append(ex.getMessage()).append(" ");
        Throwable cause = ex;
        while (cause.getCause() != null) {
            cause = cause.getCause();
            error.append(cause).append(" ");
            error.append(cause.getMessage()).append(" ");
        }
        return null;
    } catch (CommandException ex) {
        fileDeleteInProgress = false;
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Dataset Save Failed", " - " + ex.toString()));
        return null;
    }
    if (fileDeleteInProgress) {
        JsfHelper.addSuccessMessage(JH.localize("file.message.deleteSuccess"));
        fileDeleteInProgress = false;
    } else {
        JsfHelper.addSuccessMessage(JH.localize("file.message.editSuccess"));
    }
    setVersion("DRAFT");
    return "";
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) IllegalCommandException(edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException) EJBException(javax.ejb.EJBException) FacesMessage(javax.faces.application.FacesMessage) UpdateDatasetCommand(edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetCommand)

Aggregations

UpdateDatasetCommand (edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetCommand)12 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)9 EJBException (javax.ejb.EJBException)6 Dataset (edu.harvard.iq.dataverse.Dataset)5 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)5 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)5 IllegalCommandException (edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException)5 SwordError (org.swordapp.server.SwordError)4 DataFile (edu.harvard.iq.dataverse.DataFile)3 Dataverse (edu.harvard.iq.dataverse.Dataverse)3 FacesMessage (javax.faces.application.FacesMessage)3 ConstraintViolation (javax.validation.ConstraintViolation)3 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)2 ArrayList (java.util.ArrayList)2 DepositReceipt (org.swordapp.server.DepositReceipt)2 FileReplaceException (edu.harvard.iq.dataverse.datasetutility.FileReplaceException)1 CommandExecutionException (edu.harvard.iq.dataverse.engine.command.exception.CommandExecutionException)1 CreateDatasetCommand (edu.harvard.iq.dataverse.engine.command.impl.CreateDatasetCommand)1 DeleteDataFileCommand (edu.harvard.iq.dataverse.engine.command.impl.DeleteDataFileCommand)1 RestrictFileCommand (edu.harvard.iq.dataverse.engine.command.impl.RestrictFileCommand)1