Skip to content

Commit 2b208de

Browse files
committed
Added the cancellation feature to batch updating
- Added early cancellation check in the call() method. - Switched from stream-based to for-loop processing for improved cancellation handling. - Introduced notifyCancellation() to log and notify users upon task cancellation. - Ensured processed entries are finalized and recorded with undo even when cancelled.
1 parent 0ce2092 commit 2b208de

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/main/java/org/jabref/gui/mergeentries/BatchEntryMergeTask.java

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package org.jabref.gui.mergeentries;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45
import java.util.Optional;
56
import java.util.concurrent.atomic.AtomicInteger;
6-
import java.util.stream.Collectors;
77

88
import javax.swing.undo.UndoManager;
99

@@ -48,26 +48,56 @@ private void configureTask() {
4848
@Override
4949
public List<String> call() throws Exception {
5050
try {
51+
if (isCancelled()) {
52+
notifyCancellation();
53+
return List.of();
54+
}
55+
5156
List<String> updatedEntries = processMergeEntries();
57+
58+
if (isCancelled()) {
59+
notifyCancellation();
60+
finalizeOperation(updatedEntries);
61+
return updatedEntries;
62+
}
63+
5264
finalizeOperation(updatedEntries);
5365
LOGGER.debug("Merge operation completed. Processed: {}, Successfully updated: {}",
5466
processedEntries.get(), successfulUpdates.get());
5567
notifySuccess(successfulUpdates.get());
5668
return updatedEntries;
5769
} catch (Exception e) {
70+
if (isCancelled()) {
71+
notifyCancellation();
72+
return List.of();
73+
}
5874
LOGGER.error("Critical error during merge operation", e);
5975
notifyError(e);
6076
throw e;
6177
}
6278
}
6379

80+
private void notifyCancellation() {
81+
LOGGER.debug("Merge operation was cancelled. Processed: {}, Successfully updated: {}",
82+
processedEntries.get(), successfulUpdates.get());
83+
context.notificationService().notify(
84+
Localization.lang("Merge operation cancelled after updating %0 entry(s)", successfulUpdates.get()));
85+
}
86+
6487
private List<String> processMergeEntries() {
65-
return context.entries().stream()
66-
.takeWhile(_ -> !isCancelled())
67-
.map(this::processSingleEntryWithProgress)
68-
.filter(Optional::isPresent)
69-
.map(Optional::get)
70-
.collect(Collectors.toList());
88+
List<String> updatedEntries = new ArrayList<>();
89+
90+
for (BibEntry entry : context.entries()) {
91+
Optional<String> result = processSingleEntryWithProgress(entry);
92+
result.ifPresent(updatedEntries::add);
93+
94+
if (isCancelled()) {
95+
LOGGER.debug("Cancellation requested after processing entry {}", processedEntries.get());
96+
break;
97+
}
98+
}
99+
100+
return updatedEntries;
71101
}
72102

73103
private Optional<String> processSingleEntryWithProgress(BibEntry entry) {

src/main/resources/l10n/JabRef_en.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,7 @@ Entry\ from\ %0=Entry from %0
17211721
Merge\ entry\ with\ %0\ information=Merge entry with %0 information
17221722
Get\ bibliographic\ data\ from\ %0\ (fully\ automated)=Get bibliographic data from %0 (fully automated)
17231723
Batch\ update\ successful.\ %0\ entry(s)\ updated.=Batch update successful. %0 entry(s) updated.
1724+
Merge\ operation\ cancelled\ after\ updating\ %0\ entry(s)=Merge operation cancelled after updating %0 entry(s)
17241725
Error\ processing\ entry=Error processing entry
17251726
Merge\ operation\ failed\:\ %0=Merge operation failed: %0
17261727
No\ entries\ available\ for\ merging=No entries available for merging

0 commit comments

Comments
 (0)