Merge branch 'release/5.11.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / PreferencesUtil.java
index b0b9d53478f56290df01296ca22f5345ab2df214..ad4eb54f00395984314e128927f41d916159545c 100644 (file)
@@ -13,6 +13,7 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.lang.reflect.Field;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.ArrayList;
@@ -239,6 +240,18 @@ public class PreferencesUtil implements IPreferenceKeys {
                 prefValue = getPreferenceStore().
                         getString(name);
             }
+            if (StringUtils.isBlank(prefValue) || (pref == null && !override)){
+                IPreferencePredicate pred = PreferencePredicate.getByKey(name);
+                if (pred != null){
+                    if (pred.getDefaultValue() instanceof String){
+                        return (String)pred.getDefaultValue();
+                    }else if (pred.getDefaultValue() != null){
+                        return pred.getDefaultValue().toString();
+                    }
+                }
+                return prefValue;
+            }
+
        }else if (pref != null){
            if (pref.getValue() == null){
                Object result = PreferencePredicate.getByKey(pref.getPredicate()).getDefaultValue();
@@ -307,8 +320,17 @@ public class PreferencesUtil implements IPreferenceKeys {
             if (getPreferenceStore().contains(dbSpecific)){
                 result = getPreferenceStore().getInt(dbSpecific);
             }else{
-                result =  getPreferenceStore().
-                        getInt(name);
+                if (result == null){
+                    IPreferencePredicate pred = PreferencePredicate.getByKey(name);
+                    if (pred != null){
+                        if (pred.getDefaultValue() instanceof Integer){
+                            return (Integer)pred.getDefaultValue();
+                        }else if (pred.getDefaultValue() != null){
+                            return Integer.valueOf(pred.getDefaultValue().toString());
+                        }
+                    }
+                    return 0;
+                }
             }
         }
         return result;
@@ -337,6 +359,14 @@ public class PreferencesUtil implements IPreferenceKeys {
                 if (getPreferenceStore().contains(dbSpecific)){
                     return getPreferenceStore().getBoolean(dbSpecific);
                 }else{
+                    IPreferencePredicate pred = PreferencePredicate.getByKey(name);
+                    if (pred != null){
+                        if (pred.getDefaultValue() instanceof Boolean){
+                            return (Boolean)pred.getDefaultValue();
+                        }else if (pred.getDefaultValue() != null){
+                            return Boolean.valueOf(pred.getDefaultValue().toString());
+                        }
+                    }
                     return null;
                 }
              }else{
@@ -413,8 +443,15 @@ public class PreferencesUtil implements IPreferenceKeys {
             if (getPreferenceStore().contains(dbSpecific)){
                 result = getPreferenceStore().getFloat(dbSpecific);
             }else{
-                result =  getPreferenceStore().
-                        getFloat(name);
+                IPreferencePredicate pred = PreferencePredicate.getByKey(name);
+                if (pred != null){
+                    if (pred.getDefaultValue() instanceof Float){
+                        return (float)pred.getDefaultValue();
+                    }else if (pred.getDefaultValue() != null){
+                        return Float.valueOf(pred.getDefaultValue().toString());
+                    }
+                }
+                return 0;
             }
         }
         return result;
@@ -921,7 +958,7 @@ public class PreferencesUtil implements IPreferenceKeys {
 
 
         //Name Details
-        getPreferenceStore().setDefault(createPreferenceString(PreferencePredicate.NameDetailsView.getKey()), new NameDetailsConfigurator(false).toString());
+       // getPreferenceStore().setDefault(createPreferenceString(PreferencePredicate.NameDetailsView.getKey()), new NameDetailsConfigurator(false).toString());
 
         //Navigator preferences
         getPreferenceStore().setDefault(createPreferenceString(PreferencePredicate.TaxonNodeOrder.getKey()), NavigatorOrderEnum.RankAndNameOrder.getKey());
@@ -946,6 +983,9 @@ public class PreferencesUtil implements IPreferenceKeys {
         getPreferenceStore().setDefault(createPreferenceString(prefOverrideKey(PreferencePredicate.SearchForIdentifierAndTitleCache.getKey())), false);
         getPreferenceStore().setDefault(createPreferenceString(PreferencePredicate.SearchForIdentifierAsDefault.getKey()), Boolean.valueOf(PreferencePredicate.SearchForIdentifierAsDefault.getDefaultValue().toString()));
         getPreferenceStore().setDefault(createPreferenceString(prefOverrideKey(PreferencePredicate.SearchForIdentifierAsDefault.getKey())), false);
+        getPreferenceStore().setDefault(createPreferenceString(prefOverrideKey(PreferencePredicate.ShowModifierFreeText.getKey())), false);
+        getPreferenceStore().setDefault(createPreferenceString(prefOverrideKey(PreferencePredicate.ShowModifier.getKey())), false);
+        getPreferenceStore().setDefault(createPreferenceString(prefOverrideKey(PreferencePredicate.DistributionEditorActivated.getKey())), false);
 
     }
 
@@ -1361,7 +1401,7 @@ public class PreferencesUtil implements IPreferenceKeys {
     }
 
     public static NameDetailsConfigurator getPreferredNameDetailsConfiguration( boolean local) {
-        NameDetailsConfigurator config = new NameDetailsConfigurator(true);
+        NameDetailsConfigurator config = new NameDetailsConfigurator();
         CdmPreferenceCache cache = CdmPreferenceCache.instance();
         CdmPreference preference = null;
         String value;
@@ -1381,83 +1421,45 @@ public class PreferencesUtil implements IPreferenceKeys {
             value = getStringValue(PreferencePredicate.NameDetailsView.getKey(), local);
         }
         if (value!= null){
-            String [] sections = value.split(";");
-            Map<String, Boolean> sectionMap = new HashMap<String, Boolean>();
-            String[] sectionValues;
-            for (String sectionValue: sections){
-                sectionValues = sectionValue.split(":");
-                sectionMap.put(sectionValues[0], Boolean.valueOf(sectionValues[1]));
-            }
-            config.setSimpleDetailsViewActivated(getValue(sectionMap, "simpleViewActivated"));
-            config.setTaxonSectionActivated(getValue(sectionMap, "taxon"));
-            config.setSecDetailsActivated(getValue(sectionMap, "taxon.SecDetails"));
-            config.setSecEnabled(getValue(sectionMap, "taxon.SecEnabled"));
-            config.setLSIDActivated(getValue(sectionMap, "lsid"));
-            config.setNomenclaturalCodeActived(getValue(sectionMap, "nc"));
-            config.setAppendedPhraseActivated(getValue(sectionMap, "ap"));
-            config.setRankActivated(getValue(sectionMap, "rank"));
-            config.setNameCacheActivated(getValue(sectionMap, "nameCache"));
-            config.setAtomisedEpithetsActivated(getValue(sectionMap, "atomisedEpithets"));
-            config.setAuthorshipSectionActivated(getValue(sectionMap,"author"));
-            config.setAuthorCacheActivated(getValue(sectionMap, "authorCache"));
-            config.setNomenclaturalReferenceSectionActivated(sectionMap.get("nomRef"));
-            config.setNomenclaturalStatusSectionActivated(getValue(sectionMap, "nomStat"));
-            config.setProtologueActivated(getValue(sectionMap,"protologue"));
-            config.setTypeDesignationSectionActivated(getValue(sectionMap,"typeDes"));
-            config.setNameRelationsSectionActivated(getValue(sectionMap,"nameRelation"));
-            config.setHybridActivated(getValue(sectionMap,"hybrid"));
-            config.setNameApprobiationActivated(getValue(sectionMap, "nameApproviation"));
+            fillNameDetailsConfigurator(config, value);
+
+
         }else {
             return null;
         }
         return config;
     }
 
-    public static NameDetailsConfigurator getPreferredNameDetailsConfiguration() {
-        NameDetailsConfigurator config = new NameDetailsConfigurator(true);
-
-        String value;
-
-        value = getStringValue(PreferencePredicate.NameDetailsView.getKey(), false);
-        if (value != null){
-            String [] sections = value.split(";");
-            Map<String, Boolean> sectionMap = new HashMap<String, Boolean>();
-            String[] sectionValues;
-            for (String sectionValue: sections){
+    public static void fillNameDetailsConfigurator(NameDetailsConfigurator config, String value) {
+        String [] sections = value.split(";");
+        Map<String, Boolean> sectionMap = new HashMap<>();
+        String[] sectionValues;
+        for (String sectionValue: sections){
+            if (sectionValue.contains(":")){
                 sectionValues = sectionValue.split(":");
                 sectionMap.put(sectionValues[0], Boolean.valueOf(sectionValues[1]));
             }
+        }
+        for (Field field: config.getClass().getDeclaredFields()){
+            try {
+                config.getClass().getDeclaredField(field.getName()).set(config, getValue(sectionMap, field.getName()));
+            } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException
+                    | SecurityException e) {
+               logger.debug(e.getMessage());
+            }
+        }
+    }
 
-            config.setSimpleDetailsViewActivated(getValue(sectionMap, "simpleViewActivated"));
-
-            config.setTaxonSectionActivated(getValue(sectionMap, "taxon"));
-
-            config.setSecDetailsActivated(getValue(sectionMap, "taxon.SecDetails"));
-            config.setSecEnabled(getValue(sectionMap, "taxon.SecEnabled"));
-
-            config.setLSIDActivated(getValue(sectionMap, "lsid"));
-
-            config.setNomenclaturalCodeActived(getValue(sectionMap, "nc"));
-
-            config.setAppendedPhraseActivated(getValue(sectionMap, "ap"));
-
-            config.setRankActivated(getValue(sectionMap, "rank"));
-
-            config.setAtomisedEpithetsActivated(getValue(sectionMap, "atomisedEpithets"));
-
-            config.setAuthorshipSectionActivated(getValue(sectionMap,"author"));
-
-            config.setNomenclaturalReferenceSectionActivated(sectionMap.get("nomRef"));
-
-            config.setNomenclaturalStatusSectionActivated(getValue(sectionMap, "nomStat"));
-
-            config.setProtologueActivated(getValue(sectionMap,"protologue"));
-
-            config.setTypeDesignationSectionActivated(getValue(sectionMap,"typeDes"));
+    public static NameDetailsConfigurator getPreferredNameDetailsConfiguration() {
+        NameDetailsConfigurator config = new NameDetailsConfigurator();
 
-            config.setNameRelationsSectionActivated(getValue(sectionMap,"nameRelation"));
+        String value;
 
-                config.setHybridActivated(getValue(sectionMap,"hybrid"));
+        value = getStringValue(PreferencePredicate.NameDetailsView.getKey(), false);
+        if (value != null){
+            fillNameDetailsConfigurator(config, value);
+        }else {
+            return null;
         }
         return config;
     }
@@ -1579,16 +1581,21 @@ public class PreferencesUtil implements IPreferenceKeys {
     }
     public static Abcd206ImportConfigurator getLocalAbcdImportConfigurator(boolean skipCheckOverride){
        Abcd206ImportConfigurator config = Abcd206ImportConfigurator.NewInstance(null,null);
+       CdmPreference pref = CdmPreferenceCache.instance().get(PreferencePredicate.AbcdImportConfig.getKey());
+       if (pref  == null || pref.isAllowOverride()){
        String configString = PreferencesUtil.getStringValue(IPreferenceKeys.LAST_USED_ABCD_CONFIG);
-       if (StringUtils.isBlank(configString)){
-           configString = getStringValue(PreferencePredicate.AbcdImportConfig.getKey(), skipCheckOverride);
-           extractAbcdConfiguratorFromPreferenceString(config, configString);
-           if (config.getNomenclaturalCode() == null){
-               config.setNomenclaturalCode(getPreferredNomenclaturalCode());
+           if (StringUtils.isBlank(configString)){
+               configString = getStringValue(PreferencePredicate.AbcdImportConfig.getKey(), skipCheckOverride);
+               extractAbcdConfiguratorFromPreferenceString(config, configString);
+               if (config.getNomenclaturalCode() == null){
+                   config.setNomenclaturalCode(getPreferredNomenclaturalCode());
+               }
+           }else{
+               config = Abcd206ImportConfigurator.NewInstance(null, null);
+               PreferencesUtil.extractAbcdConfiguratorFromPreferenceString(config, configString);
            }
        }else{
-           config = Abcd206ImportConfigurator.NewInstance(null, null);
-           PreferencesUtil.extractAbcdConfiguratorFromPreferenceString(config, configString);
+           extractAbcdConfiguratorFromPreferenceString(config, pref.getValue());
        }
 
        return config;