diff --git a/src/main/java/org/support/project/knowledge/bat/DataTransferBat.java b/src/main/java/org/support/project/knowledge/bat/DataTransferBat.java index 9ac6d2d11..77fbe805c 100644 --- a/src/main/java/org/support/project/knowledge/bat/DataTransferBat.java +++ b/src/main/java/org/support/project/knowledge/bat/DataTransferBat.java @@ -21,10 +21,11 @@ public static void main(String[] args) throws Exception { try { initLogName("DataTransferBat.log"); configInit(ClassUtils.getShortClassName(DataTransferBat.class)); - + DataTransferLogic.get().requestTransfer(); DataTransferBat bat = new DataTransferBat(); bat.dbInit(); bat.start(); + } catch (Exception e) { LOG.error("any error", e); throw e; @@ -38,10 +39,10 @@ public void run() { try { AppConfig appConfig = ConfigLoader.load(AppConfig.APP_CONFIG, AppConfig.class); String[] parms = { "-tcp", "-baseDir", appConfig.getDatabasePath() }; - + + LOG.info("start h2 database"); Server server = Server.createTcpServer(parms); server.start(); - // System.out.println("Database start..."); serverStarted = true; while (runing) { diff --git a/src/main/java/org/support/project/knowledge/dao/gen/GenMailLocaleTemplatesDao.java b/src/main/java/org/support/project/knowledge/dao/gen/GenMailLocaleTemplatesDao.java index 924319dfe..88db5e38f 100644 --- a/src/main/java/org/support/project/knowledge/dao/gen/GenMailLocaleTemplatesDao.java +++ b/src/main/java/org/support/project/knowledge/dao/gen/GenMailLocaleTemplatesDao.java @@ -103,6 +103,15 @@ public MailLocaleTemplatesEntity selectOnKey(String key, String templateId) { String sql = SQLManager.getInstance().getSql("/org/support/project/knowledge/dao/sql/MailLocaleTemplatesDao/MailLocaleTemplatesDao_select_on_key.sql"); return executeQuerySingle(sql, MailLocaleTemplatesEntity.class, key, templateId); } + /** + * Select data that not deleted on KEY column. + * @param key key + * @return list + */ + public List selectOnKey(String key) { + String sql = SQLManager.getInstance().getSql("/org/support/project/knowledge/dao/sql/MailLocaleTemplatesDao/MailLocaleTemplatesDao_select_on_col_key.sql"); + return executeQueryList(sql, MailLocaleTemplatesEntity.class, key); + } /** * Select data that not deleted on TEMPLATE_ID column. * @param templateId templateId @@ -118,7 +127,7 @@ public List selectOnTemplateId(String templateId) { * @return list */ public List physicalSelectOnKey(String key) { - String sql = SQLManager.getInstance().getSql("/org/support/project/knowledge/dao/sql/MailLocaleTemplatesDao/MailLocaleTemplatesDao_physical_select_on_key.sql"); + String sql = SQLManager.getInstance().getSql("/org/support/project/knowledge/dao/sql/MailLocaleTemplatesDao/MailLocaleTemplatesDao_physical_select_on_col_key.sql"); return executeQueryList(sql, MailLocaleTemplatesEntity.class, key); } /** diff --git a/src/main/java/org/support/project/knowledge/logic/DataTransferLogic.java b/src/main/java/org/support/project/knowledge/logic/DataTransferLogic.java index 4015cfb5e..32c61491f 100644 --- a/src/main/java/org/support/project/knowledge/logic/DataTransferLogic.java +++ b/src/main/java/org/support/project/knowledge/logic/DataTransferLogic.java @@ -74,27 +74,27 @@ public void transferData(ConnectionConfig from, ConnectionConfig to) throws Exce initDB.start(); // コピー先のDBの初期化 - LOG.info("migrate to db"); + LOG.info("init and migrate to db : " + to.getName()); ConnectionManager.getInstance().setDefaultConnectionName(to.getName()); - DatabaseControlDao dao1 = new DatabaseControlDao(); - org.support.project.web.dao.gen.DatabaseControlDao dao2 = new org.support.project.web.dao.gen.DatabaseControlDao(); - dao1.setConnectionName(to.getName()); - dao2.setConnectionName(to.getName()); - dao1.dropAllTable(); - dao2.dropAllTable(); + DatabaseControlDao knowledgeControlDao = new DatabaseControlDao(); + org.support.project.web.dao.gen.DatabaseControlDao webControlDao = new org.support.project.web.dao.gen.DatabaseControlDao(); + knowledgeControlDao.setConnectionName(to.getName()); + webControlDao.setConnectionName(to.getName()); + knowledgeControlDao.dropAllTable(); + webControlDao.dropAllTable(); initDB.start(); // データコピー先のDBに入っている、初期データを削除 LOG.info("clear init data from to db."); ConnectionManager.getInstance().setDefaultConnectionName(to.getName()); List truncateTargets = new ArrayList(); - truncateTargets.add(GroupsDao.get()); +// truncateTargets.add(GroupsDao.get()); truncateTargets.add(RolesDao.get()); truncateTargets.add(UserRolesDao.get()); truncateTargets.add(UsersDao.get()); - truncateTargets.add(SystemsDao.get()); - truncateTargets.add(TemplateItemsDao.get()); - truncateTargets.add(TemplateMastersDao.get()); +// truncateTargets.add(SystemsDao.get()); +// truncateTargets.add(TemplateItemsDao.get()); +// truncateTargets.add(TemplateMastersDao.get()); for (AbstractDao targetDao : truncateTargets) { targetDao.setConnectionName(to.getName()); Method truncateMethods = targetDao.getClass().getMethod("truncate"); @@ -149,7 +149,7 @@ public void transferData(ConnectionConfig from, ConnectionConfig to) throws Exce List list = (List) selectAllMethods.invoke(dao, args); // Toへデータ登録 - LOG.info(" -> insert data to " + to.getName()); + LOG.info(" -> insert data(100) to " + to.getName()); ConnectionManager.getInstance().setDefaultConnectionName(to.getName()); setConnectionNameMethods.invoke(dao, to.getName()); TransactionManager transactionManager = Container.getComp(TransactionManager.class); @@ -158,8 +158,26 @@ public void transferData(ConnectionConfig from, ConnectionConfig to) throws Exce if (LOG.isTraceEnabled()) { LOG.trace(entity); } - Method insertMethods = class1.getMethod("rawPhysicalInsert", entity.getClass()); - insertMethods.invoke(dao, entity); + Method getKeyValuesMethods = entity.getClass().getMethod("getKeyValues"); + Object[] keys = (Object[]) getKeyValuesMethods.invoke(entity); + Class[] params = new Class[keys.length]; + StringBuilder builder = new StringBuilder(); + for (int j = 0; j < keys.length; j++) { + params[j] = keys[j].getClass(); + if (j > 0) { + builder.append(","); + } + builder.append(keys[j]); + } + LOG.trace(class1.getName() + " : " + builder.toString()); + Method physicalSelectOnKeyMethods = class1.getMethod("physicalSelectOnKey", params); + Object obj = physicalSelectOnKeyMethods.invoke(dao, keys); + if (obj == null) { + Method insertMethods = class1.getMethod("rawPhysicalInsert", entity.getClass()); + insertMethods.invoke(dao, entity); + } else { + LOG.warn("skip (already exists): " + class1.getName() + " : " + builder.toString()); + } } transactionManager.commit(to.getName()); } diff --git a/src/main/resources/log4j.xml b/src/main/resources/log4j.xml index b21b03ab5..c75941cea 100644 --- a/src/main/resources/log4j.xml +++ b/src/main/resources/log4j.xml @@ -13,7 +13,7 @@ - + diff --git a/src/main/resources/org/support/project/knowledge/dao/sql/MailLocaleTemplatesDao/MailLocaleTemplatesDao_physical_select_on_col_key.sql b/src/main/resources/org/support/project/knowledge/dao/sql/MailLocaleTemplatesDao/MailLocaleTemplatesDao_physical_select_on_col_key.sql new file mode 100644 index 000000000..f62abb4ee --- /dev/null +++ b/src/main/resources/org/support/project/knowledge/dao/sql/MailLocaleTemplatesDao/MailLocaleTemplatesDao_physical_select_on_col_key.sql @@ -0,0 +1,4 @@ +SELECT * FROM MAIL_LOCALE_TEMPLATES + WHERE +KEY = ? +; diff --git a/src/main/resources/org/support/project/knowledge/dao/sql/MailLocaleTemplatesDao/MailLocaleTemplatesDao_physical_select_on_key.sql b/src/main/resources/org/support/project/knowledge/dao/sql/MailLocaleTemplatesDao/MailLocaleTemplatesDao_physical_select_on_key.sql index f62abb4ee..cf4ca6c9f 100644 --- a/src/main/resources/org/support/project/knowledge/dao/sql/MailLocaleTemplatesDao/MailLocaleTemplatesDao_physical_select_on_key.sql +++ b/src/main/resources/org/support/project/knowledge/dao/sql/MailLocaleTemplatesDao/MailLocaleTemplatesDao_physical_select_on_key.sql @@ -1,4 +1,5 @@ SELECT * FROM MAIL_LOCALE_TEMPLATES WHERE KEY = ? + AND TEMPLATE_ID = ? ; diff --git a/src/main/resources/org/support/project/knowledge/dao/sql/MailLocaleTemplatesDao/MailLocaleTemplatesDao_select_on_col_key.sql b/src/main/resources/org/support/project/knowledge/dao/sql/MailLocaleTemplatesDao/MailLocaleTemplatesDao_select_on_col_key.sql new file mode 100644 index 000000000..1c8e90348 --- /dev/null +++ b/src/main/resources/org/support/project/knowledge/dao/sql/MailLocaleTemplatesDao/MailLocaleTemplatesDao_select_on_col_key.sql @@ -0,0 +1,4 @@ +SELECT * FROM MAIL_LOCALE_TEMPLATES + WHERE +TEMPLATE_ID = ? + AND DELETE_FLAG = 0;