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.util.ArrayList;
018: import java.util.Iterator;
019: import java.util.List;
020:
021: /** Default implementation of the message container. It is still an abstract class.
022: * Subclasses may (and probably will) override protected List getContainedMessages()
023: * to supply list of contained messages.
024: * All other methods of message container are implemented based on this returned list */
025: public abstract class DefaultMessageContainerImplementation implements
026: MessageContainer {
027: /** This method should be overridden by the derived class. It is expected to
028: * return list of contained messages. null should never be returned,
029: * return empty list if no messages are stored */
030: protected List getContainedMessages() {
031: return new ArrayList(0);
032: }
033:
034: /** Returns true if this container contains one or more messages of any kind */
035: public boolean hasMessages() {
036: List lContainedMessages = getContainedMessages();
037: return (lContainedMessages.size() > 0);
038: }
039:
040: /** Returns combined array of zero or more contained messages of any kind.
041: * This is the convenience method. It can be used, for example, to just get all messages
042: * irrespective of their type and print them out */
043: public Message[] getMessages() {
044: List lContainedMessages = getContainedMessages();
045: return (Message[]) lContainedMessages
046: .toArray(new Message[lContainedMessages.size()]);
047: }
048:
049: /* Returns true if this container contains one or more failure messages. */
050: public boolean hasFailureMessages() {
051: List lContainedMessages = getContainedMessages();
052: if (lContainedMessages.size() > 0) {
053: Iterator lIter = lContainedMessages.iterator();
054: while (lIter.hasNext()) {
055: Message lMessage = (Message) lIter.next();
056: if (lMessage.isFailure())
057: return true;
058: }
059: }
060: return false;
061: }
062:
063: /* Returns array of zero or more contained error messages. */
064: public Message[] getFailureMessages() {
065: List lContainedMessages = getContainedMessages();
066: ArrayList lFailureMessages = new ArrayList();
067: if (lContainedMessages.size() > 0) {
068: Iterator lIter = lContainedMessages.iterator();
069: while (lIter.hasNext()) {
070: Message lMessage = (Message) lIter.next();
071: if (lMessage.isFailure())
072: lFailureMessages.add(lMessage);
073: }
074: }
075: return (Message[]) lFailureMessages
076: .toArray(new Message[lFailureMessages.size()]);
077: }
078:
079: /** Returns true if this container contains one or more error messages. */
080: public boolean hasErrorMessages() {
081: List lContainedMessages = getContainedMessages();
082: if (lContainedMessages.size() > 0) {
083: Iterator lIter = lContainedMessages.iterator();
084: while (lIter.hasNext()) {
085: Message lMessage = (Message) lIter.next();
086: if (lMessage.isError())
087: return true;
088: }
089: }
090: return false;
091: }
092:
093: /** Returns array of zero or more contained error messages. */
094: public Message[] getErrorMessages() {
095: List lContainedMessages = getContainedMessages();
096: ArrayList lErrorMessages = new ArrayList();
097: if (lContainedMessages.size() > 0) {
098: Iterator lIter = lContainedMessages.iterator();
099: while (lIter.hasNext()) {
100: Message lMessage = (Message) lIter.next();
101: if (lMessage.isError())
102: lErrorMessages.add(lMessage);
103: }
104: }
105: return (Message[]) lErrorMessages
106: .toArray(new Message[lErrorMessages.size()]);
107: }
108:
109: /** Returns true if this container contains one or more warning messages. */
110: public boolean hasWarningMessages() {
111: List lContainedMessages = getContainedMessages();
112: if (lContainedMessages.size() > 0) {
113: Iterator lIter = lContainedMessages.iterator();
114: while (lIter.hasNext()) {
115: Message lMessage = (Message) lIter.next();
116: if (lMessage.isWarning())
117: return true;
118: }
119: }
120: return false;
121: }
122:
123: /** Returns array of zero or more contained warning messages. */
124: public Message[] getWarningMessages() {
125: List lContainedMessages = getContainedMessages();
126: ArrayList lWarningMessages = new ArrayList();
127: if (lContainedMessages.size() > 0) {
128: Iterator lIter = lContainedMessages.iterator();
129: while (lIter.hasNext()) {
130: Message lMessage = (Message) lIter.next();
131: if (lMessage.isWarning())
132: lWarningMessages.add(lMessage);
133: }
134: }
135: return (Message[]) lWarningMessages
136: .toArray(new Message[lWarningMessages.size()]);
137: }
138:
139: /** Returns true if this container contains one or more information messages */
140: public boolean hasInformationMessages() {
141: List lContainedMessages = getContainedMessages();
142: if (lContainedMessages.size() > 0) {
143: Iterator lIter = lContainedMessages.iterator();
144: while (lIter.hasNext()) {
145: Message lMessage = (Message) lIter.next();
146: if ((!lMessage.isError()) && (!lMessage.isWarning()))
147: return true;
148: }
149: }
150: return false;
151: }
152:
153: /** Returns array of zero or more contained information messages */
154: public Message[] getInformationMessages() {
155: List lContainedMessages = getContainedMessages();
156: ArrayList lInformationMessages = new ArrayList();
157: if (lContainedMessages.size() > 0) {
158: Iterator lIter = lContainedMessages.iterator();
159: while (lIter.hasNext()) {
160: Message lMessage = (Message) lIter.next();
161: if ((!lMessage.isError()) && (!lMessage.isWarning()))
162: lInformationMessages.add(lMessage);
163: }
164: }
165: return (Message[]) lInformationMessages
166: .toArray(new Message[lInformationMessages.size()]);
167: }
168:
169: /** Returns true if this container contains one or more messages with specified Id */
170: public boolean hasMessagesWithId(String pRequiredMessageId) {
171: List lContainedMessages = getContainedMessages();
172: if (lContainedMessages.size() > 0) {
173: Iterator lIter = lContainedMessages.iterator();
174: while (lIter.hasNext()) {
175: Message lMessage = (Message) lIter.next();
176: if (lMessage.getId().equals(pRequiredMessageId))
177: return true;
178: }
179: }
180: return false;
181: }
182:
183: /** Returns array of zero or more messages with specified Id */
184: public Message[] getMessagesWithId(String pRequiredMessageId) {
185: List lContainedMessages = getContainedMessages();
186: ArrayList lMessagesWithId = new ArrayList();
187: if (lContainedMessages.size() > 0) {
188: Iterator lIter = lContainedMessages.iterator();
189: while (lIter.hasNext()) {
190: Message lMessage = (Message) lIter.next();
191: if (lMessage.getId().equals(pRequiredMessageId))
192: lMessagesWithId.add(lMessage);
193: }
194: }
195: return (Message[]) lMessagesWithId
196: .toArray(new Message[lMessagesWithId.size()]);
197: }
198: }
|