001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.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/ecl2.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 */
016package org.kuali.rice.krad.util;
017
018import org.apache.commons.lang.ArrayUtils;
019import org.apache.commons.lang.StringUtils;
020
021import java.io.Serializable;
022import java.util.Arrays;
023
024/**
025 * Contains the error message key and parameters for a specific instantiation of an error message
026 *
027 * @author Kuali Rice Team (rice.collab@kuali.org)
028 */
029public class ErrorMessage implements Serializable {
030    private static final long serialVersionUID = 4397449554212875250L;
031
032    private String namespaceCode;
033    private String componentCode;
034
035    private String errorKey;
036    private String[] messageParameters;
037
038    private String messagePrefixKey;
039    private String[] messagePrefixParameters;
040
041    private String messageSuffixKey;
042    private String[] messageSuffixParameters;
043
044    /**
045     * Default constructor
046     */
047    public ErrorMessage() {
048    }
049
050    /**
051     * Convenience constructor which sets both fields
052     *
053     * @param errorKey - message key for the error
054     * @param messageParameters - zero or more parameters for the message text
055     */
056    public ErrorMessage(String errorKey, String... messageParameters) {
057        if (StringUtils.isBlank(errorKey)) {
058            StringBuilder builder = null;
059            if (messageParameters != null && messageParameters.length > 0) {
060                builder = new StringBuilder("  Message parameters are: ");
061                for (String param: messageParameters) {
062                    builder.append(param).append("\n");
063                }
064            } else {
065                builder = new StringBuilder("  Message parameters were null or empty.");
066            }
067            throw new IllegalArgumentException("invalid (blank) errorKey." + builder.toString());
068        }
069
070        setErrorKey(errorKey);
071        setMessageParameters((String[]) ArrayUtils.clone(messageParameters));
072    }
073
074    /**
075     * Namespace code (often an application or module code) the error message is associated with
076     *
077     * <p>
078     * Used with the component code and error key for retrieving the message text (and prefix, suffix). If null,
079     * the default namespace code will be used
080     * </p>
081     *
082     * @return String error namespace code
083     */
084    public String getNamespaceCode() {
085        return namespaceCode;
086    }
087
088    /**
089     * Setter for the error's associated namespace code
090     *
091     * @param namespaceCode
092     */
093    public void setNamespaceCode(String namespaceCode) {
094        this.namespaceCode = namespaceCode;
095    }
096
097    /**
098     * A code within the namespace that identifies a component or group the error message is associated with
099     *
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}