#1279
authorem.lee <em.lee@localhost>
Tue, 12 Jan 2010 13:46:26 +0000 (13:46 +0000)
committerem.lee <em.lee@localhost>
Tue, 12 Jan 2010 13:46:26 +0000 (13:46 +0000)
taxeditor-store/src/main/java/eu/etaxonomy/taxeditor/store/CdmStore.java

index f008102af88222559e6f8253a05228ce263ad225..8524bf80b64cabfbd2b7a3d21f680d713ff65b1a 100644 (file)
@@ -11,6 +11,8 @@ package eu.etaxonomy.taxeditor.store;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
+import java.util.StringTokenizer;
 
 import org.apache.log4j.Logger;
 import org.eclipse.core.runtime.Status;
@@ -34,13 +36,16 @@ import eu.etaxonomy.cdm.api.service.IVocabularyService;
 import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
 import eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator;
 import eu.etaxonomy.cdm.database.DataSourceNotFoundException;
+import eu.etaxonomy.cdm.database.DatabaseSchemaMismatchException;
 import eu.etaxonomy.cdm.database.DbSchemaValidation;
 import eu.etaxonomy.cdm.database.ICdmDataSource;
 import eu.etaxonomy.cdm.ext.IEditGeoService;
 import eu.etaxonomy.cdm.model.agent.AgentBase;
 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
+import eu.etaxonomy.cdm.model.common.CdmMetaData;
 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
 import eu.etaxonomy.cdm.model.common.Language;
+import eu.etaxonomy.cdm.model.common.CdmMetaData.MetaDataPropertyName;
 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
 import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
@@ -126,6 +131,8 @@ public class CdmStore{
                        instance = new CdmStore(cdmDatasource, dbSchemaValidation, applicationContextBean);
                        logger.info("Application context initialized.");
                        
+                       checkDatabaseSchemaCompatibility();
+                       
                        return instance;
                } catch (DataSourceNotFoundException e) {
                        StoreUtil.errorDialog("Chosen Datasource is not available", "Could not connect to the chosen " +
@@ -136,6 +143,10 @@ public class CdmStore{
                        StoreUtil.errorDialog("Corrupt Datasource", "The configuration for the chosen " +
                                        "datasource '" + cdmDatasource + "' is corrupt. Please check settings in datasources.xml.");
                        logger.error(e);
+               } catch (DatabaseSchemaMismatchException e){
+                       StoreUtil.errorDialog("Database Schema incompatible", "The database schema for the chosen " +
+                                       "datasource '" + cdmDatasource + "' is not valid for this version of the taxonomic editor.");
+                       logger.error(e);
                } catch (Exception e){
                        StoreUtil.errorDialog("Unknown Datasource Error", "An error occurred while connecting to the datasource." +
                                        "Please refer to the error log.");
@@ -147,6 +158,88 @@ public class CdmStore{
                return null;
        }
 
+       /**
+        * Checks whether the current database schema is compatible with the editor version.
+        * @throws DatabaseSchemaMismatchException
+        */
+       private static void checkDatabaseSchemaCompatibility()
+                       throws DatabaseSchemaMismatchException {
+
+               ICommonService commonService = getCommonService();
+               Map<MetaDataPropertyName, CdmMetaData> allCommonData = commonService.getCdmMetaData();
+
+               if (allCommonData.containsKey(MetaDataPropertyName.DB_SCHEMA_VERSION)) {
+                       
+                       String currentSchemaVersion = getCurrentSchemaVersion(allCommonData);
+                       String databaseSchemaVersion = getDatabaseSchemaVersion(allCommonData);
+                       
+                       compareVersions(currentSchemaVersion, databaseSchemaVersion);
+               }
+
+       }
+
+       /**
+        * Compares version numbers of the current schema version with the database schema version.
+        * @param currentSchemaVersion
+        * @param databaseSchemaVersion
+        * @throws DatabaseSchemaMismatchException
+        */
+       private static void compareVersions(String currentSchemaVersion,
+                       String databaseSchemaVersion)
+                       throws DatabaseSchemaMismatchException {
+               if (!currentSchemaVersion.equals(databaseSchemaVersion)) {
+                       throw new DatabaseSchemaMismatchException();
+               }
+       }
+
+       /**
+        * Gets the database schema version.
+        * @param allCommonData
+        * @return database schema version.
+        */
+       private static String getDatabaseSchemaVersion(
+                       Map<MetaDataPropertyName, CdmMetaData> allCommonData) {
+               // Get database schema version
+               CdmMetaData metaData = allCommonData.get(MetaDataPropertyName.DB_SCHEMA_VERSION);
+               String versionProperty = metaData.getMetaDataPropertyName();
+               String databaseSchemaVersion = getVersion(versionProperty);
+               return databaseSchemaVersion;
+       }
+
+       /**
+        * Gets the current schema version.
+        * @param allCommonData
+        * @return Current schema version.
+        */
+       private static String getCurrentSchemaVersion(
+                       Map<MetaDataPropertyName, CdmMetaData> allCommonData) {
+               // Get current schema version
+               String versionProperty = allCommonData.get(MetaDataPropertyName.DB_SCHEMA_VERSION).getValue();
+               String currentSchemaVersion = getVersion(versionProperty);
+               return currentSchemaVersion;
+       }
+
+       /**
+        * @param versionProperty
+        * @return Version number as string.
+        */
+       private static String getVersion(String versionProperty) {
+               return versionProperty.substring(0, secondIndexOf(versionProperty, ".", 2));
+       }
+
+       /**
+        * Calculates the n-th occurrence of a string.
+        * @param versionProperty
+        * @return Index of N-th occurence of a string.
+        */
+       private static int secondIndexOf(String versionProperty, String pattern, int occurence) {
+               int currentIndex = -1;
+               for (int i=0; i<occurence; i++) {
+                       currentIndex = versionProperty.indexOf(pattern, currentIndex + 1);
+               }
+               return currentIndex;
+       }
+
        /**
         * 
         */