Search in sources :

Example 1 with DeleteDataFileCommand

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

the class EditDatafilesPage method save.

public String save() {
    if (!saveEnabled) {
        return "";
    }
    if (isFileReplaceOperation()) {
        try {
            return saveReplacementFile();
        } catch (FileReplaceException ex) {
            String errMsg = ex.getMessage();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Dataset Save Failed", errMsg));
            logger.log(Level.SEVERE, "Dataset save failed for replace operation: {0}", errMsg);
            return null;
        }
    }
    // Save the NEW files permanently:
    ingestService.addFiles(workingVersion, newFiles);
    if (workingVersion.getId() == null || datasetUpdateRequired) {
        logger.fine("issuing the dataset update command");
        if (datasetUpdateRequired) {
            for (int i = 0; i < workingVersion.getFileMetadatas().size(); i++) {
                for (FileMetadata fileMetadata : fileMetadatas) {
                    if (fileMetadata.getDataFile().getStorageIdentifier() != null) {
                        if (fileMetadata.getDataFile().getStorageIdentifier().equals(workingVersion.getFileMetadatas().get(i).getDataFile().getStorageIdentifier())) {
                            workingVersion.getFileMetadatas().set(i, fileMetadata);
                        }
                    }
                }
            }
            if (tabularDataTagsUpdated) {
                for (int i = 0; i < dataset.getFiles().size(); i++) {
                    for (FileMetadata fileMetadata : fileMetadatas) {
                        if (fileMetadata.getDataFile().getStorageIdentifier() != null) {
                            if (fileMetadata.getDataFile().getStorageIdentifier().equals(dataset.getFiles().get(i).getStorageIdentifier())) {
                                dataset.getFiles().set(i, fileMetadata.getDataFile());
                            }
                        }
                    }
                }
                tabularDataTagsUpdated = false;
            }
        }
        Command<Dataset> cmd;
        try {
            cmd = new UpdateDatasetCommand(dataset, dvRequestService.getDataverseRequest(), filesToBeDeleted);
            ((UpdateDatasetCommand) cmd).setValidateLenient(true);
            dataset = 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(" ");
            }
            logger.log(Level.INFO, "Couldn''t save dataset: {0}", error.toString());
            populateDatasetUpdateFailureMessage();
            return null;
        } catch (CommandException ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Dataset Save Failed", " - " + ex.toString()));
            logger.severe(ex.getMessage());
            populateDatasetUpdateFailureMessage();
            return null;
        }
        datasetUpdateRequired = false;
        saveEnabled = false;
    } else {
        // This is an existing Draft version (and nobody has explicitly
        // requested that the entire dataset is updated). So we'll try to update
        // only the filemetadatas and/or files affected, and not the
        // entire version.
        // TODO: in 4.3, create SaveDataFileCommand!
        // -- L.A. Sep. 21 2015, 4.2
        Timestamp updateTime = new Timestamp(new Date().getTime());
        workingVersion.setLastUpdateTime(updateTime);
        dataset.setModificationTime(updateTime);
        StringBuilder saveError = new StringBuilder();
        for (FileMetadata fileMetadata : fileMetadatas) {
            if (fileMetadata.getDataFile().getCreateDate() == null) {
                fileMetadata.getDataFile().setCreateDate(updateTime);
                fileMetadata.getDataFile().setCreator((AuthenticatedUser) session.getUser());
            }
            fileMetadata.getDataFile().setModificationTime(updateTime);
            try {
                // DataFile savedDatafile = datafileService.save(fileMetadata.getDataFile());
                fileMetadata = datafileService.mergeFileMetadata(fileMetadata);
                logger.fine("Successfully saved DataFile " + fileMetadata.getLabel() + " in the database.");
            } catch (EJBException ex) {
                saveError.append(ex).append(" ");
                saveError.append(ex.getMessage()).append(" ");
                Throwable cause = ex;
                while (cause.getCause() != null) {
                    cause = cause.getCause();
                    saveError.append(cause).append(" ");
                    saveError.append(cause.getMessage()).append(" ");
                }
            }
        }
        // Remove / delete any files that were removed
        for (FileMetadata fmd : filesToBeDeleted) {
            // check if this file is being used as the default thumbnail
            if (fmd.getDataFile().equals(dataset.getThumbnailFile())) {
                logger.fine("deleting the dataset thumbnail designation");
                dataset.setThumbnailFile(null);
            }
            if (!fmd.getDataFile().isReleased()) {
                // if file is draft (ie. new to this version, delete; otherwise just remove filemetadata object)
                try {
                    commandEngine.submit(new DeleteDataFileCommand(fmd.getDataFile(), dvRequestService.getDataverseRequest()));
                    dataset.getFiles().remove(fmd.getDataFile());
                    workingVersion.getFileMetadatas().remove(fmd);
                    // todo: clean this up some when we clean the create / update dataset methods
                    for (DataFileCategory cat : dataset.getCategories()) {
                        cat.getFileMetadatas().remove(fmd);
                    }
                } catch (CommandException cmde) {
                // TODO:
                // add diagnostics reporting for individual data files that
                // we failed to delete.
                }
            } else {
                datafileService.removeFileMetadata(fmd);
                fmd.getDataFile().getFileMetadatas().remove(fmd);
                workingVersion.getFileMetadatas().remove(fmd);
            }
        }
        String saveErrorString = saveError.toString();
        if (saveErrorString != null && !saveErrorString.isEmpty()) {
            logger.log(Level.INFO, "Couldn''t save dataset: {0}", saveErrorString);
            populateDatasetUpdateFailureMessage();
            return null;
        }
        // the id for null, just in case)
        if (mode == FileEditMode.UPLOAD) {
            if (dataset.getId() != null) {
                dataset = datasetService.find(dataset.getId());
            }
        }
    }
    newFiles.clear();
    workingVersion = dataset.getEditVersion();
    logger.fine("working version id: " + workingVersion.getId());
    if (mode == FileEditMode.SINGLE) {
        JsfHelper.addSuccessMessage(getBundleString("file.message.editSuccess"));
    } else {
        JsfHelper.addSuccessMessage(getBundleString("dataset.message.filesSuccess"));
    }
    // queue the data ingest jobs for asynchronous execution:
    if (mode == FileEditMode.UPLOAD) {
        ingestService.startIngestJobs(dataset, (AuthenticatedUser) session.getUser());
    }
    if (mode == FileEditMode.SINGLE && fileMetadatas.size() > 0) {
        // If this was a "single file edit", i.e. an edit request sent from
        // the individual File Landing page, we want to redirect back to
        // the landing page. BUT ONLY if the file still exists - i.e., if
        // the user hasn't just deleted it!
        versionString = "DRAFT";
        return returnToFileLandingPage();
    }
    // if (newDraftVersion) {
    // return returnToDraftVersionById();
    // }
    logger.fine("Redirecting to the dataset page, from the edit/upload page.");
    return returnToDraftVersion();
}
Also used : CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) Timestamp(java.sql.Timestamp) Date(java.util.Date) FileReplaceException(edu.harvard.iq.dataverse.datasetutility.FileReplaceException) DeleteDataFileCommand(edu.harvard.iq.dataverse.engine.command.impl.DeleteDataFileCommand) EJBException(javax.ejb.EJBException) FacesMessage(javax.faces.application.FacesMessage) UpdateDatasetCommand(edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetCommand)

Aggregations

FileReplaceException (edu.harvard.iq.dataverse.datasetutility.FileReplaceException)1 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)1 DeleteDataFileCommand (edu.harvard.iq.dataverse.engine.command.impl.DeleteDataFileCommand)1 UpdateDatasetCommand (edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetCommand)1 Timestamp (java.sql.Timestamp)1 Date (java.util.Date)1 EJBException (javax.ejb.EJBException)1 FacesMessage (javax.faces.application.FacesMessage)1