001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/jsf/tags/sakai_2-4-1/app/src/java/org/sakaiproject/jsf/app/MessageSaver.java $
003: * $Id: MessageSaver.java 9278 2006-05-10 23:29:21Z ray@media.berkeley.edu $
004: **********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.jsf.app;
021:
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Vector;
025:
026: import javax.faces.application.FacesMessage;
027: import javax.faces.context.FacesContext;
028: import javax.servlet.http.HttpSession;
029:
030: /**
031: * <p>
032: * MessageSaver has utility methods to save FacesMessage objects from one request to the next request (the messages are saved attributes of the current HttpSession).
033: * </p>
034: *
035: * @author University of Michigan, Sakai Software Development Team
036: * @version $Revision: 9278 $
037: */
038: public class MessageSaver {
039: /** The session attribute name used to store the latest batch of messages. */
040: public static final String ATTR_MSGS = "org.sakaiproject.jsf.messages";
041:
042: /**
043: * Restore saved messages.
044: *
045: * @param context
046: * The current faces context.
047: */
048: public static void restoreMessages(FacesContext context) {
049: if (context == null)
050: return;
051:
052: // look in the session
053: HttpSession s = (HttpSession) context.getExternalContext()
054: .getSession(false);
055: if (s == null)
056: return;
057:
058: // get messages
059: List msgs = (List) s.getAttribute(ATTR_MSGS);
060: if (msgs != null) {
061: // process each one - add it to this context's message set
062: for (Iterator iMessages = msgs.iterator(); iMessages
063: .hasNext();) {
064: FacesMessage msg = (FacesMessage) iMessages.next();
065: // Note: attributed to no specific tree element
066: context.addMessage(null, msg);
067: }
068:
069: s.removeAttribute(ATTR_MSGS);
070: }
071: }
072:
073: /**
074: * Save current messages for later restoration.
075: *
076: * @param context
077: * The current faces context.
078: */
079: public static void saveMessages(FacesContext context) {
080: if (context == null)
081: return;
082:
083: // look in the session
084: HttpSession s = (HttpSession) context.getExternalContext()
085: .getSession(false);
086: if (s == null)
087: return;
088:
089: // collect the messages from the context for restoration on the next rendering
090: List msgs = new Vector();
091: for (Iterator iMessages = context.getMessages(); iMessages
092: .hasNext();) {
093: FacesMessage msg = (FacesMessage) iMessages.next();
094: msgs.add(msg);
095: }
096:
097: // store the messages for this mode to find
098: s.setAttribute(ATTR_MSGS, msgs);
099: }
100: }
|