details:   https://code.openbravo.com/erp/devel/pi/rev/90d07920e4b6
changeset: 32977:90d07920e4b6
user:      Carlos Aristu <carlos.aristu <at> openbravo.com>
date:      Fri Nov 10 08:26:33 2017 +0100
summary:   related to issue 37267: move static js code from ftl to a js file

details:   https://code.openbravo.com/erp/devel/pi/rev/904f31b433ec
changeset: 32978:904f31b433ec
user:      Carlos Aristu <carlos.aristu <at> openbravo.com>
date:      Fri Nov 10 08:29:38 2017 +0100
summary:   fixes issue 37267: improve readability of getLabels() method

  Besides improving the readability, not the that the active flag when looking 
for the translations is now taken into account. Otherwise, translations of 
deactivated messages were being sent to the client.

diffstat:

 
modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/I18NComponent.java
           |   27 +--
 
modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/KernelComponentProvider.java
 |    6 +-
 
modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/templates/i18n.ftl
           |   76 +-------
 
modules/org.openbravo.client.kernel/web/org.openbravo.client.kernel/js/ob-i18n.js
                |  100 ++++++++++
 4 files changed, 112 insertions(+), 97 deletions(-)

diffs (295 lines):

diff -r 1aeebc4d61e0 -r 904f31b433ec 
modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/I18NComponent.java
--- 
a/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/I18NComponent.java
    Thu Nov 09 13:52:00 2017 +0100
+++ 
b/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/I18NComponent.java
    Fri Nov 10 08:29:38 2017 +0100
@@ -18,18 +18,14 @@
  */
 package org.openbravo.client.kernel;
 
-import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
 import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.dal.service.OBQuery;
-import org.openbravo.model.ad.module.Module;
 import org.openbravo.model.ad.ui.Message;
 import org.openbravo.model.ad.ui.MessageTrl;
 
@@ -72,35 +68,24 @@
    * @return a collection of labels.
    */
   public Collection<Label> getLabels() {
-    final Map<String, Label> labels = new HashMap<String, Label>();
+    final Map<String, Label> labels = new HashMap<>();
     OBContext.setAdminMode();
     try {
-      final OBQuery<Module> moduleQuery = OBDal.getInstance()
-          .createQuery(Module.class, "id != '0'");
-      final List<String> modules = new ArrayList<String>();
-      for (Module module : moduleQuery.list()) {
-        modules.add(module.getId());
-      }
-
-      if (modules.isEmpty()) {
-        return Collections.emptyList();
-      }
-
       // first read the labels from the base table
       final OBQuery<Message> messages = 
OBDal.getInstance().createQuery(Message.class,
-          "module.id in (:modules) or includeInI18N='Y'");
-      messages.setNamedParameter("modules", modules);
+          "module.id!='0' or includeInI18N='Y'");
       for (Message message : messages.list()) {
         final Label label = new Label();
         label.setKey(message.getSearchKey());
         label.setValue(message.getMessageText());
         labels.put(message.getSearchKey(), label);
       }
+
       final OBQuery<MessageTrl> messagesTrl = OBDal
           .getInstance()
-          .createQuery(MessageTrl.class,
-              "(message.module.id in (:modules) or message.includeInI18N='Y') 
and language.id=:languageId");
-      messagesTrl.setNamedParameter("modules", modules);
+          .createQuery(
+              MessageTrl.class,
+              "(message.module.id!='0' or message.includeInI18N='Y') and 
message.active = true and language.id=:languageId");
       messagesTrl.setNamedParameter("languageId", 
OBContext.getOBContext().getLanguage().getId());
       for (MessageTrl message : messagesTrl.list()) {
         final Label label = new Label();
diff -r 1aeebc4d61e0 -r 904f31b433ec 
modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/KernelComponentProvider.java
--- 
a/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/KernelComponentProvider.java
  Thu Nov 09 13:52:00 2017 +0100
+++ 
b/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/KernelComponentProvider.java
  Fri Nov 10 08:29:38 2017 +0100
@@ -73,6 +73,7 @@
 
   // in case of the application component also make it role/org dependent, this
   // also covers client dependency
+  @Override
   public String getVersionParameters(String resource) {
     final String versionParam = super.getVersionParameters(resource);
     if (resource.contains(KernelConstants.APPLICATION_COMPONENT_ID)
@@ -88,7 +89,7 @@
     if (globalResources != null) {
       return globalResources;
     }
-    globalResources = new ArrayList<ComponentResource>();
+    globalResources = new ArrayList<>();
 
     globalResources.add(createStaticResource("org.openbravo.client.kernel/"
         + KernelConstants.KERNEL_COMPONENT_TYPE + "/" + 
KernelConstants.APPLICATION_COMPONENT_ID,
@@ -98,6 +99,9 @@
         + KernelConstants.KERNEL_COMPONENT_TYPE + "/"
         + KernelConstants.SESSION_DYNAMIC_COMPONENT_ID));
 
+    globalResources
+        
.add(createStaticResource("web/org.openbravo.client.kernel/js/ob-i18n.js", 
true));
+
     globalResources.add(createStaticResource(
         "web/org.openbravo.client.kernel/js/ob-kernel-utilities.js", true));
 
diff -r 1aeebc4d61e0 -r 904f31b433ec 
modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/templates/i18n.ftl
--- 
a/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/templates/i18n.ftl
    Thu Nov 09 13:52:00 2017 +0100
+++ 
b/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/templates/i18n.ftl
    Fri Nov 10 08:29:38 2017 +0100
@@ -12,90 +12,15 @@
  * under the License.
  * The Original Code is Openbravo ERP.
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2010-2012 Openbravo SLU 
+ * All portions are Copyright (C) 2010-2017 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
 */
 -->
-// jslint
-
-OB = window.OB || {};
-OB.I18N = window.OB.I18N || {};
 
 OB.I18N.labels = {
 <#list data.labels as label>
 '${label.key?js_string}':  '${label.value?js_string}'<#if 
label_has_next>,</#if>
 </#list>
-};
-
-<#--
-// key is the message key
-// params is used for parameter substitution
-// if object and property are set then the label is set directly in
-// the object
-// Note: property may also be a function expecting the label as a string
-// if the label is not defined and object and property are set
-// then a call to the server is done to request the label.
--->
-OB.I18N.getLabel = function(key, params, object, property) {
-  var label, i;
-
-  if (!OB.I18N.labels[key]) {
-    if (object && property) {
-      OB.I18N.getLabelFromServer(key, params, object, property);
-    }
-    return 'UNDEFINED ' + key;
-  }
-
-  label = OB.I18N.labels[key];
-
-  if (params && params.length && params.length > 0) {
-      for (i = 0; i < params.length; i++) {
-          label = label.replace("%" + i, params[i]);
-      }
-  }
-  if (object && property) {
-    if (Object.prototype.toString.call(object[property]) === '[object 
Function]') {
-      object[property](label);
-    } else {
-      object[property] = label;
-    }
-  }
-  return label;
-};
-
-OB.I18N.getLabelFromServer = function(key, params, object, property) {
-  var requestParameters, rpcRequest;
-
-  if (!isc) {
-    return 'UNDEFINED ' + key;
-  }
-
-  requestParameters = {};
-  requestParameters._action = 
'org.openbravo.client.kernel.GetLabelActionHandler';
-  requestParameters.key = key;
-
-  rpcRequest = {};
-  rpcRequest.actionURL = OB.Application.contextUrl + 
'org.openbravo.client.kernel';
-  rpcRequest.callback = function (response, data, request) {
-    var clientContext = response.clientContext;
-    if (data.label) {
-      OB.I18N.labels[clientContext.key] = data.label;
-      OB.I18N.getLabel(clientContext.key, clientContext.params, 
clientContext.object, clientContext.property);
-    } else {
-      if (isc.isA.Function(clientContext.object[clientContext.property])) {
-        clientContext.object[clientContext.property]('LABEL NOT FOUND ' + 
clientContext.key);
-      } else {
-        clientContext.object[clientContext.property] = 'LABEL NOT FOUND ' + 
clientContext.key;
-      }
-    }
-  };
-  rpcRequest.httpMethod = 'GET';
-  rpcRequest.contentType = 'application/json;charset=UTF-8';
-  rpcRequest.useSimpleHttp = true;
-  rpcRequest.evalResult = true;
-  rpcRequest.params = requestParameters;
-  rpcRequest.clientContext = {'key' : key, 'object': object, 'params': params, 
'property': property};
-  isc.RPCManager.sendRequest(rpcRequest);
 };
\ No newline at end of file
diff -r 1aeebc4d61e0 -r 904f31b433ec 
modules/org.openbravo.client.kernel/web/org.openbravo.client.kernel/js/ob-i18n.js
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ 
b/modules/org.openbravo.client.kernel/web/org.openbravo.client.kernel/js/ob-i18n.js
 Fri Nov 10 08:29:38 2017 +0100
@@ -0,0 +1,100 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.0  (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) 2017 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+
+//** {{{ OB.I18N.getLabel }}} **
+// Retrieves a label
+// key is the message key
+// params is used for parameter substitution
+// if object and property are set then the label is set directly in
+// the object
+// Note: property may also be a function expecting the label as a string
+// if the label is not defined and object and property are set
+// then a call to the server is done to request the label.
+OB.I18N.getLabel = function (key, params, object, property) {
+  var label, i;
+
+  if (!OB.I18N.labels[key]) {
+    if (object && property) {
+      OB.I18N.getLabelFromServer(key, params, object, property);
+    }
+    return 'UNDEFINED ' + key;
+  }
+
+  label = OB.I18N.labels[key];
+
+  if (params && params.length && params.length > 0) {
+    for (i = 0; i < params.length; i++) {
+      label = label.replace("%" + i, params[i]);
+    }
+  }
+  if (object && property) {
+    if (Object.prototype.toString.call(object[property]) === '[object 
Function]') {
+      object[property](label);
+    } else {
+      object[property] = label;
+    }
+  }
+  return label;
+};
+
+//** {{{ OB.I18N.getLabelFromServer }}} **
+// Retrieves a label from the server
+// key is the message key
+// params is used for parameter substitution
+// if object and property are set then the label is set directly in
+// the object
+OB.I18N.getLabelFromServer = function (key, params, object, property) {
+  var requestParameters, rpcRequest;
+
+  if (!isc) {
+    return 'UNDEFINED ' + key;
+  }
+
+  requestParameters = {};
+  requestParameters._action = 
'org.openbravo.client.kernel.GetLabelActionHandler';
+  requestParameters.key = key;
+
+  rpcRequest = {};
+  rpcRequest.actionURL = OB.Application.contextUrl + 
'org.openbravo.client.kernel';
+  rpcRequest.callback = function (response, data, request) {
+    var clientContext = response.clientContext;
+    if (data.label) {
+      OB.I18N.labels[clientContext.key] = data.label;
+      OB.I18N.getLabel(clientContext.key, clientContext.params, 
clientContext.object, clientContext.property);
+    } else {
+      if (isc.isA.Function(clientContext.object[clientContext.property])) {
+        clientContext.object[clientContext.property]('LABEL NOT FOUND ' + 
clientContext.key);
+      } else {
+        clientContext.object[clientContext.property] = 'LABEL NOT FOUND ' + 
clientContext.key;
+      }
+    }
+  };
+  rpcRequest.httpMethod = 'GET';
+  rpcRequest.contentType = 'application/json;charset=UTF-8';
+  rpcRequest.useSimpleHttp = true;
+  rpcRequest.evalResult = true;
+  rpcRequest.params = requestParameters;
+  rpcRequest.clientContext = {
+    'key': key,
+    'object': object,
+    'params': params,
+    'property': property
+  };
+  isc.RPCManager.sendRequest(rpcRequest);
+};
\ No newline at end of file

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to