Search in sources :

Example 61 with DataFile

use of edu.harvard.iq.dataverse.DataFile in project dataverse by IQSS.

the class IngestMessageBean method onMessage.

@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public void onMessage(Message message) {
    IngestMessage ingestMessage = null;
    Long datafile_id = null;
    try {
        ObjectMessage om = (ObjectMessage) message;
        ingestMessage = (IngestMessage) om.getObject();
        Iterator iter = ingestMessage.getFileIds().iterator();
        datafile_id = null;
        // -- L.A. Aug. 13 2014
        while (iter.hasNext()) {
            datafile_id = (Long) iter.next();
            logger.fine("Start ingest job;");
            try {
                if (ingestService.ingestAsTabular(datafile_id)) {
                    // Thread.sleep(10000);
                    logger.fine("Finished ingest job;");
                } else {
                    logger.warning("Error occurred during ingest job!");
                }
            } catch (Exception ex) {
                // ex.printStackTrace();
                // TODO:
                // this solution is working - but it would be cleaner to instead
                // make sure that all the exceptions are interrupted and appropriate
                // action taken still on the ingest service side.
                // -- L.A. Aug. 13 2014;
                logger.info("Unknown exception occurred  during ingest (supressed stack trace); re-setting ingest status.");
                if (datafile_id != null) {
                    logger.fine("looking up datafile for id " + datafile_id);
                    DataFile datafile = datafileService.find(datafile_id);
                    if (datafile != null) {
                        datafile.SetIngestProblem();
                        IngestReport errorReport = new IngestReport();
                        errorReport.setFailure();
                        if (ex.getMessage() != null) {
                            errorReport.setReport("Ingest succeeded, but failed to save the ingested tabular data in the database: " + ex.getMessage());
                        } else {
                            errorReport.setReport("Ingest succeeded, but failed to save the ingested tabular data in the database; no further information is available");
                        }
                        errorReport.setDataFile(datafile);
                        datafile.setIngestReport(errorReport);
                        datafile.setDataTables(null);
                        logger.info("trying to save datafile and the failed ingest report, id=" + datafile_id);
                        datafile = datafileService.save(datafile);
                        Dataset dataset = datafile.getOwner();
                        if (dataset != null && dataset.getId() != null) {
                            // logger.info("attempting to remove dataset lock for dataset " + dataset.getId());
                            // datasetService.removeDatasetLock(dataset.getId());
                            ingestService.sendFailNotification(dataset.getId());
                        }
                    }
                }
            }
        }
        // packed into this IngestMessage belong to the same dataset)
        if (datafile_id != null) {
            DataFile datafile = datafileService.find(datafile_id);
            if (datafile != null) {
                Dataset dataset = datafile.getOwner();
                if (dataset != null && dataset.getId() != null) {
                    datasetService.removeDatasetLocks(dataset.getId(), DatasetLock.Reason.Ingest);
                }
            }
        }
    } catch (JMSException ex) {
        // error in getting object from message; can't send e-mail
        ex.printStackTrace();
    } finally {
        // when we're done, go ahead and remove the lock (not yet)
        try {
        // datasetService.removeDatasetLock( ingestMessage.getDatasetId() );
        } catch (Exception ex) {
            // application was unable to remove the datasetLock
            ex.printStackTrace();
        }
    }
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) ObjectMessage(javax.jms.ObjectMessage) Dataset(edu.harvard.iq.dataverse.Dataset) Iterator(java.util.Iterator) JMSException(javax.jms.JMSException) JMSException(javax.jms.JMSException) TransactionAttribute(javax.ejb.TransactionAttribute)

Example 62 with DataFile

use of edu.harvard.iq.dataverse.DataFile 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 63 with DataFile

use of edu.harvard.iq.dataverse.DataFile in project dataverse by IQSS.

the class Datasets method getDatasetThumbnailCandidates.

@GET
@Path("{id}/thumbnail/candidates")
public Response getDatasetThumbnailCandidates(@PathParam("id") String idSupplied) {
    try {
        Dataset dataset = findDatasetOrDie(idSupplied);
        boolean canUpdateThumbnail = false;
        try {
            canUpdateThumbnail = permissionSvc.requestOn(createDataverseRequest(findUserOrDie()), dataset).canIssue(UpdateDatasetThumbnailCommand.class);
        } catch (WrappedResponse ex) {
            logger.info("Exception thrown while trying to figure out permissions while getting thumbnail for dataset id " + dataset.getId() + ": " + ex.getLocalizedMessage());
        }
        if (!canUpdateThumbnail) {
            return error(Response.Status.FORBIDDEN, "You are not permitted to list dataset thumbnail candidates.");
        }
        JsonArrayBuilder data = Json.createArrayBuilder();
        boolean considerDatasetLogoAsCandidate = true;
        for (DatasetThumbnail datasetThumbnail : DatasetUtil.getThumbnailCandidates(dataset, considerDatasetLogoAsCandidate)) {
            JsonObjectBuilder candidate = Json.createObjectBuilder();
            String base64image = datasetThumbnail.getBase64image();
            if (base64image != null) {
                logger.fine("found a candidate!");
                candidate.add("base64image", base64image);
            }
            DataFile dataFile = datasetThumbnail.getDataFile();
            if (dataFile != null) {
                candidate.add("dataFileId", dataFile.getId());
            }
            data.add(candidate);
        }
        return ok(data);
    } catch (WrappedResponse ex) {
        return error(Response.Status.NOT_FOUND, "Could not find dataset based on id supplied: " + idSupplied + ".");
    }
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) DatasetThumbnail(edu.harvard.iq.dataverse.dataset.DatasetThumbnail) Dataset(edu.harvard.iq.dataverse.Dataset) UpdateDatasetThumbnailCommand(edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetThumbnailCommand) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 64 with DataFile

use of edu.harvard.iq.dataverse.DataFile 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 65 with DataFile

use of edu.harvard.iq.dataverse.DataFile 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)

Aggregations

DataFile (edu.harvard.iq.dataverse.DataFile)111 Dataset (edu.harvard.iq.dataverse.Dataset)39 IOException (java.io.IOException)39 FileMetadata (edu.harvard.iq.dataverse.FileMetadata)30 ArrayList (java.util.ArrayList)25 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)20 File (java.io.File)20 FileNotFoundException (java.io.FileNotFoundException)18 Path (javax.ws.rs.Path)18 Dataverse (edu.harvard.iq.dataverse.Dataverse)17 FileInputStream (java.io.FileInputStream)16 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)14 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)13 Date (java.util.Date)13 GET (javax.ws.rs.GET)13 Test (org.junit.Test)13 Timestamp (java.sql.Timestamp)11 InputStream (java.io.InputStream)10 DataVariable (edu.harvard.iq.dataverse.datavariable.DataVariable)8 FileOutputStream (java.io.FileOutputStream)8