001: /*
002: * $Header: /cvsroot/jvalidate/jvalidate-framework/jvalidate/src/main/java/nl/knowlogy/validation/MessagesImpl.java,v 1.4 2007/04/20 12:53:18 roberthofstra Exp $
003: * $Revision: 1.4 $
004: * $Date: 2007/04/20 12:53:18 $
005: *
006: *
007: * Copyright 2004 - 2005 Knowlogy, the Netherlands, www.knowlogy.nl
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021: package nl.knowlogy.validation;
022:
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.ResourceBundle;
027:
028: import org.apache.commons.collections.MultiHashMap;
029: import org.apache.commons.lang.StringUtils;
030: import org.apache.log4j.Logger;
031:
032: /**
033: *
034: *
035: * @see nl.knowlogy.validation.Messages
036: *
037: * @author <a href="mailto:robert.hofstra@knowlogy.nl">Robert Hofstra, Knowlogy</a>
038: */
039: public class MessagesImpl implements Messages {
040:
041: public static final long serialVersionUID = 0;
042:
043: private List messagesList = new ArrayList();
044: private static final Logger logger = Logger
045: .getLogger(MessagesImpl.class);
046:
047: private MultiHashMap messagesMap = new MultiHashMap();
048:
049: public void addMessage(Message message) {
050: add(message);
051: }
052:
053: /*
054: * (non-Javadoc)
055: *
056: * @see nl.knowlogy.validation.errors.Errors#
057: */
058: public void addMessage(MessageType messageType, Object object,
059: String errorCode) {
060: add(new MessageImpl(messageType, object, null, errorCode));
061: }
062:
063: /*
064: * (non-Javadoc)
065: *
066: * @see nl.knowlogy.validation.errors.Errors#reject(java.lang.String,
067: * java.lang.Object[], java.lang.String)
068: */
069: public void addMessage(MessageType messageType, Object object,
070: String errorCode, Object[] errorArgs, String errorMessage) {
071: add(new MessageImpl(messageType, object, null, errorCode,
072: errorArgs, errorMessage));
073:
074: }
075:
076: /*
077: * (non-Javadoc)
078: *
079: * @see nl.knowlogy.validation.errors.Errors#reject(java.lang.String,
080: * java.lang.String)
081: */
082: public void addMessage(MessageType messageType, Object object,
083: String errorCode, String defaultMessage) {
084: add(new MessageImpl(messageType, object, null, errorCode,
085: defaultMessage));
086: }
087:
088: /*
089: * (non-Javadoc)
090: *
091: * @see nl.knowlogy.validation.errors.Errors#rejectValue(java.lang.String,
092: * java.lang.String)
093: */
094: public void addPropertyMessage(MessageType messageType,
095: Object object, String propertyName, String errorCode) {
096:
097: add(new MessageImpl(messageType, object, propertyName,
098: errorCode));
099:
100: }
101:
102: public void addPropertyMessage(MessageType messageType,
103: Object object, String propertyName, String errorCode,
104: String errorMessage) {
105:
106: add(new MessageImpl(messageType, object, propertyName,
107: errorCode, errorMessage));
108:
109: }
110:
111: /*
112: * (non-Javadoc)
113: *
114: * @see nl.knowlogy.metadata.errors.Errors#rejectValue(java.lang.String,
115: * java.lang.String, java.lang.Object[], java.lang.String)
116: */
117: public void addPropertyMessage(MessageType messageType,
118: Object object, String propertyName, String errorCode,
119: Object[] errorArgs, String errorMessage) {
120: add(new MessageImpl(messageType, object, propertyName,
121: errorCode, errorArgs, errorMessage));
122:
123: }
124:
125: public String toString() {
126: StringBuffer messageBuffer = new StringBuffer();
127: messageBuffer.append("Messages, [");
128:
129: for (Iterator messageIterator = messagesList.iterator(); messageIterator
130: .hasNext();) {
131: Message message = (Message) messageIterator.next();
132: messageBuffer.append("[");
133: messageBuffer.append(message.toString());
134: messageBuffer.append("]");
135: }
136: messageBuffer.append("]");
137: return messageBuffer.toString();
138: }
139:
140: public List getMessages() {
141: List copyMessageList = new ArrayList();
142: copyMessageList.addAll(messagesList);
143: return copyMessageList;
144: }
145:
146: public void addMessages(Messages messages) {
147:
148: for (Iterator messagesIter = messages.getMessages().iterator(); messagesIter
149: .hasNext();) {
150: Message message = (Message) messagesIter.next();
151: add(message);
152: }
153:
154: }
155:
156: public int getNumberOfErrorMessages() {
157: return getNumberOfMessages(MessageType.ERROR);
158: }
159:
160: public void convertMessageCodes(ResourceBundle resourceBundle) {
161: for (Iterator messageIterator = messagesList.iterator(); messageIterator
162: .hasNext();) {
163: Message message = (Message) messageIterator.next();
164: if (message.getMessage() == null
165: && message.getMessageCode() != null) {
166:
167: try {
168: String messagestr = resourceBundle
169: .getString(message.getMessageCode());
170: message.setMessage(messagestr);
171: } catch (java.util.MissingResourceException mre) {
172: logger.error("Can't find message for key "
173: + message.getMessageCode(), mre);
174: message
175: .setMessage("Field has error, no key found in resoucebundle "
176: + message.getMessageCode());
177: }
178: }
179: }
180: }
181:
182: public boolean hasErrors() {
183: return getNumberOfErrorMessages() != 0;
184: }
185:
186: public Message getObjectMessage(Object object, String fieldName) {
187: return getMessage(object, fieldName);
188: }
189:
190: /*
191: * (non-Javadoc)
192: *
193: * @see nl.knowlogy.validation.errors.Errors#getObjectError(java.lang.Object)
194: */
195: public Message getObjectMessage(Object object) {
196: return getMessage(object, false);
197: }
198:
199: /*
200: * (non-Javadoc)
201: *
202: * @see nl.knowlogy.validation.errors.Errors#getObjectError(java.lang.Object,
203: * boolean)
204: */
205: public Message getObjectMessage(Object object,
206: boolean objectLevelOnly) {
207: return getMessage(object, objectLevelOnly);
208: }
209:
210: public void add(Message message) {
211: messagesList.add(message);
212: messagesMap.put(message.getPropertyName(), message);
213: }
214:
215: public int getNumberOfMessages() {
216: return messagesList.size();
217: }
218:
219: public Message get(int index) {
220: return (Message) messagesList.get(index);
221: }
222:
223: public Message getMessage(Object object, String propertyName) {
224: for (Iterator messageListIterator = messagesList.iterator(); messageListIterator
225: .hasNext();) {
226: Message message = (Message) messageListIterator.next();
227: if ((message.getRelatedObject() == object)
228: && (propertyName.equals(message.getPropertyName()))) {
229: return message;
230: }
231: }
232: return null;
233: }
234:
235: public List getMessages(Object object, String propertyName) {
236: List errorList = new ArrayList();
237: for (Iterator messageListIterator = messagesList.iterator(); messageListIterator
238: .hasNext();) {
239: Message message = (Message) messageListIterator.next();
240: if ((message.getRelatedObject() == object)
241: && (propertyName.equals(message.getPropertyName()))) {
242: errorList.add(message);
243: }
244: }
245:
246: return errorList;
247: }
248:
249: public Message getMessage(Object object, boolean objectLevelOnly) {
250: for (Iterator messageListIterator = messagesList.iterator(); messageListIterator
251: .hasNext();) {
252: Message message = (Message) messageListIterator.next();
253: if ((message.getRelatedObject() == object)
254: && (includeForObjectLevel(
255: message.getPropertyName(), objectLevelOnly))) {
256: return message;
257: }
258: }
259: return null;
260: }
261:
262: public List getMessages(Object object, boolean objectLevelOnly) {
263: List messages = new ArrayList();
264: for (Iterator messageListIterator = messagesList.iterator(); messageListIterator
265: .hasNext();) {
266: Message message = (Message) messageListIterator.next();
267: if ((message.getRelatedObject() == object)
268: && (includeForObjectLevel(
269: message.getPropertyName(), objectLevelOnly))) {
270: messages.add(message);
271: }
272: }
273: return messages;
274: }
275:
276: private boolean includeForObjectLevel(String fieldName,
277: boolean objectLevelOnly) {
278: return !objectLevelOnly || StringUtils.isEmpty(fieldName);
279: }
280:
281: public String getMessage() {
282: return toString();
283: }
284:
285: public int getNumberOfMessages(MessageType messageType) {
286: int count = 0;
287: for (Iterator messageListIterator = messagesList.iterator(); messageListIterator
288: .hasNext();) {
289: Message message = (Message) messageListIterator.next();
290: if (message.getMessageType().equals(messageType)) {
291: count++;
292: }
293:
294: }
295: return count;
296:
297: }
298:
299: public void clear() {
300: messagesList.clear();
301: messagesMap.clear();
302: }
303: }
|