|
| 1 | +package org.jabref.gui; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.file.Path; |
| 5 | +import java.util.Optional; |
| 6 | + |
| 7 | +import org.jabref.gui.actions.SimpleCommand; |
| 8 | +import org.jabref.gui.importer.actions.OpenDatabaseAction; |
| 9 | +import org.jabref.gui.util.BackgroundTask; |
| 10 | +import org.jabref.gui.util.FileDialogConfiguration; |
| 11 | +import org.jabref.gui.util.TaskExecutor; |
| 12 | +import org.jabref.logic.crawler.Crawler; |
| 13 | +import org.jabref.logic.importer.ParseException; |
| 14 | +import org.jabref.logic.l10n.Localization; |
| 15 | +import org.jabref.model.entry.BibEntryTypesManager; |
| 16 | +import org.jabref.model.util.FileUpdateMonitor; |
| 17 | +import org.jabref.preferences.JabRefPreferences; |
| 18 | + |
| 19 | +import org.eclipse.jgit.api.errors.GitAPIException; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | + |
| 23 | +public class StartLiteratureReviewAction extends SimpleCommand { |
| 24 | + private static final Logger LOGGER = LoggerFactory.getLogger(StartLiteratureReviewAction.class); |
| 25 | + private final JabRefFrame frame; |
| 26 | + private final DialogService dialogService; |
| 27 | + private final FileUpdateMonitor fileUpdateMonitor; |
| 28 | + private final Path workingDirectory; |
| 29 | + private final TaskExecutor taskExecutor; |
| 30 | + |
| 31 | + public StartLiteratureReviewAction(JabRefFrame frame, FileUpdateMonitor fileUpdateMonitor, Path standardWorkingDirectory, TaskExecutor taskExecutor) { |
| 32 | + this.frame = frame; |
| 33 | + this.dialogService = frame.getDialogService(); |
| 34 | + this.fileUpdateMonitor = fileUpdateMonitor; |
| 35 | + this.workingDirectory = getInitialDirectory(standardWorkingDirectory); |
| 36 | + this.taskExecutor = taskExecutor; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void execute() { |
| 41 | + FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() |
| 42 | + .withInitialDirectory(workingDirectory) |
| 43 | + .build(); |
| 44 | + |
| 45 | + Optional<Path> studyDefinitionFile = dialogService.showFileOpenDialog(fileDialogConfiguration); |
| 46 | + if (studyDefinitionFile.isEmpty()) { |
| 47 | + // Do nothing if selection was canceled |
| 48 | + return; |
| 49 | + } |
| 50 | + final Crawler crawler; |
| 51 | + try { |
| 52 | + crawler = new Crawler(studyDefinitionFile.get(), fileUpdateMonitor, JabRefPreferences.getInstance().getImportFormatPreferences(), JabRefPreferences.getInstance().getSavePreferences(), new BibEntryTypesManager()); |
| 53 | + } catch (IOException | ParseException | GitAPIException e) { |
| 54 | + LOGGER.error("Error during reading of study definition file.", e); |
| 55 | + dialogService.showErrorDialogAndWait(Localization.lang("Error during reading of study definition file."), e); |
| 56 | + return; |
| 57 | + } |
| 58 | + BackgroundTask.wrap(() -> { |
| 59 | + crawler.performCrawl(); |
| 60 | + return 0; // Return any value to make this a callable instead of a runnable. This allows throwing exceptions. |
| 61 | + }) |
| 62 | + .onFailure(e -> { |
| 63 | + LOGGER.error("Error during persistence of crawling results."); |
| 64 | + dialogService.showErrorDialogAndWait(Localization.lang("Error during persistence of crawling results."), e); |
| 65 | + }) |
| 66 | + .onSuccess(unused -> new OpenDatabaseAction(frame).openFile(Path.of(studyDefinitionFile.get().getParent().toString(), "studyResult.bib"), true)) |
| 67 | + .executeWith(taskExecutor); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @return Path of current panel database directory or the standard working directory |
| 72 | + */ |
| 73 | + private Path getInitialDirectory(Path standardWorkingDirectory) { |
| 74 | + if (frame.getBasePanelCount() == 0) { |
| 75 | + return standardWorkingDirectory; |
| 76 | + } else { |
| 77 | + Optional<Path> databasePath = frame.getCurrentLibraryTab().getBibDatabaseContext().getDatabasePath(); |
| 78 | + return databasePath.map(Path::getParent).orElse(standardWorkingDirectory); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments