Fixes issue 30286: Adapt DBSourceManager to get versions in ModuleScriptHandler
authorCarlos Aristu <carlos.aristu@openbravo.com>
Tue, 21 Jul 2015 08:46:53 +0200
changeset 603 579b82cafcd8
parent 602 d5d21a543e48
child 604 86e6b8a2c9dd
Fixes issue 30286: Adapt DBSourceManager to get versions in ModuleScriptHandler
src/org/openbravo/ddlutils/task/AlterDatabaseDataAll.java
src/org/openbravo/ddlutils/task/AlterDatabaseDataMod.java
src/org/openbravo/ddlutils/task/ImportSampledata.java
src/org/openbravo/ddlutils/util/DBSMOBUtil.java
--- a/src/org/openbravo/ddlutils/task/AlterDatabaseDataAll.java	Wed May 27 14:58:33 2015 +0200
+++ b/src/org/openbravo/ddlutils/task/AlterDatabaseDataAll.java	Tue Jul 21 08:46:53 2015 +0200
@@ -169,6 +169,11 @@
       platform.alterTables(originaldb, db, !isFailonerror());
       getLog().info("Model update complete.");
 
+      // Initialize the ModuleScriptHandler that we will use later, to keep the current module
+      // versions, prior to the update
+      ModuleScriptHandler hd = new ModuleScriptHandler();
+      hd.setModulesVersionMap(DBSMOBUtil.getModulesVersion(platform));
+
       DBSMOBUtil.getInstance().moveModuleDataFromInstTables(platform, db, null);
       getLog().info("Disabling foreign keys");
       final Connection connection = platform.borrowConnection();
@@ -178,8 +183,6 @@
       platform.disableNOTNULLColumns(db, ad);
 
       // Executing modulescripts
-
-      ModuleScriptHandler hd = new ModuleScriptHandler();
       hd.setBasedir(new File(basedir + "/../"));
       hd.execute();
 
--- a/src/org/openbravo/ddlutils/task/AlterDatabaseDataMod.java	Wed May 27 14:58:33 2015 +0200
+++ b/src/org/openbravo/ddlutils/task/AlterDatabaseDataMod.java	Tue Jul 21 08:46:53 2015 +0200
@@ -144,6 +144,11 @@
       }
     }
     DBSMOBUtil.resetInstance();
+    // Initialize the ModuleScriptHandler that we will use later, to keep the current module
+    // versions, prior to the update
+    ModuleScriptHandler hd = new ModuleScriptHandler();
+    hd.setModulesVersionMap(DBSMOBUtil.getModulesVersion(platform));
+
     DBSMOBUtil.getInstance().moveModuleDataFromInstTables(platform, dbXML, module);
     ExcludeFilter excludeFilter = DBSMOBUtil.getInstance().getExcludeFilter(
         new File(model.getAbsolutePath() + "/../../../"));
@@ -322,7 +327,6 @@
       }
       Collections.sort(sortedModRows);
       for (String row : sortedModRows) {
-        ModuleScriptHandler hd = new ModuleScriptHandler();
         hd.setBasedir(new File(basedir + "/../"));
         hd.setModuleJavaPackage(row);
         hd.execute();
--- a/src/org/openbravo/ddlutils/task/ImportSampledata.java	Wed May 27 14:58:33 2015 +0200
+++ b/src/org/openbravo/ddlutils/task/ImportSampledata.java	Tue Jul 21 08:46:53 2015 +0200
@@ -26,7 +26,6 @@
 import org.apache.ddlutils.io.DataToArraySink;
 import org.apache.ddlutils.io.DatabaseDataIO;
 import org.apache.ddlutils.model.Database;
-import org.apache.ddlutils.model.DatabaseData;
 import org.apache.ddlutils.model.Table;
 import org.apache.tools.ant.BuildException;
 import org.openbravo.ddlutils.util.DBSMOBUtil;
@@ -35,12 +34,11 @@
 import org.xml.sax.SAXException;
 
 /**
- *
+ * 
  * @author huehner
  */
 public class ImportSampledata extends BaseDatabaseTask {
 
-
   private String basedir;
 
   public ImportSampledata() {
@@ -114,11 +112,11 @@
           log.info("Importing sampledata from folder: " + entry.getPath());
 
           /**
-           * If there is an AD_CLIENT.xml in the folder to import, check if that client is not already in the db.
-           * If it is -> skip it.
-           * If no AD_CLIENT.xml is present in the folder, skip the check. That allows for easy support of incremental
-           * sampledata loading into existing clients.
-           *
+           * If there is an AD_CLIENT.xml in the folder to import, check if that client is not
+           * already in the db. If it is -> skip it. If no AD_CLIENT.xml is present in the folder,
+           * skip the check. That allows for easy support of incremental sampledata loading into
+           * existing clients.
+           * 
            */
           File clientFile = new File(entry, "AD_CLIENT.xml");
           if (clientFile.exists()) {
@@ -162,6 +160,7 @@
       log.info("Running modulescripts...");
       try {
         ModuleScriptHandler hd = new ModuleScriptHandler();
+        hd.setModulesVersionMap(DBSMOBUtil.getModulesVersion(platform));
         hd.setBasedir(new File(basedir));
         hd.execute();
       } catch (Exception e) {
@@ -192,7 +191,8 @@
   /**
    * Parses a single sourcedata style database and returns it as a list of DynaBeans
    */
-  private static Vector<DynaBean> readOneDataXml(Database db, File file) throws IOException, SAXException {
+  private static Vector<DynaBean> readOneDataXml(Database db, File file) throws IOException,
+      SAXException {
     final DatabaseDataIO dbdio = new DatabaseDataIO();
     DataReader dataReader = dbdio.getConfiguredCompareDataReader(db);
 
@@ -205,7 +205,9 @@
 
   /**
    * Loads the Dynabean for the specified AD_CLIENT from the database
-   * @param clientId ad_client_id
+   * 
+   * @param clientId
+   *          ad_client_id
    */
   private DynaBean findClient(final Platform platform, Database db, String clientId) {
     Connection connection = platform.borrowConnection();
@@ -214,10 +216,10 @@
     OBDatasetTable dsTable = new OBDatasetTable();
     dsTable.setWhereclause("1=1");
     dsTable.setSecondarywhereclause("ad_client_id" + "=" + "'" + clientId + "'");
-    Vector<DynaBean> rowsNewData = dbIO.readRowsFromTableList(connection, platform, db,
-        table, dsTable, null);
+    Vector<DynaBean> rowsNewData = dbIO.readRowsFromTableList(connection, platform, db, table,
+        dsTable, null);
     platform.returnConnection(connection);
-    if (rowsNewData != null && rowsNewData.size() >0) {
+    if (rowsNewData != null && rowsNewData.size() > 0) {
       return rowsNewData.get(0);
     }
     return null;
--- a/src/org/openbravo/ddlutils/util/DBSMOBUtil.java	Wed May 27 14:58:33 2015 +0200
+++ b/src/org/openbravo/ddlutils/util/DBSMOBUtil.java	Tue Jul 21 08:46:53 2015 +0200
@@ -11,6 +11,7 @@
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.Properties;
 import java.util.StringTokenizer;
 import java.util.Vector;
@@ -290,6 +291,28 @@
     return databaseRevision;
   }
 
+  public static Map<String, String> getModulesVersion(Platform platform) {
+    final String sql = "SELECT ad_module_id AS moduleid, version AS version FROM ad_module";
+    final Connection connection = platform.borrowConnection();
+    ResultSet resultSet = null;
+    Map<String, String> moduleVersionMap = new HashMap<String, String>();
+    try {
+      final PreparedStatement statement = connection.prepareStatement(sql);
+      statement.execute();
+      resultSet = statement.getResultSet();
+      while (resultSet.next()) {
+        String moduleId = resultSet.getString("moduleid");
+        String moduleVersion = resultSet.getString("version");
+        moduleVersionMap.put(moduleId, moduleVersion);
+      }
+      resultSet.close();
+    } catch (final Exception e) {
+    } finally {
+      platform.returnConnection(connection);
+    }
+    return moduleVersionMap;
+  }
+
   public static void verifyRevision(Platform platform, String codeRevision, Logger _log) {
     String databaseRevision = getDBRevision(platform);