diff --git a/dspace-api/src/main/java/org/dspace/content/enhancer/script/ItemEnhancerScript.java b/dspace-api/src/main/java/org/dspace/content/enhancer/script/ItemEnhancerScript.java index 3100920dc17c..bbe8a02122fa 100644 --- a/dspace-api/src/main/java/org/dspace/content/enhancer/script/ItemEnhancerScript.java +++ b/dspace-api/src/main/java/org/dspace/content/enhancer/script/ItemEnhancerScript.java @@ -31,6 +31,7 @@ * */ public class ItemEnhancerScript extends DSpaceRunnable> { + private final int PAGE_SIZE = 20; private ItemService itemService; @@ -57,7 +58,7 @@ public void internalRun() throws Exception { context.turnOffAuthorisationSystem(); try { - enhanceItems(); + enhanceItems(context); context.complete(); handler.logInfo("Enhancement completed with success"); } catch (Exception e) { @@ -68,20 +69,28 @@ public void internalRun() throws Exception { } } - private void enhanceItems() { - findItemsToEnhance().forEachRemaining(this::enhanceItem); + private void enhanceItems(Context context) { + try { + int total = itemService.countArchivedItems(context); + for (int offset = 0; offset < total; offset += PAGE_SIZE) { + findItemsToEnhance(offset).forEachRemaining(this::enhanceItem); + } + context.commit(); + context.clear(); + } catch (SQLException e) { + throw new SQLRuntimeException(e); + } } - private Iterator findItemsToEnhance() { + private Iterator findItemsToEnhance(int offset) { try { - return itemService.findAll(context); + return itemService.findAll(context, PAGE_SIZE, offset); } catch (SQLException e) { throw new SQLRuntimeException(e); } } private void enhanceItem(Item item) { - itemEnhancerService.enhance(context, item, force); uncacheItem(item); } diff --git a/dspace-api/src/main/java/org/dspace/content/enhancer/service/ItemEnhancerService.java b/dspace-api/src/main/java/org/dspace/content/enhancer/service/ItemEnhancerService.java index d345c55e82f5..d7f7318249b2 100644 --- a/dspace-api/src/main/java/org/dspace/content/enhancer/service/ItemEnhancerService.java +++ b/dspace-api/src/main/java/org/dspace/content/enhancer/service/ItemEnhancerService.java @@ -24,7 +24,15 @@ public interface ItemEnhancerService { /** * Enhances the given item with all the item enhancers defined adding virtual - * metadata fields on it. + * metadata fields on it. ItemEnhancer will use the information stored in the + * source metadata to decide if virtual metadata must be calculated. This could + * lead to stale information if the given item is linked to the same related items + * than before but in the mean time the related items have been changed in a way + * that could affect the generated virtual metadata (for example a publication + * listing 3 authors assuming that we are flatting on the publication the information + * about the author current affiliation would not update the virtual affiliation + * if this method is invoked on the item without touching the author list - in this + * scenario you need to use the {@link #forceEnhancement(Context, Item)} method * * @param context the DSpace Context * @param item the item to enhance diff --git a/dspace-api/src/main/java/org/dspace/core/Context.java b/dspace-api/src/main/java/org/dspace/core/Context.java index 4d3079240c3d..ad82ee83a841 100644 --- a/dspace-api/src/main/java/org/dspace/core/Context.java +++ b/dspace-api/src/main/java/org/dspace/core/Context.java @@ -23,6 +23,7 @@ import org.apache.logging.log4j.Logger; import org.dspace.authorize.ResourcePolicy; import org.dspace.content.DSpaceObject; +import org.dspace.core.exception.SQLRuntimeException; import org.dspace.eperson.EPerson; import org.dspace.eperson.Group; import org.dspace.eperson.factory.EPersonServiceFactory; @@ -33,6 +34,7 @@ import org.dspace.storage.rdbms.DatabaseConfigVO; import org.dspace.storage.rdbms.DatabaseUtils; import org.dspace.utils.DSpace; +import org.hibernate.Session; import org.springframework.util.CollectionUtils; /** @@ -451,6 +453,14 @@ public void commit() throws SQLException { } } + public void clear() { + try { + ((Session) dbConnection.getSession()).clear(); + reloadContextBoundEntities(); + } catch (SQLException e) { + throw new SQLRuntimeException(e); + } + } /** * Dispatch any events (cached in current Context) to configured EventListeners (consumers)