ref #5969: get the preference NC from db when initialize preference store and save...
authorKatja Luther <k.luther@bgbm.org>
Tue, 13 Sep 2016 09:56:48 +0000 (11:56 +0200)
committerKatja Luther <k.luther@bgbm.org>
Tue, 13 Sep 2016 09:57:25 +0000 (11:57 +0200)
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/cdm/api/application/CdmApplicationRemoteConfiguration.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/dataimport/transientServices/TransientCdmRepository.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/CdmDataSourceRepository.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceCredentialsWizardPage.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/io/wizard/NomenclaturalCodeWizardPage.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/preference/NomenclaturalCodePreferences.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/preference/PreferencesUtil.java

index c9f360f..2cb3143 100644 (file)
@@ -59,6 +59,7 @@ import eu.etaxonomy.cdm.api.service.INameService;
 import eu.etaxonomy.cdm.api.service.IOccurrenceService;\r
 import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;\r
 import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;\r
+import eu.etaxonomy.cdm.api.service.IPreferenceService;\r
 import eu.etaxonomy.cdm.api.service.IProgressMonitorService;\r
 import eu.etaxonomy.cdm.api.service.IReferenceService;\r
 import eu.etaxonomy.cdm.api.service.IService;\r
@@ -413,6 +414,11 @@ public class CdmApplicationRemoteConfiguration implements ICdmApplicationConfigu
     public IGroupService getGroupService(){\r
         return (IGroupService) getService(IGroupService.class, "/remoting/group.service", new CdmServiceRequestExecutor());\r
     }\r
+    \r
+    @Override\r
+    public IPreferenceService getPreferenceService(){\r
+        return (IPreferenceService) getService(IPreferenceService.class, "/remoting/preference.service", new CdmServiceRequestExecutor());\r
+    }\r
 \r
 \r
     @Override\r
index f3d1772..efca745 100644 (file)
@@ -39,6 +39,7 @@ import eu.etaxonomy.cdm.api.service.INameService;
 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
 import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
 import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
+import eu.etaxonomy.cdm.api.service.IPreferenceService;
 import eu.etaxonomy.cdm.api.service.IProgressMonitorService;
 import eu.etaxonomy.cdm.api.service.IReferenceService;
 import eu.etaxonomy.cdm.api.service.IService;
@@ -442,4 +443,10 @@ public class TransientCdmRepository implements ICdmApplicationConfiguration {
     public IMetadataService getMetadataService() {
         return defaultApplicationConfiguration.getMetadataService();
     }
+
+       @Override
+       public IPreferenceService getPreferenceService() {
+               
+               return defaultApplicationConfiguration.getPreferenceService();
+       }
 }
index 0275cc4..ccdd5e2 100644 (file)
@@ -174,7 +174,7 @@ public class CdmDataSourceRepository{
         */
        public static ICdmDataSource createDefaultH2DataSource() {
            ICdmDataSource h2DataSource = CdmDataSource.NewH2EmbeddedInstance(
-                DEFAULT_DATASOURCE_NAME, "sa", "", PreferencesUtil.getPreferredNomenclaturalCode());
+                       DEFAULT_DATASOURCE_NAME, "sa", "", PreferencesUtil.getPreferredNomenclaturalCode(true));
         save(h2DataSource.getName(), h2DataSource);
         return h2DataSource;
        }
@@ -188,7 +188,7 @@ public class CdmDataSourceRepository{
        public static boolean setCurrentCdmSource(ICdmSource cdmSource) {
                currentCdmSource = cdmSource;
                NomenclaturalCode dataSourceNomenclaturalCode = cdmSource.getNomenclaturalCode();
-               NomenclaturalCode applicationNomenclaturalCode = PreferencesUtil.getPreferredNomenclaturalCode();
+               NomenclaturalCode applicationNomenclaturalCode = PreferencesUtil.getPreferredNomenclaturalCode(true);
 
                if( dataSourceNomenclaturalCode != null && ! dataSourceNomenclaturalCode.equals(applicationNomenclaturalCode)){
                        PreferencesUtil.setPreferredNomenclaturalCode(dataSourceNomenclaturalCode);
index 04c9eb4..b225e77 100644 (file)
@@ -258,7 +258,7 @@ public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage impl
                nomenclaturalCodeGroup = new Group(composite , SWT.NONE);
                nomenclaturalCodeGroup.setLayout(new GridLayout());
 
-               nomenclaturalCode = dataSource != null ? dataSource.getNomenclaturalCode() : PreferencesUtil.getPreferredNomenclaturalCode();
+               nomenclaturalCode = dataSource != null ? dataSource.getNomenclaturalCode() : PreferencesUtil.getPreferredNomenclaturalCode(true);
 
                for (final NomenclaturalCode code : NomenclaturalCodeHelper.getSupportedCodes()) {
                        Button button = new Button(nomenclaturalCodeGroup, SWT.RADIO);
index af287ca..7358857 100644 (file)
@@ -73,7 +73,7 @@ public class NomenclaturalCodeWizardPage extends WizardPage {
                final Group group = new Group(container, SWT.NONE);
                group.setLayout(new GridLayout());
 
-               NomenclaturalCode preferredCode = PreferencesUtil.getPreferredNomenclaturalCode();
+               NomenclaturalCode preferredCode = PreferencesUtil.getPreferredNomenclaturalCode(false);
 
                // set preferred code as default
                configurator.setNomenclaturalCode(preferredCode);
index fd3b217..b52eee0 100644 (file)
@@ -8,15 +8,25 @@
  */
 package eu.etaxonomy.taxeditor.preference;
 
+import java.util.Iterator;
 import java.util.List;
 
+import org.eclipse.jface.preference.FieldEditor;
 import org.eclipse.jface.preference.FieldEditorPreferencePage;
 import org.eclipse.jface.preference.RadioGroupFieldEditor;
 import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.IWorkbenchPreferencePage;
 
+import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
+import eu.etaxonomy.cdm.api.service.IClassificationService;
+import eu.etaxonomy.cdm.api.service.IPreferenceService;
+import eu.etaxonomy.cdm.model.metadata.CdmPreference;
+import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
+import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
+import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
 import eu.etaxonomy.taxeditor.model.NomenclaturalCodeHelper;
+import eu.etaxonomy.taxeditor.store.CdmStore;
 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
 
 /**
@@ -73,6 +83,23 @@ public class NomenclaturalCodePreferences extends FieldEditorPreferencePage
                setPreferenceStore(TaxeditorStorePlugin.getDefault()
                                .getPreferenceStore());
                setDescription("Choose which nomenclatural code you would like to use for scientific names unless otherwise specified.");
+               PreferencesUtil.setPreferredNomenclaturalCode(null);
        }
+       
+       @Override
+        public boolean performOk() {
+               
+               boolean result = super.performOk();
+               if (result){
+                       String value = getPreferenceStore().getString(IPreferenceKeys.PREFERRED_NOMENCLATURAL_CODE_KEY);
+                       CdmPreference pref = CdmPreference.NewDatabaseInstance( PreferencePredicate.NomenclaturalCode, value);
+                       ICdmApplicationConfiguration controller = CdmStore.getCurrentApplicationConfiguration();
+                       IPreferenceService service = controller.getPreferenceService();
+                       service.set(pref);
+               }
+        return result;
+    }
+       
+       
 
 }
index cf3a9d8..d6a8f6b 100644 (file)
@@ -34,8 +34,10 @@ import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Shell;
 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;
@@ -48,6 +50,10 @@ import eu.etaxonomy.cdm.model.common.Language;
 import eu.etaxonomy.cdm.model.common.MarkerType;
 import eu.etaxonomy.cdm.model.common.TermBase;
 import eu.etaxonomy.cdm.model.description.FeatureTree;
+import eu.etaxonomy.cdm.model.metadata.CdmPreference;
+import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
+import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
+import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
 import eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy;
 import eu.etaxonomy.cdm.strategy.match.IMatchStrategy;
@@ -104,10 +110,29 @@ public class PreferencesUtil implements IPreferenceKeys {
         */
        public static void setPreferredNomenclaturalCode(
                        NomenclaturalCode preferredCode) {
+               ICdmApplicationConfiguration controller;
+               controller = CdmStore.getCurrentApplicationConfiguration();
+               PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
+               CdmPreference preference = null;
+               if (preferredCode == null){
+                       preference = controller.getPreferenceService().find(key);
+                       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));
+                               getPreferenceKey(preferredCode));
        }
 
+       public static NomenclaturalCode getPreferredNomenclaturalCode(){
+               return getPreferredNomenclaturalCode(false);
+       }
+       
        /**
         * <p>
         * getPreferredNomenclaturalCode
@@ -115,11 +140,30 @@ public class PreferencesUtil implements IPreferenceKeys {
         *
         * @return a {@link eu.etaxonomy.cdm.model.name.NomenclaturalCode} object.
         */
-       public static NomenclaturalCode getPreferredNomenclaturalCode() {
-
-               for (NomenclaturalCode code : NomenclaturalCodeHelper.getAllCodes()) {
-                       String preferredCode = getPreferenceStore().getString(
+       public static NomenclaturalCode getPreferredNomenclaturalCode(boolean preConnected) {
+               ICdmApplicationConfiguration controller;
+               CdmPreference pref = null;
+               if (!preConnected){
+                       try{
+                               controller = CdmStore.getCurrentApplicationConfiguration();
+                               PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
+                           pref = controller.getPreferenceService().find(key);
+                       }catch(Exception e){
+                               e.printStackTrace();
+                       }
+               }
+               
+           String preferredCode;
+           if(pref == null){
+               preferredCode = getPreferenceStore().getString(
                                        PREFERRED_NOMENCLATURAL_CODE_KEY);
+           }else{
+               preferredCode = pref.getValue();
+           }
+           
+               for (NomenclaturalCode code : NomenclaturalCodeHelper.getAllCodes()) {
+//                     String preferredCode = getPreferenceStore().getString(
+//                                     PREFERRED_NOMENCLATURAL_CODE_KEY);
                        if (getPreferenceKey(code).equals(preferredCode)) {
                                return code;
                        }
@@ -411,7 +455,7 @@ public class PreferencesUtil implements IPreferenceKeys {
                // First time Editor is opened, no nomenclatural code has been set
 
 
-               if (PreferencesUtil.getPreferredNomenclaturalCode() == null) {
+               if (PreferencesUtil.getPreferredNomenclaturalCode(true) == null) {
                        PreferencesUtil.setPreferredNomenclaturalCode(NomenclaturalCode.ICNAFP);
                        /*