001 /**
002 * Copyright 2005-2012 The Kuali Foundation
003 *
004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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 org.kuali.rice.kns.util;
017
018 import org.apache.struts.action.ActionMessage;
019 import org.apache.struts.action.ActionMessages;
020 import org.kuali.rice.krad.util.ErrorMessage;
021 import org.kuali.rice.krad.util.MessageMap;
022
023 import java.io.Serializable;
024 import java.util.Iterator;
025 import java.util.List;
026 import java.util.Set;
027
028 /**
029 * 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
030 *
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 *
033 */
034 public abstract class MessageContainer implements Serializable {
035 private MessageMap errorMap;
036
037 protected MessageContainer(MessageMap errorMap) {
038 this.errorMap = errorMap;
039 }
040
041 protected MessageMap getMessageMap() {
042 return errorMap;
043 }
044
045 public ActionMessages getRequestMessages() {
046 ActionMessages requestErrors = new ActionMessages();
047 for (Iterator<String> iter = getMessagePropertyNames().iterator(); iter.hasNext();) {
048 String property = iter.next();
049 List errorList = (List) getMessagesForProperty(property);
050
051 for (Iterator iterator = errorList.iterator(); iterator.hasNext();) {
052 ErrorMessage errorMessage = (ErrorMessage) iterator.next();
053
054 // add ActionMessage with any parameters
055 requestErrors.add(property, new ActionMessage(errorMessage.getErrorKey(), errorMessage.getMessageParameters()));
056 }
057 }
058 return requestErrors;
059 }
060
061 public abstract int getMessageCount();
062
063 public abstract List<String> getMessagePropertyList();
064
065 protected abstract Set<String> getMessagePropertyNames();
066
067 protected abstract List getMessagesForProperty(String propertyName);
068 }