# HG changeset patch
# User Javier Armendáriz <javier.armendariz@openbravo.com>
# Date 1553690972 -3600
#      Wed Mar 27 13:49:32 2019 +0100
# Node ID 72d396653a7430671532a08e6422c192b09ec850
# Parent  e29acd5ca4b7f05d86037edb120af478f6ca89b7
Fixed issue 40449: Performance problems in Show Audit Trail popup.

Added a new preference 'ShowAuditTrailUserFilter' that, when its set to 'N',
will hide the User selector in audit trail filter fields. In case the number
of users available are really high, this will prevent to load all of them and
thus improving popup performance.

diff --git a/src-db/database/sourcedata/AD_REF_LIST.xml b/src-db/database/sourcedata/AD_REF_LIST.xml
--- a/src-db/database/sourcedata/AD_REF_LIST.xml
+++ b/src-db/database/sourcedata/AD_REF_LIST.xml
@@ -10030,6 +10030,18 @@
 <!--6912EEA43DD54AD7AEB11F28AF34E9C4-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
 <!--6912EEA43DD54AD7AEB11F28AF34E9C4--></AD_REF_LIST>
 
+<!--69169157E1904E88821128E7FB08DC39--><AD_REF_LIST>
+<!--69169157E1904E88821128E7FB08DC39-->  <AD_REF_LIST_ID><![CDATA[69169157E1904E88821128E7FB08DC39]]></AD_REF_LIST_ID>
+<!--69169157E1904E88821128E7FB08DC39-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--69169157E1904E88821128E7FB08DC39-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--69169157E1904E88821128E7FB08DC39-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--69169157E1904E88821128E7FB08DC39-->  <VALUE><![CDATA[ShowAuditTrailUserFilter]]></VALUE>
+<!--69169157E1904E88821128E7FB08DC39-->  <NAME><![CDATA[Show Audit Trail User filter]]></NAME>
+<!--69169157E1904E88821128E7FB08DC39-->  <DESCRIPTION><![CDATA[If set to 'N', user list in audit trail filter won't be shown, else it will be shown]]></DESCRIPTION>
+<!--69169157E1904E88821128E7FB08DC39-->  <AD_REFERENCE_ID><![CDATA[A26BA480E2014707B47257024C3CBFF7]]></AD_REFERENCE_ID>
+<!--69169157E1904E88821128E7FB08DC39-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--69169157E1904E88821128E7FB08DC39--></AD_REF_LIST>
+
 <!--6952F41D079442D1B332D5C2E2DBCDE9--><AD_REF_LIST>
 <!--6952F41D079442D1B332D5C2E2DBCDE9-->  <AD_REF_LIST_ID><![CDATA[6952F41D079442D1B332D5C2E2DBCDE9]]></AD_REF_LIST_ID>
 <!--6952F41D079442D1B332D5C2E2DBCDE9-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
diff --git a/src/org/openbravo/erpCommon/businessUtility/AuditTrailPopup.java b/src/org/openbravo/erpCommon/businessUtility/AuditTrailPopup.java
--- a/src/org/openbravo/erpCommon/businessUtility/AuditTrailPopup.java
+++ b/src/org/openbravo/erpCommon/businessUtility/AuditTrailPopup.java
@@ -11,7 +11,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2009-2016 Openbravo SLU 
+ * All portions are Copyright (C) 2009-2019 Openbravo SLU
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -58,6 +58,7 @@
 import org.openbravo.erpCommon.obps.ActivationKey.FeatureRestriction;
 import org.openbravo.erpCommon.utility.ComboTableData;
 import org.openbravo.erpCommon.utility.OBError;
+import org.openbravo.erpCommon.utility.PropertyException;
 import org.openbravo.erpCommon.utility.SQLReturnObject;
 import org.openbravo.erpCommon.utility.Utility;
 import org.openbravo.erpCommon.utility.UtilityData;
@@ -253,8 +254,17 @@
     log4j.debug(
         "POPUP-HISTORY - tabId: " + tabId + ", tableId: " + tableId + ", inpRecordId: " + recordId);
 
+    List<String> discards = new ArrayList<>();
+
+    if (!shouldShowUserFilter(vars)) {
+      discards.add("userSelect");
+    }
+
+    String[] discard = new String[discards.size()];
+    discards.toArray(discard);
+
     XmlDocument xmlDocument = xmlEngine
-        .readXmlTemplate("org/openbravo/erpCommon/businessUtility/AuditTrailPopupHistory")
+        .readXmlTemplate("org/openbravo/erpCommon/businessUtility/AuditTrailPopupHistory", discard)
         .createXmlDocument();
 
     xmlDocument.setParameter("directory", "var baseDirectory = \"" + strReplaceWith + "/\";\n");
@@ -279,16 +289,7 @@
     xmlDocument.setParameter("dateTosaveFormat", vars.getJavaDataTimeFormat());
 
     // user combobox (restricted to login users only)
-    try {
-      ComboTableData cmd = new ComboTableData(vars, this, "19", "AD_User_ID", "",
-          "C48E4CAE3C2A4C5DBC2E011D8AD2C428",
-          Utility.getContext(this, vars, "#AccessibleOrgTree", "AuditTrailPopup"),
-          Utility.getContext(this, vars, "#User_Client", "AuditTrailPopup"), 0);
-      cmd.fillParameters(null, "AuditTrailPopup", "");
-      xmlDocument.setData("reportAD_User_ID", "liststructure", cmd.select(false));
-    } catch (Exception e) {
-      log4j.error("Error getting adUser combo content", e);
-    }
+    fillUserComboData(vars, xmlDocument);
 
     // fields combobox (filtered to be in same tab)
     try {
@@ -360,6 +361,30 @@
     out.close();
   }
 
+  private void fillUserComboData(VariablesSecureApp vars, XmlDocument xmlDocument) {
+    if (shouldShowUserFilter(vars)) {
+      try {
+        ComboTableData cmd = new ComboTableData(vars, this, "19", "AD_User_ID", "",
+            "C48E4CAE3C2A4C5DBC2E011D8AD2C428",
+            Utility.getContext(this, vars, "#AccessibleOrgTree", "AuditTrailPopup"),
+            Utility.getContext(this, vars, "#User_Client", "AuditTrailPopup"), 0);
+        cmd.fillParameters(null, "AuditTrailPopup", "");
+        xmlDocument.setData("reportAD_User_ID", "liststructure", cmd.select(false));
+      } catch (Exception e) {
+        log4j.error("Error getting adUser combo content", e);
+      }
+    }
+  }
+
+  private boolean shouldShowUserFilter(VariablesSecureApp vars) {
+    try {
+      return !"N".equals(Preferences.getPreferenceValue("ShowAuditTrailUserFilter", true,
+          vars.getClient(), vars.getOrg(), vars.getUser(), vars.getRole(), null));
+    } catch (PropertyException e) {
+      return true;
+    }
+  }
+
   /**
    * Gets the translated name of the business element stored in a table.
    * 
@@ -402,7 +427,7 @@
     // tab link from the deleted records view
     String parentLinkFilter = vars.getStringParameter("inpParentLinkFilter", IsIDFilter.instance);
     boolean haveParentLink = (parentLinkFilter != null && !parentLinkFilter.isEmpty());
-    List<String> discards = new ArrayList<String>();
+    List<String> discards = new ArrayList<>();
     if (haveParentLink) {
       discards.add("discardLinkBack");
     }
@@ -426,6 +451,11 @@
       }
       links.setLength(links.length() - 2);
     }
+
+    if (!shouldShowUserFilter(vars)) {
+      discards.add("userSelect");
+    }
+
     String[] discard = new String[discards.size()];
     discards.toArray(discard);
     XmlDocument xmlDocument = xmlEngine
@@ -455,17 +485,7 @@
     xmlDocument.setParameter("dateTodisplayFormat", vars.getSessionValue("#AD_SqlDateTimeFormat"));
     xmlDocument.setParameter("dateTosaveFormat", vars.getSessionValue("#AD_SqlDateTimeFormat"));
 
-    // user combobox (restricted to login users only)
-    try {
-      ComboTableData cmd = new ComboTableData(vars, this, "19", "AD_User_ID", "",
-          "C48E4CAE3C2A4C5DBC2E011D8AD2C428",
-          Utility.getContext(this, vars, "#AccessibleOrgTree", "AuditTrailPopup"),
-          Utility.getContext(this, vars, "#User_Client", "AuditTrailPopup"), 0);
-      cmd.fillParameters(null, "AuditTrailPopup", "");
-      xmlDocument.setData("reportAD_User_ID", "liststructure", cmd.select(false));
-    } catch (Exception e) {
-      log4j.error("Error getting adUser combo content", e);
-    }
+    fillUserComboData(vars, xmlDocument);
 
     // param for building 'Back to history' link
     xmlDocument.setParameter("recordId", recordId);
diff --git a/src/org/openbravo/erpCommon/businessUtility/AuditTrailPopupDeleted.html b/src/org/openbravo/erpCommon/businessUtility/AuditTrailPopupDeleted.html
--- a/src/org/openbravo/erpCommon/businessUtility/AuditTrailPopupDeleted.html
+++ b/src/org/openbravo/erpCommon/businessUtility/AuditTrailPopupDeleted.html
@@ -11,7 +11,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2009-2011 Openbravo SLU 
+ * All portions are Copyright (C) 2009-2019 Openbravo SLU
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -370,8 +370,8 @@
 			         
 			       </tr>
 			       <tr>
-                     <td class="TitleCell"><span class="LabelText">User</span></td>
-                     <td colspan="2" class="Combo_ContentCell">
+                     <td class="TitleCell" id="userSelect"><span class="LabelText">User</span></td>
+                     <td colspan="2" class="Combo_ContentCell" id="userSelect">
                        <select class="Combo Combo_TwoCells_width" name="inpUser" id="reportAD_User_ID_S">
                          <option value=""></option>
                          <div id="reportAD_User_ID"></div>
diff --git a/src/org/openbravo/erpCommon/businessUtility/AuditTrailPopupHistory.html b/src/org/openbravo/erpCommon/businessUtility/AuditTrailPopupHistory.html
--- a/src/org/openbravo/erpCommon/businessUtility/AuditTrailPopupHistory.html
+++ b/src/org/openbravo/erpCommon/businessUtility/AuditTrailPopupHistory.html
@@ -11,7 +11,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2009-2011 Openbravo SLU 
+ * All portions are Copyright (C) 2009-2019 Openbravo SLU
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -365,8 +365,8 @@
 			         
 			       </tr>
 			       <tr>
-                     <td class="TitleCell"><span class="LabelText">User</span></td>
-                     <td colspan="2" class="Combo_ContentCell">
+                     <td class="TitleCell" id="userSelect"><span class="LabelText">User</span></td>
+                     <td colspan="2" class="Combo_ContentCell" id="userSelect">
                        <select class="Combo Combo_TwoCells_width" name="inpUser" id="reportAD_User_ID_S">
                          <option value=""></option>
                          <div id="reportAD_User_ID"></div>
