Skip to content

Commit

Permalink
Partial implementation of opt-in/out dialogue
Browse files Browse the repository at this point in the history
  • Loading branch information
btut committed Aug 19, 2021
1 parent 3440b32 commit e5222ce
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.gui.bibtexextractor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.undo.UndoManager;
Expand Down Expand Up @@ -32,7 +33,7 @@ public class BibtexExtractorViewModel {

private final StringProperty inputTextProperty = new SimpleStringProperty("");
private final DialogService dialogService;
private final GrobidCitationFetcher currentCitationfetcher;
private final PreferencesService preferencesService;
private final TaskExecutor taskExecutor;
private final ImportHandler importHandler;

Expand All @@ -45,7 +46,7 @@ public BibtexExtractorViewModel(BibDatabaseContext bibdatabaseContext,
StateManager stateManager) {

this.dialogService = dialogService;
currentCitationfetcher = new GrobidCitationFetcher(preferencesService.getImportSettingsPreferences(), preferencesService.getImportFormatPreferences());
this.preferencesService = preferencesService;
this.taskExecutor = taskExecutor;
this.importHandler = new ImportHandler(
bibdatabaseContext,
Expand All @@ -61,7 +62,22 @@ public StringProperty inputTextProperty() {
}

public void startParsing() {
BackgroundTask.wrap(() -> currentCitationfetcher.performSearch(inputTextProperty.getValue()))
if (preferencesService.getImportSettingsPreferences().isGrobidEnabled()) {
parseUsingGrobid();
} else {
parseUsingBibtexExtractor();
}
}

private void parseUsingBibtexExtractor() {
BibEntry parsedEntry = new BibtexExtractor().extract(inputTextProperty.getValue());
importHandler.importEntries(List.of(parsedEntry));
trackNewEntry(parsedEntry, "ParseWithBibTeXExtractor");
}

private void parseUsingGrobid() {
GrobidCitationFetcher grobidCitationFetcher = new GrobidCitationFetcher(preferencesService.getImportSettingsPreferences(), preferencesService.getImportFormatPreferences());
BackgroundTask.wrap(() -> grobidCitationFetcher.performSearch(inputTextProperty.getValue()))
.onRunning(() -> dialogService.notify(Localization.lang("Your text is being parsed...")))
.onFailure((e) -> {
if (e instanceof FetcherException) {
Expand All @@ -76,14 +92,14 @@ public void startParsing() {
dialogService.notify(Localization.lang("%0 entries were parsed from your query.", String.valueOf(parsedEntries.size())));
importHandler.importEntries(parsedEntries);
for (BibEntry bibEntry : parsedEntries) {
trackNewEntry(bibEntry);
trackNewEntry(bibEntry, "ParseWithGrobid");
}
}).executeWith(taskExecutor);
}

private void trackNewEntry(BibEntry bibEntry) {
private void trackNewEntry(BibEntry bibEntry, String eventMessage) {
Map<String, String> properties = new HashMap<>();
properties.put("EntryType", bibEntry.typeProperty().getValue().getName());
Globals.getTelemetryClient().ifPresent(client -> client.trackEvent("ParseWithGrobid", properties, new HashMap<>()));
Globals.getTelemetryClient().ifPresent(client -> client.trackEvent(eventMessage, properties, new HashMap<>()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,18 @@ public ExtractBibtexDialog() {

buttonParse = (Button) getDialogPane().lookupButton(parseButtonType);
buttonParse.setTooltip(new Tooltip((Localization.lang("Starts the extraction and adds the resulting entries to the currently opened database"))));
buttonParse.setOnAction(event -> viewModel.startParsing());
buttonParse.setOnAction(event -> {
if (!preferencesService.getImportSettingsPreferences().isGrobidEnabled() && !preferencesService.getImportSettingsPreferences().isGrobidOptOut()) {
boolean confirmGrobidUsage = dialogService.showConfirmationDialogWithOptOutAndWait(
"Remote services",
"Allow sending PDF files and raw citation strings to a JabRef online service (Grobid) to determine Metadata",
"Use BibTeX parser instead",
this::optOutOfGrobid
);
preferencesService.storeImportSettingsPreferences(preferencesService.getImportSettingsPreferences().withGrobidEnabled(confirmGrobidUsage));

This comment has been minimized.

Copy link
@koppor

koppor Aug 19, 2021

Member

We should have an interface for storeImportSettingsPreferences. - We should never pass around preferencesService to have the number of dependencies low

}
viewModel.startParsing();
});
buttonParse.disableProperty().bind(viewModel.inputTextProperty().isEmpty());
}

Expand All @@ -56,4 +67,8 @@ private void initialize() {
this.viewModel = new BibtexExtractorViewModel(database, dialogService, preferencesService, fileUpdateMonitor, taskExecutor, undoManager, stateManager);
input.textProperty().bindBidirectional(viewModel.inputTextProperty());
}

private void optOutOfGrobid(boolean optOut) {
preferencesService.storeImportSettingsPreferences(preferencesService.getImportSettingsPreferences().withGrobidOptOut(optOut));
}
}
17 changes: 17 additions & 0 deletions src/main/java/org/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.jabref.logic.help.HelpFile;
import org.jabref.logic.importer.EntryBasedFetcher;
import org.jabref.logic.importer.WebFetchers;
import org.jabref.logic.importer.fileformat.PdfMergeMetadataImporter;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;
Expand Down Expand Up @@ -360,6 +361,22 @@ private void setupToolBar() {
fetcherMenuItem.setOnAction(event -> fetchAndMerge(fetcher));
fetcherMenu.getItems().add(fetcherMenuItem);
}
// Treat the PdfMergeMetadataImporter separately since if the use never accepted or disabled grobid, we ask to enable it
MenuItem pdfMergeMetadataImporterMenuItem = new MenuItem("PDFmergemetadata");
pdfMergeMetadataImporterMenuItem.setOnAction(event -> {
if (!preferencesService.getImportSettingsPreferences().isGrobidEnabled() && !preferencesService.getImportSettingsPreferences().isGrobidOptOut()) {
boolean confirmGrobidUsage = dialogService.showConfirmationDialogWithOptOutAndWait(
"Remote services",
"Allow sending PDF files and raw citation strings to a JabRef online service (Grobid) to determine Metadata",
"Use other importers instead",
(optOut) -> preferencesService.storeImportSettingsPreferences(preferencesService.getImportSettingsPreferences().withGrobidOptOut(optOut))
);
preferencesService.storeImportSettingsPreferences(preferencesService.getImportSettingsPreferences().withGrobidEnabled(confirmGrobidUsage));
}
PdfMergeMetadataImporter.EntryBasedFetcherWrapper pdfMergeMetadataImporter= new PdfMergeMetadataImporter.EntryBasedFetcherWrapper(preferencesService.getImportSettingsPreferences(), preferencesService.getImportFormatPreferences(), preferencesService.getFilePreferences(), databaseContext, preferencesService.getDefaultEncoding());
fetchAndMerge(pdfMergeMetadataImporter);
});
fetcherMenu.getItems().add(pdfMergeMetadataImporterMenuItem);
fetcherButton.setOnMouseClicked(event -> fetcherMenu.show(fetcherButton, Side.RIGHT, 0, 0));
}

Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/jabref/logic/importer/WebFetchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ public static SortedSet<EntryBasedFetcher> getEntryBasedFetchers(ImportSettingsP
set.add(new MathSciNet(importFormatPreferences));
set.add(new CrossRef());
set.add(new ZbMATH(importFormatPreferences));
set.add(new PdfMergeMetadataImporter.EntryBasedFetcherWrapper(importSettingsPreferences, importFormatPreferences, filePreferences, databaseContext, defaultEncoding));
return set;
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2365,3 +2365,5 @@ Found\ match\ in\ %0=Found match in %0
Grobid\ URL=Grobid URL
Remote\ services=Remote services
Allow\ sending\ PDF\ files\ and\ raw\ citation\ strings\ to\ a\ JabRef\ online\ service\ (Grobid)\ to\ determine\ Metadata=Allow sending PDF files and raw citation strings to a JabRef online service (Grobid) to determine Metadata
Use\ BibTeX\ parser\ instead=Use BibTeX parser instead
Use\ other\ importers instead=Use other importers instead

0 comments on commit e5222ce

Please sign in to comment.