ref #5971: add an admin page to the prefs and add cdm pref nomenclatural code
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / PreferencesUtil.java
index 0dd68af2e2d410f485d2e20b4e36bff430864f8a..f4f32c9b217c4c1405f8e058b4ba09c6aa05791d 100644 (file)
@@ -37,7 +37,6 @@ import org.eclipse.ui.PlatformUI;
 import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeConfigurator;
 import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
-import eu.etaxonomy.cdm.api.service.IPreferenceService;
 import eu.etaxonomy.cdm.api.service.ITermService;
 import eu.etaxonomy.cdm.api.service.config.FindTaxaAndNamesConfiguratorImpl;
 import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
@@ -108,38 +107,50 @@ public class PreferencesUtil implements IPreferenceKeys {
         *            a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode}
         *            object.
         */
-       public static void setPreferredNomenclaturalCode(
-                       NomenclaturalCode preferredCode) {
-               ICdmApplicationConfiguration controller;
-               controller = CdmStore.getCurrentApplicationConfiguration();
-               PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
-               CdmPreference preference = null;
-               if (controller == null){
-                       return;
-               }
-               if (preferredCode == null){
-                       preference = controller.getPreferenceService().find(key);
-                       if (preference == null){
-                               return;
-                       } else{
-                               int index = StringUtils.lastIndexOf(preference.getValue(), ".");
-                               UUID uuid = UUID.fromString(preference.getValue().substring(index +1, preference.getValue().length()));
-                               preferredCode = NomenclaturalCode.getByUuid(uuid);
-                       }
-               } else{
-                       preference = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode, preferredCode.getKey());
-                       controller.getPreferenceService().set(preference);
-               }
-                
-           
-               getPreferenceStore().setValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
-                               getPreferenceKey(preferredCode));
+       public static NomenclaturalCode setPreferredNomenclaturalCode(
+                       NomenclaturalCode preferredCode, boolean local) {
+           if (local){
+               getPreferenceStore().setValue(PREFERRED_NOMENCLATURAL_CODE_KEY,
+                    getPreferenceKey(preferredCode));
+           }
+           else{
+               ICdmApplicationConfiguration controller;
+               controller = CdmStore.getCurrentApplicationConfiguration();
+               PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
+               CdmPreference preference = null;
+               if (controller == null){
+                       return null;
+               }
+               if (preferredCode == null){
+                       preference = controller.getPreferenceService().find(key);
+                       if (preference == null){
+                               return null;
+                       } else{
+                               int index = StringUtils.lastIndexOf(preference.getValue(), ".");
+                               UUID uuid = UUID.fromString(preference.getValue().substring(index +1, preference.getValue().length()));
+                               preferredCode = NomenclaturalCode.getByUuid(uuid);
+                               return preferredCode;
+                       }
+               } else{
+                       preference = CdmPreference.NewInstance(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode, preferredCode.getKey());
+                       controller.getPreferenceService().set(preference);
+
+               }
+           }
+        return preferredCode;
+
+
+
        }
 
        public static NomenclaturalCode getPreferredNomenclaturalCode(){
-               return getPreferredNomenclaturalCode(false);
+           if (getPreferenceStore().getBoolean(OVERRIDE_NOMENCLATURAL_CODE_KEY)) {
+            return getPreferredNomenclaturalCode(true);
+        } else{
+            return getPreferredNomenclaturalCode(false);
+        }
        }
-       
+
        /**
         * <p>
         * getPreferredNomenclaturalCode
@@ -147,10 +158,10 @@ public class PreferencesUtil implements IPreferenceKeys {
         *
         * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
         */
-       public static NomenclaturalCode getPreferredNomenclaturalCode(boolean preConnected) {
+       public static NomenclaturalCode getPreferredNomenclaturalCode(boolean useLocal) {
                ICdmApplicationConfiguration controller;
                CdmPreference pref = null;
-               if (!preConnected){
+               if (!useLocal){
                        try{
                                controller = CdmStore.getCurrentApplicationConfiguration();
                                PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
@@ -159,7 +170,7 @@ public class PreferencesUtil implements IPreferenceKeys {
                                e.printStackTrace();
                        }
                }
-               
+
            String preferredCode;
            if(pref == null){
                preferredCode = getPreferenceStore().getString(
@@ -167,10 +178,8 @@ public class PreferencesUtil implements IPreferenceKeys {
            }else{
                preferredCode = pref.getValue();
            }
-           
+
                for (NomenclaturalCode code : NomenclaturalCodeHelper.getAllCodes()) {
-//                     String preferredCode = getPreferenceStore().getString(
-//                                     PREFERRED_NOMENCLATURAL_CODE_KEY);
                        if (getPreferenceKey(code).equals(preferredCode)) {
                                return code;
                        }
@@ -475,7 +484,7 @@ public class PreferencesUtil implements IPreferenceKeys {
 
 
                if (PreferencesUtil.getPreferredNomenclaturalCode(true) == null) {
-                       PreferencesUtil.setPreferredNomenclaturalCode(NomenclaturalCode.ICNAFP);
+                       PreferencesUtil.setPreferredNomenclaturalCode(NomenclaturalCode.ICNAFP, true);
                        /*
 
                        StoreUtil.info("No nomencatural code set.");