fixes #805 and started to work on #835
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / store / CdmStore.java
index e9abec031477e0d1329f0d26dd1f16acab12351d..77d7d858d5e76cc699ab61abbb26dad42dbd0aa1 100644 (file)
@@ -14,9 +14,8 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.UUID;
 
-import javax.security.auth.spi.LoginModule;
-
 import org.apache.log4j.Logger;
+import org.eclipse.core.runtime.Status;
 import org.springframework.security.Authentication;
 import org.springframework.security.context.SecurityContextHolder;
 import org.springframework.security.providers.ProviderManager;
@@ -46,8 +45,7 @@ import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.model.taxon.TaxonomicTree;
 import eu.etaxonomy.cdm.persistence.query.MatchMode;
 import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
-import eu.etaxonomy.taxeditor.model.TaxeditorLoginModule;
-import eu.etaxonomy.taxeditor.user.AuthenticatedUserBar;
+import eu.etaxonomy.taxeditor.datasource.ChooseDatasourceDialog;
 
 /**
  * This implementation of ICdmDataRepository depends on hibernate sessions to store the data correctly 
@@ -76,7 +74,7 @@ public class CdmStore{
        
        private static DbSchemaValidation dbSchemaValidation;
 
-       private static LoginModule loginModule;
+       private static LoginManager loginManager;
        
        /**
         * 
@@ -94,15 +92,37 @@ public class CdmStore{
        public static CdmStore getDefault(String applicationContextBean){
                if(instance == null){
                        logger.info("Initializing application context ...");
-                       ICdmDataSource cdmDatasource = CdmDataSourceRepository.getDefault().
-                                       getCurrentDataSource();
-                       
-                       instance = new CdmStore(cdmDatasource, getDbSchemaValidation(), applicationContextBean);
                        
-                       logger.info("Application context initialized.");
+                       // Prompt user for datasource if there are more than one
+                       if(CdmDataSourceRepository.getDefault().getAll().size() > 1){
+                               ChooseDatasourceDialog chooseDataSource = new ChooseDatasourceDialog(StoreUtil.getShell());
+                               if(chooseDataSource.open() == Status.OK){
+                                       initialize(applicationContextBean);
+
+                               }else{
+                                       // no datasource chosen.
+                                       logger.warn("No datasource chosen. Exiting now.");
+                                       System.exit(1);
+                               }       
+                       }else{
+                               // Start default cdm database
+                               initialize(applicationContextBean);
+                       }
+               
                }
                return instance;
        }
+       
+       private static CdmStore initialize(String applicationContextBean){      
+               
+               ICdmDataSource cdmDatasource = CdmDataSourceRepository.getDefault().getCurrentDataSource();
+               
+               CdmStore instance = new CdmStore(cdmDatasource, getDbSchemaValidation(), applicationContextBean);
+               
+               logger.info("Application context initialized.");
+               
+               return instance;
+       }
 
        /**
         * @return
@@ -311,38 +331,14 @@ public class CdmStore{
                
                return nomReferenceTypeMap;
        }
-
-       /**
-        * TODO implement LoginModule to handle the whole login stuff
-        * 
-        * @param token
-        */
-       public static void authenticate(UsernamePasswordAuthenticationToken token){
-               Authentication authentication = CdmStore.getAuthenticationManager().authenticate(token);
-               SecurityContextHolder.getContext().setAuthentication(authentication);
-               
-               // TODO register a listener on LoginModule instead of calling a set method here
-//             AuthenticatedUserBar.setAuthenticatedUser(getAuthenticatedUser().getUsername());
-       }
-       
-       public static User getAuthenticatedUser(){
-               Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
-               
-               if(authentication != null 
-                               && authentication.getPrincipal() != null 
-                               && authentication.getPrincipal() instanceof User){
-                       return (User)authentication.getPrincipal();
-               }
-               return null;
-       }
        
        /**
         * incomplete! do note use!
         */
-       public static LoginModule getLoginModule(){
-               if(loginModule == null){
-                       loginModule = new TaxeditorLoginModule();
+       public static LoginManager getLoginManager(){
+               if(loginManager == null){
+                       loginManager = new LoginManager();
                }
-               return loginModule;
+               return loginManager;
        }
 }