001: /*
002: * Copyright 2006 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.core.util;
017:
018: import java.io.Serializable;
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Set;
023:
024: import org.apache.struts.action.ActionMessage;
025: import org.apache.struts.action.ActionMessages;
026: import org.kuali.RiceKeyConstants;
027:
028: /**
029: * Provides access to a copy of an ErrorMap and information derived from it. Necessary because ErrorMap implements the Map
030: * interface, which for some reason makes JSTL unwilling to translate ErrorMap.errorCount into a call to the getErrorCount method of
031: * that ErrorMap instance.
032: *
033: * Since I had to create this class to provide easy access to the error count (which must be computed as the sum of the sizes of the
034: * error message lists of all properties in the ErrorMap), I also moved in the existing code which massaged the contents of the
035: * ErrorMap for the purposes of export to the JSP.
036: *
037: *
038: */
039: public class ErrorContainer implements Serializable {
040: private final ErrorMap errorMap;
041: private final int errorCount;
042:
043: /**
044: * Constructs an ErrorContainer
045: *
046: * @param errorMap
047: */
048: public ErrorContainer(ErrorMap errorMap) {
049: this .errorMap = errorMap;
050: this .errorCount = errorMap.getErrorCount();
051: }
052:
053: /**
054: * @return number of errors in the ErrorMap used to initialize this container
055: */
056: public int getErrorCount() {
057: if (hasFormatterError()) {
058: return 0;
059: }
060: return errorCount;
061: }
062:
063: /**
064: * @return simple List of all properies for which errorMessages exist in the ErrorMap used to initialize this container
065: */
066: public List getErrorPropertyList() {
067: List properties = new ArrayList();
068:
069: for (Iterator iter = errorMap.keySet().iterator(); iter
070: .hasNext();) {
071: properties.add(iter.next());
072: }
073:
074: return properties;
075: }
076:
077: /**
078: * This method checks whether the errorMap contains at least a formatter error.
079: * @return boolean true if the errorMap contains a formatter error and false otherwise
080: */
081: private boolean hasFormatterError() {
082: if (errorMap.getErrorCount() > 0) {
083: for (String errorKey : (Set<String>) errorMap.keySet()) {
084: TypedArrayList errorValues = errorMap
085: .getMessages(errorKey);
086: for (ErrorMessage errorMessage : (List<ErrorMessage>) errorValues) {
087: if (errorMessage
088: .getErrorKey()
089: .equals(
090: RiceKeyConstants.ERROR_DOCUMENT_MAINTENANCE_FORMATTING_ERROR)) {
091: return true;
092: }
093: }
094: }
095: }
096: return false;
097: }
098:
099: /**
100: * @return ActionMessages instance containing error messages constructed from the contents of the ErrorMap with which this
101: * container was initialized
102: */
103: public ActionMessages getRequestErrors() {
104: ActionMessages requestErrors = new ActionMessages();
105: for (Iterator iter = GlobalVariables.getErrorMap().keySet()
106: .iterator(); iter.hasNext();) {
107: String property = (String) iter.next();
108: List errorList = (List) GlobalVariables.getErrorMap().get(
109: property);
110:
111: for (Iterator iterator = errorList.iterator(); iterator
112: .hasNext();) {
113: ErrorMessage errorMessage = (ErrorMessage) iterator
114: .next();
115:
116: // add ActionMessage with any parameters
117: requestErrors.add(property, new ActionMessage(
118: errorMessage.getErrorKey(), errorMessage
119: .getMessageParameters()));
120: }
121: }
122: return requestErrors;
123: }
124: }
|