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;
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;
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 " +
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.");
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;
+ }
+
/**
*
*/