|
3 | 3 | import java.util.HashMap;
|
4 | 4 | import java.util.Map;
|
5 | 5 |
|
| 6 | +import javax.swing.undo.UndoManager; |
| 7 | + |
6 | 8 | import javafx.beans.property.SimpleStringProperty;
|
7 | 9 | import javafx.beans.property.StringProperty;
|
8 | 10 |
|
9 | 11 | import org.jabref.Globals;
|
| 12 | +import org.jabref.gui.DialogService; |
| 13 | +import org.jabref.gui.StateManager; |
| 14 | +import org.jabref.gui.externalfiles.ImportHandler; |
| 15 | +import org.jabref.gui.externalfiletype.ExternalFileTypes; |
| 16 | +import org.jabref.gui.util.BackgroundTask; |
| 17 | +import org.jabref.gui.util.TaskExecutor; |
| 18 | +import org.jabref.logic.importer.fetcher.GrobidCitationFetcher; |
| 19 | +import org.jabref.logic.l10n.Localization; |
10 | 20 | import org.jabref.model.database.BibDatabaseContext;
|
11 | 21 | import org.jabref.model.entry.BibEntry;
|
12 |
| -import org.jabref.model.entry.types.EntryType; |
13 |
| -import org.jabref.model.entry.types.StandardEntryType; |
| 22 | +import org.jabref.model.util.FileUpdateMonitor; |
| 23 | +import org.jabref.preferences.JabRefPreferences; |
14 | 24 |
|
15 | 25 | public class BibtexExtractorViewModel {
|
16 | 26 |
|
17 | 27 | private final StringProperty inputTextProperty = new SimpleStringProperty("");
|
18 |
| - private final BibDatabaseContext bibdatabaseContext; |
19 |
| - |
20 |
| - public BibtexExtractorViewModel(BibDatabaseContext bibdatabaseContext) { |
21 |
| - this.bibdatabaseContext = bibdatabaseContext; |
| 28 | + private DialogService dialogService; |
| 29 | + private GrobidCitationFetcher currentCitationfetcher; |
| 30 | + private TaskExecutor taskExecutor; |
| 31 | + private ImportHandler importHandler; |
| 32 | + |
| 33 | + public BibtexExtractorViewModel(BibDatabaseContext bibdatabaseContext, DialogService dialogService, |
| 34 | + JabRefPreferences jabRefPreferences, FileUpdateMonitor fileUpdateMonitor, TaskExecutor taskExecutor, UndoManager undoManager, StateManager stateManager) { |
| 35 | + this.dialogService = dialogService; |
| 36 | + currentCitationfetcher = new GrobidCitationFetcher(jabRefPreferences.getImportFormatPreferences()); |
| 37 | + this.taskExecutor = taskExecutor; |
| 38 | + this.importHandler = new ImportHandler(dialogService, bibdatabaseContext, ExternalFileTypes.getInstance(), jabRefPreferences.getFilePreferences(), jabRefPreferences.getImportFormatPreferences(), jabRefPreferences.getUpdateFieldPreferences(), fileUpdateMonitor, undoManager, stateManager); |
22 | 39 | }
|
23 | 40 |
|
24 | 41 | public StringProperty inputTextProperty() {
|
25 | 42 | return this.inputTextProperty;
|
26 | 43 | }
|
27 | 44 |
|
28 |
| - public void startExtraction() { |
29 |
| - |
30 |
| - BibtexExtractor extractor = new BibtexExtractor(); |
31 |
| - BibEntry entity = extractor.extract(inputTextProperty.getValue()); |
32 |
| - this.bibdatabaseContext.getDatabase().insertEntry(entity); |
33 |
| - trackNewEntry(StandardEntryType.Article); |
| 45 | + public void startParsing() { |
| 46 | + BackgroundTask.wrap(() -> currentCitationfetcher.performSearch(inputTextProperty.getValue())) |
| 47 | + .onRunning(() -> dialogService.notify(Localization.lang("Your text is being parsed..."))) |
| 48 | + .onSuccess(parsedEntries -> { |
| 49 | + dialogService.notify(Localization.lang("%0 entries were parsed from your query.", String.valueOf(parsedEntries.size()))); |
| 50 | + importHandler.importEntries(parsedEntries); |
| 51 | + for (BibEntry bibEntry : parsedEntries) { |
| 52 | + trackNewEntry(bibEntry); |
| 53 | + } |
| 54 | + }).executeWith(taskExecutor); |
34 | 55 | }
|
35 | 56 |
|
36 |
| - private void trackNewEntry(EntryType type) { |
| 57 | + private void trackNewEntry(BibEntry bibEntry) { |
37 | 58 | Map<String, String> properties = new HashMap<>();
|
38 |
| - properties.put("EntryType", type.getName()); |
39 |
| - |
40 |
| - Globals.getTelemetryClient().ifPresent(client -> client.trackEvent("NewEntry", properties, new HashMap<>())); |
| 59 | + properties.put("EntryType", bibEntry.typeProperty().getValue().getName()); |
| 60 | + Globals.getTelemetryClient().ifPresent(client -> client.trackEvent("ParseWithGrobid", properties, new HashMap<>())); |
41 | 61 | }
|
42 | 62 | }
|
0 commit comments