1 /**
2 * Copyright 2005-2012 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
19 import org.apache.struts.action.ActionMessage;
20 import org.apache.struts.action.ActionMessages;
21 import org.kuali.rice.core.api.util.RiceKeyConstants;
22 import org.kuali.rice.krad.util.ErrorMessage;
23 import org.kuali.rice.krad.util.MessageMap;
24 import org.springframework.util.AutoPopulatingList;
25
26 import java.io.Serializable;
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.List;
30
31
32 /**
33 * Provides access to a copy of an ErrorMap and information derived from it. Necessary because ErrorMap implements the Map
34 * interface, which for some reason makes JSTL unwilling to translate ErrorMap.errorCount into a call to the getErrorCount method of
35 * that ErrorMap instance.
36 *
37 * 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
38 * error message lists of all properties in the ErrorMap), I also moved in the existing code which massaged the contents of the
39 * ErrorMap for the purposes of export to the JSP.
40 *
41 *
42 */
43 public class ErrorContainer implements Serializable {
44 private final MessageMap errorMap;
45 private final int errorCount;
46
47 /**
48 * Constructs an ErrorContainer
49 *
50 * @param errorMap
51 */
52 public ErrorContainer(MessageMap errorMap) {
53 this.errorMap = errorMap;
54 this.errorCount = errorMap.getErrorCount();
55 }
56
57 /**
58 * @return number of errors in the ErrorMap used to initialize this container
59 */
60 public int getErrorCount() {
61 if (hasFormatterError()) {
62 return 0;
63 }
64 return errorCount;
65 }
66
67 /**
68 * @return simple List of all properies for which errorMessages exist in the ErrorMap used to initialize this container
69 */
70 public List getErrorPropertyList() {
71 List properties = new ArrayList();
72
73 for (Iterator iter = errorMap.getAllPropertiesWithErrors().iterator(); iter.hasNext();) {
74 properties.add(iter.next());
75 }
76
77 return properties;
78 }
79
80 /**
81 * This method checks whether the errorMap contains at least a formatter error.
82 * @return boolean true if the errorMap contains a formatter error and false otherwise
83 */
84 private boolean hasFormatterError() {
85 if (errorMap.getErrorCount()>0) {
86 for (String errorKey : errorMap.getAllPropertiesWithErrors()) {
87 AutoPopulatingList errorValues = errorMap.getMessages(errorKey);
88 for (ErrorMessage errorMessage : (List<ErrorMessage>)errorValues) {
89 if (errorMessage.getErrorKey().equals(RiceKeyConstants.ERROR_DOCUMENT_MAINTENANCE_FORMATTING_ERROR)) {
90 return true;
91 }
92 }
93 }
94 }
95 return false;
96 }
97
98 /**
99 * @return ActionMessages instance containing error messages constructed from the contents of the ErrorMap with which this
100 * container was initialized
101 */
102 public ActionMessages getRequestErrors() {
103 ActionMessages requestErrors = new ActionMessages();
104 for (Iterator iter = errorMap.getAllPropertiesWithErrors().iterator(); iter.hasNext();) {
105 String property = (String) iter.next();
106 List errorList = (List) errorMap.getErrorMessagesForProperty(property);
107
108 for (Iterator iterator = errorList.iterator(); iterator.hasNext();) {
109 ErrorMessage errorMessage = (ErrorMessage) iterator.next();
110
111 // add ActionMessage with any parameters
112 requestErrors.add(property, new ActionMessage(errorMessage.getErrorKey(), errorMessage.getMessageParameters()));
113 }
114 }
115 return requestErrors;
116 }
117 }