001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.calyxo.control;
017:
018: import javax.servlet.http.HttpServletRequest;
019: import javax.servlet.jsp.PageContext;
020:
021: import de.odysseus.calyxo.base.Message;
022: import de.odysseus.calyxo.base.Messages;
023: import de.odysseus.calyxo.base.ModuleContext;
024: import de.odysseus.calyxo.base.ModuleSupport;
025:
026: /**
027: * Messages support class.
028: * <p/>
029: * Actions may produce errors, warnings and infos during request processing.
030: * Corresponding messages can be retreived from and saved to the current
031: * request using this class.
032: * <p/>
033: * There are several convenience methods to save error, warning and info
034: * messages without explicitely getting the messages.
035: * <p/>
036: * There's one instance per module.
037: * Use one of the <code>getInstance()</code> methods to get it.
038: *
039: * @author Christoph Beck
040: */
041: public abstract class MessageSupport {
042:
043: /**
044: * Module scope key where the message support instance is stored.
045: */
046: public static final String MESSAGE_SUPPORT_KEY = "de.odysseus.calyxo.control.message";
047:
048: /**
049: * Lookup message support for module determined by specified request
050: */
051: public static final MessageSupport getInstance(
052: HttpServletRequest request) {
053: ModuleContext context = ModuleSupport.getInstance(request)
054: .getModuleContext(request);
055: return (MessageSupport) context
056: .getAttribute(MESSAGE_SUPPORT_KEY);
057: }
058:
059: /**
060: * Convenience method.
061: * @see #getInstance(HttpServletRequest)
062: */
063: public static final MessageSupport getInstance(
064: PageContext pageContext) {
065: return getInstance((HttpServletRequest) pageContext
066: .getRequest());
067: }
068:
069: /**
070: * Lookup message support for specified module
071: */
072: public static final MessageSupport getInstance(ModuleContext context) {
073: return (MessageSupport) context
074: .getAttribute(MESSAGE_SUPPORT_KEY);
075: }
076:
077: /**
078: * Save the specified messages as errors for the specified request.
079: * This will replace any existing error messages.
080: * @param request the request we are processing
081: * @param messages the messages to be saved
082: */
083: public abstract void setErrors(HttpServletRequest request,
084: Messages messages);
085:
086: /**
087: * Get error messages for the specified request.
088: * @param request the request we are processing
089: * @param create if <code>true</code>, create, save and return a new instance
090: * if no error messages are present
091: * @return messages instance or <code>null</code>
092: */
093: public abstract Messages getErrors(HttpServletRequest request,
094: boolean create);
095:
096: /**
097: * Add message as error message, associated with a key
098: * @param request the request we are processing
099: * @param key the key used when adding the message to the {@link Messages} object
100: * @param message the message to be added
101: */
102: public final void addError(HttpServletRequest request, String key,
103: Message message) {
104: getErrors(request, true).addMessage(key, message);
105: }
106:
107: /**
108: * Add message as global error message
109: * @param request the request we are processing
110: * @param message the message to be added
111: */
112: public final void addError(HttpServletRequest request,
113: Message message) {
114: getErrors(request, true).addGlobalMessage(message);
115: }
116:
117: /**
118: * Save the specified messages as warnings for the specified request.
119: * This will replace any existing warning messages.
120: * @param request the request we are processing
121: * @param messages the messages to be saved
122: */
123: public abstract void setWarnings(HttpServletRequest request,
124: Messages messages);
125:
126: /**
127: * Get warning messages for the specified request.
128: * @param request the request we are processing
129: * @param create if <code>true</code>, create, save and return a new instance
130: * if no warning messages are present
131: * @return messages instance or <code>null</code>
132: */
133: public abstract Messages getWarnings(HttpServletRequest request,
134: boolean create);
135:
136: /**
137: * Add message as warning message, associated with a key
138: * @param request the request we are processing
139: * @param key the key used when adding the message to the {@link Messages} object
140: * @param message the message to be added
141: */
142: public final void addWarning(HttpServletRequest request,
143: String key, Message message) {
144: getWarnings(request, true).addMessage(key, message);
145: }
146:
147: /**
148: * Add message as global warning message
149: * @param request the request we are processing
150: * @param message the message to be added
151: */
152: public final void addWarning(HttpServletRequest request,
153: Message message) {
154: getWarnings(request, true).addGlobalMessage(message);
155: }
156:
157: /**
158: * Save the specified messages as infos for the specified request.
159: * This will replace any existing info messages.
160: * @param request the request we are processing
161: * @param messages the messages to be saved
162: */
163: public abstract void setInfos(HttpServletRequest request,
164: Messages messages);
165:
166: /**
167: * Get info messages for the specified request.
168: * @param request the request we are processing
169: * @param create if <code>true</code>, create, save and return a new instance
170: * if no info messages are present
171: * @return messages instance or <code>null</code>
172: */
173: public abstract Messages getInfos(HttpServletRequest request,
174: boolean create);
175:
176: /**
177: * Add message as info message, associated with a key
178: * @param request the request we are processing
179: * @param key the key used when adding the message to the {@link Messages} object
180: * @param message the message to be added
181: */
182: public final void addInfo(HttpServletRequest request, String key,
183: Message message) {
184: getInfos(request, true).addMessage(key, message);
185: }
186:
187: /**
188: * Add message as global info message
189: * @param request the request we are processing
190: * @param message the message to be added
191: */
192: public final void addInfo(HttpServletRequest request,
193: Message message) {
194: getInfos(request, true).addGlobalMessage(message);
195: }
196: }
|