From ecabf2571a163cf4f7dbc09a78943795ffc5cdb8 Mon Sep 17 00:00:00 2001 From: tylerjmchugh Date: Tue, 22 Oct 2024 16:00:22 -0400 Subject: [PATCH 1/2] Delete resources when a new working copy is cancelled --- .../geonet/kernel/datamanager/draft/DraftMetadataUtils.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/src/main/java/org/fao/geonet/kernel/datamanager/draft/DraftMetadataUtils.java b/core/src/main/java/org/fao/geonet/kernel/datamanager/draft/DraftMetadataUtils.java index 21cf26e85bd..8d54c88492c 100644 --- a/core/src/main/java/org/fao/geonet/kernel/datamanager/draft/DraftMetadataUtils.java +++ b/core/src/main/java/org/fao/geonet/kernel/datamanager/draft/DraftMetadataUtils.java @@ -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.*; @@ -649,6 +650,10 @@ public void cancelEditingSession(ServiceContext context, String id) throws Excep metadataValidationRepository.deleteAllById_MetadataId(intId); metadataStatusRepository.deleteAllById_MetadataId(intId); + // Delete resources + Store store = context.getBean("resourceStore", Store.class); + store.delResources(context, intId); + // --- remove metadata xmlSerializer.delete(id, ServiceContext.get()); searchManager.delete(String.format("+id:%s", id)); From 6dca1d10bda08460829481d8b7a00f49e619765f Mon Sep 17 00:00:00 2001 From: tylerjmchugh Date: Wed, 23 Oct 2024 12:12:26 -0400 Subject: [PATCH 2/2] Delete resources only after commit --- .../datamanager/draft/DraftMetadataUtils.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/fao/geonet/kernel/datamanager/draft/DraftMetadataUtils.java b/core/src/main/java/org/fao/geonet/kernel/datamanager/draft/DraftMetadataUtils.java index 8d54c88492c..6b67295b12f 100644 --- a/core/src/main/java/org/fao/geonet/kernel/datamanager/draft/DraftMetadataUtils.java +++ b/core/src/main/java/org/fao/geonet/kernel/datamanager/draft/DraftMetadataUtils.java @@ -54,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; @@ -636,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); @@ -650,16 +653,29 @@ public void cancelEditingSession(ServiceContext context, String id) throws Excep metadataValidationRepository.deleteAllById_MetadataId(intId); metadataStatusRepository.deleteAllById_MetadataId(intId); - // Delete resources - Store store = context.getBean("resourceStore", Store.class); - store.delResources(context, intId); - // --- remove metadata xmlSerializer.delete(id, ServiceContext.get()); searchManager.delete(String.format("+id:%s", id)); // 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); }