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