Skip to content

Commit

Permalink
Select the entry which has smaller dictonary order when merge (#7708)
Browse files Browse the repository at this point in the history
  • Loading branch information
SuXiChangZhen authored May 10, 2021
1 parent 4282394 commit edae432
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,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));
}
}


0 comments on commit edae432

Please sign in to comment.