From df7a6577b95f5da5f93cd02520d02df02716efc2 Mon Sep 17 00:00:00 2001 From: Nitin Suresh Date: Thu, 1 Jun 2023 20:17:48 -0700 Subject: [PATCH 1/4] add ability to use doi in web search --- .../fetcher/WebSearchPaneViewModel.java | 33 ++++++++++++++++--- .../fetcher/WebSearchPaneViewModelTest.java | 18 ++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModel.java b/src/main/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModel.java index 6aa6e7177d5..e675dd17093 100644 --- a/src/main/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModel.java +++ b/src/main/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModel.java @@ -1,7 +1,9 @@ package org.jabref.gui.importer.fetcher; import java.util.Map; +import java.util.Optional; import java.util.SortedSet; +import java.util.concurrent.Callable; import javafx.beans.property.ListProperty; import javafx.beans.property.ObjectProperty; @@ -20,8 +22,11 @@ import org.jabref.logic.importer.ParserResult; import org.jabref.logic.importer.SearchBasedFetcher; import org.jabref.logic.importer.WebFetchers; +import org.jabref.logic.importer.fetcher.DoiFetcher; import org.jabref.logic.l10n.Localization; +import org.jabref.model.entry.identifier.DOI; import org.jabref.model.strings.StringUtil; +import org.jabref.model.util.OptionalUtil; import org.jabref.preferences.PreferencesService; import org.jabref.preferences.SidePanePreferences; @@ -43,6 +48,7 @@ public class WebSearchPaneViewModel { private final ListProperty fetchers = new SimpleListProperty<>(FXCollections.observableArrayList()); private final StringProperty query = new SimpleStringProperty(); private final DialogService dialogService; + private final PreferencesService preferencesService; private final StateManager stateManager; private final Validator searchQueryValidator; @@ -51,6 +57,7 @@ public class WebSearchPaneViewModel { public WebSearchPaneViewModel(PreferencesService preferencesService, DialogService dialogService, StateManager stateManager) { this.dialogService = dialogService; this.stateManager = stateManager; + this.preferencesService = preferencesService; SortedSet allFetchers = WebFetchers.getSearchBasedFetchers( preferencesService.getImportFormatPreferences(), @@ -77,6 +84,13 @@ public WebSearchPaneViewModel(PreferencesService preferencesService, DialogServi // in case user did not enter something, it is treated as valid (to avoid UI WTFs) return null; } + + // check if DOI is present + Optional doi = DOI.findInText(queryText); + if (doi.isPresent()) { + return null; + } + try { parser.parse(queryText, NO_EXPLICIT_FIELD); return null; @@ -130,16 +144,27 @@ public void search() { } SearchBasedFetcher activeFetcher = getSelectedFetcher(); + BackgroundTask task; + Callable parserResultCallable = () -> new ParserResult(activeFetcher.performSearch(query)); + String fetcherName = activeFetcher.getName(); + + Optional doi = DOI.findInText(query); + if (doi.isPresent()) { + DoiFetcher doiFetcher = new DoiFetcher(preferencesService.getImportFormatPreferences()); + parserResultCallable = () -> new ParserResult(OptionalUtil.toList(doiFetcher.performSearchById(doi.get().getDOI()))); + fetcherName = doiFetcher.getName(); + } + + String finalFetcherName = fetcherName; Globals.getTelemetryClient().ifPresent(client -> - client.trackEvent("search", Map.of("fetcher", activeFetcher.getName()), Map.of())); + client.trackEvent("search", Map.of("fetcher", finalFetcherName), Map.of())); - BackgroundTask task; - task = BackgroundTask.wrap(() -> new ParserResult(activeFetcher.performSearch(query))) + task = BackgroundTask.wrap(parserResultCallable) .withInitialMessage(Localization.lang("Processing %0", query)); task.onFailure(dialogService::showErrorDialogAndWait); ImportEntriesDialog dialog = new ImportEntriesDialog(stateManager.getActiveDatabase().get(), task); - dialog.setTitle(activeFetcher.getName()); + dialog.setTitle(fetcherName); dialogService.showCustomDialogAndWait(dialog); } diff --git a/src/test/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModelTest.java b/src/test/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModelTest.java index 6903ce89cae..a1f4a724e16 100644 --- a/src/test/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModelTest.java +++ b/src/test/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModelTest.java @@ -57,4 +57,22 @@ void notCorrectQueryValidationStatus() { viewModel.queryProperty().setValue("Miami AND Beach OR Houston AND Texas"); assertFalse(viewModel.queryValidationStatus().validProperty().not().getValue()); } + + @Test + void queryConsistingOfDOIIsValid() { + viewModel.queryProperty().setValue("10.1007/JHEP02(2023)082"); + assertTrue(viewModel.queryValidationStatus().validProperty().getValue()); + } + + @Test + void canExtractDOIFromQueryText() { + viewModel.queryProperty().setValue("this is the DOI: 10.1007/JHEP02(2023)082, other text"); + assertTrue(viewModel.queryValidationStatus().validProperty().getValue()); + } + + @Test + void queryConsistingOfInvalidDOIIsInvalid() { + viewModel.queryProperty().setValue("101.1007/JHEP02(2023)082"); + assertFalse(viewModel.queryValidationStatus().validProperty().getValue()); + } } From 1f807b7f3bd1aca087c7592e6e07b22573befabc Mon Sep 17 00:00:00 2001 From: Nitin Suresh Date: Thu, 1 Jun 2023 20:24:25 -0700 Subject: [PATCH 2/4] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f984267e2ba..70f461445e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve - We added the possibility to automatically fetch entries when an ISBN is pasted on the main table. [#9864](https://github.com/JabRef/jabref/issues/9864) - We added the option to disable the automatic linking of files in the entry editor [#5105](https://github.com/JabRef/jabref/issues/5105) - We added the link icon for ISBNs in linked identifiers column. [#9819](https://github.com/JabRef/jabref/issues/9819) +- We added the ability to search for a DOI directly from 'Web Search'. [#9674](https://github.com/JabRef/jabref/issues/9674) ### Changed From 79a71fdb40c156e8bea0c828997b52380fd6efb0 Mon Sep 17 00:00:00 2001 From: Nitin Suresh Date: Sun, 4 Jun 2023 12:36:04 -0700 Subject: [PATCH 3/4] update variable initialization --- .../jabref/gui/importer/fetcher/WebSearchPaneViewModel.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModel.java b/src/main/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModel.java index e675dd17093..601baa49d37 100644 --- a/src/main/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModel.java +++ b/src/main/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModel.java @@ -85,9 +85,9 @@ public WebSearchPaneViewModel(PreferencesService preferencesService, DialogServi return null; } - // check if DOI is present Optional doi = DOI.findInText(queryText); if (doi.isPresent()) { + // in case the query contains a DOI, it is treated as valid return null; } @@ -144,7 +144,6 @@ public void search() { } SearchBasedFetcher activeFetcher = getSelectedFetcher(); - BackgroundTask task; Callable parserResultCallable = () -> new ParserResult(activeFetcher.performSearch(query)); String fetcherName = activeFetcher.getName(); @@ -159,7 +158,7 @@ public void search() { Globals.getTelemetryClient().ifPresent(client -> client.trackEvent("search", Map.of("fetcher", finalFetcherName), Map.of())); - task = BackgroundTask.wrap(parserResultCallable) + BackgroundTask task = BackgroundTask.wrap(parserResultCallable) .withInitialMessage(Localization.lang("Processing %0", query)); task.onFailure(dialogService::showErrorDialogAndWait); From 30463618679fd4cf2ad09c344ede054e64fa7ece Mon Sep 17 00:00:00 2001 From: Nitin Suresh Date: Mon, 5 Jun 2023 06:31:09 -0700 Subject: [PATCH 4/4] update variable declaration for clarity --- .../jabref/gui/importer/fetcher/WebSearchPaneViewModel.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModel.java b/src/main/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModel.java index 601baa49d37..b038a406f5c 100644 --- a/src/main/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModel.java +++ b/src/main/java/org/jabref/gui/importer/fetcher/WebSearchPaneViewModel.java @@ -154,7 +154,7 @@ public void search() { fetcherName = doiFetcher.getName(); } - String finalFetcherName = fetcherName; + final String finalFetcherName = fetcherName; Globals.getTelemetryClient().ifPresent(client -> client.trackEvent("search", Map.of("fetcher", finalFetcherName), Map.of())); @@ -163,7 +163,7 @@ public void search() { task.onFailure(dialogService::showErrorDialogAndWait); ImportEntriesDialog dialog = new ImportEntriesDialog(stateManager.getActiveDatabase().get(), task); - dialog.setTitle(fetcherName); + dialog.setTitle(finalFetcherName); dialogService.showCustomDialogAndWait(dialog); }