Author: tchemit Date: 2011-09-22 17:21:31 +0200 (Thu, 22 Sep 2011) New Revision: 3298 Url: http://chorem.org/repositories/revision/lima/3298 Log: improve migration code Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/migration/MigrationV0_5_2.java Modified: trunk/lima-business/src/main/java/org/chorem/lima/business/migration/MigrationV0_5_2.java =================================================================== --- trunk/lima-business/src/main/java/org/chorem/lima/business/migration/MigrationV0_5_2.java 2011-09-22 12:59:51 UTC (rev 3297) +++ trunk/lima-business/src/main/java/org/chorem/lima/business/migration/MigrationV0_5_2.java 2011-09-22 15:21:31 UTC (rev 3298) @@ -33,8 +33,9 @@ boolean showSql, boolean showProgression) throws TopiaException { + if (log.isInfoEnabled()) { - log.info("Migrates to version 0.6"); + log.info("Migrates to version 0.5.2"); } // find all account with no master account and attach them to a master @@ -66,55 +67,46 @@ // this is a master account, nothing to do return; } + String masterAccountNumber = null; + if (length > 3) { // fill with master account with 3 digits - String masterAccountNumber = accountNumber.substring(0, 3); - Account masterAccount = getMasterAccount(accountDAO, account, masterAccountNumber); - account.setMasterAccount(masterAccount); - // let's fill master account - fillMasterAccountNumber(accountDAO, masterAccount); - return; - } + masterAccountNumber = accountNumber.substring(0, 3); + } else if (length == 3) { - if (length == 3) { + // fill with master account with 2 digits + masterAccountNumber = accountNumber.substring(0, 2); + } else if (length == 2) { - // fill with master account with 3 digits - String masterAccountNumber = accountNumber.substring(0, 2); - Account masterAccount = getMasterAccount(accountDAO, account, masterAccountNumber); - account.setMasterAccount(masterAccount); - - // let's fill master account - fillMasterAccountNumber(accountDAO, masterAccount); - return; + // fill with master account with 1 digits + masterAccountNumber = accountNumber.substring(0, 1); } + if (masterAccountNumber != null) { - if (length == 2) { - - // fill with master account with 3 digits - String masterAccountNumber = accountNumber.substring(0, 1); Account masterAccount = getMasterAccount(accountDAO, account, masterAccountNumber); + + if (log.isInfoEnabled()) { + log.info("Set masterAccount [" + masterAccountNumber + "] to account [" + accountNumber + "]"); + } account.setMasterAccount(masterAccount); // let's fill master account fillMasterAccountNumber(accountDAO, masterAccount); - return; } - - } protected Account getMasterAccount(AccountDAO accountDAO, Account subAccount, - String masterAccountNumber) throws TopiaException { + String accountNumber) throws TopiaException { Account masterAccount = - accountDAO.findByAccountNumber(masterAccountNumber); + accountDAO.findByAccountNumber(accountNumber); if (masterAccount == null) { // creates it - masterAccount = accountDAO.create(Account.PROPERTY_ACCOUNT_NUMBER, masterAccountNumber); + masterAccount = accountDAO.create(Account.PROPERTY_ACCOUNT_NUMBER, accountNumber); } masterAccount.addSubAccounts(subAccount); return masterAccount;