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

Implement mass addition of bib information (Closes #372) #12025

Draft
wants to merge 46 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
887dd35
feat: enhance right-click context menu with new actions (extract refe…
KUMOAWAI Oct 19, 2024
656facf
fix: Restore MERGE_WITH_FETCHED_ENTRY and add BATCH_MERGE_WITH_FETCHE…
KUMOAWAI Oct 19, 2024
0409856
Refactor and optimize FetchAndMergeEntry class
KUMOAWAI Oct 19, 2024
489b512
Merge branch 'JabRef:main' into fix-for-issue-#372
KUMOAWAI Oct 19, 2024
dc138ce
Updated CHANGELOG.md
KUMOAWAI Oct 19, 2024
0532df4
Added a period at the end of the description in CHANGELOG.md.
KUMOAWAI Oct 19, 2024
012dd9a
Updated JabRef_en.properties
KUMOAWAI Oct 19, 2024
f8267ac
Move and rename "Mass Getting bibliographic data" action
KUMOAWAI Oct 20, 2024
74f2473
Refactor FetchAndMergeEntry class for improved functionality and read…
KUMOAWAI Oct 20, 2024
29759a6
Merge branch 'main' into fix-for-issue-#372
koppor Oct 20, 2024
37cfeae
Refactor entry fetching and merging process
KUMOAWAI Oct 20, 2024
77e0d51
Updated CHANGELOG.md
KUMOAWAI Oct 20, 2024
e6f0ad2
Merge branch 'main' into fix-for-issue-#372
KUMOAWAI Oct 20, 2024
c5b7b4c
refactor: Separate UI and core logic in entry merging system
KUMOAWAI Oct 25, 2024
5b829b1
Merge remote-tracking branch 'upstream/fix-for-issue-#372' into fix-f…
KUMOAWAI Oct 25, 2024
350502e
Merge branch 'main' into fix-for-issue-#372
KUMOAWAI Oct 25, 2024
fe46c0c
Refactored updateFieldIfNecessaryWithUndo method in MergeEntriesHelpe…
KUMOAWAI Oct 25, 2024
8eac6a9
Merge remote-tracking branch 'upstream/fix-for-issue-#372' into fix-f…
KUMOAWAI Oct 25, 2024
5d54083
refactor: Replace String.format with string concatenation in MergingI…
KUMOAWAI Oct 25, 2024
93072cf
refactor: Fixed incorrect placeholder in updateProgress Method
KUMOAWAI Oct 25, 2024
262d25d
Updated JabRef_en.properties to fix the failing LocalizationConsisten…
KUMOAWAI Oct 25, 2024
4667124
Merge branch 'main' into fix-for-issue-#372
KUMOAWAI Oct 26, 2024
ae4df14
refactor: optimize BibEntry merging and fetching logic:
KUMOAWAI Oct 26, 2024
5ed3d70
Roll Back: FetchAndMergeEntry.java and MergeWithFetchedEntryAction.java.
KUMOAWAI Oct 27, 2024
74a6285
Merge branch 'main' into fix-for-issue-#372
koppor Oct 27, 2024
8d9bdce
Refactor: improve MergingIdBasedFetcher, BatchEntryMergeWithFetchedD…
KUMOAWAI Oct 27, 2024
95e9ae8
Fixed the minor import format issue.
KUMOAWAI Oct 27, 2024
9fdbbb4
Merge remote-tracking branch 'upstream/fix-for-issue-#372' into fix-f…
KUMOAWAI Oct 27, 2024
ce1ea3e
Merge branch 'main' into fix-for-issue-#372
KUMOAWAI Oct 27, 2024
7305f3c
Apply suggestions from code review
KUMOAWAI Oct 27, 2024
9dce81a
Refactor: Reorder parameters to (fetcher, library) in merge operations
KUMOAWAI Oct 27, 2024
5f24815
Merge branch 'main' into fix-for-issue-#372
KUMOAWAI Oct 27, 2024
d17fd88
Merge branch 'main' into fix-for-issue-#372
KUMOAWAI Oct 30, 2024
61f5a8f
Refactor: Deleted the empty line before package
KUMOAWAI Oct 31, 2024
86051ac
Merge branch 'main' into fix-for-issue-#372
KUMOAWAI Oct 31, 2024
198f2d0
Merge branch 'main' into fix-for-issue-#372
KUMOAWAI Oct 31, 2024
ace1975
Merge branch 'main' into fix-for-issue-#372
KUMOAWAI Nov 13, 2024
74681e1
Refactor: Deleted the empty line before package
KUMOAWAI Nov 16, 2024
3197ef4
refactor: Extract background processing into BatchEntryMergeTask, opt…
KUMOAWAI Nov 16, 2024
c04a2fa
Updated JabRef_en.properties
KUMOAWAI Nov 16, 2024
d2566db
Merge branch 'main' into fix-for-issue-#372
KUMOAWAI Nov 17, 2024
4d1b88d
Apply suggestions from code review
KUMOAWAI Nov 17, 2024
e8645a2
Modify log messages correspondingly.
KUMOAWAI Nov 17, 2024
ac8c59d
Refactor: Move string normalization from StringNormalizer to StringUtil
KUMOAWAI Nov 17, 2024
5172bc2
Merge branch 'main' into fix-for-issue-#372
KUMOAWAI Nov 18, 2024
05b3c42
Merge branch 'main' into fix-for-issue-#372
KUMOAWAI Dec 5, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We added automatic browser extension install on Windows for Chrome and Edge. [#6076](https://github.com/JabRef/jabref/issues/6076)
- We added a search bar for filtering keyboard shortcuts. [#11686](https://github.com/JabRef/jabref/issues/11686)
- By double clicking on a local citation in the Citation Relations Tab you can now jump the linked entry. [#11955](https://github.com/JabRef/jabref/pull/11955)
- Added batch fetching of bibliographic data for multiple entries in the "Lookup" menu. [koppor#372](https://github.com/koppor/jabref/issues/372)
- We use the menu icon for background tasks as a progress indicator to visualise an import's progress when dragging and dropping several PDF files into the main table. [#12072](https://github.com/JabRef/jabref/pull/12072)

### Changed
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/jabref/gui/actions/StandardActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public enum StandardActions implements Action {
OPEN_URL(Localization.lang("Open URL or DOI"), IconTheme.JabRefIcons.WWW, KeyBinding.OPEN_URL_OR_DOI),
SEARCH_SHORTSCIENCE(Localization.lang("Search ShortScience")),
MERGE_WITH_FETCHED_ENTRY(Localization.lang("Get bibliographic data from %0", "DOI/ISBN/...")),
BATCH_MERGE_WITH_FETCHED_ENTRY(Localization.lang("Get bibliographic data from %0 (fully automated)", "DOI/ISBN/...")),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, what I written in the last review, but I think, it was about consistency?

Haven't I written MULTI_ be used as prefix?

image

ATTACH_FILE(Localization.lang("Attach file"), IconTheme.JabRefIcons.ATTACH_FILE),
ATTACH_FILE_FROM_URL(Localization.lang("Attach file from URL"), IconTheme.JabRefIcons.DOWNLOAD_FILE),
PRIORITY(Localization.lang("Priority"), IconTheme.JabRefIcons.PRIORITY),
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/org/jabref/gui/frame/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.jabref.gui.linkedfile.RedownloadMissingFilesAction;
import org.jabref.gui.maintable.NewLibraryFromPdfActionOffline;
import org.jabref.gui.maintable.NewLibraryFromPdfActionOnline;
import org.jabref.gui.mergeentries.BatchEntryMergeWithFetchedDataAction;
import org.jabref.gui.mergeentries.MergeEntriesAction;
import org.jabref.gui.plaincitationparser.PlainCitationParserAction;
import org.jabref.gui.preferences.GuiPreferences;
Expand Down Expand Up @@ -273,7 +274,20 @@ private void createMenu() {

new SeparatorMenuItem(),

factory.createMenuItem(StandardActions.FIND_UNLINKED_FILES, new FindUnlinkedFilesAction(dialogService, stateManager))
factory.createMenuItem(StandardActions.FIND_UNLINKED_FILES, new FindUnlinkedFilesAction(dialogService, stateManager)),

new SeparatorMenuItem(),

factory.createMenuItem(
StandardActions.BATCH_MERGE_WITH_FETCHED_ENTRY,
new BatchEntryMergeWithFetchedDataAction(
frame::getCurrentLibraryTab,
preferences,
dialogService,
stateManager,
taskExecutor
)
)
);

final MenuItem pushToApplicationMenuItem = factory.createMenuItem(pushToApplicationCommand.getAction(), pushToApplicationCommand);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package org.jabref.gui.mergeentries;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

import javafx.application.Platform;

import org.jabref.gui.LibraryTab;
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.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.logic.util.TaskExecutor;
import org.jabref.model.entry.BibEntry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Handles the merging of multiple bibliography entries with fetched data.
* This action performs background fetching and merging of entries while
* providing progress updates to the user.
*/
public class BatchEntryMergeWithFetchedDataAction extends SimpleCommand {

private static final Logger LOGGER = LoggerFactory.getLogger(BatchEntryMergeWithFetchedDataAction.class);

private final Supplier<LibraryTab> tabSupplier;
private final GuiPreferences preferences;
private final NotificationService notificationService;
private final TaskExecutor taskExecutor;

public BatchEntryMergeWithFetchedDataAction(Supplier<LibraryTab> tabSupplier,
GuiPreferences preferences,
NotificationService notificationService,
StateManager stateManager,
TaskExecutor taskExecutor) {
this.tabSupplier = tabSupplier;
this.preferences = preferences;
this.notificationService = notificationService;
this.taskExecutor = taskExecutor;
this.executable.bind(ActionHelper.needsDatabase(stateManager));
}

@Override
public void execute() {
LibraryTab libraryTab = tabSupplier.get();
if (libraryTab == null) {
LOGGER.error("Cannot perform batch merge: no library is open.");
return;
}

List<BibEntry> libraryEntries = libraryTab.getDatabase().getEntries();
if (libraryEntries.isEmpty()) {
notificationService.notify(Localization.lang("empty library"));
return;
}

MergeContext context = new MergeContext(
libraryTab,
libraryEntries,
new MergingIdBasedFetcher(preferences.getImportFormatPreferences()),
notificationService
);

taskExecutor.execute(createBackgroundTask(context));
}

private static BackgroundTask<List<String>> createBackgroundTask(MergeContext context) {
BackgroundTask<List<String>> task = new BackgroundTask<>() {
@Override
public List<String> call() {
return processMergeEntries(context, this);
}
};

task.setTitle(Localization.lang("Fetching and merging entries"));
task.showToUser(true);

task.onSuccess(updatedEntries ->
handleSuccess(updatedEntries, context));

task.onFailure(ex ->
handleFailure(ex, context.notificationService()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can chain these calls.

General comment - do not do now - read on -- You can also use wrap for a shorter syntax. See org.jabref.gui.preferences.preview.PreviewTabViewModel#setValues for an example.


return task;
}

private static List<String> processMergeEntries(MergeContext context, BackgroundTask<?> task) {
int totalEntries = context.entries().size();

for (int i = 0; i < totalEntries && !task.isCancelled(); i++) {
BibEntry libraryEntry = context.entries().get(i);
updateProgress(i, totalEntries, task);
fetchAndMergeEntry(context, libraryEntry);
}

finalizeCompoundEdit(context);
return context.updatedEntries();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter task is strange. Can you inline this method?

}

private static void fetchAndMergeEntry(MergeContext context, BibEntry libraryEntry) {
LOGGER.debug("Processing library entry: {}", libraryEntry);

context.fetcher().fetchEntry(libraryEntry)
.filter(MergingIdBasedFetcher.FetcherResult::hasChanges)
.ifPresent(result -> Platform.runLater(() -> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is runLater() needed? The whole thing runs in a background task - and there is no interaction with the UI, is it?

MergeEntriesHelper.mergeEntries(result.mergedEntry(), libraryEntry, context.compoundEdit());
libraryEntry.getCitationKey().ifPresent(context.updatedEntries()::add);
}));
}

private static void finalizeCompoundEdit(MergeContext context) {
if (!context.updatedEntries().isEmpty()) {
Platform.runLater(() -> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary?

context.compoundEdit().end();
context.libraryTab().getUndoManager().addEdit(context.compoundEdit());
});
}
}

private static void updateProgress(int currentIndex, int totalEntries, BackgroundTask<?> task) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static method are a strong indicator for a bad design.

Please, simply create a new class inherting from BackgroundTask. See org.jabref.gui.externalfiles.UnlinkedFilesCrawler for an example.

Platform.runLater(() -> {
task.updateMessage(Localization.lang("Processing entry %0 of %1", currentIndex + 1, totalEntries));
task.updateProgress(currentIndex, totalEntries);
});
}

private static void handleSuccess(List<String> updatedEntries, MergeContext context) {
Platform.runLater(() -> {
String message = updatedEntries.isEmpty()
? Localization.lang("No updates found.")
: Localization.lang("Batch update successful. %0 entries updated.",
updatedEntries.size());

LOGGER.debug("Batch update completed. {}", message);
context.notificationService().notify(message);
});
}

private static void handleFailure(Exception ex, NotificationService notificationService) {
LOGGER.error("Error during fetch and merge", ex);
Platform.runLater(() ->
notificationService.notify(
Localization.lang("Error while fetching and merging entries: %0", ex.getMessage())
)
);
}

private record MergeContext(
LibraryTab libraryTab,
List<BibEntry> entries,
MergingIdBasedFetcher fetcher,
NamedCompound compoundEdit,
List<String> updatedEntries,
NotificationService notificationService
) {
MergeContext(LibraryTab libraryTab, List<BibEntry> entries, MergingIdBasedFetcher fetcher, NotificationService notificationService) {
this(
libraryTab,
entries,
fetcher,
new NamedCompound(Localization.lang("Merge entries")),
Collections.synchronizedList(new ArrayList<>()),
notificationService
);
}
}
}
102 changes: 102 additions & 0 deletions src/main/java/org/jabref/gui/mergeentries/MergeEntriesHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package org.jabref.gui.mergeentries;

import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;

import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableChangeType;
import org.jabref.gui.undo.UndoableFieldChange;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldFactory;
import org.jabref.model.entry.types.EntryType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Helper class for merging bibliography entries with undo support.
* Source entry data is merged into the library entry, with longer field values preferred
* and obsolete fields removed.
*/
public final class MergeEntriesHelper {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no test for this. Please add a JUNit test (ctrl+shift+t in IntelliJ gets you started). -- Reading a unit test makes it easier to check whether the logic s correct.


private static final Logger LOGGER = LoggerFactory.getLogger(MergeEntriesHelper.class);

private MergeEntriesHelper() {
}

/**
* Merges two BibEntry objects with undo support.
*
* @param entryFromFetcher The entry containing new information (source, from the fetcher)
* @param entryFromLibrary The entry to be updated (target, from the library)
* @param undoManager Compound edit to collect undo information
*/
public static void mergeEntries(BibEntry entryFromFetcher, BibEntry entryFromLibrary, NamedCompound undoManager) {
LOGGER.debug("Entry from fetcher: {}", entryFromFetcher);
LOGGER.debug("Entry from library: {}", entryFromLibrary);

mergeEntryType(entryFromFetcher, entryFromLibrary, undoManager);
mergeFields(entryFromFetcher, entryFromLibrary, undoManager);
removeFieldsNotPresentInFetcher(entryFromFetcher, entryFromLibrary, undoManager);
}

private static void mergeEntryType(BibEntry entryFromFetcher, BibEntry entryFromLibrary, NamedCompound undoManager) {
EntryType fetcherType = entryFromFetcher.getType();
EntryType libraryType = entryFromLibrary.getType();

if (!libraryType.equals(fetcherType)) {
LOGGER.debug("Updating type {} -> {}", libraryType, fetcherType);
entryFromLibrary.setType(fetcherType);
undoManager.addEdit(new UndoableChangeType(entryFromLibrary, libraryType, fetcherType));
}
}

private static void mergeFields(BibEntry entryFromFetcher, BibEntry entryFromLibrary, NamedCompound undoManager) {
Set<Field> allFields = new LinkedHashSet<>();
allFields.addAll(entryFromFetcher.getFields());
allFields.addAll(entryFromLibrary.getFields());

for (Field field : allFields) {
Optional<String> fetcherValue = entryFromFetcher.getField(field);
Optional<String> libraryValue = entryFromLibrary.getField(field);

fetcherValue.ifPresent(newValue -> {
if (shouldUpdateField(newValue, libraryValue)) {
LOGGER.debug("Updating field {}: {} -> {}", field, libraryValue.orElse(null), newValue);
entryFromLibrary.setField(field, newValue);
undoManager.addEdit(new UndoableFieldChange(entryFromLibrary, field, libraryValue.orElse(null), newValue));
}
});
}
}

private static boolean shouldUpdateField(String fetcherValue, Optional<String> libraryValue) {
return libraryValue.map(value -> fetcherValue.length() > value.length())
.orElse(true);
}

/**
* Removes fields from the library entry that are not present in the fetcher entry.
* This ensures the merged entry only contains fields that are considered current
* according to the fetched data.
*/
private static void removeFieldsNotPresentInFetcher(BibEntry entryFromFetcher, BibEntry entryFromLibrary, NamedCompound undoManager) {
Set<Field> obsoleteFields = new LinkedHashSet<>(entryFromLibrary.getFields());
obsoleteFields.removeAll(entryFromFetcher.getFields());

for (Field field : obsoleteFields) {
if (FieldFactory.isInternalField(field)) {
continue;
}

entryFromLibrary.getField(field).ifPresent(value -> {
LOGGER.debug("Removing obsolete field {} with value {}", field, value);
entryFromLibrary.clearField(field);
undoManager.addEdit(new UndoableFieldChange(entryFromLibrary, field, value, null));
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No empty line at the beginning of a file (not sure why this file was touched by you)

package org.jabref.gui.mergeentries;

import javax.swing.undo.UndoManager;
Expand Down
Loading
Loading