Search in sources :

Example 31 with FieldChange

use of org.jabref.model.FieldChange in project jabref by JabRef.

the class CleanupWorkerTest method cleanupDoesNothingByDefault.

@Test
public void cleanupDoesNothingByDefault() throws IOException {
    BibEntry entry = new BibEntry();
    entry.setCiteKey("Toot");
    entry.setField("pdf", "aPdfFile");
    entry.setField("some", "1st");
    entry.setField("doi", "http://dx.doi.org/10.1016/0001-8708(80)90035-3");
    entry.setField("month", "01");
    entry.setField("pages", "1-2");
    entry.setField("date", "01/1999");
    entry.setField("pdf", "aPdfFile");
    entry.setField("ps", "aPsFile");
    entry.setField("file", "link::");
    entry.setField("journal", "test");
    entry.setField("title", "<b>hallo</b> units 1 A case AlGaAs and latex $\\alpha$$\\beta$");
    entry.setField("abstract", "Réflexions");
    File tempFile = bibFolder.newFile();
    LinkedFile fileField = new LinkedFile("", tempFile.getAbsolutePath(), "");
    entry.setField("file", FileFieldWriter.getStringRepresentation(fileField));
    List<FieldChange> changes = worker.cleanup(emptyPreset, entry);
    Assert.assertEquals(Collections.emptyList(), changes);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) LinkedFile(org.jabref.model.entry.LinkedFile) FieldChange(org.jabref.model.FieldChange) LinkedFile(org.jabref.model.entry.LinkedFile) File(java.io.File) Test(org.junit.Test)

Example 32 with FieldChange

use of org.jabref.model.FieldChange in project jabref by JabRef.

the class CleanupWorkerTest method cleanupDoiReturnsChangeWhenDoiInURLField.

@Test
public void cleanupDoiReturnsChangeWhenDoiInURLField() {
    CleanupPreset preset = new CleanupPreset(CleanupPreset.CleanupStep.CLEAN_UP_DOI);
    BibEntry entry = new BibEntry();
    entry.setField("url", "http://dx.doi.org/10.1016/0001-8708(80)90035-3");
    List<FieldChange> changes = worker.cleanup(preset, entry);
    List<FieldChange> changeList = new ArrayList<>();
    changeList.add(new FieldChange(entry, "doi", null, "10.1016/0001-8708(80)90035-3"));
    changeList.add(new FieldChange(entry, "url", "http://dx.doi.org/10.1016/0001-8708(80)90035-3", null));
    Assert.assertEquals(changeList, changes);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) FieldChange(org.jabref.model.FieldChange) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 33 with FieldChange

use of org.jabref.model.FieldChange in project jabref by JabRef.

the class CleanupWorkerTest method cleanupDoiReturnsChanges.

@Test
public void cleanupDoiReturnsChanges() {
    CleanupPreset preset = new CleanupPreset(CleanupPreset.CleanupStep.CLEAN_UP_DOI);
    BibEntry entry = new BibEntry();
    entry.setField("doi", "http://dx.doi.org/10.1016/0001-8708(80)90035-3");
    List<FieldChange> changes = worker.cleanup(preset, entry);
    FieldChange expectedChange = new FieldChange(entry, "doi", "http://dx.doi.org/10.1016/0001-8708(80)90035-3", "10.1016/0001-8708(80)90035-3");
    Assert.assertEquals(Collections.singletonList(expectedChange), changes);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) FieldChange(org.jabref.model.FieldChange) Test(org.junit.Test)

Example 34 with FieldChange

use of org.jabref.model.FieldChange in project jabref by JabRef.

the class SpecialFieldDatabaseChangeListener method listen.

@Subscribe
public void listen(EntryAddedEvent event) {
    if (!Globals.prefs.isKeywordSyncEnabled()) {
        return;
    }
    final BibEntry entry = event.getBibEntry();
    // NamedCompount code similar to SpecialFieldUpdateListener
    NamedCompound nc = new NamedCompound(Localization.lang("Synchronized special fields based on keywords"));
    List<FieldChange> changes = SpecialFieldsUtils.syncSpecialFieldsFromKeywords(entry, Globals.prefs.getKeywordDelimiter());
    for (FieldChange change : changes) {
        nc.addEdit(new UndoableFieldChange(change));
    }
// Don't insert the compound into the undoManager,
// it would be added before the component which undoes the insertion of the entry and creates heavy problems
// (which prohibits the undo the deleting multiple entries)
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) NamedCompound(org.jabref.gui.undo.NamedCompound) FieldChange(org.jabref.model.FieldChange) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) Subscribe(com.google.common.eventbus.Subscribe)

Example 35 with FieldChange

use of org.jabref.model.FieldChange in project jabref by JabRef.

the class DoiCleanup method cleanup.

@Override
public List<FieldChange> cleanup(BibEntry entry) {
    List<FieldChange> changes = new ArrayList<>();
    // First check if the Doi Field is empty
    if (entry.hasField(FieldName.DOI)) {
        String doiFieldValue = entry.getField(FieldName.DOI).orElse(null);
        Optional<DOI> doi = DOI.parse(doiFieldValue);
        if (doi.isPresent()) {
            String newValue = doi.get().getDOI();
            if (!doiFieldValue.equals(newValue)) {
                entry.setField(FieldName.DOI, newValue);
                FieldChange change = new FieldChange(entry, FieldName.DOI, doiFieldValue, newValue);
                changes.add(change);
            }
            // Doi field seems to contain Doi -> cleanup note, url, ee field
            for (String field : FIELDS) {
                entry.getField(field).flatMap(DOI::parse).ifPresent(unused -> removeFieldValue(entry, field, changes));
            }
        }
    } else {
        // As the Doi field is empty we now check if note, url, or ee field contains a Doi
        for (String field : FIELDS) {
            Optional<DOI> doi = entry.getField(field).flatMap(DOI::parse);
            if (doi.isPresent()) {
                // update Doi
                String oldValue = entry.getField(FieldName.DOI).orElse(null);
                String newValue = doi.get().getDOI();
                entry.setField(FieldName.DOI, newValue);
                FieldChange change = new FieldChange(entry, FieldName.DOI, oldValue, newValue);
                changes.add(change);
                removeFieldValue(entry, field, changes);
            }
        }
    }
    return changes;
}
Also used : ArrayList(java.util.ArrayList) FieldChange(org.jabref.model.FieldChange) DOI(org.jabref.model.entry.identifier.DOI)

Aggregations

FieldChange (org.jabref.model.FieldChange)35 BibEntry (org.jabref.model.entry.BibEntry)17 ArrayList (java.util.ArrayList)13 UndoableFieldChange (org.jabref.gui.undo.UndoableFieldChange)8 NamedCompound (org.jabref.gui.undo.NamedCompound)7 Test (org.junit.Test)7 KeywordList (org.jabref.model.entry.KeywordList)6 LinkedFile (org.jabref.model.entry.LinkedFile)6 IOException (java.io.IOException)3 File (java.io.File)2 Path (java.nio.file.Path)2 Keyword (org.jabref.model.entry.Keyword)2 FieldChangedEvent (org.jabref.model.entry.event.FieldChangedEvent)2 Subscribe (com.google.common.eventbus.Subscribe)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 AbstractUndoableEdit (javax.swing.undo.AbstractUndoableEdit)1 BasePanel (org.jabref.gui.BasePanel)1 CleanupWorker (org.jabref.logic.cleanup.CleanupWorker)1