mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-07 01:54:22 +00:00
88061: Refactor VersioningConsumer
This commit is contained in:
@@ -10,7 +10,10 @@ package org.dspace.versioning;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.dspace.content.Item;
|
||||
import org.dspace.content.Relationship;
|
||||
import org.dspace.content.factory.ContentServiceFactory;
|
||||
import org.dspace.content.service.ItemService;
|
||||
import org.dspace.core.Constants;
|
||||
@@ -19,26 +22,28 @@ import org.dspace.event.Consumer;
|
||||
import org.dspace.event.Event;
|
||||
import org.dspace.versioning.factory.VersionServiceFactory;
|
||||
import org.dspace.versioning.service.VersionHistoryService;
|
||||
import org.dspace.versioning.service.VersioningService;
|
||||
|
||||
/**
|
||||
* When a new version of an item is published, unarchive the previous version and
|
||||
* update {@link Relationship#latestVersionStatus} of the relevant relationships.
|
||||
*
|
||||
* @author Fabio Bolognesi (fabio at atmire dot com)
|
||||
* @author Mark Diggory (markd at atmire dot com)
|
||||
* @author Ben Bosman (ben at atmire dot com)
|
||||
*/
|
||||
public class VersioningConsumer implements Consumer {
|
||||
|
||||
private static Set<Item> itemsToProcess;
|
||||
private static final Logger log = LogManager.getLogger(VersioningConsumer.class);
|
||||
|
||||
private Set<Item> itemsToProcess;
|
||||
|
||||
private VersionHistoryService versionHistoryService;
|
||||
private VersioningService versioningService;
|
||||
private ItemService itemService;
|
||||
|
||||
|
||||
@Override
|
||||
public void initialize() throws Exception {
|
||||
versionHistoryService = VersionServiceFactory.getInstance().getVersionHistoryService();
|
||||
versioningService = VersionServiceFactory.getInstance().getVersionService();
|
||||
itemService = ContentServiceFactory.getInstance().getItemService();
|
||||
}
|
||||
|
||||
@@ -49,35 +54,75 @@ public class VersioningConsumer implements Consumer {
|
||||
@Override
|
||||
public void consume(Context ctx, Event event) throws Exception {
|
||||
if (itemsToProcess == null) {
|
||||
itemsToProcess = new HashSet<Item>();
|
||||
itemsToProcess = new HashSet<>();
|
||||
}
|
||||
|
||||
int st = event.getSubjectType();
|
||||
int et = event.getEventType();
|
||||
|
||||
if (st == Constants.ITEM && et == Event.INSTALL) {
|
||||
Item item = (Item) event.getSubject(ctx);
|
||||
if (item != null && item.isArchived()) {
|
||||
VersionHistory history = versionHistoryService.findByItem(ctx, item);
|
||||
if (history != null) {
|
||||
Version latest = versionHistoryService.getLatestVersion(ctx, history);
|
||||
Version previous = versionHistoryService.getPrevious(ctx, history, latest);
|
||||
if (previous != null) {
|
||||
Item previousItem = previous.getItem();
|
||||
if (previousItem != null) {
|
||||
previousItem.setArchived(false);
|
||||
itemsToProcess.add(previousItem);
|
||||
//Fire a new modify event for our previous item
|
||||
//Due to the need to reindex the item in the search
|
||||
//and browse index we need to fire a new event
|
||||
ctx.addEvent(new Event(Event.MODIFY,
|
||||
previousItem.getType(), previousItem.getID(),
|
||||
null, itemService.getIdentifiers(ctx, previousItem)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// only items
|
||||
if (event.getSubjectType() != Constants.ITEM) {
|
||||
return;
|
||||
}
|
||||
|
||||
// only install events
|
||||
if (event.getEventType() != Event.INSTALL) {
|
||||
return;
|
||||
}
|
||||
|
||||
// get the item (should be archived)
|
||||
Item item = (Item) event.getSubject(ctx);
|
||||
if (item == null || !item.isArchived()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// get version history
|
||||
VersionHistory history = versionHistoryService.findByItem(ctx, item);
|
||||
if (history == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// get latest version
|
||||
Version latestVersion = versionHistoryService.getLatestVersion(ctx, history);
|
||||
if (latestVersion == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// get previous version
|
||||
Version previousVersion = versionHistoryService.getPrevious(ctx, history, latestVersion);
|
||||
if (previousVersion == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// get latest item
|
||||
Item latestItem = latestVersion.getItem();
|
||||
if (latestItem == null) {
|
||||
String msg = String.format(
|
||||
"Illegal state: Obtained version history of item with uuid %s, handle %s, but the latest item is null",
|
||||
item.getID(), item.getHandle()
|
||||
);
|
||||
log.error(msg);
|
||||
throw new IllegalStateException(msg);
|
||||
}
|
||||
|
||||
// get previous item
|
||||
Item previousItem = previousVersion.getItem();
|
||||
if (previousItem == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// unarchive previous item
|
||||
unarchiveItem(ctx, previousItem);
|
||||
|
||||
// TODO implement w2p 88061
|
||||
}
|
||||
|
||||
protected void unarchiveItem(Context ctx, Item item) {
|
||||
item.setArchived(false);
|
||||
itemsToProcess.add(item);
|
||||
//Fire a new modify event for our previous item
|
||||
//Due to the need to reindex the item in the search
|
||||
//and browse index we need to fire a new event
|
||||
ctx.addEvent(new Event(
|
||||
Event.MODIFY, item.getType(), item.getID(), null, itemService.getIdentifiers(ctx, item)
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user