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

DOI Fetcher using the new fetcher infrastructure #1885

Merged
merged 18 commits into from
Sep 15, 2016
Merged
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
8 changes: 5 additions & 3 deletions src/main/java/net/sf/jabref/gui/ClipBoardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import java.util.Optional;

import net.sf.jabref.Globals;
import net.sf.jabref.logic.importer.fetcher.DOItoBibTeX;
import net.sf.jabref.logic.importer.FetcherException;
import net.sf.jabref.logic.importer.fetcher.DoiFetcher;
import net.sf.jabref.logic.importer.fileformat.BibtexParser;
import net.sf.jabref.logic.util.DOI;
import net.sf.jabref.model.database.BibDatabase;
Expand Down Expand Up @@ -90,8 +91,7 @@ public List<BibEntry> extractBibEntriesFromClipboard() {
// fetch from doi
if (DOI.build(data).isPresent()) {
LOGGER.info("Found DOI in clipboard");
Optional<BibEntry> entry = DOItoBibTeX.getEntryFromDOI(new DOI(data).getDOI(),
Globals.prefs.getImportFormatPreferences());
Optional<BibEntry> entry = new DoiFetcher(Globals.prefs.getImportFormatPreferences()).performSearchById(new DOI(data).getDOI());
entry.ifPresent(result::add);
} else {
// parse bibtex string
Expand All @@ -107,6 +107,8 @@ public List<BibEntry> extractBibEntriesFromClipboard() {
LOGGER.warn("Could not parse this type", ex);
} catch (IOException ex) {
LOGGER.warn("Data is no longer available in the requested flavor", ex);
} catch (FetcherException ex) {
LOGGER.error("Error while fetching", ex);
}

}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/net/sf/jabref/gui/EntryTypeDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,10 @@ protected Optional<BibEntry> doInBackground() throws Exception {
try {
bibEntry = fetcher.performSearchById(searchID);
} catch (FetcherException e) {
LOGGER.error("Error fetching from " + fetcher.getName(), e);
JOptionPane.showMessageDialog(null, Localization.lang("Error while fetching from %0", fetcher.getName()), Localization.lang("Error"), JOptionPane.ERROR_MESSAGE);
LOGGER.error(e.getMessage(), e);
JOptionPane.showMessageDialog(null,
Localization.lang("Error while fetching from %0", fetcher.getName()) + "\n" + e.getMessage(),
Localization.lang("Error"), JOptionPane.ERROR_MESSAGE);
}
}
dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,12 @@ public boolean updateField(String field, String content) {
if (!editors.containsKey(field)) {
return false;
}

FieldEditor fieldEditor = editors.get(field);
if (fieldEditor.getText().equals(content)){
return true;
}

// trying to preserve current edit position (fixes SF bug #1285)
if(fieldEditor.getTextComponent() instanceof JTextComponent) {
int initialCaretPosition = ((JTextComponent) fieldEditor).getCaretPosition();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.sf.jabref.logic.importer.IdBasedFetcher;
import net.sf.jabref.logic.importer.fetcher.ArXiv;
import net.sf.jabref.logic.importer.fetcher.DiVA;
import net.sf.jabref.logic.importer.fetcher.DoiFetcher;
import net.sf.jabref.logic.importer.fetcher.GvkFetcher;
import net.sf.jabref.logic.importer.fetcher.IsbnFetcher;
import net.sf.jabref.logic.journals.JournalAbbreviationLoader;
Expand All @@ -21,7 +22,6 @@ public EntryFetchers(JournalAbbreviationLoader abbreviationLoader) {
entryFetchers.add(new ADSFetcher());
entryFetchers.add(new CiteSeerXFetcher());
entryFetchers.add(new DBLPFetcher());
entryFetchers.add(new DOItoBibTeXFetcher());
entryFetchers.add(new IEEEXploreFetcher(abbreviationLoader));
entryFetchers.add(new INSPIREFetcher());
entryFetchers.add(new MedlineFetcher());
Expand All @@ -44,6 +44,7 @@ public static ArrayList<IdBasedFetcher> getIdFetchers() {
ArrayList<IdBasedFetcher> list = new ArrayList<>();
list.add(new IsbnFetcher(Globals.prefs.getImportFormatPreferences()));
list.add(new DiVA(Globals.prefs.getImportFormatPreferences()));
list.add(new DoiFetcher(Globals.prefs.getImportFormatPreferences()));
list.sort((fetcher1, fetcher2) -> fetcher1.getName().compareTo(fetcher2.getName()));
return list;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public boolean processQuery(String query, ImportInspector inspector, OutputPrint
match.ifPresent(inspector::addEntry);
return match.isPresent();
} catch (FetcherException e) {
status.setStatus(Localization.lang("Error while fetching from %0", fetcher.getName()));
status.setStatus(e.getLocalizedMessage());
LOGGER.error("Error while fetching from" + fetcher.getName(), e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public boolean processQuery(String query, ImportInspector inspector, OutputPrint
matches.forEach(inspector::addEntry);
return !matches.isEmpty();
} catch (FetcherException e) {
status.setStatus(Localization.lang("Error while fetching from %0", fetcher.getName()));
status.setStatus(e.getLocalizedMessage());
LOGGER.error("Error while fetching from" + fetcher.getName(), e);
}

Expand Down
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.DOItoBibTeX;
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 = new DOItoBibTeX().getEntryFromDOI(fieldContent.get(),
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,77 @@
package net.sf.jabref.gui.mergeentries;

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

import javax.swing.SwingWorker;

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.WebFetchers;
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;


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;


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);
}

@Override
protected Optional<BibEntry> doInBackground() throws Exception {
Optional<IdBasedFetcher> fetcher = WebFetchers.getIdBasedFetcherForField(field, Globals.prefs.getImportFormatPreferences());

try {
Optional<String> fieldContentValue = fieldContent;
if (fieldContentValue.isPresent() && fetcher.isPresent()) {
return fetcher.get().performSearchById(fieldContentValue.get());
} else {
return Optional.empty();
}
} catch (FetcherException e) {
LOGGER.error("Info cannot be found", e);
return Optional.empty();
}
}

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

try {
String type = FieldName.getDisplayName(field);
Optional<BibEntry> fetchedEntry = 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()));
}
} catch (InterruptedException | ExecutionException e) {
LOGGER.error("Error while fetching Entry", e);
}
}

}
Loading