Related to issue 19778.Update methods moved to MessageUtility.
authorGorka Ion Damián <gorkaion.damian@openbravo.com>
Wed, 15 Feb 2012 12:30:11 +0100
changeset 15656 1eb3550e1a22
parent 15655 0b9c8bb43366
child 15657 7cd1c7e2ee90
Related to issue 19778.Update methods moved to MessageUtility.
src/org/openbravo/erpCommon/utility/Utility.java
--- a/src/org/openbravo/erpCommon/utility/Utility.java	Wed Feb 15 12:29:24 2012 +0100
+++ b/src/org/openbravo/erpCommon/utility/Utility.java	Wed Feb 15 12:30:11 2012 +0100
@@ -87,7 +87,6 @@
 import org.openbravo.model.ad.ui.Window;
 import org.openbravo.model.ad.utility.Image;
 import org.openbravo.model.common.enterprise.Organization;
-import org.openbravo.service.db.DalConnectionProvider;
 import org.openbravo.uiTranslation.TranslationHandler;
 import org.openbravo.utils.FileUtility;
 import org.openbravo.utils.FormatUtilities;
@@ -339,43 +338,14 @@
   }
 
   /**
-   * Translate the given code into some message from the application dictionary. It searches first
-   * in AD_Message table and if there are not matchings then in AD_Element table.
-   * 
-   * It uses DalConnectionProvider to get the ConnectionProvider and the context's language to
-   * translate the message.
-   * 
-   * @see Utility#messageBD(ConnectionProvider, String, String, boolean)
-   * 
-   * @param strCode
-   *          String with the search key to search.
-   * @return String with the translated message.
-   */
-  public static String messageBD(String strCode) {
-    String language = OBContext.getOBContext().getLanguage().getLanguage();
-    ConnectionProvider conn = new DalConnectionProvider(false);
-    return messageBD(conn, strCode, language);
-  }
-
-  /**
-   * @see Utility#messageBD(ConnectionProvider, String, String, boolean)
+   * @see MessageUtility#messageBD(ConnectionProvider, String, String, boolean)
    */
   public static String messageBD(ConnectionProvider conn, String strCode, String strLanguage) {
     return BasicUtility.messageBD(conn, strCode, strLanguage, true);
   }
 
   /**
-   * Translate the given code into some message from the application dictionary.
-   * 
-   * @param conn
-   *          Handler for the database connection.
-   * @param strCode
-   *          String with the code to search.
-   * @param strLanguage
-   *          String with the translation language.
-   * @param escape
-   *          Escape \n and " characters
-   * @return String with the translated message.
+   * @see MessageUtility#messageBD(ConnectionProvider, String, String, boolean)
    */
   public static String messageBD(ConnectionProvider conn, String strCode, String strLanguage,
       boolean escape) {
@@ -383,18 +353,7 @@
   }
 
   /**
-   * 
-   * Formats a message String into a String for html presentation. Escapes the &, <, >, " and ®, and
-   * replace the \n by <br/>
-   * and \r for space.
-   * 
-   * IMPORTANT! : this method is designed to transform the output of Utility.messageBD method, and
-   * this method replaces \n by \\n and \" by &quote. Because of that, the first replacements revert
-   * this previous replacements.
-   * 
-   * @param message
-   *          message with java formating
-   * @return html format message
+   * @see MessageUtility#formatMessageBDToHtml(String)
    */
   public static String formatMessageBDToHtml(String message) {
     return BasicUtility.formatMessageBDToHtml(message);
@@ -1082,115 +1041,28 @@
   }
 
   /**
-   * Parse the text searching @ parameters to translate.
-   * 
-   * @param text
-   *          String with the text to translate.
-   * @return String translated.
+   * @see MessageUtility#parseTranslation(ConnectionProvider, VariablesSecureApp, String, String)
    */
-  public static String parseTranslation(String text) {
-    // final VariablesSecureApp vars = RequestContext.get().getVariablesSecureApp();
-    final VariablesSecureApp vars = new VariablesSecureApp(OBContext.getOBContext().getUser()
-        .getId(), OBContext.getOBContext().getCurrentClient().getId(), OBContext.getOBContext()
-        .getCurrentOrganization().getId(), OBContext.getOBContext().getRole().getId());
-    final String language = OBContext.getOBContext().getLanguage().getLanguage();
-    return parseTranslation(new DalConnectionProvider(false), vars, null, language, text);
+  public static String parseTranslation(ConnectionProvider conn, VariablesSecureApp vars,
+      String language, String text) {
+    return MessageUtility.parseTranslation(conn, vars, language, text);
   }
 
   /**
-   * Parse the text searching @ parameters to translate.
-   * 
-   * @param conn
-   *          Handler for the database connection.
-   * @param vars
-   *          Handler for the session info.
-   * @param language
-   *          String with the language to translate.
-   * @param text
-   *          String with the text to translate.
-   * @return String translated.
+   * @see MessageUtility#parseTranslation(ConnectionProvider, VariablesSecureApp, Map, String,
+   *      String)
    */
   public static String parseTranslation(ConnectionProvider conn, VariablesSecureApp vars,
-      String language, String text) {
-    return parseTranslation(conn, vars, null, language, text);
+      Map<String, String> replaceMap, String language, String text) {
+    return MessageUtility.parseTranslation(conn, vars, replaceMap, language, text);
   }
 
   /**
-   * Parse the text searching @ parameters to translate. If replaceMap is not null and contains a
-   * replacement value for a token then it will be used, otherwise the return value of the translate
-   * method will be used for the translation.
-   * 
-   * @param conn
-   *          Handler for the database connection.
-   * @param vars
-   *          Handler for the session info.
-   * @param replaceMap
-   *          optional Map containing replacement values for the tokens
-   * @param language
-   *          String with the language to translate.
-   * @param text
-   *          String with the text to translate.
-   * @return String translated.
-   */
-  public static String parseTranslation(ConnectionProvider conn, VariablesSecureApp vars,
-      Map<String, String> replaceMap, String language, String text) {
-    if (text == null || text.length() == 0)
-      return text;
-
-    String inStr = text;
-    String token;
-    final StringBuffer outStr = new StringBuffer();
-
-    int i = inStr.indexOf("@");
-    while (i != -1) {
-      outStr.append(inStr.substring(0, i));
-      inStr = inStr.substring(i + 1, inStr.length());
-
-      final int j = inStr.indexOf("@");
-      if (j < 0) {
-        inStr = "@" + inStr;
-        break;
-      }
-
-      token = inStr.substring(0, j);
-      if (replaceMap != null && replaceMap.containsKey(token)) {
-        outStr.append(replaceMap.get(token));
-      } else {
-        outStr.append(translate(conn, vars, token, language));
-      }
-
-      inStr = inStr.substring(j + 1, inStr.length());
-      i = inStr.indexOf("@");
-    }
-
-    outStr.append(inStr);
-    return outStr.toString();
-  }
-
-  /**
-   * For each token found in the parseTranslation method, this method is called to find the correct
-   * translation.
-   * 
-   * @param conn
-   *          Handler for the database connection.
-   * @param vars
-   *          Handler for the session info.
-   * @param token
-   *          String with the token to translate.
-   * @param language
-   *          String with the language to translate.
-   * @return String with the token translated.
+   * @see MessageUtility#translate(ConnectionProvider, VariablesSecureApp, String, String)
    */
   public static String translate(ConnectionProvider conn, VariablesSecureApp vars, String token,
       String language) {
-    String strTranslate = token;
-    strTranslate = vars.getSessionValue(token);
-    if (!strTranslate.equals(""))
-      return strTranslate;
-    strTranslate = messageBD(conn, token, language);
-    if (strTranslate.equals(""))
-      return token;
-    return strTranslate;
+    return MessageUtility.translate(conn, vars, token, language);
   }
 
   /**
@@ -1414,194 +1286,28 @@
   }
 
   /**
-   * Gets the Message for the instance of the processes.
-   * 
-   * @param conn
-   *          Handler for the database connection.
-   * @param vars
-   *          Handler for the session info.
-   * @param pinstanceData
-   *          Array with the instance information.
-   * @return Object with the message.
-   * @throws ServletException
+   * @see MessageUtility#getProcessInstanceMessage(ConnectionProvider, VariablesSecureApp,
+   *      PInstanceProcessData[])
    */
   public static OBError getProcessInstanceMessage(ConnectionProvider conn, VariablesSecureApp vars,
       PInstanceProcessData[] pinstanceData) throws ServletException {
-    OBError myMessage = new OBError();
-    if (pinstanceData != null && pinstanceData.length > 0) {
-      String message = "";
-      String title = "Error";
-      String type = "Error";
-      if (!pinstanceData[0].errormsg.equals("")) {
-        message = pinstanceData[0].errormsg;
-      } else if (!pinstanceData[0].pMsg.equals("")) {
-        message = pinstanceData[0].pMsg;
-      }
-
-      if (pinstanceData[0].result.equals("1")) {
-        type = "Success";
-        title = Utility.messageBD(conn, "Success", vars.getLanguage());
-      } else if (pinstanceData[0].result.equals("0")) {
-        type = "Error";
-        title = Utility.messageBD(conn, "Error", vars.getLanguage());
-      } else {
-        type = "Warning";
-        title = Utility.messageBD(conn, "Warning", vars.getLanguage());
-      }
-
-      final int errorPos = message.indexOf("@ERROR=");
-      if (errorPos != -1) {
-        myMessage = Utility.translateError(conn, vars, vars.getLanguage(),
-            "@CODE=@" + message.substring(errorPos + 7));
-        if (log4j.isDebugEnabled())
-          log4j.debug("Error Message returned: " + myMessage.getMessage());
-        if (message.substring(errorPos + 7).equals(myMessage.getMessage())) {
-          myMessage.setMessage(parseTranslation(conn, vars, vars.getLanguage(),
-              myMessage.getMessage()));
-        }
-        if (errorPos > 0)
-          message = message.substring(0, errorPos);
-        else
-          message = "";
-      }
-      if (!message.equals("") && message.indexOf("@") != -1)
-        message = Utility.parseTranslation(conn, vars, vars.getLanguage(), message);
-      myMessage.setType(type);
-      myMessage.setTitle(title);
-      myMessage.setMessage(message + ((!message.equals("") && errorPos != -1) ? " <br> " : "")
-          + myMessage.getMessage());
-    }
-    return myMessage;
+    return MessageUtility.getProcessInstanceMessage(conn, vars, pinstanceData);
   }
 
   /**
-   * Translate the message, searching the @ parameters, and making use of the ErrorTextParser class
-   * to get the appropiated message.
-   * 
-   * @param conn
-   *          Handler for the database connection.
-   * @param vars
-   *          Handler for the session info.
-   * @param strLanguage
-   *          Language to translate.
-   * @param message
-   *          Strin with the message to translate.
-   * @return Object with the message.
+   * @see MessageUtility#translateError(ConnectionProvider, VariablesSecureApp, String, String)
    */
   public static OBError translateError(ConnectionProvider conn, VariablesSecureApp vars,
       String strLanguage, String message) {
-    final OBError myError = new OBError();
-    myError.setType("Error");
-    myError.setMessage(message);
-    if (message != null && !message.equals("")) {
-      String code = "";
-      if (log4j.isDebugEnabled())
-        log4j.debug("translateError - message: " + message);
-      if (message.startsWith("@CODE=@"))
-        message = message.substring(7);
-      else if (message.startsWith("@CODE=")) {
-        message = message.substring(6);
-        final int pos = message.indexOf("@");
-        if (pos == -1) {
-          code = message;
-          message = "";
-        } else {
-          code = message.substring(0, pos);
-          message = message.substring(pos + 1);
-        }
-      }
-      myError.setMessage(message);
-      if (log4j.isDebugEnabled())
-        log4j.debug("translateError - code: " + code + " - message: " + message);
-
-      // BEGIN Checking if is a pool problem
-      if (code != null && code.equals("NoConnectionAvailable")) {
-        myError.setType("Error");
-        myError.setTitle("Critical Error");
-        myError.setConnectionAvailable(false);
-        myError.setMessage("No database connection available");
-        return myError;
-      }
-      // END Checking if is a pool problem
-
-      // BEGIN Parsing message text
-      if (message != null && !message.equals("")) {
-        final String rdbms = conn.getRDBMS();
-        ErrorTextParser myParser = null;
-        try {
-          final Class<?> c = Class.forName("org.openbravo.erpCommon.utility.ErrorTextParser"
-              + rdbms.toUpperCase());
-          myParser = (ErrorTextParser) c.newInstance();
-        } catch (final ClassNotFoundException ex) {
-          log4j.warn("Couldn´t find class: org.openbravo.erpCommon.utility.ErrorTextParser"
-              + rdbms.toUpperCase());
-          myParser = null;
-        } catch (final Exception ex1) {
-          log4j.warn("Couldn´t initialize class: org.openbravo.erpCommon.utility.ErrorTextParser"
-              + rdbms.toUpperCase());
-          myParser = null;
-        }
-        if (myParser != null) {
-          myParser.setConnection(conn);
-          myParser.setLanguage(strLanguage);
-          myParser.setMessage(message);
-          myParser.setVars(vars);
-          try {
-            final OBError myErrorAux = myParser.parse();
-            if (myErrorAux != null
-                && !myErrorAux.getMessage().equals("")
-                && (code == null || code.equals("") || code.equals("0") || !myErrorAux.getMessage()
-                    .equalsIgnoreCase(message)))
-              return myErrorAux;
-          } catch (final Exception ex) {
-            log4j.error("Error while parsing text: " + ex);
-          }
-        }
-      } else
-        myError.setMessage(code);
-      // END Parsing message text
-
-      // BEGIN Looking for error code in AD_Message
-      if (code != null && !code.equals("")) {
-        final FieldProvider fldMessage = locateMessage(conn, code, strLanguage);
-        if (fldMessage != null) {
-          myError.setType((fldMessage.getField("msgtype").equals("E") ? "Error" : (fldMessage
-              .getField("msgtype").equals("I") ? "Info" : (fldMessage.getField("msgtype").equals(
-              "S") ? "Success" : "Warning"))));
-          myError.setMessage(fldMessage.getField("msgtext"));
-          return myError;
-        }
-      }
-      // END Looking for error code in AD_Message
-    }
-    return myError;
+    return MessageUtility.translateError(conn, vars, strLanguage, message);
   }
 
   /**
-   * Search a message in the database.
-   * 
-   * @param conn
-   *          Handler for the database connection.
-   * @param strCode
-   *          Message to search.
-   * @param strLanguage
-   *          Language to translate.
-   * @return FieldProvider with the message info.
+   * @see MessageUtility#locateMessage(ConnectionProvider, String, String)
    */
   public static FieldProvider locateMessage(ConnectionProvider conn, String strCode,
       String strLanguage) {
-    FieldProvider[] fldMessage = null;
-
-    try {
-      if (log4j.isDebugEnabled())
-        log4j.debug("Utility.messageBD - Message Code: " + strCode);
-      fldMessage = MessageBDData.messageInfo(conn, strLanguage, strCode);
-    } catch (final Exception ignore) {
-    }
-    if (fldMessage != null && fldMessage.length > 0)
-      return fldMessage[0];
-    else
-      return null;
+    return MessageUtility.locateMessage(conn, strCode, strLanguage);
   }
 
   public String getServletInfo() {