Skip to content

Commit

Permalink
119612: configurable limit on exporting items since it can take up a …
Browse files Browse the repository at this point in the history
…bunch of resources
  • Loading branch information
Jens Vannerum committed Nov 19, 2024
1 parent 2f74a76 commit b634e1e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void internalRun() throws Exception {

Iterator<Item> itemIterator = searchService.iteratorSearch(context, dso, discoverQuery);
handler.logDebug("creating dspacecsv");
DSpaceCSV dSpaceCSV = metadataDSpaceCsvExportService.export(context, itemIterator, true);
DSpaceCSV dSpaceCSV = metadataDSpaceCsvExportService.export(context, itemIterator, true, handler);
handler.logDebug("writing to file " + getFileNameOrExportFile());
handler.writeFilestream(context, getFileNameOrExportFile(), dSpaceCSV.getInputStream(), EXPORT_CSV);
context.restoreAuthSystemState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
import java.util.Set;
import java.util.UUID;

import org.apache.commons.collections.IteratorUtils;
import org.dspace.app.bulkedit.DSpaceCSV;
import org.dspace.app.util.service.DSpaceObjectUtils;
import org.dspace.content.service.ItemService;
import org.dspace.content.service.MetadataDSpaceCsvExportService;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.eperson.service.GroupService;
import org.dspace.handle.factory.HandleServiceFactory;
import org.dspace.scripts.handler.DSpaceRunnableHandler;
import org.dspace.services.ConfigurationService;
import org.springframework.beans.factory.annotation.Autowired;

/**
Expand All @@ -36,6 +39,12 @@ public class MetadataDSpaceCsvExportServiceImpl implements MetadataDSpaceCsvExpo
@Autowired
private DSpaceObjectUtils dSpaceObjectUtils;

@Autowired
private ConfigurationService configurationService;

@Autowired
private GroupService groupService;

@Override
public DSpaceCSV handleExport(Context context, boolean exportAllItems, boolean exportAllMetadata, String identifier,
DSpaceRunnableHandler handler) throws Exception {
Expand Down Expand Up @@ -74,17 +83,19 @@ public DSpaceCSV handleExport(Context context, boolean exportAllItems, boolean e
}
}

DSpaceCSV csv = this.export(context, toExport, exportAllMetadata);
DSpaceCSV csv = this.export(context, toExport, exportAllMetadata, handler);
return csv;
}

@Override
public DSpaceCSV export(Context context, Iterator<Item> toExport, boolean exportAll) throws Exception {
public DSpaceCSV export(Context context, Iterator<Item> toExport,
boolean exportAll, DSpaceRunnableHandler handler) throws Exception {
Context.Mode originalMode = context.getCurrentMode();
context.setMode(Context.Mode.READ_ONLY);

// Process each item
DSpaceCSV csv = new DSpaceCSV(exportAll);
toExport = setItemsToExportWithLimit(context, toExport, handler);
while (toExport.hasNext()) {
Item item = toExport.next();
csv.addItem(item);
Expand All @@ -97,8 +108,32 @@ public DSpaceCSV export(Context context, Iterator<Item> toExport, boolean export
}

@Override
public DSpaceCSV export(Context context, Community community, boolean exportAll) throws Exception {
return export(context, buildFromCommunity(context, community), exportAll);
public DSpaceCSV export(Context context, Community community,
boolean exportAll, DSpaceRunnableHandler handler) throws Exception {
return export(context, buildFromCommunity(context, community), exportAll, handler);
}

private Iterator<Item> setItemsToExportWithLimit(Context context, Iterator<Item> toExport,
DSpaceRunnableHandler handler) throws SQLException {
int itemExportLimit = configurationService.getIntProperty(
"metadataexport.max.items", 500);
String[] ignoreLimitGroups = configurationService.getArrayProperty(
"metadataexport.admin.groups");

for (String group : ignoreLimitGroups) {
if (groupService.isMember(context, context.getCurrentUser(), group)) {
itemExportLimit = Integer.MAX_VALUE;
break;
}
}

List<Item> items = IteratorUtils.toList(toExport);
if (items.size() > itemExportLimit) {
handler.logWarning("The amount of items to export is higher than the limit of " + itemExportLimit
+ " items. Only the first " + itemExportLimit + " items will be exported.");
items = items.subList(0, itemExportLimit);
}
return items.iterator();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public DSpaceCSV handleExport(Context context, boolean exportAllItems, boolean e
* @return A DSpaceCSV object containing the exported information
* @throws Exception If something goes wrong
*/
public DSpaceCSV export(Context context, Iterator<Item> toExport, boolean exportAll) throws Exception;
public DSpaceCSV export(Context context, Iterator<Item> toExport,
boolean exportAll, DSpaceRunnableHandler handler) throws Exception;

/**
* This method will export all the Items within the given Community to a DSpaceCSV
Expand All @@ -54,6 +55,7 @@ public DSpaceCSV handleExport(Context context, boolean exportAllItems, boolean e
* @return A DSpaceCSV object containing the exported information
* @throws Exception If something goes wrong
*/
public DSpaceCSV export(Context context, Community community, boolean exportAll) throws Exception;
public DSpaceCSV export(Context context, Community community,
boolean exportAll, DSpaceRunnableHandler handler) throws Exception;

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.dspace.AbstractIntegrationTestWithDatabase;
import org.dspace.app.bulkedit.DSpaceCSV;
import org.dspace.app.bulkedit.DSpaceCSVLine;
import org.dspace.app.scripts.handler.impl.TestDSpaceRunnableHandler;
import org.dspace.builder.CollectionBuilder;
import org.dspace.builder.CommunityBuilder;
import org.dspace.builder.ItemBuilder;
Expand All @@ -31,6 +32,9 @@
*/
public class MetadataDSpaceCsvExportServiceImplIT
extends AbstractIntegrationTestWithDatabase {

TestDSpaceRunnableHandler testDSpaceRunnableHandler = new TestDSpaceRunnableHandler();

/**
* Test of handleExport method, of class MetadataDSpaceCsvExportServiceImpl.
* @throws java.lang.Exception passed through.
Expand Down Expand Up @@ -66,7 +70,7 @@ public void testExport_3args_1()
boolean exportAll = false;
MetadataDSpaceCsvExportServiceImpl instance = new MetadataDSpaceCsvExportServiceImpl();
DSpaceCSV expResult = null;
DSpaceCSV result = instance.export(context, toExport, exportAll);
DSpaceCSV result = instance.export(context, toExport, exportAll, testDSpaceRunnableHandler);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
Expand Down Expand Up @@ -105,7 +109,7 @@ public void testMappedItem()
.getServiceManager()
.getServiceByName(MetadataDSpaceCsvExportServiceImpl.class.getCanonicalName(),
MetadataDSpaceCsvExportService.class);
DSpaceCSV result = instance.export(context, parentCommunity, false);
DSpaceCSV result = instance.export(context, parentCommunity, false, testDSpaceRunnableHandler);

// Examine the result.
List<DSpaceCSVLine> csvLines = result.getCSVLines();
Expand Down
9 changes: 9 additions & 0 deletions dspace/config/dspace.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,15 @@ org.dspace.app.itemexport.life.span.hours = 48
# cumulative sizes are more than this entry the export is not kicked off
org.dspace.app.itemexport.max.size = 200

### Bulkedit Metadata export settings
# The maximum amount of items that can be exported using the "metadata-export" script / process
# Recommend to keep this at a feasible number, as exporting large amounts of items can be resource intensive
metadataexport.max.items = 1

# A list of groups that are allowed to use the metadata-export script without any restrictions
#metadataexport.admin.groups = Administrator


### Batch Item import settings ###
# The directory where the results of imports will be placed (mapfile, upload file)
org.dspace.app.batchitemimport.work.dir = ${dspace.dir}/imports
Expand Down

0 comments on commit b634e1e

Please sign in to comment.