001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.enterprise.messages;
016:
017: import java.text.MessageFormat;
018: import java.util.Locale;
019: import java.util.ResourceBundle;
020:
021: /** Default implementation of the message object. It can be used as a base class
022: * for the concrete messages as follows :
023: * <p><code><pre>
024: * public class GreetingMessage extends DefaultMessageImplementation
025: * {
026: * private UserName mUserName;
027: *
028: * // Construct an instance of the greeting message
029: * public GreetingMessage(UserName pUserName)
030: * {
031: * super("HatMaker.Crm.Core.Greeting", false, false);
032: * mUserName = pUserName;
033: * }
034: *
035: * // Overridden to return array of message arguments used to render the message.
036: * public Object[] getMessageArguments()
037: * {
038: * return new Object[] { mUserName.toPrimitiveValue(); };
039: * }
040: *
041: * // Get the user name name
042: * public UserName getUserName()
043: * {
044: * return mUserName;
045: * }
046: * }
047: * </pre></code>
048: * The only method, which subclass might want to override is getMessageArguments().
049: *
050: */
051: public abstract class DefaultMessageImplementation implements Message {
052: private String mMessageId;
053: private boolean mIsFailure;
054: private boolean mIsError;
055: private boolean mIsWarning;
056: private String mTextResourcePackage;
057: private String mTextResourceId;
058:
059: /** Constructs message object with specifed attrributes. This version of constructor assumes that the
060: * text resource bundle resides in the same package as the mesage class and that the text resource is equals the message id.
061: * This is really the most common occurrntce.
062: * @param pMessageId - the unique identifier of the message.
063: * @param pIsFailure - mutually exclusive with pIsError and pIsWarning parameters. Set to true if this message represents failure.
064: * @param pIsError - mutually exclusive with pIsWarning and pIsFailure parameters. Set to true if this message represents error.
065: * @param pIsWarning - mutually exclusive with pIsError and pIsFailure parameters. Set to true if this message represents warning.
066: */
067: public DefaultMessageImplementation(String pMessageId,
068: boolean pIsFailure, boolean pIsError, boolean pIsWarning) {
069: mMessageId = pMessageId;
070: mIsFailure = pIsFailure;
071: mIsError = pIsError;
072: mIsWarning = pIsWarning;
073: // By default the TextResourceId equals MessageId
074: mTextResourceId = pMessageId;
075: // By default the TextResourcePackage is the same as the package of the message class
076: String lFullClassName = getClass().getName();
077: int lLastDot = lFullClassName.lastIndexOf(".");
078: mTextResourcePackage = (lLastDot > 0) ? lFullClassName
079: .substring(0, lLastDot) : null;
080: }
081:
082: // Restrict access to the default constructor
083: private DefaultMessageImplementation() {
084: }
085:
086: /** Returns unique string Id of the message. */
087: public String getId() {
088: return mMessageId;
089: }
090:
091: /** Returns true if this message is a failure message. */
092: public boolean isFailure() {
093: return mIsFailure;
094: }
095:
096: /** Returns true if this message is an error message. */
097: public boolean isError() {
098: return mIsError;
099: }
100:
101: /** Returns true if this message is a warning message. */
102: public boolean isWarning() {
103: return mIsWarning;
104: }
105:
106: /** Getter for the package name of the resource bundle where where text resource should be loaded from. */
107: public String getTextResourcePackage() {
108: return mTextResourcePackage;
109: }
110:
111: /** Setter for the package name of the resource bundle where where text resource should be loaded from. */
112: public void setTextResourcePackage(String pTextResourcePackage) {
113: mTextResourcePackage = pTextResourcePackage;
114: }
115:
116: /** Getter for the unique identifier of the text resource where message text is. */
117: public String getTextResourceId() {
118: return mTextResourceId;
119: }
120:
121: /** Setter for unique identifier of the text resource where message text is. */
122: public void setTextResourceId(String pTextResourceId) {
123: mTextResourceId = pTextResourceId;
124: }
125:
126: /** Returns an array of message arguments used to render the message.
127: * This version returns no arguments. Override to provide arguments relevant for the message. */
128: public Object[] getMessageArguments() {
129: return new Object[0];
130: }
131:
132: /** Returns human readable message text in default locale. */
133: public String toString() {
134: return toLocalizedString(Locale.getDefault());
135: }
136:
137: /** Returns human readable message text in specified locale. */
138: public String toLocalizedString(Locale pDesiredLocale) {
139: String lResourceBundleBaseName = getTextResourcePackage() != null ? (getTextResourcePackage() + ".Messages")
140: : "Messages";
141:
142: ResourceBundle lBundle = ResourceBundle.getBundle(
143: lResourceBundleBaseName, pDesiredLocale);
144: if (lBundle == null)
145: throw new java.lang.UnsupportedOperationException(
146: "Unable to load "
147: + lResourceBundleBaseName
148: + " resource bundle. toLocalizedString() operation can not be completed.");
149: String lMessagePattern = lBundle.getString(getTextResourceId());
150: if (lMessagePattern == null)
151: throw new java.lang.UnsupportedOperationException(
152: "Message text is not found in resource bundle. toLocalizedString() operation can not be completed.");
153: return MessageFormat.format(lMessagePattern,
154: getMessageArguments());
155: }
156: }
|