forked from JabRef/jabref
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cites field to bib entries for citation relation (JabRef#10752)
* Add cites field to bib entries for citation relation Change list view order Fixes JabRef/jabref-issue-melting-pot#345 * add changelog fix l10n * remove * fix * add viewmodel and tests * fix checkstyle * fix * Minor updates ^^ --------- Co-authored-by: Oliver Kopp <[email protected]>
- Loading branch information
1 parent
e55e705
commit bae698a
Showing
17 changed files
with
437 additions
and
189 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
115 changes: 115 additions & 0 deletions
115
...n/java/org/jabref/gui/entryeditor/citationrelationtab/CitationsRelationsTabViewModel.java
This file contains 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,115 @@ | ||
package org.jabref.gui.entryeditor.citationrelationtab; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.swing.undo.UndoManager; | ||
|
||
import org.jabref.gui.DialogService; | ||
import org.jabref.gui.StateManager; | ||
import org.jabref.gui.entryeditor.citationrelationtab.semanticscholar.CitationFetcher; | ||
import org.jabref.gui.externalfiles.ImportHandler; | ||
import org.jabref.gui.util.TaskExecutor; | ||
import org.jabref.logic.citationkeypattern.CitationKeyGenerator; | ||
import org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences; | ||
import org.jabref.model.database.BibDatabaseContext; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.field.StandardField; | ||
import org.jabref.model.util.FileUpdateMonitor; | ||
import org.jabref.preferences.PreferencesService; | ||
|
||
public class CitationsRelationsTabViewModel { | ||
|
||
private final BibDatabaseContext databaseContext; | ||
private final PreferencesService preferencesService; | ||
private final UndoManager undoManager; | ||
private final StateManager stateManager; | ||
private final DialogService dialogService; | ||
private final FileUpdateMonitor fileUpdateMonitor; | ||
private final TaskExecutor taskExecutor; | ||
|
||
public CitationsRelationsTabViewModel(BibDatabaseContext databaseContext, PreferencesService preferencesService, UndoManager undoManager, StateManager stateManager, DialogService dialogService, FileUpdateMonitor fileUpdateMonitor, TaskExecutor taskExecutor) { | ||
this.databaseContext = databaseContext; | ||
this.preferencesService = preferencesService; | ||
this.undoManager = undoManager; | ||
this.stateManager = stateManager; | ||
this.dialogService = dialogService; | ||
this.fileUpdateMonitor = fileUpdateMonitor; | ||
this.taskExecutor = taskExecutor; | ||
} | ||
|
||
public void importEntries(List<CitationRelationItem> entriesToImport, CitationFetcher.SearchType searchType, BibEntry existingEntry) { | ||
List<BibEntry> entries = entriesToImport.stream().map(CitationRelationItem::entry).toList(); | ||
|
||
ImportHandler importHandler = new ImportHandler( | ||
databaseContext, | ||
preferencesService, | ||
fileUpdateMonitor, | ||
undoManager, | ||
stateManager, | ||
dialogService, | ||
taskExecutor); | ||
|
||
switch (searchType) { | ||
case CITES -> importCites(entries, existingEntry, importHandler); | ||
case CITED_BY -> importCitedBy(entries, existingEntry, importHandler); | ||
} | ||
} | ||
|
||
private void importCites(List<BibEntry> entries, BibEntry existingEntry, ImportHandler importHandler) { | ||
CitationKeyPatternPreferences citationKeyPatternPreferences = preferencesService.getCitationKeyPatternPreferences(); | ||
CitationKeyGenerator generator = new CitationKeyGenerator(databaseContext, citationKeyPatternPreferences); | ||
boolean generateNewKeyOnImport = preferencesService.getImporterPreferences().generateNewKeyOnImportProperty().get(); | ||
|
||
List<String> citeKeys = getExistingEntriesFromCiteField(existingEntry); | ||
citeKeys.removeIf(String::isEmpty); | ||
for (BibEntry entryToCite : entries) { | ||
if (generateNewKeyOnImport || entryToCite.getCitationKey().isEmpty()) { | ||
String key = generator.generateKey(entryToCite); | ||
entryToCite.setCitationKey(key); | ||
addToKeyToList(citeKeys, key); | ||
} else { | ||
addToKeyToList(citeKeys, entryToCite.getCitationKey().get()); | ||
} | ||
} | ||
existingEntry.setField(StandardField.CITES, toCommaSeparatedString(citeKeys)); | ||
importHandler.importEntries(entries); | ||
} | ||
|
||
private void importCitedBy(List<BibEntry> entries, BibEntry existingEntry, ImportHandler importHandler) { | ||
CitationKeyPatternPreferences citationKeyPatternPreferences = preferencesService.getCitationKeyPatternPreferences(); | ||
CitationKeyGenerator generator = new CitationKeyGenerator(databaseContext, citationKeyPatternPreferences); | ||
boolean generateNewKeyOnImport = preferencesService.getImporterPreferences().generateNewKeyOnImportProperty().get(); | ||
|
||
for (BibEntry entryThatCitesOurExistingEntry : entries) { | ||
List<String> existingCites = getExistingEntriesFromCiteField(entryThatCitesOurExistingEntry); | ||
existingCites.removeIf(String::isEmpty); | ||
String key; | ||
if (generateNewKeyOnImport || entryThatCitesOurExistingEntry.getCitationKey().isEmpty()) { | ||
key = generator.generateKey(entryThatCitesOurExistingEntry); | ||
entryThatCitesOurExistingEntry.setCitationKey(key); | ||
} else { | ||
key = existingEntry.getCitationKey().get(); | ||
} | ||
addToKeyToList(existingCites, key); | ||
entryThatCitesOurExistingEntry.setField(StandardField.CITES, toCommaSeparatedString(existingCites)); | ||
} | ||
|
||
importHandler.importEntries(entries); | ||
} | ||
|
||
private void addToKeyToList(List<String> list, String key) { | ||
if (!list.contains(key)) { | ||
list.add(key); | ||
} | ||
} | ||
|
||
private List<String> getExistingEntriesFromCiteField(BibEntry entry) { | ||
return Arrays.stream(entry.getField(StandardField.CITES).orElse("").split(",")).collect(Collectors.toList()); | ||
} | ||
|
||
private String toCommaSeparatedString(List<String> citeentries) { | ||
return String.join(",", citeentries); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...n/java/org/jabref/gui/entryeditor/citationrelationtab/semanticscholar/AuthorResponse.java
This file contains 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
3 changes: 3 additions & 0 deletions
3
...java/org/jabref/gui/entryeditor/citationrelationtab/semanticscholar/CitationDataItem.java
This file contains 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 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 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.