Search in sources :

Example 1 with ContentSelector

use of org.jabref.model.metadata.ContentSelector in project jabref by JabRef.

the class MetaDataSerializer method getSerializedStringMap.

/**
     * Writes all data in the format <key, serialized data>.
     */
public static Map<String, String> getSerializedStringMap(MetaData metaData, GlobalBibtexKeyPattern globalCiteKeyPattern) {
    // First write all meta data except groups
    Map<String, List<String>> stringyMetaData = new HashMap<>();
    metaData.getSaveOrderConfig().ifPresent(saveOrderConfig -> stringyMetaData.put(MetaData.SAVE_ORDER_CONFIG, saveOrderConfig.getAsStringList()));
    metaData.getSaveActions().ifPresent(saveActions -> stringyMetaData.put(MetaData.SAVE_ACTIONS, saveActions.getAsStringList(OS.NEWLINE)));
    if (metaData.isProtected()) {
        stringyMetaData.put(MetaData.PROTECTED_FLAG_META, Collections.singletonList("true"));
    }
    stringyMetaData.putAll(serializeCiteKeyPattern(metaData, globalCiteKeyPattern));
    metaData.getMode().ifPresent(mode -> stringyMetaData.put(MetaData.DATABASE_TYPE, Collections.singletonList(mode.getAsString())));
    metaData.getDefaultFileDirectory().ifPresent(path -> stringyMetaData.put(MetaData.FILE_DIRECTORY, Collections.singletonList(path.trim())));
    metaData.getUserFileDirectories().forEach((user, path) -> stringyMetaData.put(MetaData.FILE_DIRECTORY + '-' + user, Collections.singletonList(path.trim())));
    for (ContentSelector selector : metaData.getContentSelectorList()) {
        stringyMetaData.put(MetaData.SELECTOR_META_PREFIX + selector.getFieldName(), selector.getValues());
    }
    Map<String, String> serializedMetaData = serializeMetaData(stringyMetaData);
    // Write groups if present.
    // Skip this if only the root node exists (which is always the AllEntriesGroup).
    metaData.getGroups().filter(root -> root.getNumberOfChildren() > 0).ifPresent(root -> serializedMetaData.put(MetaData.GROUPSTREE, serializeGroups(root)));
    // finally add all unknown meta data items to the serialization map
    Map<String, List<String>> unknownMetaData = metaData.getUnknownMetaData();
    for (Map.Entry<String, List<String>> entry : unknownMetaData.entrySet()) {
        StringBuilder value = new StringBuilder();
        value.append(OS.NEWLINE);
        for (String line : entry.getValue()) {
            value.append(line.replaceAll(";", "\\\\;") + MetaData.SEPARATOR_STRING + OS.NEWLINE);
        }
        serializedMetaData.put(entry.getKey(), value.toString());
    }
    return serializedMetaData;
}
Also used : GlobalBibtexKeyPattern(org.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern) StringUtil(org.jabref.model.strings.StringUtil) AbstractBibtexKeyPattern(org.jabref.model.bibtexkeypattern.AbstractBibtexKeyPattern) ContentSelector(org.jabref.model.metadata.ContentSelector) HashMap(java.util.HashMap) FieldFormatterCleanups(org.jabref.model.cleanup.FieldFormatterCleanups) ArrayList(java.util.ArrayList) List(java.util.List) TreeMap(java.util.TreeMap) OS(org.jabref.logic.util.OS) GroupTreeNode(org.jabref.model.groups.GroupTreeNode) Map(java.util.Map) MetaData(org.jabref.model.metadata.MetaData) Collections(java.util.Collections) HashMap(java.util.HashMap) ContentSelector(org.jabref.model.metadata.ContentSelector) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 2 with ContentSelector

use of org.jabref.model.metadata.ContentSelector in project jabref by JabRef.

the class ContentSelectorDialog method applyChanges.

private void applyChanges() {
    // Watch if we need to rebuild entry editors
    boolean changedFieldSet = false;
    // Watch if we should mark as there is data changed
    boolean anythingChanged = false;
    // cause any harm to remove them here.
    for (String fieldName : removedFields) {
        metaData.clearContentSelectors(fieldName);
        changedFieldSet = true;
        anythingChanged = true;
    }
    // Cycle through all fields that we have created listmodels for:
    for (Map.Entry<String, DefaultListModel<String>> entry : wordListModels.entrySet()) {
        // For each field name, store the values:
        if ((entry.getKey() == null) || FIELD_FIRST_LINE.equals(entry.getKey())) {
            continue;
        }
        DefaultListModel<String> lm = entry.getValue();
        int start = 0;
        // Avoid storing the <new word> marker if it is there:
        if (!lm.isEmpty()) {
            while ((start < lm.size()) && lm.get(start).equals(WORD_FIRSTLINE_TEXT)) {
                start++;
            }
        }
        Set<String> data = new HashSet<>();
        for (int wrd = start; wrd < lm.size(); wrd++) {
            String word = lm.get(wrd);
            data.add(word);
        }
        // Check if any words have been added
        if (!data.equals(new HashSet<>(metaData.getContentSelectorValuesForField(entry.getKey())))) {
            anythingChanged = true;
        }
        // Check if there are words to be added and previously there were no content selector for the field
        if (!data.isEmpty() && metaData.getContentSelectorValuesForField(entry.getKey()).isEmpty()) {
            changedFieldSet = true;
        }
        metaData.addContentSelector(new ContentSelector(entry.getKey(), new ArrayList<>(data)));
    }
    // Update all selectors in the current BasePanel.
    if (changedFieldSet) {
        // We have added or removed content selectors, update the entry editor
        panel.rebuildAllEntryEditors();
    } else if (anythingChanged) {
        // Enough to update the content selectors, if anything changed
        panel.updateAllContentSelectors();
    }
    if (anythingChanged) {
        // Mark the database updated so changes are not lost
        panel.markNonUndoableBaseChanged();
    }
    panel.getAutoCompleters().addContentSelectorValuesToAutoCompleters(panel.getBibDatabaseContext().getMetaData());
}
Also used : ContentSelector(org.jabref.model.metadata.ContentSelector) ArrayList(java.util.ArrayList) DefaultListModel(javax.swing.DefaultListModel) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 3 with ContentSelector

use of org.jabref.model.metadata.ContentSelector in project jabref by JabRef.

the class AppendDatabaseAction method mergeFromBibtex.

private static void mergeFromBibtex(BasePanel panel, ParserResult parserResult, boolean importEntries, boolean importStrings, boolean importGroups, boolean importSelectorWords) throws KeyCollisionException {
    BibDatabase fromDatabase = parserResult.getDatabase();
    List<BibEntry> appendedEntries = new ArrayList<>();
    List<BibEntry> originalEntries = new ArrayList<>();
    BibDatabase database = panel.getDatabase();
    NamedCompound ce = new NamedCompound(Localization.lang("Append library"));
    MetaData meta = parserResult.getMetaData();
    if (importEntries) {
        // Add entries
        boolean overwriteOwner = Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_OWNER);
        boolean overwriteTimeStamp = Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_TIME_STAMP);
        for (BibEntry originalEntry : fromDatabase.getEntries()) {
            BibEntry entry = (BibEntry) originalEntry.clone();
            UpdateField.setAutomaticFields(entry, overwriteOwner, overwriteTimeStamp, Globals.prefs.getUpdateFieldPreferences());
            database.insertEntry(entry);
            appendedEntries.add(entry);
            originalEntries.add(originalEntry);
            ce.addEdit(new UndoableInsertEntry(database, entry, panel));
        }
    }
    if (importStrings) {
        for (BibtexString bs : fromDatabase.getStringValues()) {
            if (!database.hasStringLabel(bs.getName())) {
                database.addString(bs);
                ce.addEdit(new UndoableInsertString(panel, database, bs));
            }
        }
    }
    if (importGroups) {
        meta.getGroups().ifPresent(newGroups -> {
            if (newGroups.getGroup() instanceof AllEntriesGroup) {
                try {
                    ExplicitGroup group = new ExplicitGroup("Imported", GroupHierarchyType.INDEPENDENT, Globals.prefs.getKeywordDelimiter());
                    newGroups.setGroup(group);
                    group.add(appendedEntries);
                } catch (IllegalArgumentException e) {
                    LOGGER.error(e);
                }
            }
            addGroups(newGroups, ce);
        });
    }
    if (importSelectorWords) {
        for (ContentSelector selector : meta.getContentSelectorList()) {
            panel.getBibDatabaseContext().getMetaData().addContentSelector(selector);
        }
    }
    ce.end();
    panel.getUndoManager().addEdit(ce);
    panel.markBaseChanged();
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) ArrayList(java.util.ArrayList) BibtexString(org.jabref.model.entry.BibtexString) UndoableInsertEntry(org.jabref.gui.undo.UndoableInsertEntry) ExplicitGroup(org.jabref.model.groups.ExplicitGroup) AllEntriesGroup(org.jabref.model.groups.AllEntriesGroup) UndoableInsertString(org.jabref.gui.undo.UndoableInsertString) NamedCompound(org.jabref.gui.undo.NamedCompound) MetaData(org.jabref.model.metadata.MetaData) ContentSelector(org.jabref.model.metadata.ContentSelector) BibDatabase(org.jabref.model.database.BibDatabase)

Example 4 with ContentSelector

use of org.jabref.model.metadata.ContentSelector in project jabref by JabRef.

the class MetaDataSerializerTest method serializeSingleContentSelectors.

@Test
public void serializeSingleContentSelectors() {
    List<String> values = new ArrayList(4);
    values.add("approved");
    values.add("captured");
    values.add("received");
    values.add("status");
    metaData.addContentSelector(new ContentSelector("status", values));
    Map<String, String> expectedSerialization = new TreeMap<>();
    expectedSerialization.put("selector_status", "approved;captured;received;status;");
    assertEquals(expectedSerialization, MetaDataSerializer.getSerializedStringMap(metaData, pattern));
}
Also used : ContentSelector(org.jabref.model.metadata.ContentSelector) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)4 ContentSelector (org.jabref.model.metadata.ContentSelector)4 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 MetaData (org.jabref.model.metadata.MetaData)2 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 DefaultListModel (javax.swing.DefaultListModel)1 NamedCompound (org.jabref.gui.undo.NamedCompound)1 UndoableInsertEntry (org.jabref.gui.undo.UndoableInsertEntry)1 UndoableInsertString (org.jabref.gui.undo.UndoableInsertString)1 OS (org.jabref.logic.util.OS)1 AbstractBibtexKeyPattern (org.jabref.model.bibtexkeypattern.AbstractBibtexKeyPattern)1 GlobalBibtexKeyPattern (org.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern)1 FieldFormatterCleanups (org.jabref.model.cleanup.FieldFormatterCleanups)1 BibDatabase (org.jabref.model.database.BibDatabase)1 BibEntry (org.jabref.model.entry.BibEntry)1 BibtexString (org.jabref.model.entry.BibtexString)1