[view] reusing grid configuration even more by calculating it at StandardWindowComponent
authorAsier Lostalé <asier.lostale@openbravo.com>
Fri, 03 Aug 2018 13:44:05 +0200
changeset 35063 7501cdfcf9c2
parent 35062 a3107f6ba3a1
child 35064 293e7b1d09b5
[view] reusing grid configuration even more by calculating it at StandardWindowComponent
modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewFieldHandler.java
modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewParameterHandler.java
modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewTab.java
modules/org.openbravo.client.application/src/org/openbravo/client/application/window/StandardWindowComponent.java
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewFieldHandler.java	Fri Aug 03 13:09:37 2018 +0200
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewFieldHandler.java	Fri Aug 03 13:44:05 2018 +0200
@@ -18,10 +18,6 @@
  */
 package org.openbravo.client.application.window;
 
-import static java.util.stream.Collectors.groupingBy;
-import static java.util.stream.Collectors.toMap;
-
-import java.util.AbstractMap.SimpleEntry;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -39,7 +35,6 @@
 import org.apache.log4j.Logger;
 import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
-import org.hibernate.criterion.Order;
 import org.openbravo.base.expression.OBScriptEngine;
 import org.openbravo.base.model.Entity;
 import org.openbravo.base.model.ModelProvider;
@@ -55,15 +50,12 @@
 import org.openbravo.client.kernel.reference.UIDefinition;
 import org.openbravo.client.kernel.reference.UIDefinitionController;
 import org.openbravo.dal.core.DalUtil;
-import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
-import org.openbravo.dal.service.OBQuery;
 import org.openbravo.data.Sqlc;
 import org.openbravo.model.ad.ui.Element;
 import org.openbravo.model.ad.ui.Field;
 import org.openbravo.model.ad.ui.FieldGroup;
 import org.openbravo.model.ad.ui.Tab;
-import org.openbravo.model.ad.ui.Window;
 
 /**
  * The backing bean for generating the fields in the tab.
@@ -804,8 +796,8 @@
     @Override
     public String getFieldProperties() {
       if (tab != null) {
-        gridConfiguration = OBViewUtil.getGridConfigurationSettings(auditTab,
-            getSystemGridConfig(), getTabGridConfig());
+        gridConfiguration = OBViewUtil.getGridConfigurationSettings(auditTab, systemGridConfig,
+            getTabGridConfig());
       }
       return "";
     }
@@ -1193,52 +1185,8 @@
     }
   }
 
-  private Optional<GCSystem> getSystemGridConfig() {
-    if (systemGridConfig == null) {
-      OBCriteria<GCSystem> gcSystemCriteria = OBDal.getInstance().createCriteria(GCSystem.class);
-      gcSystemCriteria.addOrder(Order.desc(GCTab.PROPERTY_SEQNO));
-      gcSystemCriteria.addOrder(Order.desc(GCTab.PROPERTY_ID));
-      gcSystemCriteria.setMaxResults(1);
-      systemGridConfig = Optional.ofNullable((GCSystem) gcSystemCriteria.uniqueResult());
-    }
-    return systemGridConfig;
-  }
-
-  private Map<String, Optional<GCTab>> getTabsGridConfig(Window window) {
-    if (tabsGridConfig == null) {
-      // window comes from ADCS, we need to retrieve GC from DB as it might have changed
-      OBQuery<GCTab> qGCTab = OBDal.getInstance().createQuery(GCTab.class,
-          "as g where g.tab.window = :window");
-      qGCTab.setNamedParameter("window", window);
-      Map<String, List<GCTab>> gcsByTab = qGCTab.stream() //
-          .collect(groupingBy(gcTab -> gcTab.getTab().getId()));
-
-      tabsGridConfig = window.getADTabList().stream() //
-          .map(tb -> {
-            Optional<GCTab> selectedGC;
-            if (!gcsByTab.containsKey(tb.getId())) {
-              selectedGC = Optional.empty();
-            } else {
-              List<GCTab> candidates = gcsByTab.get(tb.getId());
-              Collections.sort(candidates, (o1, o2) -> {
-                if (o1.getSeqno().compareTo(o2.getSeqno()) != 0) {
-                  return o1.getSeqno().compareTo(o2.getSeqno());
-                } else {
-                  return o1.getId().compareTo(o2.getId());
-                }
-              });
-              selectedGC = Optional.of(candidates.get(candidates.size() - 1));
-            }
-
-            return new SimpleEntry<>(tb.getId(), selectedGC);
-          }) //
-          .collect(toMap(SimpleEntry::getKey, SimpleEntry::getValue));
-    }
-    return tabsGridConfig;
-  }
-
   private Optional<GCTab> getTabGridConfig() {
-    return getTabsGridConfig(tab.getWindow()).get(tab.getId());
+    return tabsGridConfig.get(tab.getId());
   }
 
   public class OBViewField implements OBViewFieldDefinition {
@@ -1405,7 +1353,7 @@
 
     public String getFieldProperties() {
       // First obtain the gridConfigurationSettings which will be used in other places
-      getUIDefinition().establishGridConfigurationSettings(field, getSystemGridConfig(),
+      getUIDefinition().establishGridConfigurationSettings(field, systemGridConfig,
           getTabGridConfig());
 
       if (getClientClass().length() > 0) {
@@ -2396,4 +2344,10 @@
     return (entity == null) ? false : entity.hasProperty("processed");
   }
 
+  public void setGCSettings(Optional<GCSystem> systemGridConfig,
+      Map<String, Optional<GCTab>> tabsGridConfig) {
+    this.systemGridConfig = systemGridConfig;
+    this.tabsGridConfig = tabsGridConfig;
+  }
+
 }
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewParameterHandler.java	Fri Aug 03 13:09:37 2018 +0200
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewParameterHandler.java	Fri Aug 03 13:44:05 2018 +0200
@@ -275,6 +275,8 @@
 
       final OBViewTab tabComponent = paramWindow.createComponent(OBViewTab.class);
       tabComponent.setTab(tab);
+      tabComponent.setGCSettings(StandardWindowComponent.getSystemGridConfig(),
+          StandardWindowComponent.getTabsGridConfig(tab.getWindow()));
       return tabComponent.generate();
     }
 
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewTab.java	Fri Aug 03 13:09:37 2018 +0200
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewTab.java	Fri Aug 03 13:44:05 2018 +0200
@@ -26,6 +26,7 @@
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 
 import javax.inject.Inject;
 
@@ -39,6 +40,8 @@
 import org.openbravo.client.application.ApplicationConstants;
 import org.openbravo.client.application.ApplicationUtils;
 import org.openbravo.client.application.DynamicExpressionParser;
+import org.openbravo.client.application.GCSystem;
+import org.openbravo.client.application.GCTab;
 import org.openbravo.client.kernel.BaseTemplateComponent;
 import org.openbravo.client.kernel.Component;
 import org.openbravo.client.kernel.ComponentProvider;
@@ -219,6 +222,11 @@
     return buttonFields;
   }
 
+  public void setGCSettings(Optional<GCSystem> systemGridConfig,
+      Map<String, Optional<GCTab>> tabsGridConfig) {
+    fieldHandler.setGCSettings(systemGridConfig, tabsGridConfig);
+  }
+
   public List<ButtonField> getAllButtonFields() {
     if (allButtonFields != null) {
       return allButtonFields;
@@ -793,6 +801,7 @@
         org.openbravo.client.application.Process newProcess = column.getOBUIAPPProcess();
         processId = newProcess.getId();
         url = "/";
+        // TODO: check if initialized?
         command = newProcess.getJavaClassName();
         newDefinition = true;
         uiPattern = newProcess.getUIPattern();
@@ -1149,4 +1158,5 @@
     }
 
   }
+
 }
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/StandardWindowComponent.java	Fri Aug 03 13:09:37 2018 +0200
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/StandardWindowComponent.java	Fri Aug 03 13:44:05 2018 +0200
@@ -18,14 +18,26 @@
  */
 package org.openbravo.client.application.window;
 
+import static java.util.stream.Collectors.groupingBy;
+import static java.util.stream.Collectors.toMap;
+
+import java.util.AbstractMap.SimpleEntry;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
+import java.util.Map;
+import java.util.Optional;
 
 import org.apache.log4j.Logger;
+import org.hibernate.criterion.Order;
+import org.openbravo.client.application.GCSystem;
+import org.openbravo.client.application.GCTab;
 import org.openbravo.client.kernel.BaseTemplateComponent;
 import org.openbravo.client.kernel.KernelConstants;
 import org.openbravo.client.kernel.Template;
+import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
+import org.openbravo.dal.service.OBQuery;
 import org.openbravo.erpCommon.obps.ActivationKey;
 import org.openbravo.erpCommon.obps.ActivationKey.FeatureRestriction;
 import org.openbravo.model.ad.ui.Field;
@@ -129,6 +141,9 @@
       return rootTabComponent;
     }
 
+    Optional<GCSystem> systemGridConfig = getSystemGridConfig();
+    Map<String, Optional<GCTab>> tabsGridConfig = getTabsGridConfig(window);
+
     final List<OBViewTab> tempTabs = new ArrayList<OBViewTab>();
     for (Tab tab : getWindow().getADTabList()) {
       // NOTE: grid sequence and field sequence tabs do not have any fields defined!
@@ -140,6 +155,7 @@
       final OBViewTab tabComponent = createComponent(OBViewTab.class);
       tabComponent.setTab(tab);
       tabComponent.setUniqueString(uniqueString);
+      tabComponent.setGCSettings(systemGridConfig, tabsGridConfig);
       tempTabs.add(tabComponent);
       final String processView = tabComponent.getProcessViews();
       if (!"".equals(processView)) {
@@ -200,4 +216,42 @@
   public List<String> getProcessViews() {
     return processViews;
   }
+
+  static Optional<GCSystem> getSystemGridConfig() {
+    OBCriteria<GCSystem> gcSystemCriteria = OBDal.getInstance().createCriteria(GCSystem.class);
+    gcSystemCriteria.addOrder(Order.desc(GCTab.PROPERTY_SEQNO));
+    gcSystemCriteria.addOrder(Order.desc(GCTab.PROPERTY_ID));
+    gcSystemCriteria.setMaxResults(1);
+    return Optional.ofNullable((GCSystem) gcSystemCriteria.uniqueResult());
+  }
+
+  static Map<String, Optional<GCTab>> getTabsGridConfig(Window window) {
+    // window comes from ADCS, we need to retrieve GC from DB as it might have changed
+    OBQuery<GCTab> qGCTab = OBDal.getInstance().createQuery(GCTab.class,
+        "as g where g.tab.window = :window");
+    qGCTab.setNamedParameter("window", window);
+    Map<String, List<GCTab>> gcsByTab = qGCTab.stream() //
+        .collect(groupingBy(gcTab -> gcTab.getTab().getId()));
+
+    return window.getADTabList().stream() //
+        .map(tb -> {
+          Optional<GCTab> selectedGC;
+          if (!gcsByTab.containsKey(tb.getId())) {
+            selectedGC = Optional.empty();
+          } else {
+            List<GCTab> candidates = gcsByTab.get(tb.getId());
+            Collections.sort(candidates, (o1, o2) -> {
+              if (o1.getSeqno().compareTo(o2.getSeqno()) != 0) {
+                return o1.getSeqno().compareTo(o2.getSeqno());
+              } else {
+                return o1.getId().compareTo(o2.getId());
+              }
+            });
+            selectedGC = Optional.of(candidates.get(candidates.size() - 1));
+          }
+
+          return new SimpleEntry<>(tb.getId(), selectedGC);
+        }) //
+        .collect(toMap(SimpleEntry::getKey, SimpleEntry::getValue));
+  }
 }