View Javadoc
1   /**
2    * Copyright 2005-2015 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.krad.util;
17  
18  import org.apache.commons.lang.ArrayUtils;
19  import org.apache.commons.lang.StringUtils;
20  
21  import java.io.Serializable;
22  import java.util.Arrays;
23  
24  /**
25   * Contains the error message key and parameters for a specific instantiation of an error message
26   *
27   * @author Kuali Rice Team (rice.collab@kuali.org)
28   */
29  public class ErrorMessage implements Serializable {
30      private static final long serialVersionUID = 4397449554212875250L;
31  
32      private String namespaceCode;
33      private String componentCode;
34  
35      private String errorKey;
36      private String[] messageParameters;
37  
38      private String messagePrefixKey;
39      private String[] messagePrefixParameters;
40  
41      private String messageSuffixKey;
42      private String[] messageSuffixParameters;
43  
44      /**
45       * Default constructor
46       */
47      public ErrorMessage() {
48      }
49  
50      /**
51       * Convenience constructor which sets both fields
52       *
53       * @param errorKey - message key for the error
54       * @param messageParameters - zero or more parameters for the message text
55       */
56      public ErrorMessage(String errorKey, String... messageParameters) {
57          if (StringUtils.isBlank(errorKey)) {
58              StringBuilder builder = null;
59              if (messageParameters != null && messageParameters.length > 0) {
60                  builder = new StringBuilder("  Message parameters are: ");
61                  for (String param: messageParameters) {
62                      builder.append(param).append("\n");
63                  }
64              } else {
65                  builder = new StringBuilder("  Message parameters were null or empty.");
66              }
67              throw new IllegalArgumentException("invalid (blank) errorKey." + builder.toString());
68          }
69  
70          setErrorKey(errorKey);
71          setMessageParameters((String[]) ArrayUtils.clone(messageParameters));
72      }
73  
74      /**
75       * Namespace code (often an application or module code) the error message is associated with
76       *
77       * <p>
78       * Used with the component code and error key for retrieving the message text (and prefix, suffix). If null,
79       * the default namespace code will be used
80       * </p>
81       *
82       * @return String error namespace code
83       */
84      public String getNamespaceCode() {
85          return namespaceCode;
86      }
87  
88      /**
89       * Setter for the error's associated namespace code
90       *
91       * @param namespaceCode
92       */
93      public void setNamespaceCode(String namespaceCode) {
94          this.namespaceCode = namespaceCode;
95      }
96  
97      /**
98       * A code within the namespace that identifies a component or group the error message is associated with
99       *
100      * <p>
101      * Used with the namespace and error key for retrieving the message text (and prefix, suffix). If null,
102      * the default component code will be used
103      * </p>
104      *
105      * @return String component code
106      */
107     public String getComponentCode() {
108         return componentCode;
109     }
110 
111     /**
112      * Setter for the error's associated component code
113      *
114      * @param componentCode
115      */
116     public void setComponentCode(String componentCode) {
117         this.componentCode = componentCode;
118     }
119 
120     /**
121      * Sets the key to use to retrieve the message for this ErrorMessage
122      *
123      * @param errorKey
124      */
125     public void setErrorKey(String errorKey) {
126         if (StringUtils.isBlank(errorKey)) {
127             throw new IllegalArgumentException("invalid (blank) errorKey");
128         }
129 
130         this.errorKey = errorKey;
131     }
132 
133     /**
134      * Gets the message key for this ErrorMessage
135      *
136      * @return message key
137      */
138     public String getErrorKey() {
139         return errorKey;
140     }
141 
142     /**
143      * Sets the messageParameters for this ErrorMessage
144      *
145      * @param messageParameters
146      */
147     public void setMessageParameters(String[] messageParameters) {
148         this.messageParameters = messageParameters;
149     }
150 
151     /**
152      * Get the messageParameters which should be used when evaluating and generating the message for
153      * the ErrorMessage.
154      *
155      * @return the messageParameters
156      */
157     public String[] getMessageParameters() {
158         return messageParameters;
159     }
160 
161     /**
162      * @see java.lang.Object#toString()
163      */
164     @Override
165     public String toString() {
166         StringBuffer s = new StringBuffer(getErrorKey());
167 
168         String[] params = getMessageParameters();
169         if (params != null) {
170             s.append("(");
171             for (int i = 0; i < params.length; ++i) {
172                 if (i > 0) {
173                     s.append(", ");
174                 }
175                 s.append(params[i]);
176             }
177             s.append(")");
178         }
179         return s.toString();
180     }
181 
182     /**
183      * @see java.lang.Object#equals(java.lang.Object)
184      */
185     @Override
186     public boolean equals(Object obj) {
187         boolean equals = false;
188 
189         if (this == obj) {
190             equals = true;
191         } else if (obj instanceof ErrorMessage) {
192             ErrorMessage other = (ErrorMessage) obj;
193 
194             if (StringUtils.equals(getErrorKey(), other.getErrorKey())) {
195                 equals = Arrays.equals(getMessageParameters(), other.getMessageParameters());
196             }
197         }
198 
199         return equals;
200     }
201 
202     /**
203      * Defined because when you redefine equals, you must redefine hashcode.
204      *
205      * @see java.lang.Object#hashCode()
206      */
207     @Override
208     public int hashCode() {
209         int hashCode = 5011966;
210 
211         if (getErrorKey() != null) {
212             hashCode = getErrorKey().hashCode();
213         }
214 
215         return hashCode;
216     }
217 
218     /**
219      * Gets the messagePrefixKey which defines the message key for the message to be prefixed to the message
220      * defined by errorKey.  It is up to the code using this errorMessage to prepend the prefix message to the original
221      * message.
222      *
223      * @return the messagePrefixKey
224      */
225     public String getMessagePrefixKey() {
226         return messagePrefixKey;
227     }
228 
229     /**
230      * Set the messagePrefixKey
231      *
232      * @param messagePrefixKey
233      */
234     public void setMessagePrefixKey(String messagePrefixKey) {
235         this.messagePrefixKey = messagePrefixKey;
236     }
237 
238     /**
239      * Gets the messageSuffixKey which defines the message key for the message to be appended to the message
240      * defined by errorKey.  It is up to the code using this errorMessage to append the suffix message to the original
241      * message.
242      *
243      * @return the messageSuffixKey
244      */
245     public String getMessageSuffixKey() {
246         return messageSuffixKey;
247     }
248 
249     /**
250      * Set the messageSuffixKey
251      *
252      * @param messageSuffixKey
253      */
254     public void setMessageSuffixKey(String messageSuffixKey) {
255         this.messageSuffixKey = messageSuffixKey;
256     }
257 
258     /**
259      * Get the messagePrefixParameters which should be used when evaluating and generating the message for
260      * the messagePrefixKey.
261      *
262      * @return the messagePrefixParameters
263      */
264     public String[] getMessagePrefixParameters() {
265         return messagePrefixParameters;
266     }
267 
268     /**
269      * Set the messagePrefixParameters
270      *
271      * @param messagePrefixParameters
272      */
273     public void setMessagePrefixParameters(String[] messagePrefixParameters) {
274         this.messagePrefixParameters = messagePrefixParameters;
275     }
276 
277     /**
278      * Get the messagePrefixParameters which should be used when evaluating and generating the message for
279      * the messageSuffixKey.
280      *
281      * @return the messageSuffixParameters
282      */
283     public String[] getMessageSuffixParameters() {
284         return messageSuffixParameters;
285     }
286 
287     /**
288      * Set the messageSuffixParameters
289      *
290      * @param messageSuffixParameters
291      */
292     public void setMessageSuffixParameters(String[] messageSuffixParameters) {
293         this.messageSuffixParameters = messageSuffixParameters;
294     }
295 }