Merge branch 'develop' into remoting-4.0
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / store / CdmStore.java
index 5b9ed355d90dd4d8018fd98463bdfcc694d2a752..213720753c6421830f0744190d7721731cc8934a 100644 (file)
@@ -1,17 +1,14 @@
 /**
  * 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.store;
 
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.lang.reflect.Type;
 import java.util.EnumSet;
 
 import org.eclipse.core.runtime.IProgressMonitor;
@@ -20,25 +17,35 @@ import org.eclipse.swt.widgets.Display;
 import org.springframework.core.io.ClassPathResource;
 import org.springframework.core.io.Resource;
 import org.springframework.security.access.PermissionEvaluator;
-import org.springframework.security.authentication.ProviderManager;
+import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.core.Authentication;
 import org.springframework.security.core.context.SecurityContext;
 import org.springframework.security.core.context.SecurityContextHolder;
 
-import eu.etaxonomy.cdm.api.application.CdmApplicationController;
+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.conversation.ConversationHolder;
+import eu.etaxonomy.cdm.api.service.ICommonService;
 import eu.etaxonomy.cdm.api.service.IService;
+import eu.etaxonomy.cdm.config.ICdmSource;
 import eu.etaxonomy.cdm.database.DbSchemaValidation;
-import eu.etaxonomy.cdm.database.ICdmDataSource;
 import eu.etaxonomy.cdm.ext.geo.IEditGeoService;
 import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.common.Language;
 import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
-import eu.etaxonomy.cdm.persistence.hibernate.permission.Operation;
+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;
 
@@ -46,10 +53,10 @@ import eu.etaxonomy.taxeditor.view.datasource.CdmDataSourceViewPart;
  * This implementation of ICdmDataRepository depends on hibernate sessions to
  * store the data correctly for the current session. No state is held in this
  * class.
- * 
+ *
  * Only methods that either get or manipulate data are exposed here. So this
  * class acts as a facade for the methods in cdmlib-service.
- * 
+ *
  * @author n.hoffmann
  * @created 17.03.2009
  * @version 1.0
@@ -63,7 +70,7 @@ public class CdmStore {
 
        private static CdmStore instance;
 
-       private final ICdmApplicationConfiguration applicationConfiguration;
+       //private final ICdmApplicationConfiguration applicationConfiguration;
 
        private static ContextManager contextManager = new ContextManager();
 
@@ -75,38 +82,35 @@ public class CdmStore {
 
        private static EditorManager editorManager = new EditorManager();
 
+       private static UseObjectStore useObjectInitializer = new UseObjectStore();
+
        private static CdmStoreConnector job;
 
        private Language language;
 
-       private ICdmDataSource cdmDatasource;
+       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) {
-
-                       StoreUtil
-                                       .warningDialog(
-                                                       "Application is not connected to a datastore",
-                                                       instance,
-                                                       "The requested operation is only available when "
-                                                                       + "connected to a datasource. You may choose a datasource to connect to or create a new one in the datasource view.");
+               } else{// if (instance == null || !instance.isConnected) {
 
-                       StoreUtil.showView(CdmDataSourceViewPart.ID);
+                       MessagingUtils.noDataSourceWarningDialog(instance);
 
+                       AbstractUtility.showView(CdmDataSourceViewPart.ID);
+                       return null;
                }
-
-               throw new RuntimeException();
        }
 
        /**
@@ -114,36 +118,49 @@ public class CdmStore {
         */
        public static void connect() {
 
-               ICdmDataSource datasource = CdmDataSourceRepository
-                               .getCurrentDataSource();
+               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);
+               }
+
 
-               connect(datasource);
        }
 
        /**
         * Initialize with a specific datasource
-        * 
+        *
         * @param datasource
         *            a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
         */
-       public static void connect(ICdmDataSource datasource) {
-               connect(datasource, DEFAULT_DB_SCHEMA_VALIDATION,
+       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 ICdmDataSource datasource,
+       private static void connect(final ICdmSource cdmSource,
                        final DbSchemaValidation dbSchemaValidation,
                        final Resource applicationContextBean) {
-               StoreUtil.info("Connecting to datasource: " + datasource);
-
-               job = new CdmStoreConnector(Display.getDefault(), datasource,
+           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);
@@ -157,7 +174,7 @@ public class CdmStore {
 
        /**
         * Closes the current application context
-        * 
+        *
         * @param monitor
         *            a {@link org.eclipse.core.runtime.IProgressMonitor} object.
         */
@@ -165,7 +182,7 @@ public class CdmStore {
                Display.getDefault().asyncExec(new Runnable() {
                        /*
                         * (non-Javadoc)
-                        * 
+                        *
                         * @see java.lang.Runnable#run()
                         */
                        @Override
@@ -181,31 +198,33 @@ public class CdmStore {
 
        private void close() {
                isConnected = false;
-               cdmDatasource = null;
+               cdmSource = null;
+               CdmApplicationState.dispose();
        }
 
-       static void setInstance(CdmApplicationController applicationController,
-                       ICdmDataSource dataSource) {
-               instance = new CdmStore(applicationController, dataSource);
+       static void setInstance(ICdmApplicationConfiguration applicationController,
+                       ICdmSource cdmSource) {
+               instance = new CdmStore(applicationController, cdmSource);
        }
 
-       private CdmStore(CdmApplicationController applicationController,
-                       ICdmDataSource dataSource) {
-               this.applicationConfiguration = applicationController;
-               this.cdmDatasource = dataSource;
+       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 applicationConfiguration;
+                       return CdmApplicationState.getCurrentAppConfig();
                } catch (Exception e) {
-                       StoreUtil.error(CdmStore.class, e);
+                       MessagingUtils.error(CdmStore.class, e);
                }
                return null;
        }
@@ -214,9 +233,9 @@ public class CdmStore {
         * <p>
         * getCurrentApplicationController
         * </p>
-        * 
+        *
         * @return a
-        *         {@link eu.etaxonomy.cdm.api.application.CdmApplicationController}
+        *         {@link eu.etaxonomy.cdm.remote.api.application.CdmApplicationController}
         *         object.
         */
        public static ICdmApplicationConfiguration getCurrentApplicationConfiguration() {
@@ -233,79 +252,96 @@ public class CdmStore {
        /**
         * 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{
+               try {
                        conversation.startTransaction();
                }catch(Exception e){
-                       StoreUtil.errorDialog("No database connection", CdmStore.class, "No database connection available", 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 return an implementation of the given service
-        * interface or <code>null</code> if the
-        * 
+        * 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
+        * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
         */
        public static <T extends IService> T getService(Class<T> serviceClass) {
-               ICdmApplicationConfiguration configuration = getCurrentApplicationConfiguration();
-
-               Method[] methods = ICdmApplicationConfiguration.class
-                               .getDeclaredMethods();
-
-               T service = null;
-
-               for (Method method : methods) {
-                       Type type = method.getGenericReturnType();
-
-                       if (type.equals(serviceClass)) {
-                               try {
-                                       service = (T) method.invoke(configuration, null);
-                                       break;
-                               } catch (IllegalArgumentException e) {
-                                       StoreUtil.error(CdmStore.class, e);
-                               } catch (IllegalAccessException e) {
-                                       StoreUtil.error(CdmStore.class, e);
-                               } catch (InvocationTargetException e) {
-                                       StoreUtil.error(CdmStore.class, e);
-                               }
-                       }
-               }
+           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();
 
-               return service;
        }
 
        /**
         * <p>
         * getAuthenticationManager
         * </p>
-        * 
+        *
         * @return a
         *         {@link org.springframework.security.authentication.ProviderManager}
         *         object.
         */
-       public static ProviderManager getAuthenticationManager() {
+       public static AuthenticationManager getAuthenticationManager() {
                return getCurrentApplicationConfiguration().getAuthenticationManager();
        }
-       
+
        /**
         * <p>
         * getAuthenticationManager
         * </p>
-        * 
+        *
         * @return a
-        *         {@link PermissionEvaluator} object.
+        *         {@link ICdmPermissionEvaluator} object.
         */
-       public static PermissionEvaluator getPermissionEvaluator() {
+       public static ICdmPermissionEvaluator getPermissionEvaluator() {
                return getCurrentApplicationConfiguration().getPermissionEvaluator();
        }
 
@@ -313,29 +349,69 @@ public class CdmStore {
         * <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)
      *
         * @param targetDomainObject
-        * @param permission 
+        * @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();
-               return getPermissionEvaluator().hasPermission(context.getAuthentication(), targetDomainObject, permission);
+               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)
+     *
+        * @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;
+        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();
        }
 
        /*
@@ -343,10 +419,8 @@ public class CdmStore {
         */
 
        /**
-        * <p>
-        * getDefaultLanguage
-        * </p>
-        * 
+        * 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() {
@@ -360,7 +434,7 @@ public class CdmStore {
         * <p>
         * setDefaultLanguage
         * </p>
-        * 
+        *
         * @param language
         *            a {@link eu.etaxonomy.cdm.model.common.Language} object.
         */
@@ -391,7 +465,7 @@ public class CdmStore {
         * <p>
         * Getter for the field <code>loginManager</code>.
         * </p>
-        * 
+        *
         * @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
         */
        public static LoginManager getLoginManager() {
@@ -402,7 +476,7 @@ public class CdmStore {
         * <p>
         * Getter for the field <code>contextManager</code>.
         * </p>
-        * 
+        *
         * @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
         */
        public static ContextManager getContextManager() {
@@ -429,7 +503,7 @@ public class CdmStore {
         * <p>
         * Getter for the field <code>importHandler</code>.
         * </p>
-        * 
+        *
         * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
         */
        public static ImportManager getImportManager() {
@@ -440,7 +514,7 @@ public class CdmStore {
         * <p>
         * Getter for the field <code>exportHandler</code>.
         * </p>
-        * 
+        *
         * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
         */
        public static ExportManager getExportManager() {
@@ -449,32 +523,40 @@ public class CdmStore {
 
        /**
         * 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 instance.getDatasource();
-               }
-               return null;
-       }
+//     public static ICdmDataSource getDataSource() {
+//             if (isActive()) {
+//                     return (ICdmDataSource)instance.getCdmSource();
+//             }
+//             return null;
+//     }
 
        /**
         * @return
         */
-       private ICdmDataSource getDatasource() {
-               return cdmDatasource;
+       private ICdmSource getCdmSource() {
+               return cdmSource;
        }
 
 }