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 8 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
74 changes: 44 additions & 30 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 Down Expand Up @@ -54,15 +56,15 @@ 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("Look up full text documents"),
Localization.lang("Cancel"));
Localization.lang("Look up full text documents"),
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"));

if (!confirmDownload) {
basePanel.output(Localization.lang("Operation canceled."));
Expand All @@ -71,25 +73,34 @@ public void execute() {
}

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

@Override
protected Map<BibEntry, Optional<URL>> call() {
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));
AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(basePanel.getBibDatabaseContext(), Globals.prefs.getFilePreferences(), Globals.prefs.getAutoLinkPreferences(), ExternalFileTypes.getInstance());
List<BibEntry> changed = util.linkAssociatedFiles(basePanel.getSelectedEntries(), new NamedCompound(""));

for (BibEntry entry : changed) {
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved
if (entry.getFiles().isEmpty()) {
FulltextFetchers fetchers = new FulltextFetchers(Globals.prefs.getImportFormatPreferences());
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved
downloads.put(entry, fetchers.findFullTextPDF(entry));
} else {
downloads.put(entry, Optional.empty());
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved
}
updateProgress(++count, basePanel.getSelectedEntries().size());
}

Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved
return downloads;
}
};

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

dialogService.showProgressDialogAndWait(
Localization.lang("Look up full text documents"),
Localization.lang("Looking for full text document..."),
findFullTextsTask);
Localization.lang("Look up full text documents"),
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved
Localization.lang("Looking for full text document..."),
findFullTextsTask);

Globals.TASK_EXECUTOR.execute(findFullTextsTask);
}
Expand All @@ -104,16 +115,19 @@ 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")
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.

+ " -> " + Localization.lang("File"));
return;
}
//Download and link full text
addLinkedFileFromURL(result.get(), entry, dir.get());

} else if (!result.isPresent() && !entry.getFiles().isEmpty()) {
dialogService.notify(Localization.lang("Full text document found already on disk for entry %0", entry.getCiteKeyOptional().orElse(Localization.lang("undefined"))));
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved

Copy link
Member

Choose a reason for hiding this comment

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

Please no empty line here.

} 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 +146,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 @@ -2108,3 +2108,5 @@ Selected\ entry\ does\ not\ have\ an\ associated\ BibTeX\ key.=Selected entry do
Current\ search\ directory\:=Current search directory:
Set\ LaTeX\ file\ directory=Set LaTeX file directory
Group\ color=Group color

Full\ text\ document\ found\ already\ on\ disk\ for\ entry\ %0=Full text document found already on disk for entry %0