Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lookup fulltext document not finding files #5216

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 52 additions & 37 deletions src/main/java/org/jabref/gui/externalfiles/FindFullTextAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -16,6 +17,7 @@
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.gui.fieldeditors.LinkedFileViewModel;
import org.jabref.gui.fieldeditors.LinkedFilesEditorViewModel;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.logic.importer.FulltextFetchers;
import org.jabref.logic.l10n.Localization;
Expand All @@ -38,6 +40,7 @@ public class FindFullTextAction extends SimpleCommand {

private final BasePanel basePanel;
private final DialogService dialogService;
private final FulltextFetchers fetchers = new FulltextFetchers(Globals.prefs.getImportFormatPreferences());

public FindFullTextAction(BasePanel basePanel) {
this.basePanel = basePanel;
Expand All @@ -55,12 +58,11 @@ public void execute() {
if (basePanel.getSelectedEntries().size() >= WARNING_LIMIT) {
boolean confirmDownload = dialogService.showConfirmationDialogAndWait(
Localization.lang("Look up full text documents"),
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved
Localization.lang(
"You are about to look up full text documents for %0 entries.",
String.valueOf(basePanel.getSelectedEntries().size())) + "\n"
+ Localization.lang("JabRef will send at least one request per entry to a publisher.")
+ "\n"
+ Localization.lang("Do you still want to continue?"),
Localization.lang("You are about to look up full text documents for %0 entries.",
String.valueOf(basePanel.getSelectedEntries().size())) + "\n"
+ Localization.lang("JabRef will send at least one request per entry to a publisher.")
+ "\n"
+ Localization.lang("Do you still want to continue?"),
Localization.lang("Look up full text documents"),
Localization.lang("Cancel"));

Expand All @@ -70,31 +72,43 @@ public void execute() {
}
}

Task<Map<BibEntry, Optional<URL>>> findFullTextsTask = new Task<Map<BibEntry, Optional<URL>>>() {
lookupFullText();
}

private void lookupFullText() {
AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(basePanel.getBibDatabaseContext(), Globals.prefs.getFilePreferences(), Globals.prefs.getAutoLinkPreferences(), ExternalFileTypes.getInstance());

Task<List<BibEntry>> linkFilesTask = new Task<List<BibEntry>>() {

@Override
protected Map<BibEntry, Optional<URL>> call() {
protected List<BibEntry> call() {
return util.linkAssociatedFiles(basePanel.getSelectedEntries(), new NamedCompound(""));
}

@Override
protected void succeeded() {
Map<BibEntry, Optional<URL>> downloads = new ConcurrentHashMap<>();
int count = 0;
for (BibEntry entry : basePanel.getSelectedEntries()) {
FulltextFetchers fetchers = new FulltextFetchers(Globals.prefs.getImportFormatPreferences());
downloads.put(entry, fetchers.findFullTextPDF(entry));
updateProgress(++count, basePanel.getSelectedEntries().size());

for (BibEntry entry : getValue()) {
if (entry.getFiles().isEmpty()) {
downloads.put(entry, fetchers.findFullTextPDF(entry));
} else {
dialogService.notify(Localization.lang("Full text document found already on disk for entry %0. Not downloaded again.",
entry.getCiteKeyOptional().orElse(Localization.lang("undefined"))));
}
}
return downloads;
downloadMissingFullTexts(downloads);
}
};

findFullTextsTask.setOnSucceeded(value -> downloadFullTexts(findFullTextsTask.getValue()));

dialogService.showProgressDialogAndWait(
Localization.lang("Look up full text documents"),
Localization.lang("Looking for full text document..."),
findFullTextsTask);
dialogService.showProgressDialogAndWait(Localization.lang("Look up full text documents"),
Localization.lang("Looking for full text document..."),
linkFilesTask);
Globals.TASK_EXECUTOR.execute(linkFilesTask);

Globals.TASK_EXECUTOR.execute(findFullTextsTask);
}

private void downloadFullTexts(Map<BibEntry, Optional<URL>> downloads) {
private void downloadMissingFullTexts(Map<BibEntry, Optional<URL>> downloads) {
for (Map.Entry<BibEntry, Optional<URL>> download : downloads.entrySet()) {
BibEntry entry = download.getKey();
Optional<URL> result = download.getValue();
Expand All @@ -104,16 +118,17 @@ private void downloadFullTexts(Map<BibEntry, Optional<URL>> downloads) {
if (!dir.isPresent()) {

dialogService.showErrorDialogAndWait(Localization.lang("Directory not found"),
Localization.lang("Main file directory not set!") + " " + Localization.lang("Preferences")
+ " -> " + Localization.lang("File"));
Localization.lang("Main file directory not set!") + " "
+ Localization.lang("Preferences")
+ " -> " + Localization.lang("File"));
return;
}
//Download and link full text
addLinkedFileFromURL(result.get(), entry, dir.get());

} else {
dialogService.notify(Localization.lang("No full text document found for entry %0.",
entry.getCiteKeyOptional().orElse(Localization.lang("undefined"))));
entry.getCiteKeyOptional().orElse(Localization.lang("undefined"))));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change the indent.

}
}
}
Expand All @@ -132,33 +147,33 @@ private void addLinkedFileFromURL(URL url, BibEntry entry, Path targetDirectory)
if (!entry.getFiles().contains(newLinkedFile)) {

LinkedFileViewModel onlineFile = new LinkedFileViewModel(
newLinkedFile,
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved
entry,
basePanel.getBibDatabaseContext(),
Globals.TASK_EXECUTOR,
dialogService,
JabRefPreferences.getInstance().getXMPPreferences(),
JabRefPreferences.getInstance().getFilePreferences(),
ExternalFileTypes.getInstance());
newLinkedFile,
entry,
basePanel.getBibDatabaseContext(),
Globals.TASK_EXECUTOR,
dialogService,
JabRefPreferences.getInstance().getXMPPreferences(),
JabRefPreferences.getInstance().getFilePreferences(),
ExternalFileTypes.getInstance());

try {
URLDownload urlDownload = new URLDownload(newLinkedFile.getLink());
BackgroundTask<Path> downloadTask = onlineFile.prepareDownloadTask(targetDirectory, urlDownload);
downloadTask.onSuccess(destination -> {
LinkedFile downloadedFile = LinkedFilesEditorViewModel.fromFile(
destination,
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved
basePanel.getBibDatabaseContext().getFileDirectoriesAsPaths(JabRefPreferences.getInstance().getFilePreferences()), ExternalFileTypes.getInstance());
destination,
basePanel.getBibDatabaseContext().getFileDirectoriesAsPaths(JabRefPreferences.getInstance().getFilePreferences()), ExternalFileTypes.getInstance());
entry.addFile(downloadedFile);
dialogService.notify(Localization.lang("Finished downloading full text document for entry %0.",
entry.getCiteKeyOptional().orElse(Localization.lang("undefined"))));
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved
entry.getCiteKeyOptional().orElse(Localization.lang("undefined"))));
});
Globals.TASK_EXECUTOR.execute(downloadTask);
} catch (MalformedURLException exception) {
dialogService.showErrorDialogAndWait(Localization.lang("Invalid URL"), exception);
}
} else {
dialogService.notify(Localization.lang("Full text document for entry %0 already linked.",
entry.getCiteKeyOptional().orElse(Localization.lang("undefined"))));
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved
entry.getCiteKeyOptional().orElse(Localization.lang("undefined"))));
}
}
}
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 @@ -2083,6 +2083,8 @@ Import\ entries\ from\ LaTeX\ files=Import entries from LaTeX files
Import\ new\ entries=Import new entries
Group\ color=Group color

Full\ text\ document\ found\ already\ on\ disk\ for\ entry\ %0.\ Not\ downloaded\ again.=Full text document found already on disk for entry %0. Not downloaded again.

Columns=Columns
File\ type=File type
IEEE=IEEE
Expand Down