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