refactored default language selection methods.
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / PreferencesUtil.java
index 9a8c1668f7bc4587416447e2c5753c1b6371b1c2..a2df5b14019443544e4af0cbefa9d2bab40f0abf 100644 (file)
@@ -1,25 +1,30 @@
 /**
  * Copyright (C) 2007 EDIT
- * European Distributed Institute of Taxonomy 
+ * European Distributed Institute of Taxonomy
  * http://www.e-taxonomy.eu
- * 
+ *
  * The contents of this file are subject to the Mozilla Public License Version 1.1
  * See LICENSE.TXT at the top of this package for the full license terms.
  */
 
 package eu.etaxonomy.taxeditor.preference;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 import java.util.Set;
 import java.util.UUID;
 
-import org.eclipse.jface.dialogs.Dialog;
-import org.eclipse.jface.dialogs.MessageDialog;
+import org.apache.commons.lang.StringUtils;
 import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.PlatformUI;
 
 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeConfigurator;
 import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
@@ -30,6 +35,7 @@ import eu.etaxonomy.cdm.common.CdmUtils;
 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.model.common.ICdmBase;
 import eu.etaxonomy.cdm.model.common.IDefinedTerm;
+import eu.etaxonomy.cdm.model.common.ISimpleTerm;
 import eu.etaxonomy.cdm.model.common.Language;
 import eu.etaxonomy.cdm.model.common.MarkerType;
 import eu.etaxonomy.cdm.model.common.TermBase;
@@ -39,16 +45,18 @@ import eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy;
 import eu.etaxonomy.cdm.strategy.match.IMatchStrategy;
 import eu.etaxonomy.cdm.strategy.match.MatchException;
 import eu.etaxonomy.cdm.strategy.match.MatchMode;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.model.NomenclaturalCodeHelper;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 import eu.etaxonomy.taxeditor.store.StoreUtil;
 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
+import eu.etaxonomy.taxeditor.ui.dialog.DefaultLanguageDialog;
 
 /**
  * <p>
  * PreferencesUtil class.
  * </p>
- * 
+ *
  * @author p.ciardelli
  * @author n.hoffmann
  * @created 05.12.2008
@@ -57,7 +65,7 @@ import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
 public class PreferencesUtil implements IPreferenceKeys {
 
        /**
-        * 
+        *
         */
        public static final String PREFERRED_TERMS_CHANGE = "preferred_terms";
 
@@ -65,7 +73,7 @@ public class PreferencesUtil implements IPreferenceKeys {
         * <p>
         * getPreferenceStore
         * </p>
-        * 
+        *
         * @return a {@link org.eclipse.jface.preference.IPreferenceStore} object.
         */
        public static IPreferenceStore getPreferenceStore() {
@@ -76,7 +84,7 @@ public class PreferencesUtil implements IPreferenceKeys {
         * <p>
         * setPreferredNomenclaturalCode
         * </p>
-        * 
+        *
         * @param preferredCode
         *            a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode}
         *            object.
@@ -91,7 +99,7 @@ public class PreferencesUtil implements IPreferenceKeys {
         * <p>
         * getPreferredNomenclaturalCode
         * </p>
-        * 
+        *
         * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
         */
        public static NomenclaturalCode getPreferredNomenclaturalCode() {
@@ -106,10 +114,18 @@ public class PreferencesUtil implements IPreferenceKeys {
                return null;
        }
 
+       public static String getPreferredDefaultLangugae(){
+           String preferredLanguage = getPreferenceStore().getString(DEFAULT_LANGUAGE_EDITOR);
+           if(StringUtils.isNotEmpty(preferredLanguage) && StringUtils.isNotBlank(preferredLanguage)){
+               return preferredLanguage;
+           }
+           return null;
+       }
+
        /**
         * Get the match strategy for the given class that was stored in preferences
         * or the default strategy if it was not stored in preferences
-        * 
+        *
         * @param clazz
         *            a {@link java.lang.Class} object.
         * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
@@ -126,7 +142,7 @@ public class PreferencesUtil implements IPreferenceKeys {
                                try {
                                        matchStrategy.setMatchMode(fieldName, matchMode);
                                } catch (MatchException e) {
-                                       StoreUtil.error(PreferencesUtil.class, e);
+                                       MessagingUtils.error(PreferencesUtil.class, e);
                                        throw new RuntimeException(e);
                                }
                        }
@@ -138,7 +154,7 @@ public class PreferencesUtil implements IPreferenceKeys {
 
        /**
         * Stores a matchStrategy into the preference store.
-        * 
+        *
         * @param matchStrategy
         *            a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy}
         *            object.
@@ -158,7 +174,7 @@ public class PreferencesUtil implements IPreferenceKeys {
 
        /**
         * Helper method to create the preference property for a match field.
-        * 
+        *
         * @param className
         * @param fieldName
         * @return
@@ -170,7 +186,7 @@ public class PreferencesUtil implements IPreferenceKeys {
 
        /**
         * Returns the default match strategy for a given class.
-        * 
+        *
         * @param clazz
         *            a {@link java.lang.Class} object.
         * @return a {@link eu.etaxonomy.cdm.strategy.match.IMatchStrategy} object.
@@ -183,7 +199,7 @@ public class PreferencesUtil implements IPreferenceKeys {
         * <p>
         * getDateFormatPattern
         * </p>
-        * 
+        *
         * @return a {@link java.lang.String} object.
         */
        public static String getDateFormatPattern() {
@@ -196,7 +212,7 @@ public class PreferencesUtil implements IPreferenceKeys {
         * <p>
         * addTermToPreferredTerms
         * </p>
-        * 
+        *
         * @param term
         *            a T object.
         * @param <T>
@@ -215,7 +231,7 @@ public class PreferencesUtil implements IPreferenceKeys {
 
        /**
         * Construct a unique key using the CdmBase object's uuid
-        * 
+        *
         * @param cdmBase
         * @return
         */
@@ -225,14 +241,33 @@ public class PreferencesUtil implements IPreferenceKeys {
                String key = cdmBase.getClass().getName().concat(".")
                                .concat(cdmBase.getUuid().toString());
                if (key.contains("javassist")) {
-                       StoreUtil.info("proxy");
+                       MessagingUtils.info("proxy");
+               }
+               return key;
+       }
+
+       /**
+        * Construct a unique key using the CdmBase object's uuid
+        *
+        * @param cdmBase
+        * @return
+        */
+       public static String getPreferenceKey(ISimpleTerm simpleTerm) {
+               simpleTerm = (ISimpleTerm) HibernateProxyHelper.deproxy(simpleTerm);
+               String key = simpleTerm.getClass().getName().concat(".")
+                               .concat(simpleTerm.getUuid().toString());
+               if (key.contains("javassist")) {
+                       MessagingUtils.warn(PreferencesUtil.class,
+                                       "Trying to persist a preference based on a proxy class.");
                }
                return key;
        }
 
+
+
        /**
         * Construct a unique key using the CdmBase object's uuid
-        * 
+        *
         * @param cdmBase
         * @return
         */
@@ -241,7 +276,7 @@ public class PreferencesUtil implements IPreferenceKeys {
                String key = definedTerm.getClass().getName().concat(".")
                                .concat(definedTerm.getUuid().toString());
                if (key.contains("javassist")) {
-                       StoreUtil.warn(PreferencesUtil.class,
+                       MessagingUtils.warn(PreferencesUtil.class,
                                        "Trying to persist a preference based on a proxy class.");
                }
                return key;
@@ -249,7 +284,7 @@ public class PreferencesUtil implements IPreferenceKeys {
 
        /**
         * Retrieves search preferences from the preference store
-        * 
+        *
         * @return an {@link ITaxonServiceConfigurator} to pass to search methods
         */
        public static IFindTaxaAndNamesConfigurator getSearchConfigurator() {
@@ -269,7 +304,7 @@ public class PreferencesUtil implements IPreferenceKeys {
 
        /**
         * create new preferences, setting all search options to true
-        * 
+        *
         * @return a
         *         {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
         *         object.
@@ -292,7 +327,7 @@ public class PreferencesUtil implements IPreferenceKeys {
                // match mode is a simple like, actually all other match modes are kind
                // of bogus
                configurator
-                               .setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.LIKE);
+                               .setMatchMode(eu.etaxonomy.cdm.persistence.query.MatchMode.ANYWHERE);
                // we set page number and size here as this should always be unlimited
                configurator.setPageNumber(0);
                // TODO currently limit results to 10000
@@ -303,7 +338,7 @@ public class PreferencesUtil implements IPreferenceKeys {
 
        /**
         * Store search preferences
-        * 
+        *
         * @param configurator
         *            a
         *            {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator}
@@ -325,7 +360,7 @@ public class PreferencesUtil implements IPreferenceKeys {
         * <p>
         * firePreferencesChanged
         * </p>
-        * 
+        *
         * @param clazz
         *            a {@link java.lang.Class} object.
         */
@@ -342,8 +377,9 @@ public class PreferencesUtil implements IPreferenceKeys {
                getPreferenceStore().setDefault(TAXON_SERVICE_CONFIGURATOR_SYNONYMS,
                                true);
                getPreferenceStore().setDefault(EDIT_MAP_SERVICE_ACCES_POINT,
-                               "http://edit.br.fgov.be/edit_wp5/v1/areas.php");
-               getPreferenceStore().setDefault(SHOULD_CONNECT_AT_STARTUP, true);
+                               "http://edit.africamuseum.be/edit_wp5/v1.2/rest_gen.php");
+               //FIXME : changed default for SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
+               getPreferenceStore().setDefault(SHOULD_CONNECT_AT_STARTUP, false);
                getPreferenceStore().setDefault(OPENURL_ACCESS_POINT,
                                "http://www.biodiversitylibrary.org/openurl");
                getPreferenceStore().setDefault(OPENURL_IMAGE_MAX_WIDTH, "1000");
@@ -357,13 +393,17 @@ public class PreferencesUtil implements IPreferenceKeys {
         */
        public static void checkNomenclaturalCode() {
                // First time Editor is opened, no nomenclatural code has been set
+
+
                if (PreferencesUtil.getPreferredNomenclaturalCode() == null) {
+                       PreferencesUtil.setPreferredNomenclaturalCode(NomenclaturalCode.ICNAFP);
+                       /*
 
                        StoreUtil.info("No nomencatural code set.");
 
                        Shell shell = StoreUtil.getShell();
 
-                       // Query user re: preferred nom. code
+                Query user re: preferred nom. code
                        Dialog dialog = new InitNomenclaturalCodePrefDialog(shell);
                        dialog.open();
 
@@ -377,15 +417,28 @@ public class PreferencesUtil implements IPreferenceKeys {
                                                        "The following has been set as your preferred nomenclatural code:\n\n\t"
                                                                        + NomenclaturalCodeHelper
                                                                                        .getDescription(code)
-                                                                       + "\n\nYou can change the nomenclatural code at any time in the \"Preferences\" menu.");
+                                                                       + "\n\nYou can change the nomenclatural code at any time in the \"Preferences\" menu.");*/
                }
        }
 
+       public static void checkDefaultLanguage(){
+           if(PreferencesUtil.getPreferredDefaultLangugae() == null){
+              Shell shell = StoreUtil.getShell();
+              int open = new DefaultLanguageDialog(shell).open();
+              if(open == 0){//FIXME:window performed ok. Find variable for it
+                  PlatformUI.getWorkbench().restart();
+              }
+           }else{
+               //TODO:In case of a reinstall, the config.ini will be overwritten
+               //     here you create config.ini with the stored key from preferences
+           }
+       }
+
        /**
         * <p>
         * getMapServiceAccessPoint
         * </p>
-        * 
+        *
         * @return a {@link java.lang.String} object.
         */
        public static String getMapServiceAccessPoint() {
@@ -396,18 +449,20 @@ public class PreferencesUtil implements IPreferenceKeys {
         * <p>
         * shouldConnectAtStartUp
         * </p>
-        * 
+        *
         * @return a boolean.
         */
        public static boolean shouldConnectAtStartUp() {
-               return getPreferenceStore().getBoolean(SHOULD_CONNECT_AT_STARTUP);
+               //FIXME :  force SHOULD_CONNECT_AT_STARTUP to false (ticket 3828) until resolution
+               //return getPreferenceStore().getBoolean(SHOULD_CONNECT_AT_STARTUP);
+               return false;
        }
 
        /**
         * <p>
         * getDefaultFeatureTreeForTextualDescription
         * </p>
-        * 
+        *
         * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
         */
        public static FeatureTree getDefaultFeatureTreeForTextualDescription() {
@@ -421,7 +476,7 @@ public class PreferencesUtil implements IPreferenceKeys {
         * <p>
         * getDefaultFeatureTreeForStructuredDescription
         * </p>
-        * 
+        *
         * @return a {@link eu.etaxonomy.cdm.model.description.FeatureTree} object.
         */
        public static FeatureTree getDefaultFeatureTreeForStructuredDescription() {
@@ -435,7 +490,7 @@ public class PreferencesUtil implements IPreferenceKeys {
         * <p>
         * setSortRanksHierarchichally
         * </p>
-        * 
+        *
         * @param selection
         *            a boolean.
         */
@@ -447,7 +502,7 @@ public class PreferencesUtil implements IPreferenceKeys {
         * <p>
         * getSortRanksHierarchichally
         * </p>
-        * 
+        *
         * @return a boolean.
         */
        public static boolean getSortRanksHierarchichally() {
@@ -463,8 +518,8 @@ public class PreferencesUtil implements IPreferenceKeys {
                String languageUuidString = getPreferenceStore().getString(
                                GLOBAL_LANGUAGE_UUID);
 
-               if (CdmUtils.isEmpty(languageUuidString)) {
-                       return Language.DEFAULT();
+               if (CdmUtils.isBlank(languageUuidString)) {
+                       return Language.getDefaultLanguage();
                }
 
                UUID languageUuid = UUID.fromString(languageUuidString);
@@ -520,7 +575,7 @@ public class PreferencesUtil implements IPreferenceKeys {
         * <p>
         * setEditMarkerTypePreference
         * </p>
-        * 
+        *
         * @param input
         *            a {@link org.eclipse.ui.IEditorInput} object.
         * @param markerType
@@ -545,4 +600,57 @@ public class PreferencesUtil implements IPreferenceKeys {
                return configurator;
        }
 
+       /**
+        * This method will write language properties to the config.ini located in the configuration folder
+        * of the Taxonomic Ediitor. <b>This method is only used to set the default language for Taxonomic Editor.</b>
+        *
+        * @param setLanguage 0 is for german and 1 for english.
+        * @throws IOException
+        */
+    public void writePropertyToConfigFile(int setLanguage) throws IOException {
+        File file = org.eclipse.core.runtime.preferences.ConfigurationScope.INSTANCE.getLocation().toFile();
+        Properties properties = load(file.getAbsolutePath()+"/config.ini");
+        switch(setLanguage){
+        case 0:
+            properties.setProperty("osgi.nl", "de");
+            PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "de");
+            break;
+        case 1:
+            properties.setProperty("osgi.nl", "en");
+            PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.DEFAULT_LANGUAGE_EDITOR, "en");
+            break;
+        default:
+            break;
+        }
+        save(file+"/config.ini", properties);
+    }
+
+    /**
+     * This method loads a property from a given file and returns it.
+     *
+     * @param filename
+     * @return
+     * @throws IOException
+     */
+    private Properties load(String filename) throws IOException {
+        FileInputStream in = new FileInputStream(filename);
+        Properties prop = new Properties();
+        prop.load(in);
+        in.close();
+        return prop;
+    }
+
+    /**
+     * This method saves a property to the specified file.
+     *
+     * @param filename
+     * @param properties
+     * @throws IOException
+     */
+    private void save(String filename, Properties properties) throws IOException{
+        FileOutputStream fos =  new FileOutputStream(filename);
+        properties.store(fos, "");
+        fos.close();
+    }
+
 }