[view] reuse gc config so that it's not computed per field
authorAsier Lostalé <asier.lostale@openbravo.com>
Fri, 03 Aug 2018 13:09:37 +0200
changeset 35062 a3107f6ba3a1
parent 35061 901a7335da1a
child 35063 7501cdfcf9c2
[view] reuse gc config so that it's not computed per field
modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewFieldHandler.java
modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewUtil.java
modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/reference/UIDefinition.java
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewFieldHandler.java	Fri Aug 03 09:48:00 2018 +0200
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewFieldHandler.java	Fri Aug 03 13:09:37 2018 +0200
@@ -18,6 +18,10 @@
  */
 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;
@@ -26,6 +30,7 @@
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
 
 import javax.script.ScriptException;
@@ -34,6 +39,7 @@
 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;
@@ -41,18 +47,23 @@
 import org.openbravo.base.model.domaintype.ForeignKeyDomainType;
 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.KernelUtils;
 import org.openbravo.client.kernel.reference.FKSearchUIDefinition;
 import org.openbravo.client.kernel.reference.StringUIDefinition;
 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.
@@ -86,6 +97,9 @@
 
   private List<Field> ignoredFields = new ArrayList<Field>();
 
+  private Optional<GCSystem> systemGridConfig;
+  private Map<String, Optional<GCTab>> tabsGridConfig;
+
   public Tab getTab() {
     return tab;
   }
@@ -790,11 +804,10 @@
     @Override
     public String getFieldProperties() {
       if (tab != null) {
-        gridConfiguration = OBViewUtil.getGridConfigurationSettings(auditTab);
-        return "";
-      } else {
-        return "";
+        gridConfiguration = OBViewUtil.getGridConfigurationSettings(auditTab,
+            getSystemGridConfig(), getTabGridConfig());
       }
+      return "";
     }
 
     @Override
@@ -1180,6 +1193,54 @@
     }
   }
 
+  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());
+  }
+
   public class OBViewField implements OBViewFieldDefinition {
     private Field field;
     private Property property;
@@ -1344,7 +1405,8 @@
 
     public String getFieldProperties() {
       // First obtain the gridConfigurationSettings which will be used in other places
-      getUIDefinition().establishGridConfigurationSettings(field);
+      getUIDefinition().establishGridConfigurationSettings(field, getSystemGridConfig(),
+          getTabGridConfig());
 
       if (getClientClass().length() > 0) {
         return "editorType: 'OBClientClassCanvasItem', ";
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewUtil.java	Fri Aug 03 09:48:00 2018 +0200
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/OBViewUtil.java	Fri Aug 03 13:09:37 2018 +0200
@@ -18,21 +18,18 @@
  */
 package org.openbravo.client.application.window;
 
-import java.util.Collections;
-import java.util.Comparator;
 import java.util.List;
+import java.util.Optional;
 
 import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
 import org.hibernate.Hibernate;
-import org.hibernate.criterion.Order;
 import org.openbravo.base.structure.BaseOBObject;
 import org.openbravo.client.application.GCField;
 import org.openbravo.client.application.GCSystem;
 import org.openbravo.client.application.GCTab;
 import org.openbravo.client.application.Parameter;
 import org.openbravo.dal.core.OBContext;
-import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.model.ad.ui.Element;
 import org.openbravo.model.ad.ui.Field;
@@ -173,8 +170,9 @@
    *          tab whose grid configuration is to be obtained.
    * @return the grid configuration
    */
-  public static JSONObject getGridConfigurationSettings(Tab tab) {
-    return getGridConfigurationSettings(null, tab);
+  public static JSONObject getGridConfigurationSettings(Tab tab, Optional<GCSystem> sysConf,
+      Optional<GCTab> tabConf) {
+    return getGridConfigurationSettings(null, tab, sysConf, tabConf);
   }
 
   /**
@@ -184,8 +182,9 @@
    *          field whose grid configuration is to be obtained
    * @return the grid configuration
    */
-  public static JSONObject getGridConfigurationSettings(Field field) {
-    return getGridConfigurationSettings(field, field.getTab());
+  public static JSONObject getGridConfigurationSettings(Field field, Optional<GCSystem> sysConf,
+      Optional<GCTab> tabConf) {
+    return getGridConfigurationSettings(field, field.getTab(), sysConf, tabConf);
   }
 
   /**
@@ -198,70 +197,36 @@
    *          parameter will be the tab of the field
    * @return the grid configuration
    */
-  private static JSONObject getGridConfigurationSettings(Field field, Tab tab) {
+  private static JSONObject getGridConfigurationSettings(Field field, Tab tab,
+      Optional<GCSystem> sysConf, Optional<GCTab> tabConf) {
     GridConfigSettings settings = new GridConfigSettings(field);
-    int gcTabIndex = 0;
-    GCTab tabConf = null;
-    if (tab.getOBUIAPPGCTabList().size() > 1) {
-      Collections.sort(tab.getOBUIAPPGCTabList(), new GCTabComparator());
-      gcTabIndex = tab.getOBUIAPPGCTabList().size() - 1;
-      tabConf = tab.getOBUIAPPGCTabList().get(gcTabIndex);
-    } else {
-      for (GCTab t : tab.getOBUIAPPGCTabList()) {
-        tabConf = t;
-        break;
+
+    if (tabConf.isPresent()) {
+      if (field != null && field.getId() != null) {
+        // field list is cached in memory, so can be reused for all fields without the need of reach
+        // DB again
+        Optional<GCField> fieldConf = tabConf.get().getOBUIAPPGCFieldList() //
+            .stream() //
+            .filter(fieldGC -> fieldGC.getField().getId().equals(field.getId())) //
+            .findFirst();
+        if (fieldConf.isPresent()) {
+          settings.processConfig(fieldConf.get());
+        }
+      }
+
+      if (settings.shouldContinueProcessing()) {
+        // Trying to get parameters from "Grid Configuration (Tab/Field)" -> "Tab" window
+        settings.processConfig(tabConf.get());
       }
     }
 
-    if (tabConf != null && field != null && field.getId() != null) {
-      GCField fieldConf = null;
-      for (GCField fc : tabConf.getOBUIAPPGCFieldList()) {
-        // field list is cached in memory, so can be reused for all fields without the need of reach
-        // DB again
-        if (fc.getField().getId().equals(field.getId())) {
-          fieldConf = fc;
-          break;
-        }
-      }
-
-      // Trying to get parameters from "Grid Configuration (Tab/Field)" -> "Field" window
-      if (fieldConf != null) {
-        settings.processConfig(fieldConf);
-      }
-    }
-
-    if (tabConf != null && settings.shouldContinueProcessing()) {
-      // Trying to get parameters from "Grid Configuration (Tab/Field)" -> "Tab" window
-      settings.processConfig(tabConf);
-    }
-
-    if (settings.shouldContinueProcessing()) {
-      // Trying to get parameters from "Grid Configuration (System)" window
-      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);
-      List<GCSystem> sysConfs = gcSystemCriteria.list();
-
-      if (!sysConfs.isEmpty()) {
-        settings.processConfig(sysConfs.get(0));
-      }
+    if (settings.shouldContinueProcessing() && sysConf.isPresent()) {
+      settings.processConfig(sysConf.get());
     }
 
     return settings.processJSONResult();
   }
 
-  private static class GCTabComparator implements Comparator<GCTab> {
-    @Override
-    public int compare(GCTab o1, GCTab o2) {
-      if (o1.getSeqno().compareTo(o2.getSeqno()) != 0) {
-        return o1.getSeqno().compareTo(o2.getSeqno());
-      } else {
-        return o1.getId().compareTo(o2.getId());
-      }
-    }
-  }
-
   private static class GridConfigSettings {
     private Boolean canSort = null;
     private Boolean canFilter = null;
--- a/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/reference/UIDefinition.java	Fri Aug 03 09:48:00 2018 +0200
+++ b/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/reference/UIDefinition.java	Fri Aug 03 13:09:37 2018 +0200
@@ -28,6 +28,7 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
@@ -41,6 +42,8 @@
 import org.openbravo.base.model.domaintype.PrimitiveDomainType;
 import org.openbravo.base.secureApp.VariablesSecureApp;
 import org.openbravo.base.weld.WeldUtils;
+import org.openbravo.client.application.GCSystem;
+import org.openbravo.client.application.GCTab;
 import org.openbravo.client.application.Parameter;
 import org.openbravo.client.application.window.ApplicationDictionaryCachedStructures;
 import org.openbravo.client.application.window.OBViewUtil;
@@ -558,9 +561,13 @@
    * 
    * @param field
    *          the field for which the information should be computed.
+   * @param systemGC
+   * @param tabGC
    */
-  public void establishGridConfigurationSettings(Field field) {
-    this.gridConfigurationSettings = OBViewUtil.getGridConfigurationSettings(field);
+  public void establishGridConfigurationSettings(Field field, Optional<GCSystem> systemGC,
+      Optional<GCTab> tabGC) {
+    this.gridConfigurationSettings = OBViewUtil
+        .getGridConfigurationSettings(field, systemGC, tabGC);
   }
 
   // note can make sense to also enable hover of values for enums