001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sections/tags/sakai_2-4-1/sections-app-util/src/java/org/sakaiproject/tool/section/jsf/MessagingBean.java $
003: * $Id: MessagingBean.java 20002 2006-12-22 19:52:45Z jholtzman@berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Regents of the University of California and The Regents of the University of Michigan
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.tool.section.jsf;
021:
022: import java.io.Serializable;
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import javax.faces.application.FacesMessage;
028: import javax.faces.application.FacesMessage.Severity;
029:
030: import org.apache.commons.lang.builder.EqualsBuilder;
031: import org.apache.commons.lang.builder.HashCodeBuilder;
032:
033: /**
034: * A session-scoped bean to handle jsf messages across redirects.
035: *
036: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
037: */
038: public class MessagingBean {
039: private List messages;
040:
041: public MessagingBean() {
042: messages = new ArrayList();
043: }
044:
045: public boolean hasMessages() {
046: return messages.size() > 0;
047: }
048:
049: /**
050: * Returns the current list of FacesMessages, then removes them from the local list.
051: * @return
052: */
053: public List getMessagesAndClear() {
054: List list = new ArrayList();
055: for (Iterator iter = messages.iterator(); iter.hasNext();) {
056: list.add(((MessageDecorator) iter.next()).getMessage());
057: }
058: messages.clear();
059: return list;
060: }
061:
062: /**
063: * Adds a unique message.
064: *
065: * @param message
066: */
067: public void addMessage(FacesMessage message) {
068: // Don't add the message twice (in case of double-clicks). Somewhat related to SAK-3553
069: MessageDecorator decoratedMessage = new MessageDecorator(
070: message);
071: if (!messages.contains(decoratedMessage)) {
072: messages.add(decoratedMessage);
073: }
074: }
075:
076: /**
077: * Used in place of standard FacesMessage. Overrides equals so we can add
078: * unique messages and avoid duplicates.
079: *
080: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
081: *
082: */
083: class MessageDecorator implements Serializable {
084: private static final long serialVersionUID = 1L;
085:
086: FacesMessage message;
087:
088: public MessageDecorator(FacesMessage message) {
089: this .message = message;
090: }
091:
092: public FacesMessage getMessage() {
093: return message;
094: }
095:
096: public boolean equals(Object o) {
097: MessageDecorator other = (MessageDecorator) o;
098: return new EqualsBuilder().append(getSeverity(),
099: other.getSeverity()).append(getDetail(),
100: other.getDetail()).append(getSummary(),
101: other.getSummary()).isEquals();
102: }
103:
104: public int hashCode() {
105: return new HashCodeBuilder(17, 37).append(getSeverity())
106: .append(getDetail()).append(getSummary())
107: .toHashCode();
108: }
109:
110: public String getDetail() {
111: return message.getDetail();
112: }
113:
114: public Severity getSeverity() {
115: return message.getSeverity();
116: }
117:
118: public String getSummary() {
119: return message.getSummary();
120: }
121:
122: public void setDetail(String arg0) {
123: message.setDetail(arg0);
124: }
125:
126: public void setSeverity(Severity arg0) {
127: message.setSeverity(arg0);
128: }
129:
130: public void setSummary(String arg0) {
131: message.setSummary(arg0);
132: }
133:
134: public String toString() {
135: return message.toString();
136: }
137:
138: }
139: }
|