Skip to content

Commit

Permalink
1799 moved the fetcher in own SwingWorker
Browse files Browse the repository at this point in the history
  • Loading branch information
chriba committed Sep 1, 2016
1 parent 66f316d commit 76daa4d
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,19 @@

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import net.sf.jabref.Globals;
import net.sf.jabref.gui.BasePanel;
import net.sf.jabref.logic.importer.FetcherException;
import net.sf.jabref.logic.importer.fetcher.ArXiv;
import net.sf.jabref.logic.importer.fetcher.DoiFetcher;
import net.sf.jabref.logic.importer.fetcher.IsbnFetcher;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FieldName;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* Class for fetching and merging information based on a specific field
*
*/
public class FetchAndMergeEntry {

private static final Log LOGGER = LogFactory.getLog(FetchAndMergeEntry.class);

// A list of all field which are supported
public static List<String> SUPPORTED_FIELDS = Arrays.asList(FieldName.DOI, FieldName.EPRINT, FieldName.ISBN);

Expand All @@ -47,56 +36,19 @@ public FetchAndMergeEntry(BibEntry entry, BasePanel panel, String field) {
* @param panel - current BasePanel
* @param fields - List of fields to get information from, one at a time in given order
*/

public FetchAndMergeEntry(BibEntry entry, BasePanel panel, List<String> fields) {
for (String field : fields) {
Optional<String> fieldContent = entry.getField(field);

// Get better looking name for status messages
String type = FieldName.getDisplayName(field);

if (fieldContent.isPresent()) {
Optional<BibEntry> fetchedEntry = Optional.empty();
// Get entry based on field
if (FieldName.DOI.equals(field)) {
fetchedEntry = DoiFetcher.getEntryFromDOI(fieldContent.get(), null,
Globals.prefs.getImportFormatPreferences());
} else if (FieldName.ISBN.equals(field)) {
try {
fetchedEntry = new IsbnFetcher(Globals.prefs.getImportFormatPreferences ()).performSearchById(fieldContent.get());
} catch (FetcherException e) {
LOGGER.error("Info cannot be found", e);
panel.frame().setStatus(
Localization.lang("Cannot get info based on given %0: %1", type, fieldContent.get()));
}

} else if (FieldName.EPRINT.equals(field)) {
try {
fetchedEntry = new ArXiv().performSearchById(fieldContent.get());
} catch (FetcherException e) {
LOGGER.error("Info cannot be found", e);
panel.frame().setStatus(
Localization.lang("Cannot get info based on given %0: %1", type, fieldContent.get()));
}
}

if (fetchedEntry.isPresent()) {
MergeFetchedEntryDialog dialog = new MergeFetchedEntryDialog(panel, entry, fetchedEntry.get(),
type);
dialog.setVisible(true);
} else {
panel.frame()
.setStatus(Localization.lang("Cannot get info based on given %0: %1", type,
fieldContent.get()));
}
if (entry.hasField(field)) {
new FetchAndMergeWorker(panel, entry, field).execute();
} else {
panel.frame().setStatus(Localization.lang("No %0 found", type));
panel.frame().setStatus(Localization.lang("No %0 found", FieldName.getDisplayName(field)));
}
}
}

public static String getDisplayNameOfSupportedFields() {
return FieldName.orFields(SUPPORTED_FIELDS.stream().map(fieldName -> FieldName.getDisplayName(fieldName))
return FieldName.orFields(SUPPORTED_FIELDS.stream()
.map(FieldName::getDisplayName)
.collect(Collectors.toList()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package net.sf.jabref.gui.mergeentries;

import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ExecutionException;

import net.sf.jabref.Globals;
import net.sf.jabref.gui.BasePanel;
import net.sf.jabref.logic.importer.FetcherException;
import net.sf.jabref.logic.importer.IdBasedFetcher;
import net.sf.jabref.logic.importer.fetcher.ArXiv;
import net.sf.jabref.logic.importer.fetcher.DoiFetcher;
import net.sf.jabref.logic.importer.fetcher.IsbnFetcher;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FieldName;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdesktop.swingworker.SwingWorker;


public class FetchAndMergeWorker extends SwingWorker<Optional<BibEntry>, Void> {

private static final Log LOGGER = LogFactory.getLog(FetchAndMergeWorker.class);

private final BasePanel panel;
private final BibEntry entry;
private final String field;
private final Optional<String> fieldContent;
private final String type;


public FetchAndMergeWorker(BasePanel panel, BibEntry entry, String field) {
this.panel = Objects.requireNonNull(panel);
this.entry = Objects.requireNonNull(entry);
this.field = Objects.requireNonNull(field);

this.fieldContent = entry.getField(field);
this.type = FieldName.getDisplayName(field);
}

@Override
protected Optional<BibEntry> doInBackground() throws Exception {
IdBasedFetcher fetcher;
if (FieldName.DOI.equals(field)) {
fetcher = new DoiFetcher(Globals.prefs.getImportFormatPreferences());
} else if (FieldName.ISBN.equals(field)) {
fetcher = new IsbnFetcher(Globals.prefs.getImportFormatPreferences());
} else if (FieldName.EPRINT.equals(field)) {
fetcher = new ArXiv();
} else {
// Should never occur
return Optional.empty();
}

try {
return fetcher.performSearchById(fieldContent.get());
} catch (FetcherException e) {
LOGGER.error("Info cannot be found", e);
return Optional.empty();
}
}

@Override
protected void done() {
if (isCancelled()) {
return;
}

Optional<BibEntry> fetchedEntry = null;
try {
fetchedEntry = get();
} catch (InterruptedException | ExecutionException e) {
LOGGER.error("Error while fetching Entry", e);
}

if (fetchedEntry != null && fetchedEntry.isPresent()) {
MergeFetchedEntryDialog dialog = new MergeFetchedEntryDialog(panel, entry, fetchedEntry.get(), type);
dialog.setVisible(true);
} else {
panel.frame().setStatus(Localization.lang("Cannot get info based on given %0: %1", type, fieldContent.get()));
}
}

}

0 comments on commit 76daa4d

Please sign in to comment.