-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Add "Look up DOIs" to Quality menu #2442
Changes from 1 commit
bf77a68
ecbdd11
fd5b71b
b11cecc
e29be92
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package net.sf.jabref.gui.worker; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import net.sf.jabref.gui.BasePanel; | ||
import net.sf.jabref.gui.JabRefFrame; | ||
import net.sf.jabref.gui.undo.NamedCompound; | ||
import net.sf.jabref.gui.undo.UndoableFieldChange; | ||
import net.sf.jabref.logic.l10n.Localization; | ||
import net.sf.jabref.logic.util.DOI; | ||
import net.sf.jabref.model.FieldChange; | ||
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 LookupDOIsWorker extends AbstractWorker { | ||
|
||
private final JabRefFrame frame; | ||
private String message; | ||
|
||
private static final Log LOGGER = LogFactory.getLog(LookupDOIsWorker.class); | ||
|
||
public LookupDOIsWorker(JabRefFrame frame) { | ||
this.frame = Objects.requireNonNull(frame); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
BasePanel basePanel = Objects.requireNonNull(frame.getCurrentBasePanel()); | ||
List<BibEntry> bibEntries = basePanel.getSelectedEntries(); | ||
if (!bibEntries.isEmpty()) { | ||
String totalCount = Integer.toString(bibEntries.size()); | ||
NamedCompound namedCompound = new NamedCompound(Localization.lang("Look up DOIs")); | ||
int count = 0; | ||
int foundCount = 0; | ||
for (BibEntry bibEntry : bibEntries) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it make sense to directly filter out the bibtex fields which have a DOI here with a stream? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have no idea how to create a progress bar there. I found http://stackoverflow.com/a/26814087/873282, but that code looks much more complicated than the current code. Should I change it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Na then go for it now., |
||
count++; | ||
frame.output(Localization.lang("Looking up DOIs... - entry %0 out of %1 - found %2", Integer.toString(count), totalCount, Integer.toString(foundCount))); | ||
if (!bibEntry.hasField(FieldName.DOI)) { | ||
Optional<DOI> doi = DOI.fromBibEntry(bibEntry); | ||
if (doi.isPresent()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could chain the optionals with flatmap, avoid the ifPresent cascading... http://www.nurkiewicz.com/2013/08/optional-in-java-8-cheat-sheet.html There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No clue how I can do that with progress... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, was just an idea. If it does not make it easier forget about it |
||
Optional<FieldChange> fieldChange = bibEntry.setField(FieldName.DOI, doi.get().getDOI()); | ||
if (fieldChange.isPresent()) { | ||
namedCompound.addEdit(new UndoableFieldChange(fieldChange.get())); | ||
foundCount++; | ||
frame.output(Localization.lang("Looking up DOIs... - entry %0 out of %1 - found %2", Integer.toString(count), totalCount, Integer.toString(foundCount))); | ||
} | ||
} | ||
} | ||
} | ||
namedCompound.end(); | ||
if (foundCount > 0) { | ||
basePanel.getUndoManager().addEdit(namedCompound); | ||
basePanel.markBaseChanged(); | ||
} | ||
message = Localization.lang("Determined_DOIs_for_%0_entries", Integer.toString(foundCount)); | ||
} | ||
} | ||
|
||
@Override | ||
public void update() { | ||
frame.output(message); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For maximal reusability, could you please make this class independent on DOI. There are many other identifier for which such an automatic look-up makes sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ID
or let the caller set the DOI nameThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@koppor can you please rebase and I will implement then the rest. Deal?