Skip to content

Commit 832c933

Browse files
committed
this makes the auto-select feature for the designated dataset thumbnail optional #10815
1 parent 34bfb1b commit 832c933

File tree

3 files changed

+86
-69
lines changed

3 files changed

+86
-69
lines changed

src/main/java/edu/harvard/iq/dataverse/Dataset.java

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import edu.harvard.iq.dataverse.license.License;
77
import edu.harvard.iq.dataverse.makedatacount.DatasetExternalCitations;
88
import edu.harvard.iq.dataverse.makedatacount.DatasetMetrics;
9+
import edu.harvard.iq.dataverse.settings.FeatureFlags;
910
import java.nio.file.Path;
1011
import java.nio.file.Paths;
1112
import java.sql.Timestamp;
@@ -206,6 +207,10 @@ public Dataset(boolean isHarvested) {
206207
StorageUse storageUse = new StorageUse(this);
207208
this.setStorageUse(storageUse);
208209
}
210+
211+
if (!FeatureFlags.ENABLE_DATASET_THUMBNAIL_AUTOSELECT.enabled()) {
212+
this.setUseGenericThumbnail(true);
213+
}
209214
}
210215

211216
/**

src/main/java/edu/harvard/iq/dataverse/DatasetVersionServiceBean.java

+71-69
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static edu.harvard.iq.dataverse.batch.jobs.importer.filesystem.FileRecordJobListener.SEP;
1010
import edu.harvard.iq.dataverse.batch.util.LoggingUtil;
1111
import edu.harvard.iq.dataverse.search.SolrSearchResult;
12+
import edu.harvard.iq.dataverse.settings.FeatureFlags;
1213
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
1314
import edu.harvard.iq.dataverse.util.BundleUtil;
1415
import edu.harvard.iq.dataverse.util.MarkupChecker;
@@ -807,100 +808,101 @@ public Long getThumbnailByVersionId(Long versionId) {
807808
return null;
808809
}
809810

810-
Long thumbnailFileId;
811-
812-
// First, let's see if there are thumbnails that have already been
813-
// generated:
814-
try {
815-
thumbnailFileId = (Long) em.createNativeQuery("SELECT df.id "
816-
+ "FROM datafile df, filemetadata fm, datasetversion dv, dvobject o "
817-
+ "WHERE dv.id = " + versionId + " "
818-
+ "AND df.id = o.id "
819-
+ "AND fm.datasetversion_id = dv.id "
820-
+ "AND fm.datafile_id = df.id "
821-
+ "AND df.restricted = false "
822-
+ "AND df.embargo_id is null "
823-
+ "AND df.retention_id is null "
824-
+ "AND o.previewImageAvailable = true "
825-
+ "ORDER BY df.id LIMIT 1;").getSingleResult();
826-
} catch (Exception ex) {
827-
thumbnailFileId = null;
828-
}
829-
830-
if (thumbnailFileId != null) {
831-
logger.fine("DatasetVersionService,getThumbnailByVersionid(): found already generated thumbnail for version " + versionId + ": " + thumbnailFileId);
832-
assignDatasetThumbnailByNativeQuery(versionId, thumbnailFileId);
833-
return thumbnailFileId;
834-
}
835-
836-
if (!systemConfig.isThumbnailGenerationDisabledForImages()) {
837-
// OK, let's try and generate an image thumbnail!
838-
long imageThumbnailSizeLimit = systemConfig.getThumbnailSizeLimitImage();
811+
if (FeatureFlags.ENABLE_DATASET_THUMBNAIL_AUTOSELECT.enabled()) {
812+
Long thumbnailFileId;
839813

814+
// First, let's see if there are thumbnails that have already been
815+
// generated:
840816
try {
841817
thumbnailFileId = (Long) em.createNativeQuery("SELECT df.id "
842818
+ "FROM datafile df, filemetadata fm, datasetversion dv, dvobject o "
843819
+ "WHERE dv.id = " + versionId + " "
844820
+ "AND df.id = o.id "
845821
+ "AND fm.datasetversion_id = dv.id "
846822
+ "AND fm.datafile_id = df.id "
847-
+ "AND o.previewimagefail = false "
848823
+ "AND df.restricted = false "
849824
+ "AND df.embargo_id is null "
850825
+ "AND df.retention_id is null "
851-
+ "AND df.contenttype LIKE 'image/%' "
852-
+ "AND NOT df.contenttype = 'image/fits' "
853-
+ "AND df.filesize < " + imageThumbnailSizeLimit + " "
854-
+ "ORDER BY df.filesize ASC LIMIT 1;").getSingleResult();
826+
+ "AND o.previewImageAvailable = true "
827+
+ "ORDER BY df.id LIMIT 1;").getSingleResult();
855828
} catch (Exception ex) {
856829
thumbnailFileId = null;
857830
}
858831

859832
if (thumbnailFileId != null) {
860-
logger.fine("obtained file id: " + thumbnailFileId);
861-
DataFile thumbnailFile = datafileService.find(thumbnailFileId);
862-
if (thumbnailFile != null) {
863-
if (datafileService.isThumbnailAvailable(thumbnailFile)) {
864-
assignDatasetThumbnailByNativeQuery(versionId, thumbnailFileId);
865-
return thumbnailFileId;
833+
logger.fine("DatasetVersionService,getThumbnailByVersionid(): found already generated thumbnail for version " + versionId + ": " + thumbnailFileId);
834+
assignDatasetThumbnailByNativeQuery(versionId, thumbnailFileId);
835+
return thumbnailFileId;
836+
}
837+
838+
if (!systemConfig.isThumbnailGenerationDisabledForImages()) {
839+
// OK, let's try and generate an image thumbnail!
840+
long imageThumbnailSizeLimit = systemConfig.getThumbnailSizeLimitImage();
841+
842+
try {
843+
thumbnailFileId = (Long) em.createNativeQuery("SELECT df.id "
844+
+ "FROM datafile df, filemetadata fm, datasetversion dv, dvobject o "
845+
+ "WHERE dv.id = " + versionId + " "
846+
+ "AND df.id = o.id "
847+
+ "AND fm.datasetversion_id = dv.id "
848+
+ "AND fm.datafile_id = df.id "
849+
+ "AND o.previewimagefail = false "
850+
+ "AND df.restricted = false "
851+
+ "AND df.embargo_id is null "
852+
+ "AND df.retention_id is null "
853+
+ "AND df.contenttype LIKE 'image/%' "
854+
+ "AND NOT df.contenttype = 'image/fits' "
855+
+ "AND df.filesize < " + imageThumbnailSizeLimit + " "
856+
+ "ORDER BY df.filesize ASC LIMIT 1;").getSingleResult();
857+
} catch (Exception ex) {
858+
thumbnailFileId = null;
859+
}
860+
861+
if (thumbnailFileId != null) {
862+
logger.fine("obtained file id: " + thumbnailFileId);
863+
DataFile thumbnailFile = datafileService.find(thumbnailFileId);
864+
if (thumbnailFile != null) {
865+
if (datafileService.isThumbnailAvailable(thumbnailFile)) {
866+
assignDatasetThumbnailByNativeQuery(versionId, thumbnailFileId);
867+
return thumbnailFileId;
868+
}
866869
}
867870
}
868871
}
869-
}
870872

871-
// And if that didn't work, try the same thing for PDFs:
872-
if (!systemConfig.isThumbnailGenerationDisabledForPDF()) {
873-
// OK, let's try and generate an image thumbnail!
874-
long imageThumbnailSizeLimit = systemConfig.getThumbnailSizeLimitPDF();
875-
try {
876-
thumbnailFileId = (Long) em.createNativeQuery("SELECT df.id "
877-
+ "FROM datafile df, filemetadata fm, datasetversion dv, dvobject o "
878-
+ "WHERE dv.id = " + versionId + " "
879-
+ "AND df.id = o.id "
880-
+ "AND fm.datasetversion_id = dv.id "
881-
+ "AND fm.datafile_id = df.id "
882-
+ "AND o.previewimagefail = false "
883-
+ "AND df.restricted = false "
884-
+ "AND df.embargo_id is null "
885-
+ "AND df.retention_id is null "
886-
+ "AND df.contenttype = 'application/pdf' "
887-
+ "AND df.filesize < " + imageThumbnailSizeLimit + " "
888-
+ "ORDER BY df.filesize ASC LIMIT 1;").getSingleResult();
889-
} catch (Exception ex) {
890-
thumbnailFileId = null;
891-
}
873+
// And if that didn't work, try the same thing for PDFs:
874+
if (!systemConfig.isThumbnailGenerationDisabledForPDF()) {
875+
// OK, let's try and generate an image thumbnail!
876+
long imageThumbnailSizeLimit = systemConfig.getThumbnailSizeLimitPDF();
877+
try {
878+
thumbnailFileId = (Long) em.createNativeQuery("SELECT df.id "
879+
+ "FROM datafile df, filemetadata fm, datasetversion dv, dvobject o "
880+
+ "WHERE dv.id = " + versionId + " "
881+
+ "AND df.id = o.id "
882+
+ "AND fm.datasetversion_id = dv.id "
883+
+ "AND fm.datafile_id = df.id "
884+
+ "AND o.previewimagefail = false "
885+
+ "AND df.restricted = false "
886+
+ "AND df.embargo_id is null "
887+
+ "AND df.retention_id is null "
888+
+ "AND df.contenttype = 'application/pdf' "
889+
+ "AND df.filesize < " + imageThumbnailSizeLimit + " "
890+
+ "ORDER BY df.filesize ASC LIMIT 1;").getSingleResult();
891+
} catch (Exception ex) {
892+
thumbnailFileId = null;
893+
}
892894

893-
if (thumbnailFileId != null) {
894-
DataFile thumbnailFile = datafileService.find(thumbnailFileId);
895-
if (thumbnailFile != null) {
896-
if (datafileService.isThumbnailAvailable(thumbnailFile)) {
897-
assignDatasetThumbnailByNativeQuery(versionId, thumbnailFileId);
898-
return thumbnailFileId;
895+
if (thumbnailFileId != null) {
896+
DataFile thumbnailFile = datafileService.find(thumbnailFileId);
897+
if (thumbnailFile != null) {
898+
if (datafileService.isThumbnailAvailable(thumbnailFile)) {
899+
assignDatasetThumbnailByNativeQuery(versionId, thumbnailFileId);
900+
return thumbnailFileId;
901+
}
899902
}
900903
}
901904
}
902905
}
903-
904906
return null;
905907
}
906908

src/main/java/edu/harvard/iq/dataverse/settings/FeatureFlags.java

+10
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ public enum FeatureFlags {
9191
* @since Dataverse 6.3
9292
*/
9393
DISABLE_RETURN_TO_AUTHOR_REASON("disable-return-to-author-reason"),
94+
/**
95+
* This flag enables the feature that automatically selects one of the
96+
* DataFile thumbnails in the dataset/version as the dedicated thumbnail
97+
* for the dataset.
98+
*
99+
* @apiNote Raise flag by setting
100+
* "dataverse.feature.enable-dataset-thumbnail-autoselect"
101+
* @since Dataverse 6.4
102+
*/
103+
ENABLE_DATASET_THUMBNAIL_AUTOSELECT("enable-dataset-thumbnail-autoselect"),
94104
;
95105

96106
final String flag;

0 commit comments

Comments
 (0)