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();
}
}
}
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);
}
}
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 + ".");
}
}
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);
}
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());
}
}
Aggregations