Skip to content

Commit

Permalink
Merge pull request JabRef#11110 from JabRef/contentSelectorDefautlSaving
Browse files Browse the repository at this point in the history
Fix exception when saving content selectors
  • Loading branch information
Siedlerchr authored Apr 1, 2024
2 parents 84ee7e5 + 5156e18 commit db084ab
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 51 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We fixed an issue with handling of an "overflow" of authors at `[authIniN]`. [#11087](https://github.com/JabRef/jabref/issues/11087)
- We fixed an issue where an exception occurred when selecting entries in the web search results. [#11081](https://github.com/JabRef/jabref/issues/11081)
- When a new library is unsaved, there is now no warning when fetching entries with PDFs. [#11075](https://github.com/JabRef/jabref/issues/11075)
- We fixed an issue where the message "The libary has been modified by another program" occurred when editing library metadata and saving the library. [#4877](https://github.com/JabRef/jabref/issues/4877)

### Removed

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package org.jabref.gui.libraryproperties.contentselectors;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
Expand All @@ -34,10 +31,13 @@

public class ContentSelectorViewModel implements PropertiesTabViewModel {

private static final List<Field> DEFAULT_FIELD_NAMES = Arrays.asList(StandardField.AUTHOR, StandardField.JOURNAL, StandardField.KEYWORDS, StandardField.PUBLISHER);
private static final List<Field> DEFAULT_FIELD_NAMES = List.of(StandardField.AUTHOR, StandardField.JOURNAL, StandardField.KEYWORDS, StandardField.PUBLISHER);

private final MetaData metaData;

private final DialogService dialogService;

// The map from each field to its predefined strings ("keywords") that can be selected
private final Map<Field, List<String>> fieldKeywordsMap = new HashMap<>();

private final ListProperty<Field> fields = new SimpleListProperty<>(FXCollections.observableArrayList());
Expand Down Expand Up @@ -69,20 +69,22 @@ public void setValues() {
@Override
public void storeSettings() {
List<Field> metaDataFields = metaData.getContentSelectors().getFieldsWithSelectors();
List<Field> fieldNamesToRemove;

if (isDefaultMap(fieldKeywordsMap)) {
Iterator<ContentSelector> iterator = metaData.getContentSelectors().getContentSelectors().iterator();
while (iterator.hasNext()) {
metaData.clearContentSelectors(iterator.next().getField());
}
// Remove all fields of the content selector
fieldNamesToRemove = metaData.getContentSelectorsSorted().stream().map(ContentSelector::getField).toList();
} else {
fieldNamesToRemove = determineFieldsToRemove();
}

fieldKeywordsMap.forEach((field, keywords) -> updateMetaDataContentSelector(metaDataFields, field, keywords));

List<Field> fieldNamesToRemove = filterFieldsToRemove();
fieldNamesToRemove.forEach(metaData::clearContentSelectors);
}

/**
* Checks whether the given map is the default map, i.e. contains only the default field names and no associated keywords.
*/
private boolean isDefaultMap(Map<Field, List<String>> fieldKeywordsMap) {
if (fieldKeywordsMap.size() != DEFAULT_FIELD_NAMES.size()) {
return false;
Expand Down Expand Up @@ -205,11 +207,28 @@ private void removeKeyword(Field field, String keywordToRemove) {
keywords.remove(keywordToRemove);
}

private List<Field> filterFieldsToRemove() {
/**
* Determines the list of fields to remove in case a non-default map:
*
* <ul>
* <li>Fields that are not in the new list of fields and</li>
* <li>>all default fields that have no associated keywords</li>
* </ul>
*/
private List<Field> determineFieldsToRemove() {
Set<Field> newlyAddedKeywords = fieldKeywordsMap.keySet();
return metaData.getContentSelectors().getFieldsWithSelectors().stream()
.filter(field -> !newlyAddedKeywords.contains(field))
.collect(Collectors.toList());

// Remove all content selectors that are not in the new list
List<Field> result = new ArrayList(metaData.getContentSelectors().getFieldsWithSelectors().stream()
.filter(field -> !newlyAddedKeywords.contains(field))
.toList());
// Remove all unset default fields
result.addAll(fieldKeywordsMap.entrySet()
.stream()
.filter(entry -> DEFAULT_FIELD_NAMES.contains(entry.getKey()) && entry.getValue().isEmpty()).map(Map.Entry::getKey)
.toList());

return result;
}

private void updateMetaDataContentSelector(List<Field> existingFields, Field field, List<String> keywords) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private void mergeGroups(MetaData target, MetaData other, String otherFilename,
}

private void mergeContentSelectors(MetaData target, MetaData other) {
for (ContentSelector selector : other.getContentSelectorList()) {
for (ContentSelector selector : other.getContentSelectorsSorted()) {
target.addContentSelector(selector);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static Map<String, String> getSerializedStringMap(MetaData metaData,
metaData.getVersionDBStructure().ifPresent(
versionDBStructure -> stringyMetaData.put(MetaData.VERSION_DB_STRUCT, Collections.singletonList(versionDBStructure.trim())));

for (ContentSelector selector : metaData.getContentSelectorList()) {
for (ContentSelector selector : metaData.getContentSelectorsSorted()) {
stringyMetaData.put(MetaData.SELECTOR_META_PREFIX + selector.getField().getName(), selector.getValues());
}

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/jabref/model/entry/field/AMSField.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Optional;
import java.util.Set;

public enum AMSField implements Field {

FJOURNAL("fjournal");

private final String name;
private final String displayName;
private final Set<FieldProperty> properties;
private final EnumSet<FieldProperty> properties;

AMSField(String name) {
this.name = name;
Expand Down Expand Up @@ -44,7 +43,7 @@ public static <T> Optional<AMSField> fromName(T type, String name) {
}

@Override
public Set<FieldProperty> getProperties() {
public EnumSet<FieldProperty> getProperties() {
return properties;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Optional;
import java.util.Set;

import org.jabref.model.entry.types.BiblatexApaEntryType;

Expand All @@ -20,7 +19,7 @@ public enum BiblatexApaField implements Field {

private final String name;
private final String displayName;
private final Set<FieldProperty> properties;
private final EnumSet<FieldProperty> properties;

BiblatexApaField(String name) {
this.name = name;
Expand Down Expand Up @@ -58,7 +57,7 @@ public static <T> Optional<BiblatexApaField> fromName(T type, String name) {
}

@Override
public Set<FieldProperty> getProperties() {
public EnumSet<FieldProperty> getProperties() {
return properties;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Optional;
import java.util.Set;

import org.jabref.model.entry.types.BiblatexSoftwareEntryType;

Expand All @@ -20,7 +19,7 @@ public enum BiblatexSoftwareField implements Field {

private final String name;
private final String displayName;
private final Set<FieldProperty> properties;
private final EnumSet<FieldProperty> properties;

BiblatexSoftwareField(String name) {
this.name = name;
Expand Down Expand Up @@ -56,7 +55,7 @@ public static <T> Optional<BiblatexSoftwareField> fromName(T type, String name)
}

@Override
public Set<FieldProperty> getProperties() {
public EnumSet<FieldProperty> getProperties() {
return properties;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/model/entry/field/Field.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jabref.model.entry.field;

import java.util.EnumSet;
import java.util.Optional;
import java.util.Set;

import org.jabref.model.strings.StringUtil;

Expand All @@ -10,10 +10,10 @@ public interface Field {
/**
* properties contains mappings to tell the EntryEditor to add a specific function to this field,
* for instance a dropdown for selecting the month for the month field.
*
* <p>
* Note that this set needs to be mutable. This is required, because we allow standard fields to be modifiable via the UI.
*/
Set<FieldProperty> getProperties();
EnumSet<FieldProperty> getProperties();

/**
* @return A version of the field name more suitable for display
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/jabref/model/entry/field/IEEEField.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Optional;
import java.util.Set;

/**
* IEEE BSTctl fields
Expand All @@ -23,7 +22,7 @@ public enum IEEEField implements Field {
CTLUSE_URL("ctluse_url", FieldProperty.YES_NO);

private final String name;
private final Set<FieldProperty> properties;
private final EnumSet<FieldProperty> properties;

IEEEField(String name, FieldProperty first, FieldProperty... rest) {
this.name = name;
Expand All @@ -47,7 +46,7 @@ public boolean isStandardField() {
}

@Override
public Set<FieldProperty> getProperties() {
public EnumSet<FieldProperty> getProperties() {
return properties;
}
}
5 changes: 2 additions & 3 deletions src/main/java/org/jabref/model/entry/field/InternalField.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Optional;
import java.util.Set;

/**
* JabRef internal fields. These are not normal fields but mostly placeholders with special functions.
Expand Down Expand Up @@ -52,7 +51,7 @@ public enum InternalField implements Field {
INTERNAL_ID_FIELD("JabRef-internal-id");

private final String name;
private final Set<FieldProperty> properties;
private final EnumSet<FieldProperty> properties;

InternalField(String name) {
this.name = name;
Expand All @@ -76,7 +75,7 @@ public static Optional<InternalField> fromName(String name) {
}

@Override
public Set<FieldProperty> getProperties() {
public EnumSet<FieldProperty> getProperties() {
return properties;
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/jabref/model/entry/field/SpecialField.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.jabref.model.entry.KeywordList;

Expand Down Expand Up @@ -86,7 +85,7 @@ public Optional<SpecialFieldValue> parseValue(String value) {
}

@Override
public Set<FieldProperty> getProperties() {
public EnumSet<FieldProperty> getProperties() {
return EnumSet.noneOf(FieldProperty.class);
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/model/entry/field/StandardField.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public enum StandardField implements Field {

private final String name;
private final String displayName;
private final Set<FieldProperty> properties;
private final EnumSet<FieldProperty> properties;

static {
for (StandardField field : StandardField.values()) {
Expand Down Expand Up @@ -184,7 +184,7 @@ public static Optional<StandardField> fromName(String name) {
}

@Override
public Set<FieldProperty> getProperties() {
public EnumSet<FieldProperty> getProperties() {
return properties;
}

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/jabref/model/entry/field/UnknownField.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import java.util.EnumSet;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;

import org.jabref.model.strings.StringUtil;

public class UnknownField implements Field {
private String name;
private final Set<FieldProperty> properties;
private final EnumSet<FieldProperty> properties;
private final String displayName;

public UnknownField(String name) {
Expand Down Expand Up @@ -37,7 +36,7 @@ public static UnknownField fromDisplayName(String displayName) {
}

@Override
public Set<FieldProperty> getProperties() {
public EnumSet<FieldProperty> getProperties() {
return properties;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

import java.util.EnumSet;
import java.util.Objects;
import java.util.Set;

public class UserSpecificCommentField implements Field {
private static final Set<FieldProperty> PROPERTIES = EnumSet.of(FieldProperty.COMMENT, FieldProperty.MULTILINE_TEXT, FieldProperty.VERBATIM, FieldProperty.MARKDOWN);
private static final EnumSet<FieldProperty> PROPERTIES = EnumSet.of(FieldProperty.COMMENT, FieldProperty.MULTILINE_TEXT, FieldProperty.VERBATIM, FieldProperty.MARKDOWN);
private final String name;

public UserSpecificCommentField(String username) {
this.name = "comment-" + username;
}

@Override
public Set<FieldProperty> getProperties() {
public EnumSet<FieldProperty> getProperties() {
return PROPERTIES;
}

Expand Down
Loading

0 comments on commit db084ab

Please sign in to comment.