Skip to content

Commit

Permalink
have UpdateDatasetCommand pick a thumbnail file #3559
Browse files Browse the repository at this point in the history
We will need to write a migration script to populate the
thumbnailfile_id field in the dataset table since we are no longer
exercising the automatic thumbnail selection logic every time we go to
get the thumbnail. That logic is too expensive.
  • Loading branch information
pdurbin committed Mar 10, 2017
1 parent 489cfea commit a66fce2
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 63 deletions.
4 changes: 2 additions & 2 deletions src/main/java/edu/harvard/iq/dataverse/Dataset.java
Original file line number Diff line number Diff line change
Expand Up @@ -690,8 +690,8 @@ public boolean isAncestorOf( DvObject other ) {
return equals(other) || equals(other.getOwner());
}

public DatasetThumbnail getDatasetThumbnail(DatasetVersionServiceBean datasetVersionService, DataFileServiceBean dataFileService) {
return DatasetUtil.getThumbnail(this, datasetVersionService, dataFileService);
public DatasetThumbnail getDatasetThumbnail() {
return DatasetUtil.getThumbnail(this);
}

}
2 changes: 1 addition & 1 deletion src/main/java/edu/harvard/iq/dataverse/DatasetPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3514,7 +3514,7 @@ public void setTwoRavensHelper(TwoRavensHelper twoRavensHelper) {
}

public String getThumbnail() {
DatasetThumbnail datasetThumbnail = dataset.getDatasetThumbnail(datasetVersionService, datafileService);
DatasetThumbnail datasetThumbnail = dataset.getDatasetThumbnail();
if (datasetThumbnail != null) {
return datasetThumbnail.getBase64image();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ public class DatasetWidgetsPage implements java.io.Serializable {
@EJB
DatasetServiceBean datasetService;

@EJB
DatasetVersionServiceBean datasetVersionService;

@EJB
DataFileServiceBean dataFileService;

@EJB
EjbDataverseEngine commandEngine;

Expand Down Expand Up @@ -74,7 +68,7 @@ public String init() {
return permissionsWrapper.notAuthorized();
}
datasetThumbnails = DatasetUtil.getThumbnailCandidates(dataset, considerDatasetLogoAsCandidate);
datasetThumbnail = dataset.getDatasetThumbnail(datasetVersionService, dataFileService);
datasetThumbnail = dataset.getDatasetThumbnail();
if (datasetThumbnail != null) {
DataFile dataFile = datasetThumbnail.getDataFile();
if (dataFile != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2220,7 +2220,7 @@ public void deleteDatasetLogoAndUseThisDataFileAsThumbnailInstead() {
}

public boolean isThumbnailIsFromDatasetLogoRatherThanDatafile() {
DatasetThumbnail datasetThumbnail = dataset.getDatasetThumbnail(datasetVersionService, datafileService);
DatasetThumbnail datasetThumbnail = dataset.getDatasetThumbnail();
if (datasetThumbnail != null && !datasetThumbnail.isFromDataFile()) {
return true;
}
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/edu/harvard/iq/dataverse/api/Datasets.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ public class Datasets extends AbstractApiBean {

@EJB
DataFileServiceBean fileService;

@EJB
DatasetVersionServiceBean datasetVersionService;

@EJB
IngestServiceBean ingestService;
Expand Down Expand Up @@ -605,7 +602,7 @@ public InputStream getDatasetThumbnail(@PathParam("id") String idSupplied) {
} catch (WrappedResponse ex) {
return null;
}
DatasetThumbnail datasetThumbnail = dataset.getDatasetThumbnail(datasetVersionService, fileService);
DatasetThumbnail datasetThumbnail = dataset.getDatasetThumbnail();
if (datasetThumbnail == null) {
logger.info("dataset could not be found for dataset id " + dataset.getId() + ". Returning null.");
return null;
Expand Down Expand Up @@ -636,7 +633,7 @@ public Response getDatasetThumbnailMetadata(@PathParam("id") String idSupplied)
try {
Dataset dataset = findDatasetOrDie(idSupplied);
JsonObjectBuilder data = Json.createObjectBuilder();
DatasetThumbnail datasetThumbnail = dataset.getDatasetThumbnail(datasetVersionService, fileService);
DatasetThumbnail datasetThumbnail = dataset.getDatasetThumbnail();
data.add("isUseGenericThumbnail", dataset.isUseGenericThumbnail());
if (datasetThumbnail != null) {
data.add("datasetThumbnailBase64image", datasetThumbnail.getBase64image());
Expand Down
42 changes: 3 additions & 39 deletions src/main/java/edu/harvard/iq/dataverse/dataset/DatasetUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static List<DatasetThumbnail> getThumbnailCandidates(Dataset dataset, boo
return thumbnails;
}

public static DatasetThumbnail getThumbnail(Dataset dataset, DatasetVersionServiceBean datasetVersionService, DataFileServiceBean dataFileService) {
public static DatasetThumbnail getThumbnail(Dataset dataset) {
if (dataset == null) {
return null;
}
Expand All @@ -94,16 +94,8 @@ public static DatasetThumbnail getThumbnail(Dataset dataset, DatasetVersionServi
} else {
DataFile thumbnailFile = dataset.getThumbnailFile();
if (thumbnailFile == null) {
logger.fine(title + " does not have a thumbnail file set but the search card might have one");
DatasetThumbnail thumbnailThatMightBeOnSearchCard = findThumbnailThatMightBeShowingOnTheSearchCards(dataset, datasetVersionService, dataFileService);
if (thumbnailThatMightBeOnSearchCard != null) {
logger.fine(title + " does not have a thumbnail file set but a thumbnail was found as a search card thumbnail.");
return thumbnailThatMightBeOnSearchCard;
} else {
logger.fine(title + " does not have a thumbnail file set but and couldn't find one in use on the search card.");
// returning null because dataFile.equals(thumbnailFile) will never match since thumbnailFile is null and there's no point in interating through the files
return null;
}
logger.fine(title + " does not have a thumbnail.");
return null;
}
String imageSourceBase64 = ImageThumbConverter.getImageThumbAsBase64(thumbnailFile, ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE);
DatasetThumbnail datasetThumbnail = new DatasetThumbnail(imageSourceBase64, thumbnailFile);
Expand All @@ -112,34 +104,6 @@ public static DatasetThumbnail getThumbnail(Dataset dataset, DatasetVersionServi
}
}

public static DatasetThumbnail findThumbnailThatMightBeShowingOnTheSearchCards(Dataset dataset, DatasetVersionServiceBean datasetVersionService, DataFileServiceBean dataFileService) {
boolean disableThisMethodToSeeIfWeCanDeleteIt = false;
if (disableThisMethodToSeeIfWeCanDeleteIt) {
return null;
}
if (dataset == null) {
logger.info("Dataset is null so returning null.");
return null;
}
if (datasetVersionService == null || dataFileService == null) {
logger.info("Without service beans, can't determine if search cards have a thumbnail or not for dataset id " + dataset.getId());
return null;
}
Long randomThumbnail = datasetVersionService.getThumbnailByVersionId(dataset.getLatestVersion().getId());
if (randomThumbnail != null) {
DataFile thumbnailImageFile = null;
thumbnailImageFile = dataFileService.findCheapAndEasy(randomThumbnail);
if (dataFileService.isThumbnailAvailable(thumbnailImageFile)) {
String randomlySelectedThumbnail = ImageThumbConverter.getImageThumbAsBase64(
thumbnailImageFile,
ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE);
DatasetThumbnail datasetThumbnail = new DatasetThumbnail(randomlySelectedThumbnail, thumbnailImageFile);
return datasetThumbnail;
}
}
return null;
}

public static boolean deleteDatasetLogo(Dataset dataset) {
File originalFile = new File(dataset.getFileSystemDirectory().toString(), datasetLogoFilenameFinal);
boolean originalFileDeleted = originalFile.delete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,25 @@ public Dataset save(CommandContext ctxt) throws CommandException {
Timestamp updateTime = new Timestamp(new Date().getTime());
theDataset.getEditVersion().setLastUpdateTime(updateTime);
theDataset.setModificationTime(updateTime);
boolean userWantsGenericThumbnail = theDataset.isUseGenericThumbnail();
DataFile thumbnailFile = theDataset.getThumbnailFile();
boolean hasThumbnailFile = thumbnailFile != null;
for (DataFile dataFile : theDataset.getFiles()) {
if (!userWantsGenericThumbnail && !hasThumbnailFile) {
/**
* @todo What if this file isn't in the current version? What
* should happen? Probably the user shouldn't be allowed to use
* it as a thumbnail? We shouldn't allow restricted files,
* right? If we do, later we'll need to do expensive permission
* checks just to show the thumbnail and we won't be able to
* simply serve static images on a CDN as suggested by
* https://github.com/IQSS/dataverse/issues/2647
*/
if (dataFile.isImage() && !dataFile.isRestricted()) {
theDataset.setThumbnailFile(dataFile);
hasThumbnailFile = true;
}
}
if (dataFile.getCreateDate() == null) {
dataFile.setCreateDate(updateTime);
dataFile.setCreator((AuthenticatedUser) getUser());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public DatasetThumbnail execute(CommandContext ctxt) throws CommandException {
throw new CommandException("Could not find file based on id supplied: " + dataFileIdSupplied + ".", this);
}
Dataset ds1 = ctxt.datasets().setDatasetFileAsThumbnail(dataset, datasetFileThumbnailToSwitchTo);
DatasetThumbnail datasetThumbnail = ds1.getDatasetThumbnail(ctxt.datasetVersion(), ctxt.files());
DatasetThumbnail datasetThumbnail = ds1.getDatasetThumbnail();
if (datasetThumbnail != null) {
DataFile dataFile = datasetThumbnail.getDataFile();
if (dataFile != null) {
Expand Down Expand Up @@ -101,14 +101,14 @@ public DatasetThumbnail execute(CommandContext ctxt) throws CommandException {
}
Dataset datasetWithNewThumbnail = ctxt.datasets().setNonDatasetFileAsThumbnail(dataset, fileAsStream);
if (datasetWithNewThumbnail != null) {
return datasetWithNewThumbnail.getDatasetThumbnail(ctxt.datasetVersion(), ctxt.files());
return datasetWithNewThumbnail.getDatasetThumbnail();
} else {
return null;
}

case removeThumbnail:
Dataset ds2 = ctxt.datasets().removeDatasetThumbnail(dataset);
DatasetThumbnail datasetThumbnail2 = ds2.getDatasetThumbnail(ctxt.datasetVersion(), ctxt.files());
DatasetThumbnail datasetThumbnail2 = ds2.getDatasetThumbnail();
if (datasetThumbnail2 == null) {
return null;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ public class SearchServiceBean {
@EJB
DatasetServiceBean datasetService;
@EJB
DatasetVersionServiceBean datasetVersionService;
@EJB
DataFileServiceBean dataFileService;
@EJB
DatasetFieldServiceBean datasetFieldService;
@EJB
GroupServiceBean groupService;
Expand Down Expand Up @@ -505,7 +501,7 @@ public SolrQueryResponse search(DataverseRequest dataverseRequest, Dataverse dat
*/
Dataset dataset = datasetService.find(entityid);
if (dataset != null) {
DatasetThumbnail datasetThumbnail = dataset.getDatasetThumbnail(datasetVersionService, dataFileService);
DatasetThumbnail datasetThumbnail = dataset.getDatasetThumbnail();
if (datasetThumbnail != null) {
solrSearchResult.setDatasetThumbnailBase64image(datasetThumbnail.getBase64image());
solrSearchResult.setDatasetThumbnail(datasetThumbnail);
Expand Down

0 comments on commit a66fce2

Please sign in to comment.