Related to issue 14433: MMC settings tab: When you save the tab you don't know if it has been saved
authorValery Lezhebokov <valery.lezhebokov@gmail.com>
Mon, 11 Oct 2010 09:38:58 +0200
changeset 8753 7ddd3c114bad
parent 8752 a633b87e13af
child 8754 96ddca6672b7
Related to issue 14433: MMC settings tab: When you save the tab you don't know if it has been saved
src-db/database/sourcedata/AD_MESSAGE.xml
src/org/openbravo/erpCommon/utility/OBErrorBuilder.java
--- a/src-db/database/sourcedata/AD_MESSAGE.xml	Mon Oct 11 09:38:22 2010 +0200
+++ b/src-db/database/sourcedata/AD_MESSAGE.xml	Mon Oct 11 09:38:58 2010 +0200
@@ -31379,6 +31379,17 @@
 <!--69214D1297AF46B398BC55D7C7517EF4-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
 <!--69214D1297AF46B398BC55D7C7517EF4--></AD_MESSAGE>
 
+<!--6AFF7D5F89EF4A92980112A343CE5687--><AD_MESSAGE>
+<!--6AFF7D5F89EF4A92980112A343CE5687-->  <AD_MESSAGE_ID><![CDATA[6AFF7D5F89EF4A92980112A343CE5687]]></AD_MESSAGE_ID>
+<!--6AFF7D5F89EF4A92980112A343CE5687-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--6AFF7D5F89EF4A92980112A343CE5687-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--6AFF7D5F89EF4A92980112A343CE5687-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--6AFF7D5F89EF4A92980112A343CE5687-->  <VALUE><![CDATA[ModuleManagementSettingSaved]]></VALUE>
+<!--6AFF7D5F89EF4A92980112A343CE5687-->  <MSGTEXT><![CDATA[The settings have been successfully saved.]]></MSGTEXT>
+<!--6AFF7D5F89EF4A92980112A343CE5687-->  <MSGTYPE><![CDATA[I]]></MSGTYPE>
+<!--6AFF7D5F89EF4A92980112A343CE5687-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--6AFF7D5F89EF4A92980112A343CE5687--></AD_MESSAGE>
+
 <!--6B8F2914B8BB4AE6891AE3E45CF79A57--><AD_MESSAGE>
 <!--6B8F2914B8BB4AE6891AE3E45CF79A57-->  <AD_MESSAGE_ID><![CDATA[6B8F2914B8BB4AE6891AE3E45CF79A57]]></AD_MESSAGE_ID>
 <!--6B8F2914B8BB4AE6891AE3E45CF79A57-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/openbravo/erpCommon/utility/OBErrorBuilder.java	Mon Oct 11 09:38:58 2010 +0200
@@ -0,0 +1,67 @@
+package org.openbravo.erpCommon.utility;
+
+import org.openbravo.erpCommon.utility.OBError;
+
+/**
+ * Provides facility methods to build OB messages ({@link OBError}).
+ * 
+ * @author Valery Lezhebokov
+ */
+public class OBErrorBuilder {
+
+  // TODO what to do with Menu type?
+  private static enum MessageType {
+    Success, Confirmation, Info, Warning, Error
+  }
+
+  /**
+   * Build the message based on the provided one or creates new message if a <code>null</code> was
+   * passed as a base message.
+   * 
+   * @param message
+   *          a base massage.
+   * @param type
+   *          a message type.
+   * @param messageText
+   *          the text to put into the message.
+   * @return a built message.
+   */
+  public static OBError buildMessage(OBError message, String type, String... messageText) {
+
+    // XXX don't we have any AssetUtils class in OB ?
+    if (type == null || type.isEmpty() || messageText.length == 0) {
+      throw new IllegalArgumentException(String.format(
+          "Illegal arguments provided: type [%s], textMessages [%s]", type, messageText));
+    }
+
+    if (message == null) {
+      final OBError newMessage = new OBError();
+      newMessage.setMessage(constructTextMessage(messageText));
+      newMessage.setType(type);
+      return newMessage;
+    } else {
+      message.setMessage(constructTextMessage(message.getMessage(),
+          constructTextMessage(messageText)));
+      // change message type if needed
+      final MessageType curType = MessageType.valueOf(message.getType());
+      final MessageType newType = MessageType.valueOf(type);
+      if (curType.compareTo(newType) < 0) {
+        message.setType(type);
+      }
+      return message;
+    }
+  }
+
+  /**
+   * Constructs a text of the message from the set of provided {@link String}s.
+   */
+  private static String constructTextMessage(String... messagesText) {
+    final StringBuilder stringBuilder = new StringBuilder();
+    stringBuilder.append(messagesText[0]);
+    for (int i = 1; i < messagesText.length; i++) {
+      stringBuilder.append("<br/>").append(messagesText[i]);
+    }
+    return stringBuilder.toString();
+  }
+
+}