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

Select the entry which has smaller dictonary order when merge #7708

Merged
merged 6 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -490,6 +490,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We added an option in preferences to allow for integers in field "edition" when running database in bibtex mode. [#4680](https://github.com/JabRef/jabref/issues/4680)
- We added the ability to use negation in export filter layouts. [#5138](https://github.com/JabRef/jabref/pull/5138)
- Focus on Name Area instead of 'OK' button whenever user presses 'Add subgroup'. [#6307](https://github.com/JabRef/jabref/issues/6307)
- We changed the behavior of merging that the entry which has "smaller" bibkey will be selected. [#7395](https://github.com/JabRef/jabref/issues/7395)

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableInsertEntries;
import org.jabref.gui.undo.UndoableRemoveEntries;
import org.jabref.logic.bibtex.comparator.EntryComparator;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.InternalField;

public class MergeEntriesAction extends SimpleCommand {

Expand Down Expand Up @@ -52,7 +54,19 @@ public void execute() {
BibEntry one = selectedEntries.get(0);
BibEntry two = selectedEntries.get(1);

MergeEntriesDialog dlg = new MergeEntriesDialog(one, two);
// compare two entries
BibEntry first;
BibEntry second;
EntryComparator entryComparator = new EntryComparator(false, false, InternalField.KEY_FIELD);
if (entryComparator.compare(one, two) <= 0) {
first = one;
second = two;
} else {
first = two;
second = one;
}

MergeEntriesDialog dlg = new MergeEntriesDialog(first, second);
dlg.setTitle(Localization.lang("Merge entries"));
Optional<BibEntry> mergedEntry = dialogService.showCustomDialogAndWait(dlg);
if (mergedEntry.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public int compare(BibEntry e1, BibEntry e2) {
// Sort by type.
f1 = e1.getType();
f2 = e2.getType();
} else if (sortField.equals(InternalField.KEY_FIELD)) {
f1 = e1.getCitationKey().orElse(null);
f2 = e2.getCitationKey().orElse(null);
} else if (sortField.isNumeric()) {
try {
Integer i1 = Integer.parseInt((String) f1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.logic.bibtex.comparator;

import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.InternalField;
import org.jabref.model.entry.field.StandardField;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -69,4 +70,35 @@ void bothEntriesNumericAscending() throws Exception {

assertEquals(-1, entryComparator.compare(entry1, entry2));
}

@Test
void compareObjectsByKeyAscending() {
BibEntry e1 = new BibEntry();
BibEntry e2 = new BibEntry();
e1.setCitationKey("Mayer2019b");
e2.setCitationKey("Mayer2019a");
assertEquals(1, new EntryComparator(false, false, InternalField.KEY_FIELD).compare(e1, e2));
assertEquals(-1, new EntryComparator(false, false, InternalField.KEY_FIELD).compare(e2, e1));
}

@Test
void compareObjectsByKeyWithNull() {
BibEntry e1 = new BibEntry();
BibEntry e2 = new BibEntry();
e1.setCitationKey("Mayer2019b");
assertEquals(-1, new EntryComparator(false, false, InternalField.KEY_FIELD).compare(e1, e2));
assertEquals(1, new EntryComparator(false, false, InternalField.KEY_FIELD).compare(e2, e1));
}

@Test
void compareObjectsByKeyWithBlank() {
BibEntry e1 = new BibEntry();
BibEntry e2 = new BibEntry();
e1.setCitationKey("Mayer2019b");
e2.setCitationKey(" ");
assertEquals(-1, new EntryComparator(false, false, InternalField.KEY_FIELD).compare(e1, e2));
assertEquals(1, new EntryComparator(false, false, InternalField.KEY_FIELD).compare(e2, e1));
}
}