Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete resources when a new working copy is cancelled #8460

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -27,6 +27,7 @@
import com.google.common.collect.Sets;
import jeeves.server.context.ServiceContext;
import org.eclipse.jetty.io.RuntimeIOException;
import org.fao.geonet.api.records.attachments.Store;
import org.fao.geonet.api.records.attachments.StoreUtils;
import org.fao.geonet.constants.Geonet;
import org.fao.geonet.domain.*;
Expand All @@ -53,6 +54,8 @@
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import javax.annotation.Nonnull;
import javax.persistence.EntityNotFoundException;
Expand Down Expand Up @@ -635,6 +638,7 @@ public void replaceFiles(AbstractMetadata original, AbstractMetadata dest) {

@Override
public void cancelEditingSession(ServiceContext context, String id) throws Exception {
// Restore the draft to the state it was in before editing
super.cancelEditingSession(context, id);

int intId = Integer.parseInt(id);
Expand All @@ -655,6 +659,23 @@ public void cancelEditingSession(ServiceContext context, String id) throws Excep

// Unset METADATA_EDITING_CREATED_DRAFT flag
context.getUserSession().removeProperty(Geonet.Session.METADATA_EDITING_CREATED_DRAFT);

// Get the resource store
Store store = context.getBean("resourceStore", Store.class);

// Register synchronization to delete resources after commit
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
try {
// Delete resources from the store
store.delResources(context, intId);
} catch (Exception e) {
Log.error(Geonet.DATA_MANAGER, "Couldn't delete resources for draft " + id, e);
}
}
});

} catch (Exception e) {
Log.error(Geonet.DATA_MANAGER, "Couldn't cleanup draft " + id, e);
tylerjmchugh marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down