diff --git a/hal-default.db b/hal-default.db index 5386f2cb..e1a510bd 100755 Binary files a/hal-default.db and b/hal-default.db differ diff --git a/src/se/koc/hal/HalContext.java b/src/se/koc/hal/HalContext.java index 59532de6..886b2563 100755 --- a/src/se/koc/hal/HalContext.java +++ b/src/se/koc/hal/HalContext.java @@ -1,40 +1,68 @@ package se.koc.hal; import zutil.db.DBConnection; +import zutil.db.handler.PropertiesSQLHandler; +import zutil.io.file.FileUtil; +import zutil.log.LogUtil; +import java.io.File; import java.io.FileReader; +import java.io.IOException; +import java.sql.SQLException; import java.util.Properties; +import java.util.logging.Logger; public class HalContext { + private static final Logger logger = LogUtil.getLogger(); + // Constants private static final String CONF_FILE = "hal.conf"; + private static final String DB_FILE = "hal.db"; + private static final String DEFAULT_DB_FILE = "hal-default.db"; // Variables private static DBConnection db; + private static Properties defaultFileConf; private static Properties fileConf; - private static Properties defaultConf; + private static Properties dbConf; static { - defaultConf = new Properties(); - defaultConf.setProperty("http_port", ""+8080); - defaultConf.setProperty("sync_port", ""+6666); - - HalContext.initialize(); + defaultFileConf = new Properties(); + defaultFileConf.setProperty("http_port", ""+8080); + defaultFileConf.setProperty("sync_port", ""+6666); } public static void initialize(){ try { // Read conf - fileConf = new Properties(defaultConf); + fileConf = new Properties(defaultFileConf); FileReader in = new FileReader(CONF_FILE); fileConf.load(in); in.close(); // Init DB - db = new DBConnection(DBConnection.DBMS.SQLite, "hal.db"); + if(FileUtil.find(DB_FILE) == null){ + logger.info("Creating new DB..."); + resetDB(); + } + db = new DBConnection(DBConnection.DBMS.SQLite, DB_FILE); + + // Read DB conf + dbConf = new Properties(); + db.exec("SELECT * FROM conf", new PropertiesSQLHandler(dbConf)); + + // Upgrade DB needed? + DBConnection defaultDB = new DBConnection(DBConnection.DBMS.SQLite, DEFAULT_DB_FILE); + Properties defaultDBConf = + db.exec("SELECT * FROM conf", new PropertiesSQLHandler(new Properties())); + if(defaultDBConf.getProperty("db_version").compareTo(dbConf.getProperty("db_version")) > 0) { + logger.info("Upgrading DB (from: v"+dbConf.getProperty("db_version") +", to: v"+defaultDBConf.getProperty("db_version")); + upgradeDB(); + } + defaultDB.close(); } catch (Exception e){ throw new RuntimeException(e); } @@ -54,4 +82,34 @@ public class HalContext { public static DBConnection getDB(){ return db; } + + + /*************************************************************************/ + + private static void resetDB() throws IOException { + FileUtil.copy(DEFAULT_DB_FILE, DB_FILE); + } + + // Todo: + private static void upgradeDB() throws SQLException { + /* + - beginTransaction + - run a table creation with if not exists (we are doing an upgrade, so the table might not exists yet, it will fail alter and drop) + - put in a list the existing columns List columns = DBUtils.GetColumns(db, TableName); + - backup table (ALTER table " + TableName + " RENAME TO 'temp_" + TableName) + - create new table (the newest table creation schema) + - get the intersection with the new columns, this time columns taken from the upgraded table (columns.retainAll(DBUtils.GetColumns(db, TableName));) + - restore data (String cols = StringUtils.join(columns, ","); + db.execSQL(String.format( + "INSERT INTO %s (%s) SELECT %s from temp_%s", + TableName, cols, cols, TableName)); + ) + - remove backup table (DROP table 'temp_" + TableName) + - setTransactionSuccessful + */ + logger.severe("DB Upgrade not implemented yes!"); + //db.exec("BeginTransaction"); + + //db.exec("EndTransaction"); + } } diff --git a/src/se/koc/hal/PowerChallenge.java b/src/se/koc/hal/PowerChallenge.java index 4813c70c..7fef0850 100755 --- a/src/se/koc/hal/PowerChallenge.java +++ b/src/se/koc/hal/PowerChallenge.java @@ -23,19 +23,22 @@ import java.util.logging.Level; public class PowerChallenge { - private static HalDaemon[] daemons = new HalDaemon[]{ - new DataAggregatorDaemon(), - new DataSynchronizationDaemon(), - new DataSynchronizationClient() - }; + private static HalDaemon[] daemons; public static void main(String[] args) throws Exception { + // init logging + LogUtil.setGlobalLevel(Level.ALL); + LogUtil.setGlobalFormatter(new CompactLogFormatter()); - // init logging - LogUtil.setGlobalLevel(Level.ALL); - LogUtil.setGlobalFormatter(new CompactLogFormatter()); + // init DB and other configurations + HalContext.initialize(); // init daemons + daemons = new HalDaemon[]{ + new DataAggregatorDaemon(), + new DataSynchronizationDaemon(), + new DataSynchronizationClient() + }; Timer daemonTimer = new Timer(); for(HalDaemon daemon : daemons){ daemon.initiate(daemonTimer);