Related to issue 19778.Moved method to new OBMessageUtils class.
authorGorka Ion Damián <gorkaion.damian@openbravo.com>
Wed, 29 Feb 2012 13:38:49 +0100
changeset 15663 a253469978c4
parent 15662 1e06e813fdc0
child 15664 0fce8d2edeac
Related to issue 19778.Moved method to new OBMessageUtils class.
modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/KernelUtils.java
modules/org.openbravo.service.json/src/org/openbravo/service/json/AdvancedQueryBuilder.java
src/org/openbravo/erpCommon/utility/OBMessageUtils.java
src/org/openbravo/erpCommon/utility/Utility.java
--- a/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/KernelUtils.java	Wed Feb 29 12:48:10 2012 +0100
+++ b/modules/org.openbravo.client.kernel/src/org/openbravo/client/kernel/KernelUtils.java	Wed Feb 29 13:38:49 2012 +0100
@@ -37,7 +37,7 @@
 import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
-import org.openbravo.erpCommon.utility.Utility;
+import org.openbravo.erpCommon.utility.OBMessageUtils;
 import org.openbravo.model.ad.datamodel.Column;
 import org.openbravo.model.ad.module.Module;
 import org.openbravo.model.ad.module.ModuleDependency;
@@ -74,10 +74,10 @@
   private List<Module> sortedModules = null;
 
   /**
-   * @see Utility#getI18NMessage(String, String[])
+   * @see OBMessageUtils#getI18NMessage(String, String[])
    */
   public String getI18N(String key, String[] params) {
-    return Utility.getI18NMessage(key, params);
+    return OBMessageUtils.getI18NMessage(key, params);
   }
 
   public Property getPropertyFromColumn(Column column) {
--- a/modules/org.openbravo.service.json/src/org/openbravo/service/json/AdvancedQueryBuilder.java	Wed Feb 29 12:48:10 2012 +0100
+++ b/modules/org.openbravo.service.json/src/org/openbravo/service/json/AdvancedQueryBuilder.java	Wed Feb 29 13:38:49 2012 +0100
@@ -43,6 +43,7 @@
 import org.openbravo.dal.core.DalUtil;
 import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.service.OBDal;
+import org.openbravo.erpCommon.utility.OBMessageUtils;
 import org.openbravo.erpCommon.utility.Utility;
 import org.openbravo.model.ad.datamodel.Table;
 import org.openbravo.model.ad.domain.Reference;
@@ -400,7 +401,7 @@
           }
           sb.append(prop.getName());
         }
-        throw new OBException(Utility.getI18NMessage("OBJSON_InvalidProperty",
+        throw new OBException(OBMessageUtils.getI18NMessage("OBJSON_InvalidProperty",
             new String[] { value.toString(), sb.toString() }));
       }
       final Property fieldProperty = properties.get(properties.size() - 1);
@@ -524,7 +525,7 @@
     try {
       localValue = getTypeSafeValue(operator, property, localValue);
     } catch (IllegalArgumentException e) {
-      throw new OBException(Utility.getI18NMessage("OBJSON_InvalidFilterValue",
+      throw new OBException(OBMessageUtils.getI18NMessage("OBJSON_InvalidFilterValue",
           new String[] { value != null ? value.toString() : "" }));
     }
     typedParameters.add(localValue);
--- a/src/org/openbravo/erpCommon/utility/OBMessageUtils.java	Wed Feb 29 12:48:10 2012 +0100
+++ b/src/org/openbravo/erpCommon/utility/OBMessageUtils.java	Wed Feb 29 13:38:49 2012 +0100
@@ -24,12 +24,14 @@
 
 import org.apache.log4j.Logger;
 import org.hibernate.criterion.Restrictions;
+import org.openbravo.base.exception.OBException;
 import org.openbravo.base.secureApp.VariablesSecureApp;
 import org.openbravo.client.kernel.RequestContext;
 import org.openbravo.dal.core.DalUtil;
 import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
+import org.openbravo.dal.service.OBQuery;
 import org.openbravo.data.FieldProvider;
 import org.openbravo.database.ConnectionProvider;
 import org.openbravo.model.ad.process.ProcessInstance;
@@ -454,4 +456,55 @@
     }
   }
 
+  /**
+   * Returns a message in the right language with parameter substitution. Each occurence of a %
+   * parameter (%0, %1 etc) is replaced with the corresponding parameter value. in the params array.
+   * 
+   * @param key
+   *          the key of the message
+   * @param params
+   *          the parameters to substitute in the message
+   * @return the translated message with the parameters substituted
+   */
+  public static String getI18NMessage(String key, String[] params) {
+    OBContext.setAdminMode();
+    try {
+  
+      // first read the labels from the base table
+      final OBQuery<Message> messages = OBDal.getInstance().createQuery(Message.class,
+          Message.PROPERTY_SEARCHKEY + "=:key");
+      messages.setNamedParameter("key", key);
+      if (messages.list().isEmpty()) {
+        return null;
+      }
+  
+      if (messages.list().size() > 1) {
+        Utility.log4j.warn("More than one message found using key " + key);
+      }
+  
+      // pick the first one
+      final Message message = messages.list().get(0);
+      String label = message.getMessageText();
+      final String languageId = OBContext.getOBContext().getLanguage().getId();
+      for (MessageTrl messageTrl : message.getADMessageTrlList()) {
+        if (DalUtil.getId(messageTrl.getLanguage()).equals(languageId)) {
+          label = messageTrl.getMessageText();
+          break;
+        }
+      }
+      // parameter substitution
+      if (params != null && params.length > 0) {
+        int cnt = 0;
+        for (String param : params) {
+          label = label.replace("%" + cnt++, param);
+        }
+      }
+      return label;
+    } catch (Exception e) {
+      throw new OBException("Exception when getting message for key: " + key, e);
+    } finally {
+      OBContext.restorePreviousMode();
+    }
+  }
+
 }
--- a/src/org/openbravo/erpCommon/utility/Utility.java	Wed Feb 29 12:48:10 2012 +0100
+++ b/src/org/openbravo/erpCommon/utility/Utility.java	Wed Feb 29 13:38:49 2012 +0100
@@ -67,10 +67,8 @@
 import org.openbravo.base.provider.OBConfigFileProvider;
 import org.openbravo.base.secureApp.OrgTree;
 import org.openbravo.base.secureApp.VariablesSecureApp;
-import org.openbravo.dal.core.DalUtil;
 import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.service.OBDal;
-import org.openbravo.dal.service.OBQuery;
 import org.openbravo.data.FieldProvider;
 import org.openbravo.data.Sqlc;
 import org.openbravo.database.ConnectionProvider;
@@ -80,8 +78,6 @@
 import org.openbravo.erpCommon.reference.PInstanceProcessData;
 import org.openbravo.model.ad.system.ClientInformation;
 import org.openbravo.model.ad.system.SystemInformation;
-import org.openbravo.model.ad.ui.Message;
-import org.openbravo.model.ad.ui.MessageTrl;
 import org.openbravo.model.ad.ui.Process;
 import org.openbravo.model.ad.ui.Tab;
 import org.openbravo.model.ad.ui.Window;
@@ -117,57 +113,6 @@
   }
 
   /**
-   * Returns a message in the right language with parameter substitution. Each occurence of a %
-   * parameter (%0, %1 etc) is replaced with the corresponding parameter value. in the params array.
-   * 
-   * @param key
-   *          the key of the message
-   * @param params
-   *          the parameters to substitute in the message
-   * @return the translated message with the parameters substituted
-   */
-  public static String getI18NMessage(String key, String[] params) {
-    OBContext.setAdminMode();
-    try {
-
-      // first read the labels from the base table
-      final OBQuery<Message> messages = OBDal.getInstance().createQuery(Message.class,
-          Message.PROPERTY_SEARCHKEY + "=:key");
-      messages.setNamedParameter("key", key);
-      if (messages.list().isEmpty()) {
-        return null;
-      }
-
-      if (messages.list().size() > 1) {
-        log4j.warn("More than one message found using key " + key);
-      }
-
-      // pick the first one
-      final Message message = messages.list().get(0);
-      String label = message.getMessageText();
-      final String languageId = OBContext.getOBContext().getLanguage().getId();
-      for (MessageTrl messageTrl : message.getADMessageTrlList()) {
-        if (DalUtil.getId(messageTrl.getLanguage()).equals(languageId)) {
-          label = messageTrl.getMessageText();
-          break;
-        }
-      }
-      // parameter substitution
-      if (params != null && params.length > 0) {
-        int cnt = 0;
-        for (String param : params) {
-          label = label.replace("%" + cnt++, param);
-        }
-      }
-      return label;
-    } catch (Exception e) {
-      throw new OBException("Exception when getting message for key: " + key, e);
-    } finally {
-      OBContext.restorePreviousMode();
-    }
-  }
-
-  /**
    * Computes the community branding url on the basis of system information. Note the returned url
    * does not contain the protocol part, it starts with //... So the caller has to prepend it with
    * the needed protocol part (i.e. http: or https:).