Search in sources :

Example 6 with KeyCollisionException

use of org.jabref.model.database.KeyCollisionException in project jabref by JabRef.

the class BasePanel method insertEntry.

/**
     * This method is called from JabRefFrame when the user wants to create a new entry.
     *
     * @param bibEntry The new entry.
     */
public void insertEntry(final BibEntry bibEntry) {
    if (bibEntry != null) {
        try {
            bibDatabaseContext.getDatabase().insertEntry(bibEntry);
            if (Globals.prefs.getBoolean(JabRefPreferences.USE_OWNER)) {
                // Set owner field to default value
                UpdateField.setAutomaticFields(bibEntry, true, true, Globals.prefs.getUpdateFieldPreferences());
            }
            // Create an UndoableInsertEntry object.
            getUndoManager().addEdit(new UndoableInsertEntry(bibDatabaseContext.getDatabase(), bibEntry, BasePanel.this));
            output(Localization.lang("Added new '%0' entry.", bibEntry.getType()));
            // The database just changed.
            markBaseChanged();
            if (Globals.prefs.getBoolean(JabRefPreferences.AUTO_OPEN_FORM)) {
                selectionListener.editSignalled(bibEntry);
            }
            highlightEntry(bibEntry);
        } catch (KeyCollisionException ex) {
            LOGGER.info("Collision for bibtex key" + bibEntry.getId(), ex);
        }
    }
}
Also used : KeyCollisionException(org.jabref.model.database.KeyCollisionException) UndoableInsertEntry(org.jabref.gui.undo.UndoableInsertEntry)

Example 7 with KeyCollisionException

use of org.jabref.model.database.KeyCollisionException in project jabref by JabRef.

the class StringChange method makeChange.

@Override
public boolean makeChange(BasePanel panel, BibDatabase secondary, NamedCompound undoEdit) {
    if (string == null) {
        // The string was removed or renamed locally. We guess that it was removed.
        BibtexString bs = new BibtexString(label, disk);
        try {
            panel.getDatabase().addString(bs);
            undoEdit.addEdit(new UndoableInsertString(panel, panel.getDatabase(), bs));
        } catch (KeyCollisionException ex) {
            LOGGER.info("Error: could not add string '" + bs.getName() + "': " + ex.getMessage(), ex);
        }
    } else {
        string.setContent(disk);
        undoEdit.addEdit(new UndoableStringChange(panel, string, false, mem, disk));
    }
    // Update tmp database:
    if (tmpString == null) {
        BibtexString bs = new BibtexString(label, disk);
        secondary.addString(bs);
    } else {
        tmpString.setContent(disk);
    }
    return true;
}
Also used : KeyCollisionException(org.jabref.model.database.KeyCollisionException) UndoableInsertString(org.jabref.gui.undo.UndoableInsertString) UndoableStringChange(org.jabref.gui.undo.UndoableStringChange) BibtexString(org.jabref.model.entry.BibtexString)

Example 8 with KeyCollisionException

use of org.jabref.model.database.KeyCollisionException in project jabref by JabRef.

the class StringNameChange method makeChange.

@Override
public boolean makeChange(BasePanel panel, BibDatabase secondary, NamedCompound undoEdit) {
    if (panel.getDatabase().hasStringLabel(disk)) {
        // The name to change to is already in the database, so we can't comply.
        LOGGER.info("Cannot rename string '" + mem + "' to '" + disk + "' because the name " + "is already in use.");
    }
    if (string == null) {
        // The string was removed or renamed locally. We guess that it was removed.
        BibtexString bs = new BibtexString(disk, content);
        try {
            panel.getDatabase().addString(bs);
            undoEdit.addEdit(new UndoableInsertString(panel, panel.getDatabase(), bs));
        } catch (KeyCollisionException ex) {
            LOGGER.info("Error: could not add string '" + bs.getName() + "': " + ex.getMessage(), ex);
        }
    } else {
        string.setName(disk);
        undoEdit.addEdit(new UndoableStringChange(panel, string, true, mem, disk));
    }
    // Update tmp database:
    if (tmpString == null) {
        BibtexString bs = new BibtexString(disk, content);
        secondary.addString(bs);
    } else {
        tmpString.setName(disk);
    }
    return true;
}
Also used : KeyCollisionException(org.jabref.model.database.KeyCollisionException) UndoableInsertString(org.jabref.gui.undo.UndoableInsertString) UndoableStringChange(org.jabref.gui.undo.UndoableStringChange) BibtexString(org.jabref.model.entry.BibtexString)

Example 9 with KeyCollisionException

use of org.jabref.model.database.KeyCollisionException in project jabref by JabRef.

the class ImportMenuItem method mergeImportResults.

private ParserResult mergeImportResults(List<ImportFormatReader.UnknownFormatImport> imports) {
    BibDatabase database = new BibDatabase();
    ParserResult directParserResult = null;
    boolean anythingUseful = false;
    for (ImportFormatReader.UnknownFormatImport importResult : imports) {
        if (importResult == null) {
            continue;
        }
        if (ImportFormatReader.BIBTEX_FORMAT.equals(importResult.format)) {
            // Bibtex result. We must merge it into our main base.
            ParserResult pr = importResult.parserResult;
            anythingUseful = anythingUseful || pr.getDatabase().hasEntries() || (!pr.getDatabase().hasNoStrings());
            // Record the parserResult, as long as this is the first bibtex result:
            if (directParserResult == null) {
                directParserResult = pr;
            }
            // Merge entries:
            for (BibEntry entry : pr.getDatabase().getEntries()) {
                database.insertEntry(entry);
            }
            // Merge strings:
            for (BibtexString bs : pr.getDatabase().getStringValues()) {
                try {
                    database.addString((BibtexString) bs.clone());
                } catch (KeyCollisionException e) {
                // TODO: This means a duplicate string name exists, so it's not
                // a very exceptional situation. We should maybe give a warning...?
                }
            }
        } else {
            ParserResult pr = importResult.parserResult;
            Collection<BibEntry> entries = pr.getDatabase().getEntries();
            anythingUseful = anythingUseful | !entries.isEmpty();
            // set timestamp and owner
            // set timestamp and owner
            UpdateField.setAutomaticFields(entries, Globals.prefs.getUpdateFieldPreferences());
            boolean markEntries = !openInNew && EntryMarker.shouldMarkEntries();
            for (BibEntry entry : entries) {
                if (markEntries) {
                    EntryMarker.markEntry(entry, EntryMarker.IMPORT_MARK_LEVEL, false, new NamedCompound(""));
                }
                database.insertEntry(entry);
            }
        }
    }
    if (!anythingUseful) {
        return null;
    }
    if ((imports.size() == 1) && (directParserResult != null)) {
        return directParserResult;
    } else {
        return new ParserResult(database);
    }
}
Also used : KeyCollisionException(org.jabref.model.database.KeyCollisionException) ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) NamedCompound(org.jabref.gui.undo.NamedCompound) ImportFormatReader(org.jabref.logic.importer.ImportFormatReader) BibtexString(org.jabref.model.entry.BibtexString) BibDatabase(org.jabref.model.database.BibDatabase)

Example 10 with KeyCollisionException

use of org.jabref.model.database.KeyCollisionException in project jabref by JabRef.

the class BibtexParser method parse.

/**
     * Will parse the BibTex-Data found when reading from reader. Ignores any encoding supplied in the file by
     * "Encoding: myEncoding".
     * <p>
     * The reader will be consumed.
     * <p>
     * Multiple calls to parse() return the same results
     *
     * @return ParserResult
     * @throws IOException
     */
public ParserResult parse(Reader in) throws IOException {
    Objects.requireNonNull(in);
    pushbackReader = new PushbackReader(in, BibtexParser.LOOKAHEAD);
    // Bibtex related contents.
    initializeParserResult();
    parseDatabaseID();
    skipWhitespace();
    try {
        return parseFileContent();
    } catch (KeyCollisionException kce) {
        throw new IOException("Duplicate ID in bibtex file: " + kce);
    }
}
Also used : KeyCollisionException(org.jabref.model.database.KeyCollisionException) IOException(java.io.IOException) PushbackReader(java.io.PushbackReader)

Aggregations

KeyCollisionException (org.jabref.model.database.KeyCollisionException)10 BibtexString (org.jabref.model.entry.BibtexString)5 UndoableInsertEntry (org.jabref.gui.undo.UndoableInsertEntry)3 UndoableInsertString (org.jabref.gui.undo.UndoableInsertString)3 BibEntry (org.jabref.model.entry.BibEntry)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 UndoableStringChange (org.jabref.gui.undo.UndoableStringChange)2 ParserResult (org.jabref.logic.importer.ParserResult)2 EntryType (org.jabref.model.entry.EntryType)2 PushbackReader (java.io.PushbackReader)1 Path (java.nio.file.Path)1 EntryTypeDialog (org.jabref.gui.EntryTypeDialog)1 EntryEditor (org.jabref.gui.entryeditor.EntryEditor)1 NamedCompound (org.jabref.gui.undo.NamedCompound)1 ImportFormatReader (org.jabref.logic.importer.ImportFormatReader)1 BibDatabase (org.jabref.model.database.BibDatabase)1