Added a missing feature to plugin dependencies
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / store / CdmStoreConnector.java
index 853dbe42c7044ea1acd929ee9e4b22fefb3306d0..8e560cf54a0bb0381edc680ea93cf178210da7ad 100644 (file)
@@ -1,12 +1,12 @@
 // $Id$
 /**
-* Copyright (C) 2007 EDIT
-* 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.
-*/
+ * Copyright (C) 2007 EDIT
+ * 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;
 
@@ -34,11 +34,11 @@ import eu.etaxonomy.taxeditor.view.datasource.CdmDataSourceViewPart;
  * @created Dec 8, 2010
  * @version 1.0
  */
-class CdmStoreConnector extends Job{
-       private Display display;
-       private ICdmDataSource dataSource;
+class CdmStoreConnector extends Job {
+       private final Display display;
+       private final ICdmDataSource dataSource;
        private DbSchemaValidation dbSchemaValidation;
-       private Resource applicationContextBean;
+       private final Resource applicationContextBean;
 
        /**
         * @param datasource
@@ -55,66 +55,75 @@ class CdmStoreConnector extends Job{
                this.applicationContextBean = applicationContextBean;
        }
 
-
+       @Override
        public IStatus run(final IProgressMonitor monitor) {
 
                monitor.beginTask(getConnectionMessage(), 10);
-               
+
                // check if database is up and running
                checkDatabaseReachable(monitor);
-               
-               if(! monitor.isCanceled()){
+
+               if (!monitor.isCanceled()) {
                        // check if the datasource actually holds data
                        checkIsNonEmptyCdmDatabase(monitor);
                }
-               
-               if(dbSchemaValidation != DbSchemaValidation.CREATE && ! monitor.isCanceled()){
-                       // if we do not create the datasource, we want to check if the datasource is compatible with this editor
+
+               if (dbSchemaValidation != DbSchemaValidation.CREATE
+                               && !monitor.isCanceled()) {
+                       // if we do not create the datasource, we want to check if the
+                       // datasource is compatible with this editor
                        checkDbSchemaVersionCompatibility(monitor);
                }
-               
+
                // we are done with our low level checking and will free resources now
                dataSource.closeOpenConnections();
-               
-               if(! monitor.isCanceled()){
+
+               if (!monitor.isCanceled()) {
                        CdmStore.close(monitor);
                }
-               
+
                CdmApplicationController applicationController = null;
-               
-               if(! monitor.isCanceled()){
-                       CdmProgressMonitorAdapter subprogressMonitor = CdmProgressMonitorAdapter.CreateSubMonitor(monitor, 5);
+
+               if (!monitor.isCanceled()) {
+                       CdmProgressMonitorAdapter subprogressMonitor = CdmProgressMonitorAdapter
+                                       .CreateSubMonitor(monitor, IProgressMonitor.UNKNOWN);
                        // This is where we instantiate the application controller
-                       try{
-                               applicationController = CdmApplicationController.NewInstance(applicationContextBean, dataSource, dbSchemaValidation, false, subprogressMonitor);
-                       }catch(Exception e){
+                       try {
+                               applicationController = CdmApplicationController.NewInstance(
+                                               applicationContextBean, dataSource, dbSchemaValidation,
+                                               false, subprogressMonitor);
+                       } catch (Exception e) {
                                StoreUtil.error(this.getClass(), e.getMessage(), e);
                        }
                }
-               
+
                monitor.done();
-               
-               if(! monitor.isCanceled()){
+
+               if (!monitor.isCanceled()) {
                        CdmStore.setInstance(applicationController, dataSource);
-                       
-                       display.asyncExec(new Runnable(){
-                               /* (non-Javadoc)
+
+                       display.asyncExec(new Runnable() {
+                               /*
+                                * (non-Javadoc)
+                                * 
                                 * @see java.lang.Runnable#run()
                                 */
                                @Override
                                public void run() {
                                        authenticate();
-                       
+
                                        startContext();
                                }
                        });
-                                                       
-                       StoreUtil.info("Application context initialized.");     
+
+                       StoreUtil.info("Application context initialized.");
                        return Status.OK_STATUS;
-               }else{
+               } else {
                        // Show datasource view if not shown yet
-                       display.asyncExec(new Runnable(){
-                               /* (non-Javadoc)
+                       display.asyncExec(new Runnable() {
+                               /*
+                                * (non-Javadoc)
+                                * 
                                 * @see java.lang.Runnable#run()
                                 */
                                @Override
@@ -124,86 +133,99 @@ class CdmStoreConnector extends Job{
                        });
                        return Status.CANCEL_STATUS;
                }
-               
-               
+
        }
-       
-       private void authenticate(){
+
+       private void authenticate() {
                LoginDialog loginDialog = new LoginDialog(StoreUtil.getShell());
-               loginDialog.open();             
+               loginDialog.open();
        }
-       
-       private void startContext(){
+
+       private void startContext() {
                CdmStore.getContextManager().notifyContextStart();
        }
-       
+
        /**
         * @return
         */
        private String getConnectionMessage() {
                String message = "";
-               if(dataSource.getDatabaseType().equals(DatabaseTypeEnum.H2)){
+               if (dataSource.getDatabaseType().equals(DatabaseTypeEnum.H2)) {
                        message = " local CDM Store ";
-               }else{
+               } else {
                        message = " CDM Community Store ";
                }
                message += "'" + dataSource.getName() + "'";
-               
+
                message = "Connecting to" + message + ".";
-               
+
                return message;
        }
-       
+
        /**
         * @return
-        * @throws SQLException 
+        * @throws SQLException
         */
        private void checkDbSchemaVersionCompatibility(IProgressMonitor monitor) {
                monitor.subTask("Checking if datasource is compatible with this editor.");
                String dbSchemaVersion;
                boolean result = false;
                try {
-                       dbSchemaVersion = (String) dataSource.getSingleValue(MetaDataPropertyName.DB_SCHEMA_VERSION.getSqlQuery());
-                       // we assume that empty dbSchemaVersion means an empty database and skip version checking
-                       result = dbSchemaVersion == null ? true : CdmMetaData.isDbSchemaVersionCompatible(dbSchemaVersion);
+                       dbSchemaVersion = (String) dataSource
+                                       .getSingleValue(MetaDataPropertyName.DB_SCHEMA_VERSION
+                                                       .getSqlQuery());
+                       // we assume that empty dbSchemaVersion means an empty database and
+                       // skip version checking
+                       result = dbSchemaVersion == null ? true : CdmMetaData
+                                       .isDbSchemaVersionCompatible(dbSchemaVersion);
                        monitor.worked(1);
                } catch (SQLException e) {
                        //
                }
-               
-               if(!result){
+
+               if (!result) {
                        // Show an error message
-                       StoreUtil.errorDialog("DatabaseCompatibilityCheck failed", this, "The database schema for the chosen " +
-                                       "datasource '" + dataSource + "' \n is not valid for this version of the taxonomic editor. \n" +
-                                       "Please update the chosen datasource or choose a new data source to connect to in the Datasource View.", null);
-               
+                       StoreUtil
+                                       .errorDialog(
+                                                       "DatabaseCompatibilityCheck failed",
+                                                       this,
+                                                       "The database schema for the chosen "
+                                                                       + "datasource '"
+                                                                       + dataSource
+                                                                       + "' \n is not valid for this version of the taxonomic editor. \n"
+                                                                       + "Please update the chosen datasource or choose a new data source to connect to in the Datasource View.",
+                                                       null);
+
                        monitor.setCanceled(true);
-               }                                       
-               
+               }
+
        }
-       
+
        private void checkIsNonEmptyCdmDatabase(IProgressMonitor monitor) {
                monitor.subTask("Checking if datasource is a non empty CDM database.");
-               
+
                try {
-                       dataSource.getSingleValue(MetaDataPropertyName.DB_SCHEMA_VERSION.getSqlQuery());
+                       dataSource.getSingleValue(MetaDataPropertyName.DB_SCHEMA_VERSION
+                                       .getSqlQuery());
                } catch (SQLException e1) {
                        dbSchemaValidation = DbSchemaValidation.CREATE;
                }
        }
-       
-       private void checkDatabaseReachable(IProgressMonitor monitor){
+
+       private void checkDatabaseReachable(IProgressMonitor monitor) {
                try {
                        monitor.subTask("Checking if datasource is reachable.");
                        dataSource.testConnection();
                        monitor.worked(1);
                } catch (ClassNotFoundException e) {
-                       StoreUtil.errorDialog("Could not connect to chosen datasource", this, "Reason: " + e.getMessage(), e);
+                       StoreUtil.errorDialog("Could not connect to chosen datasource",
+                                       this, "Reason: " + e.getMessage(), e);
                        monitor.setCanceled(true);
                } catch (SQLException e) {
-                       StoreUtil.errorDialog("Could not connect to chosen datasource", this, "Reason: " + e.getMessage(), e);
+                       StoreUtil.errorDialog("Could not connect to chosen datasource",
+                                       this, "Reason: " + e.getMessage(), e);
                        monitor.setCanceled(true);
                }
-               
+
        }
 }