ref #8045: further implementation for default/explicit value selection preference...
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / store / CdmStore.java
index 213720753c6421830f0744190d7721731cc8934a..f69e23e89878057ebf2dfb02a55f35157cc28186 100644 (file)
@@ -14,40 +14,94 @@ import java.util.EnumSet;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.jobs.Job;
 import org.eclipse.swt.widgets.Display;
+import org.slf4j.Marker;
 import org.springframework.core.io.ClassPathResource;
 import org.springframework.core.io.Resource;
 import org.springframework.security.access.PermissionEvaluator;
 import org.springframework.security.authentication.AuthenticationManager;
 import org.springframework.security.core.Authentication;
+import org.springframework.security.core.GrantedAuthority;
 import org.springframework.security.core.context.SecurityContext;
 import org.springframework.security.core.context.SecurityContextHolder;
 
 import eu.etaxonomy.cdm.api.application.CdmApplicationException;
 import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
-import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
+import eu.etaxonomy.cdm.api.application.ICdmRepository;
+import eu.etaxonomy.cdm.api.cache.CdmServiceCacher;
 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
+import eu.etaxonomy.cdm.api.service.IAgentService;
+import eu.etaxonomy.cdm.api.service.IAnnotationService;
+import eu.etaxonomy.cdm.api.service.IClassificationService;
+import eu.etaxonomy.cdm.api.service.ICollectionService;
 import eu.etaxonomy.cdm.api.service.ICommonService;
+import eu.etaxonomy.cdm.api.service.IDescriptionService;
+import eu.etaxonomy.cdm.api.service.IEntityConstraintViolationService;
+import eu.etaxonomy.cdm.api.service.IEntityValidationService;
+import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
+import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
+import eu.etaxonomy.cdm.api.service.IGrantedAuthorityService;
+import eu.etaxonomy.cdm.api.service.IGroupService;
+import eu.etaxonomy.cdm.api.service.IMarkerService;
+import eu.etaxonomy.cdm.api.service.IMediaService;
+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.IReferenceService;
+import eu.etaxonomy.cdm.api.service.IRegistrationService;
+import eu.etaxonomy.cdm.api.service.IRightsService;
 import eu.etaxonomy.cdm.api.service.IService;
+import eu.etaxonomy.cdm.api.service.ITaxonService;
+import eu.etaxonomy.cdm.api.service.ITermService;
+import eu.etaxonomy.cdm.api.service.IUserService;
+import eu.etaxonomy.cdm.api.service.IVocabularyService;
+import eu.etaxonomy.cdm.api.service.IDescriptiveDataSetService;
+import eu.etaxonomy.cdm.api.service.molecular.IAmplificationService;
+import eu.etaxonomy.cdm.api.service.molecular.IPrimerService;
+import  eu.etaxonomy.cdm.cache.CdmRemoteCacheManager;
 import eu.etaxonomy.cdm.config.ICdmSource;
 import eu.etaxonomy.cdm.database.DbSchemaValidation;
-import eu.etaxonomy.cdm.ext.geo.IEditGeoService;
+import eu.etaxonomy.cdm.model.agent.AgentBase;
+import eu.etaxonomy.cdm.model.common.Annotation;
 import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.term.DefinedTermBase;
+import eu.etaxonomy.cdm.model.common.Group;
 import eu.etaxonomy.cdm.model.common.Language;
+import eu.etaxonomy.cdm.model.term.TermVocabulary;
+import eu.etaxonomy.cdm.model.description.DescriptionBase;
+import eu.etaxonomy.cdm.model.term.FeatureNode;
+import eu.etaxonomy.cdm.model.term.FeatureTree;
+import eu.etaxonomy.cdm.model.description.PolytomousKey;
+import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
+import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
+import eu.etaxonomy.cdm.model.media.Media;
+import eu.etaxonomy.cdm.model.media.Rights;
+import eu.etaxonomy.cdm.model.molecular.Amplification;
+import eu.etaxonomy.cdm.model.molecular.Primer;
+import eu.etaxonomy.cdm.model.name.Registration;
+import eu.etaxonomy.cdm.model.name.TaxonName;
+import eu.etaxonomy.cdm.model.occurrence.Collection;
+import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
+import eu.etaxonomy.cdm.model.reference.Reference;
+import eu.etaxonomy.cdm.model.taxon.Classification;
+import eu.etaxonomy.cdm.model.taxon.TaxonBase;
+import eu.etaxonomy.cdm.model.validation.EntityConstraintViolation;
+import eu.etaxonomy.cdm.model.validation.EntityValidation;
 import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
 import eu.etaxonomy.cdm.persistence.hibernate.permission.ICdmPermissionEvaluator;
 import eu.etaxonomy.cdm.persistence.hibernate.permission.Role;
 import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
 import eu.etaxonomy.taxeditor.io.ExportManager;
 import eu.etaxonomy.taxeditor.io.ImportManager;
-import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
-import eu.etaxonomy.taxeditor.remoting.cache.CdmRemoteCacheManager;
 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
 import eu.etaxonomy.taxeditor.session.mock.MockCdmEntitySessionManager;
 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
-import eu.etaxonomy.taxeditor.view.datasource.CdmDataSourceViewPart;
+import eu.etaxonomy.taxeditor.ui.dialog.RemotingLoginDialog;
+import eu.etaxonomy.taxeditor.util.ProgressMonitorClientManager;
+import eu.etaxonomy.taxeditor.webapp.CDMServer;
 
 /**
  * This implementation of ICdmDataRepository depends on hibernate sessions to
@@ -63,500 +117,650 @@ import eu.etaxonomy.taxeditor.view.datasource.CdmDataSourceViewPart;
  */
 public class CdmStore {
 
-       private static final Resource DEFAULT_APPLICATION_CONTEXT = new ClassPathResource(
-                       "/eu/etaxonomy/cdm/editorApplicationContext.xml",
-                       TaxeditorStorePlugin.class);
-       private static final DbSchemaValidation DEFAULT_DB_SCHEMA_VALIDATION = DbSchemaValidation.VALIDATE;
+    private static final Resource DEFAULT_APPLICATION_CONTEXT = new ClassPathResource(
+            "/eu/etaxonomy/cdm/editorApplicationContext.xml",
+            TaxeditorStorePlugin.class);
+    private static final DbSchemaValidation DEFAULT_DB_SCHEMA_VALIDATION = DbSchemaValidation.VALIDATE;
 
-       private static CdmStore instance;
+    private static CdmStore instance;
 
-       //private final ICdmApplicationConfiguration applicationConfiguration;
+    //private final ICdmRepository applicationConfiguration;
 
-       private static ContextManager contextManager = new ContextManager();
+    private static ContextManager contextManager = new ContextManager();
 
-       private static LoginManager loginManager = new LoginManager();
+    private static LoginManager loginManager = new LoginManager();
 
-       private static TermManager termManager = new TermManager();
+    private static TermManager termManager = new TermManager();
 
-       private static SearchManager searchManager = new SearchManager();
+    private static SearchManager searchManager = new SearchManager();
 
-       private static EditorManager editorManager = new EditorManager();
-
-       private static UseObjectStore useObjectInitializer = new UseObjectStore();
-
-       private static CdmStoreConnector job;
-
-       private Language language;
-
-       private ICdmSource cdmSource;
-
-       private boolean isConnected;
-
-
-
-       /**
-        * <p>
-        * getDefault
-        * </p>
-        *
-        * @return a {@link eu.etaxonomy.taxeditor.store.CdmStore} object.
-        */
-       protected static CdmStore getDefault() {
-               if (instance != null && instance.isConnected) {
-                       return instance;
-               } else{// if (instance == null || !instance.isConnected) {
-
-                       MessagingUtils.noDataSourceWarningDialog(instance);
-
-                       AbstractUtility.showView(CdmDataSourceViewPart.ID);
-                       return null;
-               }
-       }
-
-       /**
-        * Initialize the with the last edited datasource
-        */
-       public static void connect() {
-
-               ICdmSource cdmSource;
-               try {
-
-                       cdmSource = CdmDataSourceRepository.getCurrentCdmSource();
-                       connect(cdmSource);
-               } catch (Exception e) {
-                       MessagingUtils.messageDialog("Connection to CDM Source Failed", CdmStore.class, "Could not connect to target CDM Source", e);
-               }
-
-
-       }
-
-       /**
-        * Initialize with a specific datasource
-        *
-        * @param datasource
-        *            a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
-        */
-       public static void connect(ICdmSource cdmSource) {
-               connect(cdmSource, DEFAULT_DB_SCHEMA_VALIDATION,
-                               DEFAULT_APPLICATION_CONTEXT);
-       }
-
-       /**
-        * Initialize and provide
-        *
-        * @param datasource
-        * @param dbSchemaValidation
-        * @param applicationContextBean
-        */
-       private static void connect(final ICdmSource cdmSource,
-                       final DbSchemaValidation dbSchemaValidation,
-                       final Resource applicationContextBean) {
-           if(isActive()) {
-               // before we connect we clear the entity caches and the sessions
-               CdmRemoteCacheManager.removeEntityCaches();
-               if(getCurrentSessionManager() != null) {
-                   getCurrentSessionManager().disposeAll();
-               }
-           }
-               MessagingUtils.info("Connecting to datasource: " + cdmSource);
-
-               job = new CdmStoreConnector(Display.getDefault(), cdmSource,
-                               dbSchemaValidation, applicationContextBean);
-               job.setUser(true);
-               job.setPriority(Job.BUILD);
-               job.schedule();
-
-       }
-
-       public static boolean isConnecting() {
-               return job != null && job.getState() == Job.RUNNING;
-       }
-
-       /**
-        * Closes the current application context
-        *
-        * @param monitor
-        *            a {@link org.eclipse.core.runtime.IProgressMonitor} object.
-        */
-       public static void close(final IProgressMonitor monitor) {
-               Display.getDefault().asyncExec(new Runnable() {
-                       /*
-                        * (non-Javadoc)
-                        *
-                        * @see java.lang.Runnable#run()
-                        */
-                       @Override
-                       public void run() {
-                               getContextManager().notifyContextAboutToStop(monitor);
-                               if ((monitor == null || (!monitor.isCanceled()) && isActive())) {
-                                       getContextManager().notifyContextStop(monitor);
-                                       instance.close();
-                               }
-                       }
-               });
-       }
-
-       private void close() {
-               isConnected = false;
-               cdmSource = null;
-               CdmApplicationState.dispose();
-       }
-
-       static void setInstance(ICdmApplicationConfiguration applicationController,
-                       ICdmSource cdmSource) {
-               instance = new CdmStore(applicationController, cdmSource);
-       }
-
-       private CdmStore(ICdmApplicationConfiguration applicationController,
-                       ICdmSource cdmSource) {
-               CdmApplicationState.setCurrentAppConfig(applicationController);
-               CdmApplicationState.setCurrentDataChangeService(new CdmUIDataChangeService());
-               this.cdmSource = cdmSource;
-               isConnected = true;
-       }
-
-       /**
-        * All calls to the datastore require
-        *
-        * @return
-        */
-       private ICdmApplicationConfiguration getApplicationConfiguration() {
-               try {
-                       return CdmApplicationState.getCurrentAppConfig();
-               } catch (Exception e) {
-                       MessagingUtils.error(CdmStore.class, e);
-               }
-               return null;
-       }
-
-       /**
-        * <p>
-        * getCurrentApplicationController
-        * </p>
-        *
-        * @return a
-        *         {@link eu.etaxonomy.cdm.remote.api.application.CdmApplicationController}
-        *         object.
-        */
-       public static ICdmApplicationConfiguration getCurrentApplicationConfiguration() {
-               if (getDefault() != null) {
-                       return getDefault().getApplicationConfiguration();
-               }
-               return null;
-       }
-
-       /*
-        * CONVERSATIONS
-        */
-
-       /**
-        * Creates a new conversation, binds resources to the conversation and start
-        * a transaction for this conversation.
-        *
-        * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
-        *         object.
-        */
-       public static ConversationHolder createConversation() {
-               ConversationHolder conversation = getCurrentApplicationConfiguration()
-                               .NewConversation();
-               try {
-                       conversation.startTransaction();
-               }catch(Exception e){
-                       MessagingUtils.messageDialog("No database connection", CdmStore.class, "No database connection available", e);
-               }
-               return conversation;
-       }
-
-       //FIXME:Remoting should be removed after moving completely to remoting
-       private MockCdmEntitySessionManager mockCdmEntitySessionManager;
-
-       private ICdmEntitySessionManager getSessionManager() {
-               //FIXME:Remoting we should only have CdmApplicationRemoteConfiguration after move to remoting
-               //               bad hack which should be finally removed
-               if(getCurrentApplicationConfiguration() instanceof CdmApplicationRemoteController) {
-                       return ((CdmApplicationRemoteController)getCurrentApplicationConfiguration()).getCdmEntitySessionManager();
-               } else {
-                       if(mockCdmEntitySessionManager == null) {
-                               mockCdmEntitySessionManager = new MockCdmEntitySessionManager();
-                       }
-                       return mockCdmEntitySessionManager;
-               }
-       }
-
-       public static  ICdmEntitySessionManager getCurrentSessionManager() {
-               if (getDefault() != null) {
-                       return getDefault().getSessionManager();
-               }
-               return null;
-
-       }
-
-       /**
-        * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
-        * interface. If a matching getter is found the according service implementation is returned by
-        * invoking the getter otherwise the method returns <code>null</code>.
-        *
-        * @param <T>
-        * @param serviceClass
-        * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
-        */
-       public static <T extends IService> T getService(Class<T> serviceClass) {
-           T service = null;
-           try {
-               service = CdmApplicationState.getService(serviceClass);
-           } catch (CdmApplicationException cae) {
-               MessagingUtils.error(CdmStore.class, cae);
-           }
-
-           return service;
-       }
-
-       /**
-        * @see #getService(Class)
-        * As ICommonService is not extending IService we need a specific request here
-        */
-       public static ICommonService getCommonService() {
-               return CdmApplicationState.getCommonService();
-
-       }
-
-       /**
-        * <p>
-        * getAuthenticationManager
-        * </p>
-        *
-        * @return a
-        *         {@link org.springframework.security.authentication.ProviderManager}
-        *         object.
-        */
-       public static AuthenticationManager getAuthenticationManager() {
-               return getCurrentApplicationConfiguration().getAuthenticationManager();
-       }
-
-       /**
-        * <p>
-        * getAuthenticationManager
-        * </p>
-        *
-        * @return a
-        *         {@link ICdmPermissionEvaluator} object.
-        */
-       public static ICdmPermissionEvaluator getPermissionEvaluator() {
-               return getCurrentApplicationConfiguration().getPermissionEvaluator();
-       }
-
-       /**
-        * <p>
-        * getGeoService
-        * </p>
-        *
-        * @return a {@link eu.etaxonomy.cdm.ext.geo.IEditGeoService} object.
-        */
-       public static IEditGeoService getGeoService() {
-               return (IEditGeoService) getCurrentApplicationConfiguration().getBean(
-                               "editGeoService");
-       }
-
-       /*
-        * SECURITY RELATED CONVENIENCE METHODS
-        */
-
-       /**
-        * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
+    private static EditorManager editorManager = new EditorManager();
+
+    private static UseObjectStore useObjectInitializer = new UseObjectStore();
+
+    private static ProgressMonitorClientManager progressMonitorClientManager = new ProgressMonitorClientManager();
+
+    private static CdmStoreConnector job;
+
+    private static CDMServer managedServer;
+
+    private Language language;
+
+    private ICdmSource cdmSource;
+
+    private boolean isConnected;
+
+
+
+    /**
+     * <p>
+     * getDefault
+     * </p>
+     *
+     * @return a {@link eu.etaxonomy.taxeditor.store.CdmStore} object.
+     */
+    protected static CdmStore getDefault(boolean connecting){
+        if (instance != null && instance.isConnected) {
+            return instance;
+        } else{// if (instance == null || !instance.isConnected) {
+            if (connecting){
+                MessagingUtils.dataSourceNotAvailableWarningDialog(instance);
+            }else{
+                MessagingUtils.noDataSourceWarningDialog(instance);
+            }
+
+
+            return null;
+        }
+    }
+    /**
+     * <p>
+     * getDefault
+     * </p>
+     *
+     * @return a {@link eu.etaxonomy.taxeditor.store.CdmStore} object.
+     */
+    protected static CdmStore getDefault(){
+       return getDefault(false);
+    }
+
+    /**
+     * Initialize the with the last edited datasource
+     */
+    public static void connect() {
+
+        ICdmSource cdmSource;
+        try {
+
+            cdmSource = CdmDataSourceRepository.getCurrentCdmSource();
+            connect(cdmSource);
+        } catch (Exception e) {
+            MessagingUtils.messageDialog("Connection to CDM Source Failed", CdmStore.class, "Could not connect to target CDM Source", e);
+        }
+
+
+    }
+
+    /**
+     * Initialize with a specific datasource
+     *
+     * @param datasource
+     *            a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
+     */
+    public static void connect(ICdmSource cdmSource) {
+        connect(cdmSource, DEFAULT_DB_SCHEMA_VALIDATION,
+                DEFAULT_APPLICATION_CONTEXT);
+    }
+
+    public static void connect(ICdmSource cdmSource, RemotingLoginDialog loginDialog) {
+        connect(cdmSource,
+                DEFAULT_DB_SCHEMA_VALIDATION,
+                DEFAULT_APPLICATION_CONTEXT,
+                loginDialog);
+    }
+
+    /**
+     * Initialize and provide
      *
-        * @param targetDomainObject
-        * @param permission
-        * @return
-        */
-       public static boolean currentAuthentiationHasPermission(CdmBase targetDomainObject, EnumSet<CRUD> permission){
-               //TODO use getCurrentApplicationConfiguration().currentAuthentiationHasPermission(CdmBase targetDomainObject, Operation permission) instead
-               SecurityContext context = SecurityContextHolder.getContext();
-               PermissionEvaluator pe = getPermissionEvaluator();
-               boolean hasPermission = false;
-               try {
+     * @param datasource
+     * @param dbSchemaValidation
+     * @param applicationContextBean
+     */
+    private static void connect(final ICdmSource cdmSource,
+            final DbSchemaValidation dbSchemaValidation,
+            final Resource applicationContextBean) {
+
+        MessagingUtils.info("Connecting to datasource: " + cdmSource);
+
+        job = new CdmStoreConnector(Display.getDefault(), cdmSource,
+                dbSchemaValidation, applicationContextBean);
+        job.setUser(true);
+        job.setPriority(Job.BUILD);
+        job.schedule();
+
+    }
+
+    private static void connect(final ICdmSource cdmSource,
+            final DbSchemaValidation dbSchemaValidation,
+            final Resource applicationContextBean,
+            RemotingLoginDialog remotingLoginDialog) {
+        RemotingLoginDialog loginDialog = remotingLoginDialog;
+        if(isActive()) {
+            // before we connect we clear the entity caches and the sessions
+           CdmRemoteCacheManager.removeEntityCaches();
+            if(getCurrentSessionManager(true) != null) {
+                getCurrentSessionManager(true).disposeAll();
+            }
+        }
+        MessagingUtils.info("Connecting to datasource: " + cdmSource);
+
+        job = new CdmStoreConnector(Display.getDefault(),
+                cdmSource,
+                dbSchemaValidation,
+                applicationContextBean);
+        job.start(loginDialog);
+
+    }
+
+    public static boolean isConnecting() {
+        return job != null && job.getState() == Job.RUNNING;
+    }
+
+    /**
+     * Closes the current application context
+     *
+     * @param monitor
+     *            a {@link org.eclipse.core.runtime.IProgressMonitor} object.
+     */
+    public static void close(final IProgressMonitor monitor) {
+        Display.getDefault().asyncExec(new Runnable() {
+
+            @Override
+            public void run() {
+                getContextManager().notifyContextAboutToStop(monitor);
+                if ((monitor == null || (!monitor.isCanceled()) && isActive())) {
+                    getContextManager().notifyContextStop(monitor);
+                    instance.close();
+                }
+            }
+        });
+    }
+
+    public static void close(IProgressMonitor monitor, boolean async) {
+        if(async) {
+            close(monitor);
+        } else {
+            getContextManager().notifyContextAboutToStop(monitor);
+            if ((monitor == null || (!monitor.isCanceled()) && isActive())) {
+                getContextManager().notifyContextStop(monitor);
+                instance.close();
+            }
+        }
+
+    }
+    private void close() {
+        isConnected = false;
+        cdmSource = null;
+        CdmApplicationState.dispose();
+    }
+
+    static void setInstance(ICdmRepository applicationController,
+            ICdmSource cdmSource) {
+        instance = new CdmStore(applicationController, cdmSource);
+        CdmApplicationState.setCdmServiceCacher(new CdmServiceCacher());
+
+    }
+
+    private CdmStore(ICdmRepository repository,
+            ICdmSource cdmSource) {
+        CdmApplicationState.setCurrentAppConfig(repository);
+        CdmApplicationState.setCurrentDataChangeService(new CdmUIDataChangeService());
+        this.cdmSource = cdmSource;
+        isConnected = true;
+    }
+
+    /**
+     * All calls to the datastore require
+     *
+     * @return
+     */
+    private ICdmRepository getApplicationConfiguration() {
+        try {
+            return CdmApplicationState.getCurrentAppConfig();
+        } catch (Exception e) {
+            MessagingUtils.error(CdmStore.class, e);
+        }
+        return null;
+    }
+
+    /**
+     * <p>
+     * getCurrentApplicationController
+     * </p>
+     *
+     * @return a
+     *         {@link eu.etaxonomy.cdm.remote.api.application.CdmApplicationController}
+     *         object.
+     */
+    public static ICdmRepository getCurrentApplicationConfiguration() {
+        if (getDefault() != null) {
+            return getDefault().getApplicationConfiguration();
+        }
+        return null;
+    }
+
+    /*
+     * CONVERSATIONS
+     */
+
+    /**
+     * Creates a new conversation, binds resources to the conversation and start
+     * a transaction for this conversation.
+     *
+     * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
+     *         object.
+     */
+    public static ConversationHolder createConversation() {
+        ConversationHolder conversation = getCurrentApplicationConfiguration()
+                .NewConversation();
+        try {
+            conversation.startTransaction();
+        }catch(Exception e){
+            MessagingUtils.messageDialog("No database connection", CdmStore.class, "No database connection available", e);
+        }
+        return conversation;
+    }
+
+    //FIXME:Remoting should be removed after moving completely to remoting
+    private MockCdmEntitySessionManager mockCdmEntitySessionManager;
+
+    private ICdmEntitySessionManager getSessionManager() {
+        //FIXME:Remoting we should only have CdmApplicationRemoteConfiguration after move to remoting
+        //               bad hack which should be finally removed
+        if(getCurrentApplicationConfiguration() instanceof CdmApplicationRemoteController) {
+            return ((CdmApplicationRemoteController)getCurrentApplicationConfiguration()).getCdmEntitySessionManager();
+        } else {
+            if(mockCdmEntitySessionManager == null) {
+                mockCdmEntitySessionManager = new MockCdmEntitySessionManager();
+            }
+            return mockCdmEntitySessionManager;
+        }
+    }
+    public static  ICdmEntitySessionManager getCurrentSessionManager() {
+        return getCurrentSessionManager(false);
+    }
+
+
+    public static  ICdmEntitySessionManager getCurrentSessionManager(boolean connecting) {
+        if (getDefault(connecting) != null) {
+            return getDefault(connecting).getSessionManager();
+        }
+        return null;
+
+    }
+
+    /**
+     * Generic method that will scan the getters of {@link ICdmRepository} for the given service
+     * interface. If a matching getter is found the according service implementation is returned by
+     * invoking the getter otherwise the method returns <code>null</code>.
+     *
+     * @param <T>
+     * @param serviceClass
+     * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
+     */
+    public static <T extends IService> T getService(Class<T> serviceClass) {
+        T service = null;
+        try {
+            service = CdmApplicationState.getService(serviceClass);
+        } catch (CdmApplicationException cae) {
+            MessagingUtils.error(CdmStore.class, cae);
+        }
+
+        return service;
+    }
+
+    /**
+     * @see #getService(Class)
+     * As ICommonService is not extending IService we need a specific request here
+     */
+    public static ICommonService getCommonService() {
+        return CdmApplicationState.getCommonService();
+
+    }
+
+    /**
+     * <p>
+     * getAuthenticationManager
+     * </p>
+     *
+     * @return a
+     *         {@link org.springframework.security.authentication.ProviderManager}
+     *         object.
+     */
+    public static AuthenticationManager getAuthenticationManager() {
+        return getCurrentApplicationConfiguration().getAuthenticationManager();
+    }
+
+    /**
+     * <p>
+     * getAuthenticationManager
+     * </p>
+     *
+     * @return a
+     *         {@link ICdmPermissionEvaluator} object.
+     */
+    public static ICdmPermissionEvaluator getPermissionEvaluator() {
+        return getCurrentApplicationConfiguration().getPermissionEvaluator();
+    }
+
+
+    /*
+     * SECURITY RELATED CONVENIENCE METHODS
+     */
+
+    /**
+     * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
+     *
+     * @param targetDomainObject
+     * @param permission
+     * @return
+     */
+    public static boolean currentAuthentiationHasPermission(CdmBase targetDomainObject, EnumSet<CRUD> permission){
+        //TODO use getCurrentApplicationConfiguration().currentAuthentiationHasPermission(CdmBase targetDomainObject, Operation permission) instead
+        SecurityContext context = SecurityContextHolder.getContext();
+        PermissionEvaluator pe = getPermissionEvaluator();
+        boolean hasPermission = false;
+        try {
             hasPermission = getPermissionEvaluator().hasPermission(context.getAuthentication(), targetDomainObject,
                     permission);
         } catch (org.springframework.security.access.AccessDeniedException e) {
             /* IGNORE */
         }
         return hasPermission;
-       }
+    }
 
-       /**
-        * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
+    /**
+     * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
      *
-        * @param targetDomainObject
-        * @param permission
-        * @return
-        */
-       public static boolean currentAuthentiationHasPermission(Class<? extends CdmBase> targetType, EnumSet<CRUD> permission){
-           boolean hasPermission = false;
+     * @param targetDomainObject
+     * @param permission
+     * @return
+     */
+    public static boolean currentAuthentiationHasPermission(Class<? extends CdmBase> targetType, EnumSet<CRUD> permission){
+        boolean hasPermission = false;
         try {
             hasPermission = getPermissionEvaluator().hasPermission(getCurrentAuthentiation(), null, targetType.getName(), permission);
         } catch (org.springframework.security.access.AccessDeniedException e) {
             /* IGNORE */
         }
         return hasPermission;
-       }
+    }
 
-       public static boolean currentAuthentiationHasOneOfRoles(Role ... roles){
-           boolean hasPermission = false;
+    public static boolean currentAuthentiationHasOneOfRoles(Role ... roles){
+        boolean hasPermission = false;
         try {
             hasPermission =  getPermissionEvaluator().hasOneOfRoles(getCurrentAuthentiation(), roles);
         } catch (org.springframework.security.access.AccessDeniedException e) {
             /* IGNORE */
         }
         return hasPermission;
-       }
-
-       public static Authentication getCurrentAuthentiation() {
-               SecurityContext context = SecurityContextHolder.getContext();
-               return context.getAuthentication();
-       }
-
-       /*
-        * LANGUAGE
-        */
-
-       /**
-        * Provides access to the global default language set in the application preferences.
-        *
-        * @return a {@link eu.etaxonomy.cdm.model.common.Language} object.
-        */
-       public static Language getDefaultLanguage() {
-               if (getDefault().getLanguage() == null) {
-                       getDefault().setLanguage(PreferencesUtil.getGlobalLanguage());
-               }
-               return getDefault().getLanguage();
-       }
-
-       /**
-        * <p>
-        * setDefaultLanguage
-        * </p>
-        *
-        * @param language
-        *            a {@link eu.etaxonomy.cdm.model.common.Language} object.
-        */
-       public static void setDefaultLanguage(Language language) {
-               getDefault().setLanguage(language);
-       }
-
-       /**
-        * @return the language
-        */
-       private Language getLanguage() {
-               return language;
-       }
-
-       /**
-        * @param language
-        *            the language to set
-        */
-       private void setLanguage(Language language) {
-               this.language = language;
-       }
-
-       /*
-        * LOGIN
-        */
-
-       /**
-        * <p>
-        * Getter for the field <code>loginManager</code>.
-        * </p>
-        *
-        * @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
-        */
-       public static LoginManager getLoginManager() {
-               return loginManager;
-       }
-
-       /**
-        * <p>
-        * Getter for the field <code>contextManager</code>.
-        * </p>
-        *
-        * @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
-        */
-       public static ContextManager getContextManager() {
-               return contextManager;
-       }
-
-       public static TermManager getTermManager() {
-               return termManager;
-       }
-
-       public static SearchManager getSearchManager() {
-               return searchManager;
-       }
-
-       public static EditorManager getEditorManager() {
-               return editorManager;
-       }
-
-       /*
-        * IMPORT/EXPORT FACTORIES
-        */
-
-       /**
-        * <p>
-        * Getter for the field <code>importHandler</code>.
-        * </p>
-        *
-        * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
-        */
-       public static ImportManager getImportManager() {
-               return ImportManager.NewInstance(getCurrentApplicationConfiguration());
-       }
-
-       /**
-        * <p>
-        * Getter for the field <code>exportHandler</code>.
-        * </p>
-        *
-        * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
-        */
-       public static ExportManager getExportManager() {
-               return ExportManager.NewInstance(getCurrentApplicationConfiguration());
-       }
-
-       /**
-        * Whether this CdmStore is currently connected to a datasource
-        *
-        * @return a boolean.
-        */
-       public static boolean isActive() {
-               return instance != null && instance.isConnected;
-       }
-
-       public static ICdmSource getActiveCdmSource() {
-               if (isActive()) {
-                       return instance.getCdmSource();
-               }
-               return null;
-       }
-
-       /**
-        * <p>
-        * getDataSource
-        * </p>
-        *
-        * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
-        * @deprecated currently retained for backward compatibility - use {@link getActiveCdmSource()} instead
-        */
-//     public static ICdmDataSource getDataSource() {
-//             if (isActive()) {
-//                     return (ICdmDataSource)instance.getCdmSource();
-//             }
-//             return null;
-//     }
-
-       /**
-        * @return
-        */
-       private ICdmSource getCdmSource() {
-               return cdmSource;
-       }
+    }
+
+    public static Authentication getCurrentAuthentiation() {
+        SecurityContext context = SecurityContextHolder.getContext();
+        return context.getAuthentication();
+    }
+
+    /*
+     * LANGUAGE
+     */
+
+    /**
+     * Provides access to the global default language set in the application preferences.
+     *
+     * @return a {@link eu.etaxonomy.cdm.model.common.Language} object.
+     */
+    public static Language getDefaultLanguage() {
+        if (getDefault().getLanguage() == null) {
+            getDefault().setLanguage(PreferencesUtil.getGlobalLanguage());
+        }
+        return getDefault().getLanguage();
+    }
+
+    /**
+     * <p>
+     * setDefaultLanguage
+     * </p>
+     *
+     * @param language
+     *            a {@link eu.etaxonomy.cdm.model.common.Language} object.
+     */
+    public static void setDefaultLanguage(Language language) {
+        getDefault().setLanguage(language);
+    }
+
+    /**
+     * @return the language
+     */
+    private Language getLanguage() {
+        return language;
+    }
+
+    /**
+     * @param language
+     *            the language to set
+     */
+    private void setLanguage(Language language) {
+        this.language = language;
+    }
+
+    /*
+     * LOGIN
+     */
+
+    /**
+     * <p>
+     * Getter for the field <code>loginManager</code>.
+     * </p>
+     *
+     * @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
+     */
+    public static LoginManager getLoginManager() {
+        return loginManager;
+    }
+
+    /**
+     * <p>
+     * Getter for the field <code>contextManager</code>.
+     * </p>
+     *
+     * @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
+     */
+    public static ContextManager getContextManager() {
+        return contextManager;
+    }
+
+    public static TermManager getTermManager() {
+        return termManager;
+    }
+
+    public static SearchManager getSearchManager() {
+        return searchManager;
+    }
+
+    public static EditorManager getEditorManager() {
+        return editorManager;
+    }
+
+    public static ProgressMonitorClientManager getProgressMonitorClientManager() {
+        return progressMonitorClientManager;
+    }
+
+    /*
+     * IMPORT/EXPORT FACTORIES
+     */
+
+    /**
+     * <p>
+     * Getter for the field <code>importHandler</code>.
+     * </p>
+     *
+     * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
+     */
+    public static ImportManager getImportManager() {
+        return ImportManager.NewInstance(getCurrentApplicationConfiguration());
+    }
+
+    /**
+     * <p>
+     * Getter for the field <code>exportHandler</code>.
+     * </p>
+     *
+     * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
+     */
+    public static ExportManager getExportManager() {
+        return ExportManager.NewInstance(getCurrentApplicationConfiguration());
+    }
+
+    /**
+     * Whether this CdmStore is currently connected to a datasource
+     *
+     * @return a boolean.
+     */
+    public static boolean isActive() {
+        return instance != null && instance.isConnected;
+    }
+
+    public static ICdmSource getActiveCdmSource() {
+        if (isActive()) {
+            return instance.getCdmSource();
+        }
+        return null;
+    }
+
+    /**
+     * <p>
+     * getDataSource
+     * </p>
+     *
+     * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
+     * @deprecated currently retained for backward compatibility - use {@link getActiveCdmSource()} instead
+     */
+    // public static ICdmDataSource getDataSource() {
+    //         if (isActive()) {
+    //                 return (ICdmDataSource)instance.getCdmSource();
+    //         }
+    //         return null;
+    // }
+
+    /**
+     * @return
+     */
+    private ICdmSource getCdmSource() {
+        return cdmSource;
+    }
+
+
+    public static <T extends CdmBase> IService<T> getService(T cdmBase){
+        IService<T> service = null;
+        if(cdmBase!=null){
+            //get corresponding service
+            if(cdmBase.isInstanceOf(Reference.class)){
+                service = (IService<T>) getService(IReferenceService.class);
+            }
+            else if (cdmBase.isInstanceOf(AgentBase.class)){
+                service = (IService<T>) getService(IAgentService.class);
+            }
+            else if (cdmBase instanceof TaxonName) {
+                service = (IService<T>) getService(INameService.class);
+            }
+            else if (cdmBase instanceof TaxonBase) {
+                service = (IService<T>) getService(ITaxonService.class);
+            }
+            else if (cdmBase instanceof SpecimenOrObservationBase) {
+                service = (IService<T>) getService(IOccurrenceService.class);
+            }
+            else if (cdmBase instanceof Media) {
+                service = (IService<T>) getService(IMediaService.class);
+            }
+            else if (cdmBase instanceof Collection) {
+                service = (IService<T>) getService(ICollectionService.class);
+            }
+            else if (cdmBase instanceof eu.etaxonomy.cdm.model.common.User) {
+                service = (IService<T>) getService(IUserService.class);
+            }
+            else if (cdmBase instanceof Group) {
+               service = (IService<T>) getService(IGroupService.class);
+            }
+            else if (cdmBase instanceof DescriptiveDataSet) {
+               service = (IService<T>) getService(IDescriptiveDataSetService.class);
+            }
+            else if (cdmBase instanceof TermVocabulary<?>) {
+               service = (IService<T>) getService(IVocabularyService.class);
+            }
+            else if (cdmBase instanceof DefinedTermBase<?>) {
+               service = (IService<T>) getService(ITermService.class);
+            }
+            else if (cdmBase instanceof Primer) {
+                service = (IService<T>) getService(IPrimerService.class);
+            }
+            else if (cdmBase instanceof Amplification) {
+                service = (IService<T>) getService(IAmplificationService.class);
+            }
+            else if (cdmBase instanceof PolytomousKey) {
+                service = (IService<T>) getService(IPolytomousKeyService.class);
+            }
+            else if (cdmBase instanceof PolytomousKeyNode) {
+                service = (IService<T>) getService(IPolytomousKeyNodeService.class);
+            }
+            else if (cdmBase instanceof Annotation) {
+                service = (IService<T>) getService(IAnnotationService.class);
+            }
+            else if (cdmBase instanceof Classification) {
+                service = (IService<T>) getService(IClassificationService.class);
+            }
+            else if (cdmBase instanceof DescriptionBase<?>) {
+                service = (IService<T>) getService(IDescriptionService.class);
+            }
+            else if (cdmBase instanceof EntityConstraintViolation) {
+                service = (IService<T>) getService(IEntityConstraintViolationService.class);
+            }
+            else if (cdmBase instanceof EntityValidation) {
+                service = (IService<T>) getService(IEntityValidationService.class);
+            }
+            else if (cdmBase instanceof FeatureNode) {
+                service = (IService<T>) getService(IFeatureNodeService.class);
+            }
+            else if (cdmBase instanceof FeatureTree) {
+                service = (IService<T>) getService(IFeatureTreeService.class);
+            }
+            else if (cdmBase instanceof GrantedAuthority) {
+                service = (IService<T>) getService(IGrantedAuthorityService.class);
+            }
+            else if (cdmBase instanceof Marker) {
+                service = (IService<T>) getService(IMarkerService.class);
+            }
+            else if (cdmBase instanceof Registration) {
+                service = (IService<T>) getService(IRegistrationService.class);
+            }
+            else if (cdmBase instanceof Rights) {
+                service = (IService<T>) getService(IRightsService.class);
+            }
+            else{
+               String message = "Service for entity of class %s not yet implemented in TaxEditor";
+               message = String.format(message, cdmBase.getClass().getSimpleName());
+               throw new RuntimeException(message);
+            }
+        }
+        return service;
+    }
+
+    public static void setManagedServer(CDMServer server) {
+        managedServer = server;
+    }
+
+    public static CDMServer getManagedServer() {
+        return managedServer;
+    }
 
 }