Fixes Issue 0024638: An error appears when using a field in the tab display logic which is present in the header and also in the tab
authorShankar Balachandran <shankar.balachandran@openbravo.com>
Thu, 19 Sep 2013 11:04:32 +0530
changeset 21165 06606d1fa59d
parent 21164 336122139550
child 21166 407564ea9109
Fixes Issue 0024638: An error appears when using a field in the tab display logic which is present in the header and also in the tab

If a window has more than one tab with the same entity then getParentTab was returning incorrect values.
getParentTab has been modified to look at the logical arrangement of tabs alone and retrieve the parent tab.
modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/KernelUtils.java
modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/KernelUtils_data.xsql
--- a/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/KernelUtils.java	Thu Sep 19 04:12:36 2013 +0200
+++ b/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/KernelUtils.java	Thu Sep 19 11:04:32 2013 +0530
@@ -20,9 +20,10 @@
 
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.List;
 
+import javax.servlet.ServletException;
+
 import org.apache.commons.lang.StringEscapeUtils;
 import org.apache.log4j.Logger;
 import org.codehaus.jettison.json.JSONException;
@@ -33,17 +34,18 @@
 import org.openbravo.base.model.ModelProvider;
 import org.openbravo.base.model.Property;
 import org.openbravo.base.structure.BaseOBObject;
-import org.openbravo.client.application.ApplicationConstants;
 import org.openbravo.dal.core.DalUtil;
 import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
+import org.openbravo.database.ConnectionProvider;
 import org.openbravo.erpCommon.utility.OBMessageUtils;
 import org.openbravo.model.ad.datamodel.Column;
 import org.openbravo.model.ad.module.Module;
 import org.openbravo.model.ad.module.ModuleDependency;
 import org.openbravo.model.ad.ui.Field;
 import org.openbravo.model.ad.ui.Tab;
+import org.openbravo.service.db.DalConnectionProvider;
 
 /**
  * Contains several utility methods used in the kernel.
@@ -385,47 +387,17 @@
    * @return The parent tab of the given tab
    */
   public Tab getParentTab(Tab tab) {
-    List<Tab> tabsOfWindow = tab.getWindow().getADTabList();
-    ArrayList<Entity> entities = new ArrayList<Entity>();
-    HashMap<Entity, Tab> tabOfEntity = new HashMap<Entity, Tab>();
-    Entity theEntity = null;
-    if (ApplicationConstants.DATASOURCEBASEDTABLE.equals(tab.getTable().getDataOriginType())) {
-      theEntity = ModelProvider.getInstance().getEntityByTableId(tab.getTable().getId());
-    } else {
-      theEntity = ModelProvider.getInstance().getEntityByTableName(tab.getTable().getDBTableName());
-    }
-
-    for (Tab aTab : tabsOfWindow) {
-      Entity entity = null;
-      if (ApplicationConstants.DATASOURCEBASEDTABLE.equals(aTab.getTable().getDataOriginType())) {
-        entity = ModelProvider.getInstance().getEntityByTableId(tab.getTable().getId());
-      } else {
-        entity = ModelProvider.getInstance().getEntityByTableName(aTab.getTable().getDBTableName());
+    ConnectionProvider connection = new DalConnectionProvider();
+    Tab targetTab = null;
+    String tabId = null;
+    try {
+      tabId = KernelUtilsData.getParentTab(connection, tab.getWindow().getId(), tab.getTabLevel()
+          .toString(), tab.getSequenceNumber().toString());
+      if (tabId != null) {
+        targetTab = OBDal.getInstance().get(Tab.class, tabId);
       }
-      entities.add(entity);
-      if (!tabOfEntity.containsKey(entity)){
-        tabOfEntity.put(entity, aTab);
-      }
-    }
-
-    if (tab.getColumn() != null) {
-      final String colId = (String) DalUtil.getId(tab.getColumn());
-      for (Property prop : theEntity.getProperties()) {
-        if (prop.getColumnId() != null && prop.getColumnId().equals(colId)
-            && prop.getTargetEntity() != null && prop.isParent()
-            && tabOfEntity.containsKey(prop.getTargetEntity())) {
-          return tabOfEntity.get(prop.getTargetEntity());
-        }
-      }
-    }
-
-    Tab targetTab = null;
-    for (Property property : theEntity.getProperties()) {
-      if (property.isParent()) {
-        if (property.getTargetEntity() != null && entities.contains(property.getTargetEntity())) {
-          targetTab = tabOfEntity.get(property.getTargetEntity());
-        }
-      }
+    } catch (ServletException e) {
+      log.error(e.getMessage(), e);
     }
     return targetTab;
   }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/KernelUtils_data.xsql	Thu Sep 19 11:04:32 2013 +0530
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.1  (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 SLU
+ * All portions are Copyright (C) 2013 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+-->
+
+<SqlClass name="KernelUtilsData" package="org.openbravo.client.kernel">
+   <SqlClassComment></SqlClassComment>
+
+   <SqlMethod name="getParentTab" type="preparedStatement" return="String">
+    <Sql><![CDATA[
+        select t.ad_tab_id
+        from ad_tab t
+        where t.ad_window_id = ?
+        and t.seqno =
+        (
+        select max(seqno)
+        from ad_tab
+        where ad_window_id = t.ad_window_id
+        and tabLevel = (TO_NUMBER(?)-1)
+        and seqno < TO_NUMBER(?)
+        )
+    ]]></Sql>
+    <Parameter name="windowId"/>
+    <Parameter name="tabLevel"/>
+    <Parameter name="sequenceNumber"/>
+  </SqlMethod>
+
+</SqlClass>