Related with issue 32769: Codereview improvements.
authorInigo Sanchez <inigo.sanchez@openbravo.com>
Sun, 15 May 2016 21:28:20 +0200
changeset 29506 9f9148601089
parent 29505 ea18c2c210a6
child 29507 7eb95ad4b19b
Related with issue 32769: Codereview improvements.
modules/org.openbravo.service.datasource/src-db/database/sourcedata/AD_REF_LIST.xml
modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/DataSourceServlet.java
src-db/database/sourcedata/AD_REF_LIST.xml
--- a/modules/org.openbravo.service.datasource/src-db/database/sourcedata/AD_REF_LIST.xml	Fri May 13 11:26:47 2016 +0200
+++ b/modules/org.openbravo.service.datasource/src-db/database/sourcedata/AD_REF_LIST.xml	Sun May 15 21:28:20 2016 +0200
@@ -24,6 +24,18 @@
 <!--23658853CF2D417B8FB94FE849DB3337-->  <AD_MODULE_ID><![CDATA[A44B9BA75C354D8FB2E3F7D6EB6BFDC4]]></AD_MODULE_ID>
 <!--23658853CF2D417B8FB94FE849DB3337--></AD_REF_LIST>
 
+<!--39FF39F25DD84390B4617D66E52C08EE--><AD_REF_LIST>
+<!--39FF39F25DD84390B4617D66E52C08EE-->  <AD_REF_LIST_ID><![CDATA[39FF39F25DD84390B4617D66E52C08EE]]></AD_REF_LIST_ID>
+<!--39FF39F25DD84390B4617D66E52C08EE-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--39FF39F25DD84390B4617D66E52C08EE-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--39FF39F25DD84390B4617D66E52C08EE-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--39FF39F25DD84390B4617D66E52C08EE-->  <VALUE><![CDATA[OBSERDS_CSVExportTranslateYesNoReference]]></VALUE>
+<!--39FF39F25DD84390B4617D66E52C08EE-->  <NAME><![CDATA[Translate Yes/No Reference in Export To CSV]]></NAME>
+<!--39FF39F25DD84390B4617D66E52C08EE-->  <DESCRIPTION><![CDATA[This preference allows the user to translate Yes/No references when uses export to CSV. If this preference is set to true the Yes/No references will translate when export to CSV.]]></DESCRIPTION>
+<!--39FF39F25DD84390B4617D66E52C08EE-->  <AD_REFERENCE_ID><![CDATA[A26BA480E2014707B47257024C3CBFF7]]></AD_REFERENCE_ID>
+<!--39FF39F25DD84390B4617D66E52C08EE-->  <AD_MODULE_ID><![CDATA[A44B9BA75C354D8FB2E3F7D6EB6BFDC4]]></AD_MODULE_ID>
+<!--39FF39F25DD84390B4617D66E52C08EE--></AD_REF_LIST>
+
 <!--62980B3E5545466D91CA8B2AC352AF72--><AD_REF_LIST>
 <!--62980B3E5545466D91CA8B2AC352AF72-->  <AD_REF_LIST_ID><![CDATA[62980B3E5545466D91CA8B2AC352AF72]]></AD_REF_LIST_ID>
 <!--62980B3E5545466D91CA8B2AC352AF72-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- a/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/DataSourceServlet.java	Fri May 13 11:26:47 2016 +0200
+++ b/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/DataSourceServlet.java	Sun May 15 21:28:20 2016 +0200
@@ -54,7 +54,6 @@
 import org.openbravo.base.exception.OBSecurityException;
 import org.openbravo.base.model.Entity;
 import org.openbravo.base.model.Property;
-import org.openbravo.base.model.domaintype.BooleanDomainType;
 import org.openbravo.base.model.domaintype.EnumerateDomainType;
 import org.openbravo.base.secureApp.VariablesSecureApp;
 import org.openbravo.base.session.OBPropertiesProvider;
@@ -289,6 +288,8 @@
 
   private class QueryJSONWriterToCSV extends DefaultJsonDataService.QueryResultWriter {
 
+    private static final String YES_NO_REFERENCE_ID = "20";
+
     Writer writer;
     String fieldSeparator;
     String decimalSeparator;
@@ -302,10 +303,12 @@
     List<String> dateTimeCols = new ArrayList<String>();
     List<String> timeCols = new ArrayList<String>();
     List<String> numericCols = new ArrayList<String>();
+    List<String> yesNoCols = new ArrayList<String>();
     Map<String, DecimalFormat> formats = new HashMap<String, DecimalFormat>();
     int clientUTCOffsetMiliseconds;
     TimeZone clientTimeZone;
-    Window windowToCsv = null;
+    String translatedLabelYes;
+    String translatedLabelNo;
 
     public QueryJSONWriterToCSV(HttpServletRequest request, HttpServletResponse response,
         Map<String, String> parameters, Entity entity) {
@@ -408,6 +411,8 @@
             }
           }
 
+          boolean preferenceCalculateFirst = true;
+          boolean translateYesNoReferences = false;
           for (String propKey : properties.keySet()) {
             final Property prop = properties.get(propKey);
             Column col = OBDal.getInstance().get(Column.class, prop.getColumnId());
@@ -462,11 +467,15 @@
               timeCols.add(propKey);
             } else if (prop.isPrimitive() && prop.isNumericType()) {
               numericCols.add(propKey);
-            }
-
-            // save current window to get preference properly
-            if (prop.getDomainType() instanceof BooleanDomainType) {
-              windowToCsv = window;
+            } else if (isYesNoReference(prop)) {
+              // Calculate if it is needed translate YesNo reference in export to CSV.
+              if (preferenceCalculateFirst) {
+                translateYesNoReferences = translateYesNoReferencesInCsv(window);
+                preferenceCalculateFirst = false;
+              }
+              if (translateYesNoReferences) {
+                yesNoCols.add(propKey);
+              }
             }
 
             if (!(prop.getDomainType() instanceof EnumerateDomainType)) {
@@ -518,6 +527,23 @@
       }
     }
 
+    private boolean isYesNoReference(Property prop) {
+      final Column column = OBDal.getInstance().get(Column.class, prop.getColumnId());
+      return YES_NO_REFERENCE_ID.equals((String) DalUtil.getId(column.getReference()));
+    }
+
+    private boolean translateYesNoReferencesInCsv(Window windowToCsv) {
+      boolean shouldCheck = false;
+      try {
+        shouldCheck = "Y".equals(Preferences.getPreferenceValue(
+            "OBSERDS_CSVExportTranslateYesNoReference", true, OBContext.getOBContext()
+                .getCurrentClient(), OBContext.getOBContext().getCurrentOrganization(), OBContext
+                .getOBContext().getUser(), OBContext.getOBContext().getRole(), windowToCsv));
+      } catch (PropertyException prefNotDefined) {
+      }
+      return shouldCheck;
+    }
+
     private void writeJSONProperties(JSONObject row) {
       final Iterator<?> itKeysF = row.keys();
       Vector<String> keys = new Vector<String>();
@@ -560,8 +586,6 @@
         }
 
         boolean isFirst = true;
-        boolean shouldCheckTranslation = false;
-        boolean preferenceCalculateFirst = true;
         while (itKeys.hasNext()) {
           String key = (String) itKeys.next();
           if (key.endsWith(JsonConstants.IDENTIFIER)) {
@@ -629,22 +653,8 @@
             SimpleDateFormat timeFormat = JsonUtils.createTimeFormatWithoutGMTOffset();
             timeFormat.setLenient(true);
             keyValue = timeFormat.format(clientTimezoneDate);
-          } else if (keyValue instanceof Boolean && keyValue != null) {
-            // Calculate if it is needed translate YesNo reference in export to csv.
-            if (preferenceCalculateFirst == true) {
-              shouldCheckTranslation = getValueShouldCheckTranslationPreference();
-              preferenceCalculateFirst = false;
-            }
-            if (shouldCheckTranslation) {
-              String userLanguage = OBContext.getOBContext().getLanguage().getLanguage();
-              if (keyValue.toString().equals("true")) {
-                keyValue = Utility.messageBD(new DalConnectionProvider(false), "OBUISC_Yes",
-                    userLanguage);
-              } else if (keyValue.toString().equals("false")) {
-                keyValue = Utility.messageBD(new DalConnectionProvider(false), "OBUISC_No",
-                    userLanguage);
-              }
-            }
+          } else if (yesNoCols.contains(key) && keyValue != null) {
+            keyValue = (Boolean) keyValue ? getTranslatedLabelYes() : getTranslatedLabelNo();
           }
 
           if (keyValue != null && !keyValue.toString().equals("null")) {
@@ -662,16 +672,23 @@
       }
     }
 
-    private boolean getValueShouldCheckTranslationPreference() {
-      boolean shouldCheck = false;
-      try {
-        shouldCheck = "Y".equals(Preferences.getPreferenceValue("AllowExportCSV_YesNoTranslated",
-            true, OBContext.getOBContext().getCurrentClient(), OBContext.getOBContext()
-                .getCurrentOrganization(), OBContext.getOBContext().getUser(), OBContext
-                .getOBContext().getRole(), windowToCsv));
-      } catch (PropertyException prefNotDefined) {
+    private String getTranslatedLabelYes() {
+      if (translatedLabelYes == null) {
+        translatedLabelYes = getTranslatedLabel("OBUISC_Yes");
       }
-      return shouldCheck;
+      return translatedLabelYes;
+    }
+
+    private String getTranslatedLabelNo() {
+      if (translatedLabelNo == null) {
+        translatedLabelNo = getTranslatedLabel("OBUISC_No");
+      }
+      return translatedLabelNo;
+    }
+
+    private String getTranslatedLabel(String label) {
+      String userLanguage = OBContext.getOBContext().getLanguage().getLanguage();
+      return Utility.messageBD(new DalConnectionProvider(false), label, userLanguage);
     }
 
     private Date convertFromLocalToClientTimezone(Date localDate) {
--- a/src-db/database/sourcedata/AD_REF_LIST.xml	Fri May 13 11:26:47 2016 +0200
+++ b/src-db/database/sourcedata/AD_REF_LIST.xml	Sun May 15 21:28:20 2016 +0200
@@ -8897,17 +8897,6 @@
 <!--3946D07DF63C47E7A11BFADCF160D6B6-->  <SEQNO><![CDATA[20]]></SEQNO>
 <!--3946D07DF63C47E7A11BFADCF160D6B6--></AD_REF_LIST>
 
-<!--39FF39F25DD84390B4617D66E52C08EE--><AD_REF_LIST>
-<!--39FF39F25DD84390B4617D66E52C08EE-->  <AD_REF_LIST_ID><![CDATA[39FF39F25DD84390B4617D66E52C08EE]]></AD_REF_LIST_ID>
-<!--39FF39F25DD84390B4617D66E52C08EE-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
-<!--39FF39F25DD84390B4617D66E52C08EE-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
-<!--39FF39F25DD84390B4617D66E52C08EE-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
-<!--39FF39F25DD84390B4617D66E52C08EE-->  <VALUE><![CDATA[AllowExportCSV_YesNoTranslated]]></VALUE>
-<!--39FF39F25DD84390B4617D66E52C08EE-->  <NAME><![CDATA[Allow Export to CSV YesNo translated]]></NAME>
-<!--39FF39F25DD84390B4617D66E52C08EE-->  <AD_REFERENCE_ID><![CDATA[A26BA480E2014707B47257024C3CBFF7]]></AD_REFERENCE_ID>
-<!--39FF39F25DD84390B4617D66E52C08EE-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
-<!--39FF39F25DD84390B4617D66E52C08EE--></AD_REF_LIST>
-
 <!--3A38B5315B64459793BF256CEB6703B4--><AD_REF_LIST>
 <!--3A38B5315B64459793BF256CEB6703B4-->  <AD_REF_LIST_ID><![CDATA[3A38B5315B64459793BF256CEB6703B4]]></AD_REF_LIST_ID>
 <!--3A38B5315B64459793BF256CEB6703B4-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>