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