Skip to content

Commit

Permalink
[DSC-1256] Avoid NPE checking bitstream content when metadata are mis…
Browse files Browse the repository at this point in the history
…sing on cris layout
  • Loading branch information
eskander committed Feb 15, 2024
1 parent af719df commit 0a7f039
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
54 changes: 28 additions & 26 deletions dspace-api/src/main/java/org/dspace/content/ItemServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,28 +216,6 @@ public Thumbnail getThumbnail(Context context, Item item) throws SQLException {
if (thumbnail != null) {
return thumbnail;
}
// If no thumbnail is retrieved by the first strategy
// then use the fallback strategy
Bitstream thumbBitstream = null;
List<Bundle> originalBundles = getBundles(item, "ORIGINAL");
Bitstream primaryBitstream = null;
if (CollectionUtils.isNotEmpty(originalBundles)) {
primaryBitstream = originalBundles.get(0).getPrimaryBitstream();
}
if (primaryBitstream == null) {
primaryBitstream = bitstreamService.getFirstBitstream(item, "ORIGINAL");
}
if (primaryBitstream != null) {
thumbBitstream = bitstreamService.getThumbnail(context, primaryBitstream);
if (thumbBitstream == null) {
thumbBitstream = bitstreamService.getFirstBitstream(item, "THUMBNAIL");
}
}

if (thumbBitstream != null) {
return new Thumbnail(thumbBitstream, primaryBitstream);
}

return null;
}

Expand All @@ -252,7 +230,27 @@ private Thumbnail thumbnailLayoutTabConfigurationStrategy(Context context, Item

List<CrisLayoutField> thumbFields = getThumbnailFields(crisLayoutTabs);
if (CollectionUtils.isEmpty(thumbFields)) {
return null;
// If no thumbnail is retrieved by the first strategy
// then use the fallback strategy
Bitstream thumbBitstream = null;
List<Bundle> originalBundles = getBundles(item, "ORIGINAL");
Bitstream primaryBitstream = null;
if (CollectionUtils.isNotEmpty(originalBundles)) {
primaryBitstream = originalBundles.get(0).getPrimaryBitstream();
}
if (primaryBitstream == null) {
primaryBitstream = bitstreamService.getFirstBitstream(item, "ORIGINAL");
}
if (primaryBitstream != null) {
thumbBitstream = bitstreamService.getThumbnail(context, primaryBitstream);
if (thumbBitstream == null) {
thumbBitstream = bitstreamService.getFirstBitstream(item, "THUMBNAIL");
}
}

if (thumbBitstream != null) {
return new Thumbnail(thumbBitstream, primaryBitstream);
}
}
return retrieveThumbnailFromFields(context, item, thumbFields);
}
Expand Down Expand Up @@ -312,9 +310,13 @@ private Thumbnail retrieveThumbnail(Context context, Item item, String bundle,
if (CollectionUtils.isNotEmpty(bundles)) {
Optional<Bitstream> primaryBitstream = bundles.get(0).getBitstreams().stream().filter(bitstream -> {
return bitstream.getMetadata().stream().anyMatch(metadataValue -> {
return metadataValue.getMetadataField().getID() == metadataField.getID()
&& metadataValue.getValue() != null
&& metadataValue.getValue().equalsIgnoreCase(value);
if (metadataField != null) {
return metadataValue.getMetadataField().getID() == metadataField.getID()
&& metadataValue.getValue() != null
&& metadataValue.getValue().equalsIgnoreCase(value);
} else {
return true;
}
});
}).findFirst();
if (primaryBitstream.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -208,13 +209,10 @@ private boolean isMetadataFieldPresent(DSpaceObject item, MetadataField metadata
}

private boolean isBitstreamPresent(Context context, Item item, CrisLayoutFieldBitstream field) {

Map<String, String> filters = Map.of();

if (field.getMetadataField() != null) {
filters = Map.of(field.getMetadataField().toString('.'), field.getMetadataValue());
Map<String, String> filters = new HashMap<>();
if (field.getMetadataField() != null && StringUtils.isNotBlank(field.getMetadataValue())) {
filters.put(field.getMetadataField().toString('.'), field.getMetadataValue());
}

try {
return bitstreamService.findShowableByItem(context, item.getID(), field.getBundle(), filters).size() > 0;
} catch (SQLException e) {
Expand Down

0 comments on commit 0a7f039

Please sign in to comment.