Driver driver = null;
try {
driver = (Driver) Class.forName(DRIVER).newInstance();
}
catch (ClassNotFoundException e) {
throw new WGInvalidDatabaseException("Necessary JDBC driver not found: " + e.getMessage());
}
catch (InstantiationException e) {
throw new WGInvalidDatabaseException("Cannot instantiate neccessary JDBC driver", e);
}
catch (IllegalAccessException e) {
throw new WGInvalidDatabaseException("Cannot instantiate neccessary JDBC driver", e);
}
// Build creations options
Map creationOptions = db.getCreationOptions();
// Hibernate configuration
WGDatabase.putDefaultOption(creationOptions, "hibernate.dialect", HSQLDialect.class.getName());
WGDatabase.putDefaultOption(creationOptions, "hibernate.connection.driver_class", DRIVER);
if (user == null) {
user = "sa";
}
if (pwd == null) {
pwd = "";
}
// Build hsql path
String hsqlPath = buildHsqlPath(db, path);
String jdbcPath = "jdbc:hsqldb:" + hsqlPath;
// Create a first connection to the database to do some tests
Properties props = new Properties();
props.put("user", user);
props.put("password", pwd);
props.put("hsqldb.default_table_type", "cached");
props.put("shutdown", "true");
JDBCConnectionProvider conProvider = null;
boolean isInitialized = false;
boolean migrateToCS5 = false;
double csVersion = 0;
try {
conProvider = new JDBCConnectionProvider(jdbcPath, driver.getClass().getName(), props, false);
List<String> tables = conProvider.getDatabaseTables();
// Look if the database is empty and we must initialize it
String contentTable = (String) Groq.selectFrom(tables).whereEqualsIgnoreCase("content").singleValue();
if (contentTable != null) {
isInitialized = true;
if (db.getCreationOptions().containsKey(WGDatabase.COPTION_CONTENT_STORE_VERSION)) {
// Look if the desired target format is CS5 (or higher)
double targetCsVersion = Double.valueOf((String) db.getCreationOptions().get(WGDatabase.COPTION_CONTENT_STORE_VERSION)).doubleValue();
if (targetCsVersion >= WGDatabase.CSVERSION_WGA5) {
// Look if the actual format it is lower than CS5. If so we automatically migrate it to that format
csVersion = determineCSVersion(conProvider);
if (csVersion < WGDatabase.CSVERSION_WGA5) {
migrateToCS5 = true;
isInitialized = false;
}
}
}
}
}
catch (Exception e) {
throw new WGInvalidDatabaseException("Unable to connect to database", e);
}
finally {
if (conProvider != null) {
try {
conProvider.close();
}
catch (JDBCConnectionException e) {
}
}
}
WGDatabase importSource = null;
if (migrateToCS5) {
try {
importSource = prepareCSDump(db, getHsqlBaseFile(path), user, pwd, csVersion);
}
catch (Exception e) {
throw new WGInvalidDatabaseException("Exception while migrating HSQL content store to CS5 format", e);
}
}
if (isInitialized == false) {
WGFactory.getLogger().info("Initializing empty database " + db.getDbReference() + " as a WGA Content Store");