fixed bug 0009015: WAD should check that the compiling window/tabs must have an indentifier
authorAsier Lostalé <asier.lostale@openbravo.com>
Fri, 16 Oct 2009 11:23:50 +0200
changeset 5256 7bffda1b57ed
parent 5255 ca66387612a0
child 5257 1e072d3d7f4a
fixed bug 0009015: WAD should check that the compiling window/tabs must have an indentifier
build.xml
src-wad/src/org/openbravo/wad/Wad.java
src-wad/src/org/openbravo/wad/validation/WADValidationResult.java
src-wad/src/org/openbravo/wad/validation/WADValidator.java
src-wad/src/org/openbravo/wad/validation/WADValidator_data.xsql
src/build.xml
--- a/build.xml	Fri Oct 16 11:23:38 2009 +0200
+++ b/build.xml	Fri Oct 16 11:23:50 2009 +0200
@@ -105,9 +105,8 @@
   <property name="local" value="true" />
   <property name="apply.modules.complete.compilation" value="false" />
   <property name="calculate.core.revision" value="true"/>
+  <property name="stopOnWadError" value="true"/>
 
-
-  
   <available file=".hg" property="is.hg" />
 
   <condition property="calculate.core.rev">
--- a/src-wad/src/org/openbravo/wad/Wad.java	Fri Oct 16 11:23:38 2009 +0200
+++ b/src-wad/src/org/openbravo/wad/Wad.java	Fri Oct 16 11:23:50 2009 +0200
@@ -24,6 +24,7 @@
 import java.io.FileReader;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -44,6 +45,8 @@
 import org.openbravo.wad.controls.WADHidden;
 import org.openbravo.wad.controls.WADLabelControl;
 import org.openbravo.wad.controls.WadControlLabelBuilder;
+import org.openbravo.wad.validation.WADValidationResult;
+import org.openbravo.wad.validation.WADValidator;
 import org.openbravo.xmlEngine.XmlDocument;
 import org.openbravo.xmlEngine.XmlEngine;
 import org.xml.sax.helpers.DefaultHandler;
@@ -258,7 +261,12 @@
       else
         quick = argv[16].equals("quick");
 
-      // System.out.println("16: " + argv[16] + " l " + argv.length);
+      boolean failOnErrorVerification;
+      if (argv.length <= 17 || argv[17].equals("true")) {
+        failOnErrorVerification = true;
+      } else {
+        failOnErrorVerification = false;
+      }
 
       if (quick) {
         module = "%";
@@ -349,6 +357,32 @@
         return;
       }
 
+      // Calculate windows to generate
+      String strCurrentWindow;
+      final StringTokenizer st = new StringTokenizer(strWindowName, ",", false);
+      ArrayList<TabsData> td = new ArrayList<TabsData>();
+      while (st.hasMoreTokens()) {
+        strCurrentWindow = st.nextToken().trim();
+        TabsData tabsDataAux[];
+        if (quick)
+          tabsDataAux = TabsData.selectQuick(wad.pool);
+        else if (module.equals("%"))
+          tabsDataAux = TabsData.selectTabs(wad.pool, strCurrentWindow);
+        else
+          tabsDataAux = TabsData.selectTabsinModules(wad.pool, strCurrentWindow, module);
+        td.addAll(Arrays.asList(tabsDataAux));
+      }
+      TabsData[] tabsData = td.toArray(new TabsData[0]);
+      log4j.info(tabsData.length + " tabs to compile.");
+
+      log4j.info("Verifing tabs...");
+      WADValidator validator = new WADValidator(wad.pool, tabsData);
+      WADValidationResult validationResult = validator.validate();
+      validationResult.printLog();
+      if (validationResult.hasErrors() && failOnErrorVerification) {
+        throw new Exception("Tabs verification has errors");
+      }
+
       // Call to update the table identifiers
       log4j.info("Updating table identifiers");
       WadData.updateIdentifiers(wad.pool, quick ? "Y" : "N");
@@ -385,29 +419,18 @@
           log4j.info("No changes in web.xml");
       }
 
-      String strCurrentWindow;
-      final StringTokenizer st = new StringTokenizer(strWindowName, ",", false);
-      while (st.hasMoreTokens()) {
-        strCurrentWindow = st.nextToken().trim();
-        TabsData tabsData[];
-        if (quick)
-          tabsData = TabsData.selectQuick(wad.pool);
-        else if (module.equals("%"))
-          tabsData = TabsData.selectTabs(wad.pool, strCurrentWindow);
-        else
-          tabsData = TabsData.selectTabsinModules(wad.pool, strCurrentWindow, module);
-        if (tabsData.length == 0)
-          log4j.info("No windows to compile");
-        if (generateTabs) {
-          for (int i = 0; i < tabsData.length; i++) {
-            // don't compile if it is in an unactive branch
-            if (wad.allTabParentsActive(tabsData[i].tabid)) {
-              log4j.info("Processing Window: " + tabsData[i].windowname + " - Tab: "
-                  + tabsData[i].tabname + " - id: " + tabsData[i].tabid);
-              log4j.debug("Processing: " + tabsData[i].tabid);
-              wad.processTab(fileFin, fileFinReloads, tabsData[i], fileTrl, dirBaseTrl,
-                  translateStr, fileBase, fileBaseAplication);
-            }
+      if (tabsData.length == 0)
+        log4j.info("No windows to compile");
+
+      if (generateTabs) {
+        for (int i = 0; i < tabsData.length; i++) {
+          // don't compile if it is in an unactive branch
+          if (wad.allTabParentsActive(tabsData[i].tabid)) {
+            log4j.info("Processing Window: " + tabsData[i].windowname + " - Tab: "
+                + tabsData[i].tabname + " - id: " + tabsData[i].tabid);
+            log4j.debug("Processing: " + tabsData[i].tabid);
+            wad.processTab(fileFin, fileFinReloads, tabsData[i], fileTrl, dirBaseTrl, translateStr,
+                fileBase, fileBaseAplication);
           }
         }
       }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-wad/src/org/openbravo/wad/validation/WADValidationResult.java	Fri Oct 16 11:23:50 2009 +0200
@@ -0,0 +1,129 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html 
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License. 
+ * The Original Code is Openbravo ERP. 
+ * The Initial Developer of the Original Code is Openbravo SL 
+ * All portions are Copyright (C) 2009 Openbravo SL 
+ * All Rights Reserved. 
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+
+package org.openbravo.wad.validation;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+
+/**
+ * Contains information of all the warnings and errors detected during the WAD validation
+ * 
+ */
+public class WADValidationResult {
+  Logger log = Logger.getLogger(WADValidationResult.class);
+
+  /**
+   * Types of possible WAD validations, they have an identifier and a description
+   * 
+   */
+  public enum WADValidationType {
+    SQL("SQL"), MISSING_IDENTIFIER("Missing Identifier");
+
+    private String description;
+
+    WADValidationType(String description) {
+      this.description = description;
+    }
+
+    /**
+     * Returns the description for the validation type
+     * 
+     * @return description
+     */
+    public String getDescription() {
+      return description;
+    }
+  }
+
+  private HashMap<WADValidationType, List<String>> errors = new HashMap<WADValidationType, List<String>>();
+  private HashMap<WADValidationType, List<String>> warnings = new HashMap<WADValidationType, List<String>>();
+
+  /**
+   * Adds a warning message to a validation type
+   * 
+   * @param validationType
+   *          validation type to add the warning to
+   * @param warning
+   *          warning message
+   */
+  public void addWarning(WADValidationType validationType, String warning) {
+    addToResult(warnings, validationType, warning);
+  }
+
+  /**
+   * Adds an error message to a validation type
+   * 
+   * @param validationType
+   *          validation type to add the error to
+   * @param error
+   *          error message
+   */
+  public void addError(WADValidationType validationType, String warning) {
+    addToResult(errors, validationType, warning);
+  }
+
+  private void addToResult(Map<WADValidationType, List<String>> result,
+      WADValidationType validationType, String msg) {
+
+    List<String> msgList = result.get(validationType);
+    if (msgList == null) {
+      msgList = new ArrayList<String>();
+      result.put(validationType, msgList);
+    }
+    msgList.add(msg);
+  }
+
+  /**
+   * Returns true in case the validation contain errors
+   * 
+   * @return
+   */
+  public boolean hasErrors() {
+    return errors.size() > 0;
+  }
+
+  /**
+   * Prints the result in the log
+   */
+  public void printLog() {
+    for (WADValidationType type : warnings.keySet()) {
+      log.warn("+++++++++++++++++++++++++++++++++++++++++++++++++++");
+      log.warn("Warnings for Validation type: " + type.getDescription());
+      log.warn("+++++++++++++++++++++++++++++++++++++++++++++++++++");
+      for (String warn : warnings.get(type)) {
+        log.warn(warn);
+      }
+    }
+
+    for (WADValidationType type : errors.keySet()) {
+      log.error("+++++++++++++++++++++++++++++++++++++++++++++++++++");
+      log.error("Errors for Validation type: " + type.getDescription());
+      log.error("+++++++++++++++++++++++++++++++++++++++++++++++++++");
+      for (String warn : errors.get(type)) {
+        log.warn(warn);
+      }
+    }
+
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-wad/src/org/openbravo/wad/validation/WADValidator.java	Fri Oct 16 11:23:50 2009 +0200
@@ -0,0 +1,83 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html 
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License. 
+ * The Original Code is Openbravo ERP. 
+ * The Initial Developer of the Original Code is Openbravo SL 
+ * All portions are Copyright (C) 2009 Openbravo SL 
+ * All Rights Reserved. 
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+
+package org.openbravo.wad.validation;
+
+import org.openbravo.data.FieldProvider;
+import org.openbravo.database.ConnectionProvider;
+import org.openbravo.wad.validation.WADValidationResult.WADValidationType;
+
+/**
+ * Performs a series of validations for WAD tabs
+ * 
+ */
+public class WADValidator {
+  private FieldProvider[] tabs;
+  private ConnectionProvider conn;
+
+  /**
+   * Constructor
+   * 
+   * @param conn
+   *          Database ConnectionProvider
+   * @param tabs
+   *          Tabs to check
+   */
+  public WADValidator(ConnectionProvider conn, FieldProvider[] tabs) {
+    this.tabs = tabs;
+    this.conn = conn;
+  }
+
+  /**
+   * Performs the validations on the assigned tabs
+   * 
+   * @return the result of the validations
+   */
+  public WADValidationResult validate() {
+    WADValidationResult result = new WADValidationResult();
+    if (tabs != null && tabs.length > 0) {
+      validateIdentifier(result);
+    }
+    return result;
+  }
+
+  private void validateIdentifier(WADValidationResult result) {
+    try {
+      WADValidatorData data[] = WADValidatorData.select(conn, getTabIDs());
+      for (WADValidatorData issue : data) {
+        result.addError(WADValidationType.MISSING_IDENTIFIER, issue.windowname + " > "
+            + issue.tabname + ": table " + issue.tablename + " has not identifier.");
+      }
+    } catch (Exception e) {
+      result.addWarning(WADValidationType.SQL,
+          "Error when executing query for validating identifiers: " + e.getMessage());
+    }
+  }
+
+  private String getTabIDs() {
+    StringBuffer result = new StringBuffer();
+    for (FieldProvider tab : tabs) {
+      if (result.length() > 0) {
+        result.append(", ");
+      }
+      result.append("'").append(tab.getField("tabId")).append("'");
+    }
+    return result.toString();
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-wad/src/org/openbravo/wad/validation/WADValidator_data.xsql	Fri Oct 16 11:23:50 2009 +0200
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html 
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License. 
+ * The Original Code is Openbravo ERP. 
+ * The Initial Developer of the Original Code is Openbravo SL 
+ * All portions are Copyright (C) 2009 Openbravo SL 
+ * All Rights Reserved. 
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+-->
+
+<SqlClass name="WADValidatorData" package="org.openbravo.wad.validation">
+  <SqlMethod name="select" type="preparedStatement" return="multiple">
+    <Sql>
+      <![CDATA[
+        select tb.ad_window_id, tb.ad_tab_id, tb.ad_table_id, 
+               w.name as windowname, tb.name as tabname, t.tablename
+          from ad_tab tb, ad_table t, ad_window w
+         where not exists (select 1
+                             from ad_column c
+                            where c.ad_table_id = tb.ad_table_id
+                              and c.isidentifier = 'Y')
+          and w.ad_window_id = tb.ad_window_id
+          and t.ad_table_id = tb.ad_table_id
+          and tb.ad_tab_id in ('1')
+      ]]>
+    </Sql>
+    <Parameter name="tabs" optional="true" type="replace" after="and tb.ad_tab_id in (" text="'1'"/>
+  </SqlMethod>
+  
+</SqlClass>
--- a/src/build.xml	Fri Oct 16 11:23:38 2009 +0200
+++ b/src/build.xml	Fri Oct 16 11:23:50 2009 +0200
@@ -345,8 +345,8 @@
   </target>
 
   <target name="wad" depends="init">
-    <java classname="org.openbravo.wad.Wad" jvm="${env.JAVA_HOME}/bin/java" fork="yes" maxmemory="${build.maxmemory}">
-      <arg line="'${base.config}' '${tab}' '${build.AD}/org/openbravo/erpWindows' '${build.AD}/org/openbravo/erpCommon' '${build.sqlc}/src' '${webTab}' '${build.AD}/org/openbravo/erpCommon/ad_actionButton' '${base.design}' '${base.translate.structure}' '${client.web.xml}' '..' '${attach.path}' '${web.url}' '${base.src}' '${complete}' '${module}'" />
+    <java classname="org.openbravo.wad.Wad" jvm="${env.JAVA_HOME}/bin/java" fork="yes" maxmemory="${build.maxmemory}" failonerror="true">
+      <arg line="'${base.config}' '${tab}' '${build.AD}/org/openbravo/erpWindows' '${build.AD}/org/openbravo/erpCommon' '${build.sqlc}/src' '${webTab}' '${build.AD}/org/openbravo/erpCommon/ad_actionButton' '${base.design}' '${base.translate.structure}' '${client.web.xml}' '..' '${attach.path}' '${web.url}' '${base.src}' '${complete}' '${module}' '${stopOnWadError}'" />
       <classpath refid="project.class.path" />
     </java>
     <antcall target="postwad" inheritall="true" inheritrefs="true" />