-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Fix issue #11189 part 00 refactor citation relation tab logic #11845
base: main
Are you sure you want to change the base?
Changes from 4 commits
3785cb1
8048da8
18db75e
0e9de3c
9a31735
b1133d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,17 +36,20 @@ | |
import org.jabref.gui.collab.entrychange.PreviewWithSourceTab; | ||
import org.jabref.gui.desktop.os.NativeDesktop; | ||
import org.jabref.gui.entryeditor.EntryEditorTab; | ||
import org.jabref.gui.entryeditor.citationrelationtab.semanticscholar.CitationFetcher; | ||
import org.jabref.gui.entryeditor.citationrelationtab.semanticscholar.SemanticScholarFetcher; | ||
import org.jabref.gui.icon.IconTheme; | ||
import org.jabref.gui.preferences.GuiPreferences; | ||
import org.jabref.gui.util.NoSelectionModel; | ||
import org.jabref.gui.util.ViewModelListCellFactory; | ||
import org.jabref.logic.bibtex.BibEntryWriter; | ||
import org.jabref.logic.bibtex.FieldPreferences; | ||
import org.jabref.logic.bibtex.FieldWriter; | ||
import org.jabref.logic.citation.repository.LRUBibEntryRelationsCache; | ||
import org.jabref.logic.citation.repository.LRUBibEntryRelationsRepository; | ||
import org.jabref.logic.citation.service.SearchCitationsRelationsService; | ||
import org.jabref.logic.database.DuplicateCheck; | ||
import org.jabref.logic.exporter.BibWriter; | ||
import org.jabref.logic.importer.fetcher.CitationFetcher; | ||
import org.jabref.logic.importer.fetcher.SemanticScholarCitationFetcher; | ||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.logic.os.OS; | ||
import org.jabref.logic.util.BackgroundTask; | ||
|
@@ -85,7 +88,7 @@ public class CitationRelationsTab extends EntryEditorTab { | |
private final GuiPreferences preferences; | ||
private final LibraryTab libraryTab; | ||
private final TaskExecutor taskExecutor; | ||
private final BibEntryRelationsRepository bibEntryRelationsRepository; | ||
private final SearchCitationsRelationsService searchCitationsRelationsService; | ||
private final CitationsRelationsTabViewModel citationsRelationsTabViewModel; | ||
private final DuplicateCheck duplicateCheck; | ||
private final BibEntryTypesManager entryTypesManager; | ||
|
@@ -109,9 +112,22 @@ public CitationRelationsTab(DialogService dialogService, | |
|
||
this.entryTypesManager = bibEntryTypesManager; | ||
this.duplicateCheck = new DuplicateCheck(entryTypesManager); | ||
this.bibEntryRelationsRepository = new BibEntryRelationsRepository(new SemanticScholarFetcher(preferences.getImporterPreferences()), | ||
new BibEntryRelationsCache()); | ||
citationsRelationsTabViewModel = new CitationsRelationsTabViewModel(databaseContext, preferences, undoManager, stateManager, dialogService, fileUpdateMonitor, taskExecutor); | ||
var bibEntryRelationsRepository = new LRUBibEntryRelationsRepository( | ||
new LRUBibEntryRelationsCache() | ||
); | ||
this.searchCitationsRelationsService = new SearchCitationsRelationsService( | ||
new SemanticScholarCitationFetcher(preferences.getImporterPreferences()), | ||
bibEntryRelationsRepository | ||
); | ||
citationsRelationsTabViewModel = new CitationsRelationsTabViewModel( | ||
databaseContext, | ||
preferences, | ||
undoManager, | ||
stateManager, | ||
dialogService, | ||
fileUpdateMonitor, | ||
taskExecutor | ||
); | ||
} | ||
|
||
/** | ||
|
@@ -405,41 +421,54 @@ private void searchForRelations(BibEntry entry, CheckListView<CitationRelationIt | |
citedByTask.cancel(); | ||
} | ||
|
||
BackgroundTask<List<BibEntry>> task; | ||
|
||
if (searchType == CitationFetcher.SearchType.CITES) { | ||
task = BackgroundTask.wrap(() -> { | ||
if (shouldRefresh) { | ||
bibEntryRelationsRepository.forceRefreshReferences(entry); | ||
} | ||
return bibEntryRelationsRepository.getReferences(entry); | ||
}); | ||
citingTask = task; | ||
} else { | ||
task = BackgroundTask.wrap(() -> { | ||
if (shouldRefresh) { | ||
bibEntryRelationsRepository.forceRefreshCitations(entry); | ||
} | ||
return bibEntryRelationsRepository.getCitations(entry); | ||
}); | ||
citedByTask = task; | ||
} | ||
|
||
task.onRunning(() -> prepareToSearchForRelations(abortButton, refreshButton, importButton, progress, task)) | ||
.onSuccess(fetchedList -> onSearchForRelationsSucceed(entry, listView, abortButton, refreshButton, | ||
searchType, importButton, progress, fetchedList, observableList)) | ||
this.createBackGroundTask(entry, searchType, shouldRefresh) | ||
.consumeOnRunning(task -> prepareToSearchForRelations( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could probably be renamed |
||
abortButton, refreshButton, importButton, progress, task | ||
)) | ||
.onSuccess(fetchedList -> onSearchForRelationsSucceed( | ||
entry, | ||
listView, | ||
abortButton, | ||
refreshButton, | ||
searchType, | ||
importButton, | ||
progress, | ||
fetchedList, | ||
observableList | ||
)) | ||
.onFailure(exception -> { | ||
LOGGER.error("Error while fetching citing Articles", exception); | ||
hideNodes(abortButton, progress, importButton); | ||
listView.setPlaceholder(new Label(Localization.lang("Error while fetching citing entries: %0", | ||
exception.getMessage()))); | ||
|
||
listView.setPlaceholder( | ||
new Label(Localization.lang( | ||
"Error while fetching citing entries: %0", exception.getMessage()) | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do not reformat. Our tooling cannot deal with that - see https://devdocs.jabref.org/code-howtos/localization.html for some hints. |
||
); | ||
refreshButton.setVisible(true); | ||
dialogService.notify(exception.getMessage()); | ||
}) | ||
.executeWith(taskExecutor); | ||
} | ||
|
||
private BackgroundTask<List<BibEntry>> createBackGroundTask( | ||
BibEntry entry, CitationFetcher.SearchType searchType, boolean shouldRefresh | ||
) { | ||
return switch (searchType) { | ||
case CitationFetcher.SearchType.CITES -> { | ||
citingTask = BackgroundTask.wrap( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not really appreciate this solution. The method should return a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK for me. You can add a TODO comment if you want. |
||
() -> this.searchCitationsRelationsService.searchReferences(entry, shouldRefresh) | ||
); | ||
yield citingTask; | ||
} | ||
case CitationFetcher.SearchType.CITED_BY -> { | ||
citedByTask = BackgroundTask.wrap( | ||
() -> this.searchCitationsRelationsService.searchCitations(entry, shouldRefresh) | ||
); | ||
yield citedByTask; | ||
} | ||
}; | ||
} | ||
|
||
private void onSearchForRelationsSucceed(BibEntry entry, CheckListView<CitationRelationItem> listView, | ||
Button abortButton, Button refreshButton, | ||
CitationFetcher.SearchType searchType, Button importButton, | ||
|
@@ -456,7 +485,7 @@ private void onSearchForRelationsSucceed(BibEntry entry, CheckListView<CitationR | |
.map(localEntry -> new CitationRelationItem(entr, localEntry, true)) | ||
.orElseGet(() -> new CitationRelationItem(entr, false))) | ||
.toList() | ||
); | ||
); | ||
|
||
if (!observableList.isEmpty()) { | ||
listView.refresh(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.jabref.logic.citation.repository; | ||
|
||
import java.util.List; | ||
|
||
import org.jabref.model.entry.BibEntry; | ||
|
||
public interface BibEntryRelationsRepository { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one will be re-implemented in next PR. |
||
|
||
void insertCitations(BibEntry entry, List<BibEntry> citations); | ||
|
||
List<BibEntry> readCitations(BibEntry entry); | ||
|
||
boolean containsCitations(BibEntry entry); | ||
|
||
void insertReferences(BibEntry entry, List<BibEntry> citations); | ||
|
||
List<BibEntry> readReferences(BibEntry entry); | ||
|
||
boolean containsReferences(BibEntry entry); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.jabref.logic.citation.repository; | ||
|
||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.identifier.DOI; | ||
|
||
import org.eclipse.jgit.util.LRUMap; | ||
|
||
public class LRUBibEntryRelationsCache { | ||
private static final Integer MAX_CACHED_ENTRIES = 100; | ||
private static final Map<DOI, Set<BibEntry>> CITATIONS_MAP = new LRUMap<>(MAX_CACHED_ENTRIES, MAX_CACHED_ENTRIES); | ||
private static final Map<DOI, Set<BibEntry>> REFERENCES_MAP = new LRUMap<>(MAX_CACHED_ENTRIES, MAX_CACHED_ENTRIES); | ||
|
||
public List<BibEntry> getCitations(BibEntry entry) { | ||
return entry | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returns a copy now. |
||
.getDOI() | ||
.stream() | ||
.flatMap(doi -> CITATIONS_MAP.getOrDefault(doi, Set.of()).stream()) | ||
.toList(); | ||
} | ||
|
||
public List<BibEntry> getReferences(BibEntry entry) { | ||
return entry | ||
.getDOI() | ||
.stream() | ||
.flatMap(doi -> REFERENCES_MAP.getOrDefault(doi, Set.of()).stream()) | ||
.toList(); | ||
} | ||
|
||
public void cacheOrMergeCitations(BibEntry entry, List<BibEntry> citations) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method used to rewrite data, now it does merge inputs according the method name. |
||
entry.getDOI().ifPresent(doi -> { | ||
var cachedRelations = CITATIONS_MAP.getOrDefault(doi, new LinkedHashSet<>()); | ||
cachedRelations.addAll(citations); | ||
CITATIONS_MAP.put(doi, cachedRelations); | ||
}); | ||
} | ||
|
||
public void cacheOrMergeReferences(BibEntry entry, List<BibEntry> references) { | ||
entry.getDOI().ifPresent(doi -> { | ||
var cachedRelations = REFERENCES_MAP.getOrDefault(doi, new LinkedHashSet<>()); | ||
cachedRelations.addAll(references); | ||
REFERENCES_MAP.put(doi, cachedRelations); | ||
}); | ||
} | ||
|
||
public boolean citationsCached(BibEntry entry) { | ||
return entry.getDOI().map(CITATIONS_MAP::containsKey).orElse(false); | ||
} | ||
|
||
public boolean referencesCached(BibEntry entry) { | ||
return entry.getDOI().map(REFERENCES_MAP::containsKey).orElse(false); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Introduces a service layer that segregates the fetching and repository logic definitions.