Skip to content

Commit

Permalink
[WIP] Batch Insert entries (#5691)
Browse files Browse the repository at this point in the history
* Initial changes to remove entry functionality

* Fix logic error in KeyChangeListener

* Fix logic error in BasePanel

* Changes to tests

* Fix typo

* Move remove entry calls to batch version

* Un-propagate for loop in KeyChangeListener

* Finalize changes to SharedEntriesNotPresentEvent

* Fix bug

* Fix other compile errors

* Fix bug and update tests

* Fix tests

* Fix test omission

* Update l10n

* For loop to citationStyle

* Add comment for method not working

* Clarify var name

* Allow single entry for undo

* Replace compound edit undo with normal undo in BasePanel

* Typo

* Simplify loop in DBMSSynchronizer

* Use if instead of stream

* Pluralize Javadoc

* EntryEvent -> EntriesEvent in Javadoc, comments, and var names

* Make imports explicit in BasePanel

* Batch delete to SQL

* Final EntriesRemovedEvent fixes

* Fix checkStyleMain

* More checkStyle fixes

* Initial changes to batch insertEntries calls

* More fixes

* More batching insertEntry

* Move List coercion into DuplicateSearchResult method

* Even more batching insertEntry

* DefaultAuxParser batch insertEntry

* MrDLibImporter batch insertEntry

* Add comment about usage of BasePanel.insertEntry

* Fix typo

* Fix pesky BibDatabaseTest error with setStrings

* Fixed BibDatabase Javadoc

* Finish master merge, start changing DBMS tests

* Add comment

* Final fixes including checkStyle

* Add DBMSProcessor tests

* Fix checkStyleTest

* Finished batching calls to insertEntries

* Minor change for consistency

* Update comments

* Update comment

* Another update comment

* EntryAddedEvent -> EntriesAddedEvnet and removed AllInsertsFinishedEvent

* Fixed some tests, aux parser still failing

* AuxParserTest passes now

* Fix checkstyle

* Make corrections

* Typo

* More streaming

* Add UndoableInsertEntries constructor

* Fix bug

* Get rid of "Integrity Check Failed" info

* Properly close databases in test to avoid database listener errors

* Fix database tests by adding entries after firing EntryAddedEvent

* Remove redundant DBMS connection closure from DBMSSychronizerTest

* Merge test resources from master

* Remove firstEntry hack

* Remove duplicate code from paste() by and pluralize insertEntries() in BasePanel

* Comment fix

* Clarify BasePanel insertEntries JavaDoc

* Rename vars

* Rename vars

* Remove unnecessary TODO and rename EntriesRemovedListener

* Merge fixes

* Remove old comment

* checkStyle fixes
  • Loading branch information
abepolk authored and tobiasdiez committed Dec 30, 2019
1 parent 7a53b3a commit 371a863
Show file tree
Hide file tree
Showing 32 changed files with 321 additions and 312 deletions.
59 changes: 34 additions & 25 deletions src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
import org.jabref.gui.undo.CountingUndoManager;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableFieldChange;
import org.jabref.gui.undo.UndoableInsertEntry;
import org.jabref.gui.undo.UndoableInsertEntries;
import org.jabref.gui.undo.UndoableRemoveEntries;
import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.gui.worker.SendAsEMailAction;
Expand All @@ -80,8 +80,8 @@
import org.jabref.model.database.KeyCollisionException;
import org.jabref.model.database.event.BibDatabaseContextChangedEvent;
import org.jabref.model.database.event.CoarseChangeFilter;
import org.jabref.model.database.event.EntriesAddedEvent;
import org.jabref.model.database.event.EntriesRemovedEvent;
import org.jabref.model.database.event.EntryAddedEvent;
import org.jabref.model.database.shared.DatabaseLocation;
import org.jabref.model.database.shared.DatabaseSynchronizer;
import org.jabref.model.entry.BibEntry;
Expand Down Expand Up @@ -168,7 +168,7 @@ public BasePanel(JabRefFrame frame, BasePanelPreferences preferences, BibDatabas
setupActions();

this.getDatabase().registerListener(new SearchListener());
this.getDatabase().registerListener(new EntryRemovedListener());
this.getDatabase().registerListener(new EntriesRemovedListener());

// ensure that at each addition of a new entry, the entry is added to the groups interface
this.bibDatabaseContext.getDatabase().registerListener(new GroupTreeListener());
Expand Down Expand Up @@ -605,29 +605,39 @@ public void registerUndoableChanges(List<FieldChange> changes) {
}
}

public void insertEntry(final BibEntry bibEntry) {
if (bibEntry != null) {
insertEntries(Collections.singletonList(bibEntry));
}
}

/**
* This method is called from JabRefFrame when the user wants to create a new entry.
* This method is called from JabRefFrame when the user wants to create a new entry or entries.
* It is necessary when the user would expect the added entry or one of the added entries
* to be selected in the entry editor
*
* @param bibEntry The new entry.
* @param entries The new entries.
*/
public void insertEntry(final BibEntry bibEntry) {
if (bibEntry != null) {

public void insertEntries(final List<BibEntry> entries) {
if (!entries.isEmpty()) {
try {
bibDatabaseContext.getDatabase().insertEntry(bibEntry);
bibDatabaseContext.getDatabase().insertEntries(entries);

// Set owner and timestamp
UpdateField.setAutomaticFields(bibEntry, true, true, Globals.prefs.getUpdateFieldPreferences());

// Create an UndoableInsertEntry object.
getUndoManager().addEdit(new UndoableInsertEntry(bibDatabaseContext.getDatabase(), bibEntry));
for (BibEntry entry : entries) {
UpdateField.setAutomaticFields(entry, true, true, Globals.prefs.getUpdateFieldPreferences());
}
// Create an UndoableInsertEntries object.
getUndoManager().addEdit(new UndoableInsertEntries(bibDatabaseContext.getDatabase(), entries));

markBaseChanged(); // The database just changed.
if (Globals.prefs.getBoolean(JabRefPreferences.AUTO_OPEN_FORM)) {
showAndEdit(bibEntry);
showAndEdit(entries.get(0));
}
clearAndSelect(bibEntry);
clearAndSelect(entries.get(0));
} catch (KeyCollisionException ex) {
LOGGER.info("Collision for bibtex key" + bibEntry.getId(), ex);
LOGGER.info("Collision for bibtex key" + ex.getId(), ex);
}
}
}
Expand Down Expand Up @@ -1111,22 +1121,21 @@ public void searchAndOpen() {
private class GroupTreeListener {

@Subscribe
public void listen(EntryAddedEvent addedEntryEvent) {
// if the added entry is an undo don't add it to the current group
if (addedEntryEvent.getEntriesEventSource() == EntriesEventSource.UNDO) {
public void listen(EntriesAddedEvent addedEntriesEvent) {
// if the event is an undo, don't add it to the current group
if (addedEntriesEvent.getEntriesEventSource() == EntriesEventSource.UNDO) {
return;
}

// Automatically add new entry to the selected group (or set of groups)
// Automatically add new entries to the selected group (or set of groups)
if (Globals.prefs.getBoolean(JabRefPreferences.AUTO_ASSIGN_GROUP)) {
final List<BibEntry> entries = Collections.singletonList(addedEntryEvent.getBibEntry());
Globals.stateManager.getSelectedGroup(bibDatabaseContext).forEach(
selectedGroup -> selectedGroup.addEntriesToGroup(entries));
selectedGroup -> selectedGroup.addEntriesToGroup(addedEntriesEvent.getBibEntries()));
}
}
}

private class EntryRemovedListener {
private class EntriesRemovedListener {

@Subscribe
public void listen(EntriesRemovedEvent entriesRemovedEvent) {
Expand All @@ -1141,8 +1150,8 @@ public void listen(EntriesRemovedEvent entriesRemovedEvent) {
private class SearchAutoCompleteListener {

@Subscribe
public void listen(EntryAddedEvent addedEntryEvent) {
DefaultTaskExecutor.runInJavaFXThread(() -> searchAutoCompleter.indexEntry(addedEntryEvent.getBibEntry()));
public void listen(EntriesAddedEvent addedEntriesEvent) {
DefaultTaskExecutor.runInJavaFXThread(() -> addedEntriesEvent.getBibEntries().forEach(entry -> searchAutoCompleter.indexEntry(entry)));
}

@Subscribe
Expand All @@ -1158,7 +1167,7 @@ public void listen(EntryChangedEvent entryChangedEvent) {
private class SearchListener {

@Subscribe
public void listen(EntryAddedEvent addedEntryEvent) {
public void listen(EntriesAddedEvent addedEntryEvent) {
DefaultTaskExecutor.runInJavaFXThread(() -> frame.getGlobalSearchBar().performSearch());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.jabref.gui.autocompleter;

import org.jabref.model.database.event.EntryAddedEvent;
import java.util.List;

import org.jabref.model.database.event.EntriesAddedEvent;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.event.EntryChangedEvent;

import com.google.common.eventbus.Subscribe;
Expand All @@ -17,8 +20,11 @@ public AutoCompleteUpdater(SuggestionProviders suggestionProviders) {
}

@Subscribe
public void listen(EntryAddedEvent addedEntryEvent) {
suggestionProviders.indexEntry(addedEntryEvent.getBibEntry());
public void listen(EntriesAddedEvent entryAddedEvent) {
List<BibEntry> entries = entryAddedEvent.getBibEntries();
for (BibEntry entry : entries) {
suggestionProviders.indexEntry(entry);
}
}

@Subscribe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.jabref.JabRefGUI;
import org.jabref.gui.preview.PreviewViewer;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableInsertEntry;
import org.jabref.gui.undo.UndoableInsertEntries;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
Expand All @@ -26,7 +26,7 @@ public EntryAddChangeViewModel(BibEntry entry) {
@Override
public void makeChange(BibDatabaseContext database, NamedCompound undoEdit) {
database.getDatabase().insertEntry(entry);
undoEdit.addEdit(new UndoableInsertEntry(database.getDatabase(), entry));
undoEdit.addEdit(new UndoableInsertEntries(database.getDatabase(), entry));
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/gui/collab/EntryChangeViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.jabref.gui.mergeentries.MergeEntries;
import org.jabref.gui.mergeentries.MergeEntries.DefaultRadioButtonSelectionMode;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableInsertEntry;
import org.jabref.gui.undo.UndoableInsertEntries;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
Expand Down Expand Up @@ -43,8 +43,8 @@ public EntryChangeViewModel(BibEntry entry, BibEntry newEntry, BibDatabaseContex
public void makeChange(BibDatabaseContext database, NamedCompound undoEdit) {
database.getDatabase().removeEntry(oldEntry);
database.getDatabase().insertEntry(mergePanel.getMergeEntry());
undoEdit.addEdit(new UndoableInsertEntry(database.getDatabase(), oldEntry));
undoEdit.addEdit(new UndoableInsertEntry(database.getDatabase(), mergePanel.getMergeEntry()));
undoEdit.addEdit(new UndoableInsertEntries(database.getDatabase(), oldEntry));
undoEdit.addEdit(new UndoableInsertEntries(database.getDatabase(), mergePanel.getMergeEntry()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.jabref.gui.duplicationFinder.DuplicateResolverDialog.DuplicateResolverResult;
import org.jabref.gui.duplicationFinder.DuplicateResolverDialog.DuplicateResolverType;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableInsertEntry;
import org.jabref.gui.undo.UndoableInsertEntries;
import org.jabref.gui.undo.UndoableRemoveEntries;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.DefaultTaskExecutor;
Expand Down Expand Up @@ -167,10 +167,8 @@ private void handleDuplicates(DuplicateSearchResult result) {
}
// and adding merged entries:
if (!result.getToAdd().isEmpty()) {
for (BibEntry entry : result.getToAdd()) {
panel.getDatabase().insertEntry(entry);
compoundEdit.addEdit(new UndoableInsertEntry(panel.getDatabase(), entry));
}
compoundEdit.addEdit(new UndoableInsertEntries(panel.getDatabase(), result.getToAdd()));
panel.getDatabase().insertEntries(result.getToAdd());
panel.markBaseChanged();
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/gui/externalfiles/ImportHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.jabref.gui.DialogService;
import org.jabref.gui.StateManager;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.gui.undo.UndoableInsertEntry;
import org.jabref.gui.undo.UndoableInsertEntries;
import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator;
import org.jabref.logic.externalfiles.ExternalFilesContentImporter;
import org.jabref.logic.importer.ImportFormatPreferences;
Expand Down Expand Up @@ -94,7 +94,7 @@ public void importAsNewEntries(List<Path> files) {
}

importEntries(entriesToAdd);
entriesToAdd.forEach(entry -> ce.addEdit(new UndoableInsertEntry(database.getDatabase(), entry)));
ce.addEdit(new UndoableInsertEntries(database.getDatabase(), entriesToAdd));
}
ce.end();
undoManager.addEdit(ce);
Expand All @@ -109,7 +109,7 @@ private BibEntry createEmptyEntryWithLink(Path file) {

public void importEntries(List<BibEntry> entries) {
//TODO: Add undo/redo
//ce.addEdit(new UndoableInsertEntry(panel.getDatabase(), entry));
//undoManager.addEdit(new UndoableInsertEntries(panel.getDatabase(), entries));

database.getDatabase().insertEntries(entries);

Expand Down
11 changes: 3 additions & 8 deletions src/main/java/org/jabref/gui/importer/ImportAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -135,9 +134,7 @@ private ParserResult mergeImportResults(List<ImportFormatReader.UnknownFormatImp
directParserResult = pr;
}
// Merge entries:
for (BibEntry entry : pr.getDatabase().getEntries()) {
database.insertEntry(entry);
}
database.insertEntries(pr.getDatabase().getEntries());

// Merge strings:
for (BibtexString bs : pr.getDatabase().getStringValues()) {
Expand All @@ -151,16 +148,14 @@ private ParserResult mergeImportResults(List<ImportFormatReader.UnknownFormatImp
} else {

ParserResult pr = importResult.parserResult;
Collection<BibEntry> entries = pr.getDatabase().getEntries();
List<BibEntry> entries = pr.getDatabase().getEntries();

anythingUseful = anythingUseful | !entries.isEmpty();

// set timestamp and owner
UpdateField.setAutomaticFields(entries, Globals.prefs.getUpdateFieldPreferences()); // set timestamp and owner

for (BibEntry entry : entries) {
database.insertEntry(entry);
}
database.insertEntries(entries);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.jabref.gui.actions.BaseAction;
import org.jabref.gui.importer.AppendDatabaseDialog;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableInsertEntry;
import org.jabref.gui.undo.UndoableInsertEntries;
import org.jabref.gui.undo.UndoableInsertString;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.FileDialogConfiguration;
Expand Down Expand Up @@ -59,8 +59,7 @@ private static void mergeFromBibtex(BasePanel panel, ParserResult parserResult,
boolean importStrings, boolean importGroups, boolean importSelectorWords) throws KeyCollisionException {

BibDatabase fromDatabase = parserResult.getDatabase();
List<BibEntry> appendedEntries = new ArrayList<>();
List<BibEntry> originalEntries = new ArrayList<>();
List<BibEntry> entriesToAppend = new ArrayList<>();
BibDatabase database = panel.getDatabase();

NamedCompound ce = new NamedCompound(Localization.lang("Append library"));
Expand All @@ -74,11 +73,10 @@ private static void mergeFromBibtex(BasePanel panel, ParserResult parserResult,
BibEntry entry = (BibEntry) originalEntry.clone();
UpdateField.setAutomaticFields(entry, overwriteOwner, overwriteTimeStamp,
Globals.prefs.getUpdateFieldPreferences());
database.insertEntry(entry);
appendedEntries.add(entry);
originalEntries.add(originalEntry);
ce.addEdit(new UndoableInsertEntry(database, entry));
entriesToAppend.add(entry);
}
database.insertEntries(entriesToAppend);
ce.addEdit(new UndoableInsertEntries(database, entriesToAppend));
}

if (importStrings) {
Expand All @@ -99,7 +97,7 @@ private static void mergeFromBibtex(BasePanel panel, ParserResult parserResult,
ExplicitGroup group = new ExplicitGroup("Imported", GroupHierarchyType.INDEPENDENT,
Globals.prefs.getKeywordDelimiter());
newGroups.setGroup(group);
group.add(appendedEntries);
group.add(entriesToAppend);
} catch (IllegalArgumentException e) {
LOGGER.error("Problem appending entries to group", e);
}
Expand Down
32 changes: 4 additions & 28 deletions src/main/java/org/jabref/gui/maintable/MainTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,14 @@
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.gui.keyboard.KeyBinding;
import org.jabref.gui.keyboard.KeyBindingRepository;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableInsertEntry;
import org.jabref.gui.util.ControlHelper;
import org.jabref.gui.util.CustomLocalDragboard;
import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.gui.util.ViewModelTableRowFactory;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.UpdateField;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.event.AllInsertsFinishedEvent;
import org.jabref.model.database.event.EntriesAddedEvent;
import org.jabref.model.entry.BibEntry;
import org.jabref.preferences.JabRefPreferences;

import com.google.common.eventbus.Subscribe;
import org.slf4j.Logger;
Expand Down Expand Up @@ -130,8 +126,8 @@ public MainTable(MainTableDataModel model, JabRefFrame frame,
}

@Subscribe
public void listen(AllInsertsFinishedEvent event) {
DefaultTaskExecutor.runInJavaFXThread(() -> clearAndSelect(event.getBibEntry()));
public void listen(EntriesAddedEvent event) {
DefaultTaskExecutor.runInJavaFXThread(() -> clearAndSelect(event.getFirstEntry()));
}

public void clearAndSelect(BibEntry bibEntry) {
Expand Down Expand Up @@ -215,28 +211,8 @@ private void clearAndSelectLast() {
public void paste() {
// Find entries in clipboard
List<BibEntry> entriesToAdd = Globals.clipboardManager.extractData();

panel.insertEntries(entriesToAdd);
if (!entriesToAdd.isEmpty()) {
// Add new entries
NamedCompound ce = new NamedCompound((entriesToAdd.size() > 1 ? Localization.lang("paste entries") : Localization.lang("paste entry")));
for (BibEntry entryToAdd : entriesToAdd) {
UpdateField.setAutomaticFields(entryToAdd, Globals.prefs.getUpdateFieldPreferences());

database.getDatabase().insertEntry(entryToAdd);

ce.addEdit(new UndoableInsertEntry(database.getDatabase(), entryToAdd));
}
ce.end();
undoManager.addEdit(ce);

// Show editor if user want us to do this
BibEntry firstNewEntry = entriesToAdd.get(0);
if (Globals.prefs.getBoolean(JabRefPreferences.AUTO_OPEN_FORM)) {
panel.showAndEdit(firstNewEntry);
}

// Select and focus first new entry
clearAndSelect(firstNewEntry);
this.requestFocus();
}
}
Expand Down
Loading

0 comments on commit 371a863

Please sign in to comment.