-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Implement mass addition of bib information #12025
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
Merged
Merged
Changes from 70 commits
Commits
Show all changes
75 commits
Select commit
Hold shift + click to select a range
887dd35
feat: enhance right-click context menu with new actions (extract refe…
BojiZhang 656facf
fix: Restore MERGE_WITH_FETCHED_ENTRY and add BATCH_MERGE_WITH_FETCHE…
BojiZhang 0409856
Refactor and optimize FetchAndMergeEntry class
BojiZhang 489b512
Merge branch 'JabRef:main' into fix-for-issue-#372
BojiZhang dc138ce
Updated CHANGELOG.md
BojiZhang 0532df4
Added a period at the end of the description in CHANGELOG.md.
BojiZhang 012dd9a
Updated JabRef_en.properties
BojiZhang f8267ac
Move and rename "Mass Getting bibliographic data" action
BojiZhang 74f2473
Refactor FetchAndMergeEntry class for improved functionality and read…
BojiZhang 29759a6
Merge branch 'main' into fix-for-issue-#372
koppor 37cfeae
Refactor entry fetching and merging process
BojiZhang 77e0d51
Updated CHANGELOG.md
BojiZhang e6f0ad2
Merge branch 'main' into fix-for-issue-#372
BojiZhang c5b7b4c
refactor: Separate UI and core logic in entry merging system
BojiZhang 5b829b1
Merge remote-tracking branch 'upstream/fix-for-issue-#372' into fix-f…
BojiZhang 350502e
Merge branch 'main' into fix-for-issue-#372
BojiZhang fe46c0c
Refactored updateFieldIfNecessaryWithUndo method in MergeEntriesHelpe…
BojiZhang 8eac6a9
Merge remote-tracking branch 'upstream/fix-for-issue-#372' into fix-f…
BojiZhang 5d54083
refactor: Replace String.format with string concatenation in MergingI…
BojiZhang 93072cf
refactor: Fixed incorrect placeholder in updateProgress Method
BojiZhang 262d25d
Updated JabRef_en.properties to fix the failing LocalizationConsisten…
BojiZhang 4667124
Merge branch 'main' into fix-for-issue-#372
BojiZhang ae4df14
refactor: optimize BibEntry merging and fetching logic:
BojiZhang 5ed3d70
Roll Back: FetchAndMergeEntry.java and MergeWithFetchedEntryAction.java.
BojiZhang 74a6285
Merge branch 'main' into fix-for-issue-#372
koppor 8d9bdce
Refactor: improve MergingIdBasedFetcher, BatchEntryMergeWithFetchedD…
BojiZhang 95e9ae8
Fixed the minor import format issue.
BojiZhang 9fdbbb4
Merge remote-tracking branch 'upstream/fix-for-issue-#372' into fix-f…
BojiZhang ce1ea3e
Merge branch 'main' into fix-for-issue-#372
BojiZhang 7305f3c
Apply suggestions from code review
BojiZhang 9dce81a
Refactor: Reorder parameters to (fetcher, library) in merge operations
BojiZhang 5f24815
Merge branch 'main' into fix-for-issue-#372
BojiZhang d17fd88
Merge branch 'main' into fix-for-issue-#372
BojiZhang 61f5a8f
Refactor: Deleted the empty line before package
BojiZhang 86051ac
Merge branch 'main' into fix-for-issue-#372
BojiZhang 198f2d0
Merge branch 'main' into fix-for-issue-#372
BojiZhang ace1975
Merge branch 'main' into fix-for-issue-#372
BojiZhang 74681e1
Refactor: Deleted the empty line before package
BojiZhang 3197ef4
refactor: Extract background processing into BatchEntryMergeTask, opt…
BojiZhang c04a2fa
Updated JabRef_en.properties
BojiZhang d2566db
Merge branch 'main' into fix-for-issue-#372
BojiZhang 4d1b88d
Apply suggestions from code review
BojiZhang e8645a2
Modify log messages correspondingly.
BojiZhang ac8c59d
Refactor: Move string normalization from StringNormalizer to StringUtil
BojiZhang 5172bc2
Merge branch 'main' into fix-for-issue-#372
BojiZhang 05b3c42
Merge branch 'main' into fix-for-issue-#372
BojiZhang 81043b7
Merge branch 'main' into fix-for-issue-#372
BojiZhang 1a7bbb7
Merge branch 'main' into fix-for-issue-#372
BojiZhang b345e6b
Merge branch 'main' into fix-for-issue-#372
BojiZhang 0ce2092
Modified the StringUtilClassIsSmall() test in StringUtilTest class.
BojiZhang 2b208de
Added the cancellation feature to batch updating
BojiZhang e018398
Attempted to fix a failed test by modifying CHANGELOG.md.
BojiZhang cd4a318
Attempted to fix a conflict by modifying CHANGELOG.md.
BojiZhang 96ff453
Merge branch 'main' into fix-for-issue-#372
BojiZhang c37cc82
Merge branch 'main' into fix-for-issue-#372
subhramit 7c47166
Merge branch 'main' into fix-for-issue-#372
BojiZhang 5ad79bb
Merge branch 'main' into fix-for-issue-#372
BojiZhang bcd1318
Attempted to fix an issue in RightClickMenu
BojiZhang ae9f742
Use named groups
subhramit 9d0d1c4
Fix codespaces indent
subhramit b919860
Merge remote-tracking branch 'origin/main' into fix-for-issue-#372
koppor b0da898
Fix logger import
koppor a0d2c8f
Remove unnecessary separator
koppor a70195d
Remove obsolete comment
koppor 8057a82
Reorder lookup menu
koppor da44233
Compilefix
koppor 7f0d9c6
Merge branch 'main' into fix-for-issue-#372
koppor 23b6172
add test case
calixtus 1a7cf26
Remove normalize method
calixtus 983091a
Refactor RemoveWhitespace to NonSpaceWhitespaceRemover
calixtus db3e91d
Fix CHANGELOG.md
calixtus e394341
Remove argument record
calixtus 4dd5004
Simplify
calixtus c223e24
Rewrite
calixtus 88ce43f
l10n
calixtus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 183 additions & 0 deletions
183
jabgui/src/main/java/org/jabref/gui/mergeentries/BatchEntryMergeTask.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package org.jabref.gui.mergeentries; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import javax.swing.undo.UndoManager; | ||
calixtus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import org.jabref.gui.undo.NamedCompound; | ||
import org.jabref.logic.importer.fetcher.MergingIdBasedFetcher; | ||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.logic.util.BackgroundTask; | ||
import org.jabref.logic.util.NotificationService; | ||
import org.jabref.model.entry.BibEntry; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* A background task that handles fetching and merging of bibliography entries. | ||
* This implementation provides improved concurrency handling, better Optional usage, | ||
* and more robust error handling. | ||
*/ | ||
public class BatchEntryMergeTask extends BackgroundTask<List<String>> { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(BatchEntryMergeTask.class); | ||
private final MergeContext context; | ||
private final NamedCompound compoundEdit; | ||
private final AtomicInteger processedEntries; | ||
private final AtomicInteger successfulUpdates; | ||
|
||
public BatchEntryMergeTask(MergeContext context) { | ||
this.context = context; | ||
this.compoundEdit = new NamedCompound(Localization.lang("Merge entries")); | ||
this.processedEntries = new AtomicInteger(0); | ||
this.successfulUpdates = new AtomicInteger(0); | ||
|
||
configureTask(); | ||
} | ||
|
||
private void configureTask() { | ||
setTitle(Localization.lang("Fetching and merging entry(s)")); | ||
calixtus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
withInitialMessage(Localization.lang("Starting merge operation...")); | ||
showToUser(true); | ||
} | ||
|
||
@Override | ||
public List<String> call() throws Exception { | ||
try { | ||
if (isCancelled()) { | ||
notifyCancellation(); | ||
return List.of(); | ||
} | ||
|
||
List<String> updatedEntries = processMergeEntries(); | ||
|
||
if (isCancelled()) { | ||
notifyCancellation(); | ||
finalizeOperation(updatedEntries); | ||
return updatedEntries; | ||
} | ||
|
||
finalizeOperation(updatedEntries); | ||
LOGGER.debug("Merge operation completed. Processed: {}, Successfully updated: {}", | ||
processedEntries.get(), successfulUpdates.get()); | ||
notifySuccess(successfulUpdates.get()); | ||
return updatedEntries; | ||
} catch (Exception e) { | ||
if (isCancelled()) { | ||
notifyCancellation(); | ||
return List.of(); | ||
} | ||
LOGGER.error("Critical error during merge operation", e); | ||
notifyError(e); | ||
throw e; | ||
} | ||
} | ||
|
||
private void notifyCancellation() { | ||
LOGGER.debug("Merge operation was cancelled. Processed: {}, Successfully updated: {}", | ||
processedEntries.get(), successfulUpdates.get()); | ||
context.notificationService().notify( | ||
Localization.lang("Merge operation cancelled after updating %0 entry(s)", successfulUpdates.get())); | ||
} | ||
|
||
private List<String> processMergeEntries() { | ||
List<String> updatedEntries = new ArrayList<>(); | ||
|
||
for (BibEntry entry : context.entries()) { | ||
Optional<String> result = processSingleEntryWithProgress(entry); | ||
result.ifPresent(updatedEntries::add); | ||
|
||
if (isCancelled()) { | ||
LOGGER.debug("Cancellation requested after processing entry {}", processedEntries.get()); | ||
break; | ||
} | ||
} | ||
|
||
return updatedEntries; | ||
} | ||
|
||
private Optional<String> processSingleEntryWithProgress(BibEntry entry) { | ||
updateProgress(processedEntries.incrementAndGet(), context.entries().size()); | ||
updateMessage(Localization.lang("Processing entry %0 of %1", | ||
processedEntries.get(), | ||
context.entries().size())); | ||
return processSingleEntry(entry); | ||
} | ||
|
||
private Optional<String> processSingleEntry(BibEntry entry) { | ||
try { | ||
LOGGER.debug("Processing entry: {}", entry); | ||
return context.fetcher().fetchEntry(entry) | ||
.filter(MergingIdBasedFetcher.FetcherResult::hasChanges) | ||
.flatMap(result -> { | ||
boolean changesApplied = applyMerge(entry, result); | ||
if (changesApplied) { | ||
successfulUpdates.incrementAndGet(); | ||
return entry.getCitationKey(); | ||
} | ||
return Optional.empty(); | ||
}); | ||
} catch (Exception e) { | ||
handleEntryProcessingError(entry, e); | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
private boolean applyMerge(BibEntry entry, MergingIdBasedFetcher.FetcherResult result) { | ||
synchronized (compoundEdit) { | ||
calixtus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try { | ||
return MergeEntriesHelper.mergeEntries(result.mergedEntry(), entry, compoundEdit); | ||
} catch (Exception e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One should not catch generic Exceptions |
||
LOGGER.error("Error during merge operation for entry: {}", entry, e); | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
private void handleEntryProcessingError(BibEntry entry, Exception e) { | ||
String citationKey = entry.getCitationKey().orElse("unknown"); | ||
String message = Localization.lang("Error processing entry", citationKey, e.getMessage()); | ||
LOGGER.error(message, e); | ||
context.notificationService().notify(message); | ||
} | ||
|
||
private void finalizeOperation(List<String> updatedEntries) { | ||
if (!updatedEntries.isEmpty()) { | ||
synchronized (compoundEdit) { | ||
compoundEdit.end(); | ||
context.undoManager.addEdit(compoundEdit); | ||
} | ||
} | ||
} | ||
|
||
private void notifySuccess(int updateCount) { | ||
String message = updateCount == 0 | ||
? Localization.lang("No updates found.") | ||
: Localization.lang("Batch update successful. %0 entry(s) updated.", updateCount); | ||
context.notificationService().notify(message); | ||
} | ||
|
||
private void notifyError(Exception e) { | ||
context.notificationService().notify( | ||
Localization.lang("Merge operation failed: %0", e.getMessage())); | ||
} | ||
|
||
/** | ||
* Record containing all the context needed for the merge operation. | ||
* Implements defensive copying to ensure immutability. | ||
*/ | ||
public record MergeContext( | ||
List<BibEntry> entries, | ||
MergingIdBasedFetcher fetcher, | ||
UndoManager undoManager, | ||
NotificationService notificationService | ||
) { | ||
calixtus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public MergeContext { | ||
entries = List.copyOf(entries); | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
jabgui/src/main/java/org/jabref/gui/mergeentries/BatchEntryMergeWithFetchedDataAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.jabref.gui.mergeentries; | ||
|
||
import java.util.List; | ||
|
||
import javax.swing.undo.UndoManager; | ||
calixtus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import org.jabref.gui.StateManager; | ||
import org.jabref.gui.actions.ActionHelper; | ||
import org.jabref.gui.actions.SimpleCommand; | ||
import org.jabref.gui.preferences.GuiPreferences; | ||
import org.jabref.logic.importer.fetcher.MergingIdBasedFetcher; | ||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.logic.util.NotificationService; | ||
import org.jabref.logic.util.TaskExecutor; | ||
import org.jabref.model.database.BibDatabaseContext; | ||
import org.jabref.model.entry.BibEntry; | ||
|
||
/** | ||
* Handles batch merging of bibliography entries with fetched data. | ||
*/ | ||
public class BatchEntryMergeWithFetchedDataAction extends SimpleCommand { | ||
|
||
private final StateManager stateManager; | ||
private final UndoManager undoManager; | ||
private final GuiPreferences preferences; | ||
private final NotificationService notificationService; | ||
private final TaskExecutor taskExecutor; | ||
|
||
public BatchEntryMergeWithFetchedDataAction(StateManager stateManager, | ||
UndoManager undoManager, | ||
GuiPreferences preferences, | ||
NotificationService notificationService, | ||
TaskExecutor taskExecutor) { | ||
this.stateManager = stateManager; | ||
this.undoManager = undoManager; | ||
this.preferences = preferences; | ||
this.notificationService = notificationService; | ||
this.taskExecutor = taskExecutor; | ||
|
||
this.executable.bind(ActionHelper.needsDatabase(stateManager)); | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
if (stateManager.getActiveDatabase().isEmpty()) { | ||
return; | ||
} | ||
|
||
List<BibEntry> entries = stateManager.getActiveDatabase() | ||
.map(BibDatabaseContext::getEntries) | ||
.orElse(List.of()); | ||
calixtus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (entries.isEmpty()) { | ||
notificationService.notify(Localization.lang("No entries available for merging")); | ||
calixtus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return; | ||
} | ||
|
||
MergingIdBasedFetcher fetcher = new MergingIdBasedFetcher(preferences.getImportFormatPreferences()); | ||
BatchEntryMergeTask mergeTask = new BatchEntryMergeTask( | ||
new BatchEntryMergeTask.MergeContext(entries, | ||
fetcher, | ||
undoManager, | ||
notificationService)); | ||
|
||
mergeTask.executeWith(taskExecutor); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.