Skip to content

Commit

Permalink
Add ability to use DOI directly in Web Search (#9969)
Browse files Browse the repository at this point in the history
* add ability to use doi in web search

* add changelog entry

* update variable initialization

* update variable declaration for clarity
  • Loading branch information
aqurilla authored Jun 5, 2023
1 parent 277ec2d commit 0c0db90
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We added the link icon for ISBNs in linked identifiers column. [#9819](https://github.com/JabRef/jabref/issues/9819)
- We added drag and drop events for field 'Groups' in entry editor panel. [#569](https://github.com/koppor/jabref/issues/569)
- We added support for parsing MathML in the Medline importer. [#4273](https://github.com/JabRef/jabref/issues/4273)
- We added the ability to search for a DOI directly from 'Web Search'. [#9674](https://github.com/JabRef/jabref/issues/9674)

### Changed

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -43,6 +48,7 @@ public class WebSearchPaneViewModel {
private final ListProperty<SearchBasedFetcher> 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;
Expand All @@ -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<SearchBasedFetcher> allFetchers = WebFetchers.getSearchBasedFetchers(
preferencesService.getImportFormatPreferences(),
Expand All @@ -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;
}

Optional<DOI> doi = DOI.findInText(queryText);
if (doi.isPresent()) {
// in case the query contains a DOI, it is treated as valid
return null;
}

try {
parser.parse(queryText, NO_EXPLICIT_FIELD);
return null;
Expand Down Expand Up @@ -130,16 +144,26 @@ public void search() {
}

SearchBasedFetcher activeFetcher = getSelectedFetcher();
Callable<ParserResult> parserResultCallable = () -> new ParserResult(activeFetcher.performSearch(query));
String fetcherName = activeFetcher.getName();

Optional<DOI> 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();
}

final 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<ParserResult> task;
task = BackgroundTask.wrap(() -> new ParserResult(activeFetcher.performSearch(query)))
BackgroundTask<ParserResult> 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(finalFetcherName);
dialogService.showCustomDialogAndWait(dialog);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 0c0db90

Please sign in to comment.