View Javadoc

1   /*
2    * Copyright 2007-2009 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kns.util;
17  
18  import org.apache.struts.action.ActionMessage;
19  import org.apache.struts.action.ActionMessages;
20  import org.kuali.rice.krad.util.ErrorMessage;
21  import org.kuali.rice.krad.util.MessageMap;
22  
23  import java.io.Serializable;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Set;
27  
28  /**
29   * An adapter whose subclasses will make either an {@link org.kuali.rice.krad.util.MessageMap}'s warning or info messages available to the JSP layer
30   * 
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   *
33   */
34  public abstract class MessageContainer implements Serializable {
35  	private MessageMap errorMap;
36  	
37  	protected MessageContainer(MessageMap errorMap) {
38  		this.errorMap = errorMap;
39  	}
40  	
41  	protected MessageMap getMessageMap() {
42  		return errorMap;
43  	}
44  	
45      public ActionMessages getRequestMessages() {
46          ActionMessages requestErrors = new ActionMessages();
47          for (Iterator<String> iter = getMessagePropertyNames().iterator(); iter.hasNext();) {
48              String property = iter.next();
49              List errorList = (List) getMessagesForProperty(property);
50  
51              for (Iterator iterator = errorList.iterator(); iterator.hasNext();) {
52                  ErrorMessage errorMessage = (ErrorMessage) iterator.next();
53  
54                  // add ActionMessage with any parameters
55                  requestErrors.add(property, new ActionMessage(errorMessage.getErrorKey(), errorMessage.getMessageParameters()));
56              }
57          }
58          return requestErrors;
59      }
60      
61      public abstract int getMessageCount();
62      
63      public abstract List<String> getMessagePropertyList();
64  
65      protected abstract Set<String> getMessagePropertyNames();
66      
67      protected abstract List getMessagesForProperty(String propertyName);
68  }