/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.control;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import de.odysseus.calyxo.base.Message;
import de.odysseus.calyxo.base.Messages;
import de.odysseus.calyxo.base.ModuleContext;
import de.odysseus.calyxo.base.ModuleSupport;
/**
* Messages support class.
* <p/>
* Actions may produce errors, warnings and infos during request processing.
* Corresponding messages can be retreived from and saved to the current
* request using this class.
* <p/>
* There are several convenience methods to save error, warning and info
* messages without explicitely getting the messages.
* <p/>
* There's one instance per module.
* Use one of the <code>getInstance()</code> methods to get it.
*
* @author Christoph Beck
*/
public abstract class MessageSupport {
/**
* Module scope key where the message support instance is stored.
*/
public static final String MESSAGE_SUPPORT_KEY =
"de.odysseus.calyxo.control.message";
/**
* Lookup message support for module determined by specified request
*/
public static final MessageSupport getInstance(HttpServletRequest request) {
ModuleContext context =
ModuleSupport.getInstance(request).getModuleContext(request);
return (MessageSupport)context.getAttribute(MESSAGE_SUPPORT_KEY);
}
/**
* Convenience method.
* @see #getInstance(HttpServletRequest)
*/
public static final MessageSupport getInstance(PageContext pageContext) {
return getInstance((HttpServletRequest)pageContext.getRequest());
}
/**
* Lookup message support for specified module
*/
public static final MessageSupport getInstance(ModuleContext context) {
return (MessageSupport)context.getAttribute(MESSAGE_SUPPORT_KEY);
}
/**
* Save the specified messages as errors for the specified request.
* This will replace any existing error messages.
* @param request the request we are processing
* @param messages the messages to be saved
*/
public abstract void setErrors(HttpServletRequest request, Messages messages);
/**
* Get error messages for the specified request.
* @param request the request we are processing
* @param create if <code>true</code>, create, save and return a new instance
* if no error messages are present
* @return messages instance or <code>null</code>
*/
public abstract Messages getErrors(HttpServletRequest request, boolean create);
/**
* Add message as error message, associated with a key
* @param request the request we are processing
* @param key the key used when adding the message to the {@link Messages} object
* @param message the message to be added
*/
public final void addError(HttpServletRequest request, String key, Message message) {
getErrors(request, true).addMessage(key, message);
}
/**
* Add message as global error message
* @param request the request we are processing
* @param message the message to be added
*/
public final void addError(HttpServletRequest request, Message message) {
getErrors(request, true).addGlobalMessage(message);
}
/**
* Save the specified messages as warnings for the specified request.
* This will replace any existing warning messages.
* @param request the request we are processing
* @param messages the messages to be saved
*/
public abstract void setWarnings(HttpServletRequest request, Messages messages);
/**
* Get warning messages for the specified request.
* @param request the request we are processing
* @param create if <code>true</code>, create, save and return a new instance
* if no warning messages are present
* @return messages instance or <code>null</code>
*/
public abstract Messages getWarnings(HttpServletRequest request, boolean create);
/**
* Add message as warning message, associated with a key
* @param request the request we are processing
* @param key the key used when adding the message to the {@link Messages} object
* @param message the message to be added
*/
public final void addWarning(HttpServletRequest request, String key, Message message) {
getWarnings(request, true).addMessage(key, message);
}
/**
* Add message as global warning message
* @param request the request we are processing
* @param message the message to be added
*/
public final void addWarning(HttpServletRequest request, Message message) {
getWarnings(request, true).addGlobalMessage(message);
}
/**
* Save the specified messages as infos for the specified request.
* This will replace any existing info messages.
* @param request the request we are processing
* @param messages the messages to be saved
*/
public abstract void setInfos(HttpServletRequest request, Messages messages);
/**
* Get info messages for the specified request.
* @param request the request we are processing
* @param create if <code>true</code>, create, save and return a new instance
* if no info messages are present
* @return messages instance or <code>null</code>
*/
public abstract Messages getInfos(HttpServletRequest request, boolean create);
/**
* Add message as info message, associated with a key
* @param request the request we are processing
* @param key the key used when adding the message to the {@link Messages} object
* @param message the message to be added
*/
public final void addInfo(HttpServletRequest request, String key, Message message) {
getInfos(request, true).addMessage(key, message);
}
/**
* Add message as global info message
* @param request the request we are processing
* @param message the message to be added
*/
public final void addInfo(HttpServletRequest request, Message message) {
getInfos(request, true).addGlobalMessage(message);
}
}