-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add feature to merge .bib files into current bib #13320
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
Open
raquelgraos
wants to merge
11
commits into
JabRef:main
Choose a base branch
from
raquelgraos:fix-for-issue-12290
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d348089
Add feature to merge .bib files into current bib
raquelgraos e415652
fix: cleanup MergeBibFilesIntoCurrentBib
GuilhermeRibeiroPereira 984133d
fix: cleanup MergeBibFilesIntoCurrentBib
GuilhermeRibeiroPereira 4987209
fix: cleanup MergeBibFilesIntoCurrentBib
raquelgraos 81cd997
fix: removed Jimfs dependency for testing
GuilhermeRibeiroPereira e7736a4
Merge remote-tracking branch 'upstream/main' into fix-for-issue-12290
GuilhermeRibeiroPereira d01814c
Fix: refactored MergeBibFilesIntoCurrentBibAction and moved related p…
raquelgraos ac0b688
Merge remote-tracking branch 'upstream/main' into fix-for-issue-12290
raquelgraos e15d40d
fix: apply OpenRewrite autofixes and clean up duplicate tests
GuilhermeRibeiroPereira a358457
fix: clean up tests
GuilhermeRibeiroPereira e892ec3
Refactor:renamed path validity method & put placeholders in propertie…
raquelgraos 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
190 changes: 190 additions & 0 deletions
190
...in/java/org/jabref/gui/mergebibfilesintocurrentbib/MergeBibFilesIntoCurrentBibAction.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,190 @@ | ||
package org.jabref.gui.mergebibfilesintocurrentbib; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import javax.swing.undo.UndoManager; | ||
|
||
import org.jabref.gui.DialogService; | ||
import org.jabref.gui.StateManager; | ||
import org.jabref.gui.actions.SimpleCommand; | ||
import org.jabref.gui.mergeentries.MergeEntriesAction; | ||
import org.jabref.gui.preferences.GuiPreferences; | ||
import org.jabref.gui.undo.NamedCompound; | ||
import org.jabref.gui.undo.UndoableInsertEntries; | ||
import org.jabref.gui.util.DirectoryDialogConfiguration; | ||
import org.jabref.logic.database.DuplicateCheck; | ||
import org.jabref.logic.importer.OpenDatabase; | ||
import org.jabref.logic.importer.ParserResult; | ||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.model.database.BibDatabase; | ||
import org.jabref.model.database.BibDatabaseContext; | ||
import org.jabref.model.database.BibDatabaseMode; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.BibEntryTypesManager; | ||
import org.jabref.model.util.FileUpdateMonitor; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static org.jabref.gui.actions.ActionHelper.needsDatabase; | ||
|
||
public class MergeBibFilesIntoCurrentBibAction extends SimpleCommand { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(MergeBibFilesIntoCurrentBibAction.class); | ||
|
||
private final DialogService dialogService; | ||
private final GuiPreferences preferences; | ||
private final StateManager stateManager; | ||
private final UndoManager undoManager; | ||
private final FileUpdateMonitor fileUpdateMonitor; | ||
private final BibEntryTypesManager entryTypesManager; | ||
|
||
private boolean shouldMergeSameKeyEntries; | ||
private boolean shouldMergeDuplicateEntries; | ||
|
||
private final List<BibEntry> entriesToMerge = new ArrayList<>(); | ||
private final List<List<BibEntry>> duplicatePairsToMerge = new ArrayList<>(); | ||
private final List<List<BibEntry>> sameKeyPairsToMerge = new ArrayList<>(); | ||
|
||
public MergeBibFilesIntoCurrentBibAction(DialogService dialogService, | ||
GuiPreferences preferences, | ||
StateManager stateManager, | ||
UndoManager undoManager, | ||
FileUpdateMonitor fileUpdateMonitor, | ||
BibEntryTypesManager entryTypesManager) { | ||
this.dialogService = dialogService; | ||
this.preferences = preferences; | ||
this.stateManager = stateManager; | ||
this.undoManager = undoManager; | ||
this.fileUpdateMonitor = fileUpdateMonitor; | ||
this.entryTypesManager = entryTypesManager; | ||
|
||
this.executable.bind(needsDatabase(this.stateManager)); | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
Optional<Path> selectedDirectory = getDirectoryToMerge(); | ||
Optional<BibDatabaseContext> context = stateManager.getActiveDatabase(); | ||
|
||
MergeBibFilesIntoCurrentBibPreferences mergeBibFilesIntoCurrentBibPreferences = preferences.getMergeBibFilesIntoCurrentBibPreferences(); | ||
|
||
shouldMergeSameKeyEntries = mergeBibFilesIntoCurrentBibPreferences.shouldMergeSameKeyEntries(); | ||
shouldMergeDuplicateEntries = mergeBibFilesIntoCurrentBibPreferences.shouldMergeDuplicateEntries(); | ||
|
||
if (selectedDirectory.isPresent() && context.isPresent()) { | ||
mergeBibFilesIntoCurrentBib(selectedDirectory.get(), context.get()); | ||
} | ||
} | ||
|
||
private Optional<Path> getDirectoryToMerge() { | ||
DirectoryDialogConfiguration config = new DirectoryDialogConfiguration.Builder() | ||
.withInitialDirectory(preferences.getFilePreferences().getWorkingDirectory()) | ||
.build(); | ||
|
||
return dialogService.showDirectorySelectionDialog(config); | ||
} | ||
|
||
public void mergeBibFilesIntoCurrentBib(Path directory, BibDatabaseContext context) { | ||
BibDatabase database = context.getDatabase(); | ||
Optional<Path> databasePath = context.getDatabasePath(); | ||
DuplicateCheck duplicateCheck = new DuplicateCheck(entryTypesManager); | ||
|
||
entriesToMerge.clear(); | ||
sameKeyPairsToMerge.clear(); | ||
duplicatePairsToMerge.clear(); | ||
|
||
for (Path path : getAllBibFiles(directory, databasePath.orElseGet(() -> Path.of("")))) { | ||
ParserResult result; | ||
try { | ||
result = OpenDatabase.loadDatabase(path, preferences.getImportFormatPreferences(), fileUpdateMonitor); | ||
} catch (IOException e) { | ||
LOGGER.error("Could not load file '{}': {}", path, e.getMessage(), e); | ||
continue; | ||
} | ||
for (BibEntry toMergeEntry : result.getDatabase().getEntries()) { | ||
processEntry(toMergeEntry, database, duplicateCheck); | ||
} | ||
} | ||
|
||
database.insertEntries(entriesToMerge); | ||
performMerges(); | ||
|
||
NamedCompound compound = new NamedCompound(Localization.lang("Merge BibTeX files into current library")); | ||
compound.addEdit(new UndoableInsertEntries(database, entriesToMerge)); | ||
compound.end(); | ||
undoManager.addEdit(compound); | ||
} | ||
|
||
private void processEntry(BibEntry entry, BibDatabase database, DuplicateCheck duplicateCheck) { | ||
for (BibEntry existingEntry : database.getEntries()) { | ||
if (entry.equals(existingEntry)) { | ||
return; | ||
} else if (entry.getCitationKey().equals(existingEntry.getCitationKey())) { | ||
if (shouldMergeSameKeyEntries) { | ||
sameKeyPairsToMerge.add(List.of(entry, existingEntry)); | ||
} | ||
return; | ||
} else if (duplicateCheck.isDuplicate(entry, existingEntry, BibDatabaseMode.BIBTEX)) { | ||
if (shouldMergeDuplicateEntries) { | ||
duplicatePairsToMerge.add(List.of(entry, existingEntry)); | ||
} | ||
return; | ||
} | ||
} | ||
entriesToMerge.add(entry); | ||
} | ||
|
||
private void performMerges() { | ||
for (List<BibEntry> pair : sameKeyPairsToMerge) { | ||
mergeEntries(pair); | ||
} | ||
for (List<BibEntry> pair : duplicatePairsToMerge) { | ||
mergeEntries(pair); | ||
} | ||
} | ||
|
||
private void mergeEntries(List<BibEntry> entries) { | ||
stateManager.setSelectedEntries(entries); | ||
new MergeEntriesAction(dialogService, stateManager, undoManager, preferences).execute(); | ||
} | ||
|
||
private List<Path> getAllBibFiles(Path directory, Path databasePath) { | ||
if (!isValidPath(directory)) { | ||
return List.of(); | ||
} | ||
try (Stream<Path> stream = Files.find( | ||
directory, | ||
Integer.MAX_VALUE, | ||
(path, _) -> path.getFileName().toString().endsWith(".bib") && | ||
!path.equals(databasePath) | ||
)) { | ||
return stream.collect(Collectors.toList()); | ||
} catch (IOException e) { | ||
LOGGER.error("Error finding .bib files in '{}': {}", directory.getFileName(), e.getMessage(), e); | ||
} | ||
return List.of(); | ||
} | ||
|
||
private boolean isValidPath(Path directory) { | ||
if (!Files.exists(directory)) { | ||
dialogService.showErrorDialogAndWait(Localization.lang("Chosen folder does not exist:") + " " + directory); | ||
return false; | ||
} | ||
if (!Files.isDirectory(directory)) { | ||
dialogService.showErrorDialogAndWait(Localization.lang("Chosen path is not a folder:") + " " + directory); | ||
return false; | ||
} | ||
if (!Files.isReadable(directory)) { | ||
dialogService.showErrorDialogAndWait(Localization.lang("Chosen folder is not readable:") + " " + directory); | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...va/org/jabref/gui/mergebibfilesintocurrentbib/MergeBibFilesIntoCurrentBibPreferences.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,38 @@ | ||
package org.jabref.gui.mergebibfilesintocurrentbib; | ||
|
||
import javafx.beans.property.BooleanProperty; | ||
import javafx.beans.property.SimpleBooleanProperty; | ||
|
||
public class MergeBibFilesIntoCurrentBibPreferences { | ||
private final BooleanProperty shouldMergeSameKeyEntries = new SimpleBooleanProperty(); | ||
private final BooleanProperty shouldMergeDuplicateEntries = new SimpleBooleanProperty(); | ||
|
||
public MergeBibFilesIntoCurrentBibPreferences(boolean shouldMergeSameKeyEntries, boolean shouldMergeDuplicateEntries) { | ||
this.shouldMergeSameKeyEntries.set(shouldMergeSameKeyEntries); | ||
this.shouldMergeDuplicateEntries.set(shouldMergeDuplicateEntries); | ||
} | ||
|
||
public boolean shouldMergeSameKeyEntries() { | ||
return this.shouldMergeSameKeyEntries.get(); | ||
} | ||
|
||
public void setShouldMergeSameKeyEntries(boolean decision) { | ||
this.shouldMergeSameKeyEntries.set(decision); | ||
} | ||
|
||
public BooleanProperty shouldMergeSameKeyEntriesProperty() { | ||
return this.shouldMergeSameKeyEntries; | ||
} | ||
|
||
public boolean shouldMergeDuplicateEntries() { | ||
return this.shouldMergeDuplicateEntries.get(); | ||
} | ||
|
||
public void setShouldMergeDuplicateEntries(boolean decision) { | ||
this.shouldMergeDuplicateEntries.set(decision); | ||
} | ||
|
||
public BooleanProperty shouldMergeDuplicateEntriesProperty() { | ||
return this.shouldMergeDuplicateEntries; | ||
} | ||
} |
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
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.