Skip to content
Merged
Changes from 2 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 @@ -13,6 +13,7 @@
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;

import org.apache.logging.log4j.Logger;
import org.dspace.authorize.AuthorizeException;
Expand All @@ -25,6 +26,7 @@
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.core.LogHelper;
import org.dspace.event.Event;
import org.dspace.workflow.WorkflowItem;
import org.dspace.workflow.WorkflowItemService;
import org.dspace.workflow.factory.WorkflowServiceFactory;
Expand Down Expand Up @@ -755,14 +757,19 @@ protected void doContainerDelete(SwordContext swordContext, Item item,
WorkflowTools wft = new WorkflowTools();
if (wft.isItemInWorkspace(swordContext.getContext(), item)) {
WorkspaceItem wsi = wft.getWorkspaceItem(context, item);
workspaceItemService.deleteWrapper(context, wsi);
workspaceItemService.deleteAll(context, wsi);
// the item is deleted in the above call
Comment thread
jr-rk marked this conversation as resolved.
Outdated
} else if (wft.isItemInWorkflow(context, item)) {
WorkflowItem wfi = wft.getWorkflowItem(context, item);
workflowItemService.deleteWrapper(context, wfi);
}

// then delete the item
itemService.delete(context, item);
// then delete the item, but only if it hasn't already been deleted by the methods above.
// the delete method is called in `workspaceItemService.deleteAll(context, wsi);`,
// so it should not be called again here, as that would throw an exception.
if (!isItemAlreadyDeleted(context, item.getID())) {
itemService.delete(context, item);
}
} catch (SQLException | IOException e) {
throw new DSpaceSwordException(e);
} catch (AuthorizeException e) {
Expand All @@ -788,4 +795,24 @@ private Item getDSpaceTarget(Context context, String editUrl,

return item;
}

/**
* Returns true if a DELETE event for this item is already queued on the context
* (i.e. the item was deleted earlier in this transaction), so the caller can skip
* a second {@code itemService.delete()} that would otherwise fail.
*/
private boolean isItemAlreadyDeleted(Context context, UUID itemUUID) {
if (context.getEvents() == null) {
return false;
}

for (Event event : context.getEvents()) {
if (event.getEventType() == Event.DELETE
&& event.getSubjectType() == Constants.ITEM
&& itemUUID.equals(event.getSubjectID())) {
return true;
}
}
return false;
}
}
Loading