Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ public List<ClarinLicenseResourceMapping> findByBitstreamUUID(Context context, U
return clarinLicenseResourceMappingDAO.findByBitstreamUUID(context, bitstreamID);
}

public List<ClarinLicenseResourceMapping> findByBitstreamUUIDs(Context context, List<UUID> bitstreamIDs)
throws SQLException {
return clarinLicenseResourceMappingDAO.findByBitstreamUUIDs(context, bitstreamIDs);
}

@Override
public ClarinLicense getLicenseToAgree(Context context, UUID userId, UUID resourceID) throws SQLException {
// Load Clarin License for current bitstream.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@
public interface ClarinLicenseResourceMappingDAO extends GenericDAO<ClarinLicenseResourceMapping> {

List<ClarinLicenseResourceMapping> findByBitstreamUUID(Context context, UUID bitstreamUUID) throws SQLException;
List<ClarinLicenseResourceMapping> findByBitstreamUUIDs(Context context, List<UUID> bitstreamUUIDs)
throws SQLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ public List<ClarinLicenseResourceMapping> findByBitstreamUUID(Context context, U
return list(query);
}

@Override
public List<ClarinLicenseResourceMapping> findByBitstreamUUIDs(Context context, List<UUID> bitstreamUUIDs)
throws SQLException {
if (bitstreamUUIDs == null || bitstreamUUIDs.isEmpty()) {
return List.of();
}
Query query = createQuery(context, "SELECT clrm " +
"FROM ClarinLicenseResourceMapping clrm " +
"WHERE clrm.bitstream.id IN :bitstreamUUIDs");
query.setParameter("bitstreamUUIDs", bitstreamUUIDs);
query.setHint("org.hibernate.cacheable", Boolean.TRUE);
return list(query);
}

@Override
public void delete(Context context, ClarinLicenseResourceMapping clarinLicenseResourceMapping) throws SQLException {
clarinLicenseResourceMapping.setBitstream(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@ void attachLicense(Context context, ClarinLicense clarinLicense, Bitstream bitst

List<ClarinLicenseResourceMapping> findByBitstreamUUID(Context context, UUID bitstreamID) throws SQLException;

List<ClarinLicenseResourceMapping> findByBitstreamUUIDs(Context context, List<UUID> bitstreamIDs)
throws SQLException;

ClarinLicense getLicenseToAgree(Context context, UUID userId, UUID resourceID) throws SQLException;
}
104 changes: 48 additions & 56 deletions dspace-api/src/main/java/org/dspace/health/LicenseCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.Objects;
import java.util.UUID;

import com.amazonaws.util.CollectionUtils;
import org.dspace.content.Bitstream;
import org.dspace.content.Bundle;
import org.dspace.content.Item;
Expand All @@ -38,92 +37,85 @@ public class LicenseCheck extends Check {
private ClarinLicenseResourceMappingService clarinLicenseResourceMappingService =
ClarinServiceFactory.getInstance().getClarinLicenseResourceMappingService();

private Map<String, Integer> licensesCount = new HashMap<>();
private Map<String, List<UUID>> problemItems = new HashMap<>();

@Override
protected String run(ReportInfo ri) {
Context context = new Context();
StringBuilder sb = new StringBuilder();

Iterator<Item> items;
ItemService itemService = ContentServiceFactory.getInstance().getItemService();
Map<String, Integer> licensesCount = new HashMap<>();
Map<String, List<UUID>> problemItems = new HashMap<>();
List<UUID> bitstreamUUIDs = new ArrayList<>();
Iterator<Item> items;
try {
items = itemService.findAll(context);
} catch (SQLException e) {
throw new RuntimeException("Error while fetching items. ", e);
}

for (Iterator<Item> it = items; it.hasNext(); ) {
Item item = it.next();

List<Bundle> bundles = item.getBundles(Constants.DEFAULT_BUNDLE_NAME);
if (bundles.isEmpty()) {
licensesCount.put("no bundle", licensesCount.getOrDefault("no bundle", 0) + 1);
continue;
}

if (item.getBundles(Constants.LICENSE_BUNDLE_NAME).isEmpty()) {
problemItems.computeIfAbsent(
"UUIDs of items without license bundle", k -> new ArrayList<>()).add(item.getID());
while (items.hasNext()) {
Item item = items.next();
List<Bundle> bundles = item.getBundles(Constants.DEFAULT_BUNDLE_NAME);
if (bundles.isEmpty()) {
licensesCount.put("no bundle", licensesCount.getOrDefault("no bundle", 0) + 1);
continue;
}
if (item.getBundles(Constants.LICENSE_BUNDLE_NAME).isEmpty()) {
problemItems.computeIfAbsent(
"UUIDs of items without license bundle", k -> new ArrayList<>()).add(item.getID());
}
List<Bitstream> bitstreams = bundles.get(0).getBitstreams();
if (bitstreams.isEmpty()) {
problemItems.computeIfAbsent(
"UUIDs of items without bitstreams", k -> new ArrayList<>()).add(item.getID());
continue;
}
Bitstream firstBitstream = bitstreams.get(0);
UUID uuid = firstBitstream.getID();
bitstreamUUIDs.add(uuid);
}

List<Bitstream> bitstreams = bundles.get(0).getBitstreams();
if (bitstreams.isEmpty()) {
problemItems.computeIfAbsent(
"UUIDs of items without bitstreams", k -> new ArrayList<>()).add(item.getID());
continue;
// Batch fetch all mappings for the collected UUIDs
List<ClarinLicenseResourceMapping> mappingList =
clarinLicenseResourceMappingService.findByBitstreamUUIDs(context, bitstreamUUIDs);
Map<UUID, ClarinLicenseResourceMapping> mappingByUUID = new HashMap<>();
for (ClarinLicenseResourceMapping mapping : mappingList) {
if (mapping.getBitstream() != null) {
mappingByUUID.put(mapping.getBitstream().getID(), mapping);
}
}

// one bitstream is enough as there is only one license for all bitstreams in item
Bitstream firstBitstream = bitstreams.get(0);
UUID uuid = firstBitstream.getID();
try {
List<ClarinLicenseResourceMapping> clarinLicenseResourceMappingList =
clarinLicenseResourceMappingService.findByBitstreamUUID(context, uuid);

if (CollectionUtils.isNullOrEmpty(clarinLicenseResourceMappingList)) {
log.error("No license mapping found for bitstream with uuid {}", uuid);
// Process results in memory
for (UUID uuid : bitstreamUUIDs) {
ClarinLicenseResourceMapping mapping = mappingByUUID.get(uuid);
if (mapping == null) {
problemItems.computeIfAbsent(
"UUIDs of bitstreams without license mappings", k -> new ArrayList<>()).add(uuid);
"UUIDs of bitstreams without license mappings", k -> new ArrayList<>()).add(uuid);
continue;
}

// Every resource mapping between license and the bitstream has only one record,
// because the bitstream has unique UUID, so get the first record from the List
ClarinLicenseResourceMapping clarinLicenseResourceMapping = clarinLicenseResourceMappingList.get(0);

ClarinLicenseLabel nonExtendedLabel =
clarinLicenseResourceMapping.getLicense().getNonExtendedClarinLicenseLabel();

ClarinLicenseLabel nonExtendedLabel = mapping.getLicense().getNonExtendedClarinLicenseLabel();
if (Objects.isNull(nonExtendedLabel)) {
log.error("Item {} with id {} does not have non extended license label.",
item.getName(), item.getID());
problemItems.computeIfAbsent(
"UUIDs of bitstreams without non-extended license labels",
k -> new ArrayList<>()).add(uuid);
} else {
licensesCount.put(nonExtendedLabel.getLabel(),
licensesCount.getOrDefault(nonExtendedLabel.getLabel(), 0) + 1);
}
} catch (SQLException e) {
throw new RuntimeException("Error while fetching ClarinLicenseResourceMapping by Bitstream UUID: " +
uuid, e);
}
} catch (SQLException e) {
throw new RuntimeException("Error while fetching items or license mappings.", e);
} finally {
context.close();
}

for (Map.Entry<String, Integer> result : licensesCount.entrySet()) {
sb.append(String.format("%-20s: %d\n", result.getKey(), result.getValue()));
}

if (!problemItems.isEmpty()) {
for (Map.Entry<String, List<UUID>> problemItems : problemItems.entrySet()) {
List<UUID> uuids = problemItems.getValue();
sb.append(String.format("\n%s: %d\n", problemItems.getKey(), uuids.size()));
for (Map.Entry<String, List<UUID>> problemEntry : problemItems.entrySet()) {
List<UUID> uuids = problemEntry.getValue();
sb.append(String.format("\n%s: %d\n", problemEntry.getKey(), uuids.size()));
for (UUID uuid : uuids) {
sb.append(String.format(" %s\n", uuid));
}
}
}

context.close();
return sb.toString();
}
}