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

Add check for existing crossrefs when importing entries into a library #11922

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -87,6 +87,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We fixed an issue where the Undo/Redo buttons were active even when all libraries are closed. [#11837](https://github.com/JabRef/jabref/issues/11837)
- We fixed an issue where recently opened files were not displayed in the main menu properly. [#9042](https://github.com/JabRef/jabref/issues/9042)
- We fixed an issue where the DOI lookup would show an error when a DOI was found for an entry. [#11850](https://github.com/JabRef/jabref/issues/11850)
- We fixed an issue where adding a crossreferenced entry would not correctly generate a citation key for the child entry [#11136](https://github.com/JabRef/jabref/issues/11136)

### Removed

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/jabref/gui/externalfiles/ImportHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,25 @@ private void addToGroups(List<BibEntry> entries, Collection<GroupTreeNode> group
}
}

public void checkCrossRefKeys(List<BibEntry> entries) {
for (BibEntry possibleParentEntry : entries) {
calixtus marked this conversation as resolved.
Show resolved Hide resolved
for (BibEntry entry : bibDatabaseContext.getDatabase().getEntries()) {
Optional<String> crossRef = entry.getField(StandardField.CROSSREF);
Optional<String> childYear = entry.getField(StandardField.YEAR);
Optional<String> parentKey = possibleParentEntry.getCitationKey();
if (crossRef.isPresent() && crossRef.equals(parentKey) && !childYear.isPresent()) {
Optional<String> parentYear = possibleParentEntry.getField(StandardField.YEAR);
parentYear.ifPresent(year -> {
String existingKey = entry.getCitationKey().orElse("");
String newKey = existingKey+year;
// retroactively set citation key to match parent's year
entry.setCitationKey(newKey);
});
}
}
}
}

/**
* Generate keys for given entries.
*
Expand All @@ -318,6 +337,7 @@ private void generateKeys(List<BibEntry> entries) {
bibDatabaseContext.getDatabase(),
preferences.getCitationKeyPatternPreferences());
entries.forEach(keyGenerator::generateAndSetKey);
checkCrossRefKeys(entries);
}

public List<BibEntry> handleBibTeXData(String entries) {
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/org/jabref/gui/externalfiles/ImportHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.jabref.logic.bibtex.FieldPreferences;
import org.jabref.logic.database.DuplicateCheck;
import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.importer.ImporterPreferences;
import org.jabref.logic.util.CurrentThreadTaskExecutor;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.database.BibDatabaseContext;
Expand Down Expand Up @@ -223,4 +224,28 @@ void handleDuplicatesKeepMergeTest() {
assertFalse(bibDatabase.getEntries().contains(duplicateEntry)); // Assert that the duplicate entry was removed from the database
assertEquals(mergedEntry, result); // Assert that the merged entry is returned
}

@Test
void testCheckCrossRefKeys() {
BibDatabase bibDatabase = bibDatabaseContext.getDatabase();

BibEntry childEntry = new BibEntry();
childEntry.setField(StandardField.CROSSREF, "ParentKey");
childEntry.setCitationKey("ChildKey");

bibDatabase.insertEntry(childEntry);

assertEquals("ChildKey", childEntry.getCitationKey().orElse(""));

BibEntry parentEntry = new BibEntry();
parentEntry.setCitationKey("ParentKey");
parentEntry.setField(StandardField.YEAR, "2021");

bibDatabase.insertEntry(parentEntry);

importHandler.checkCrossRefKeys(List.of(parentEntry));

// The child entry's citation key should now be updated with the parent's year
assertEquals("ChildKey2021", childEntry.getCitationKey().orElse(""));
}
}
Loading