Author: tchemit Date: 2011-01-25 08:04:54 +0100 (Tue, 25 Jan 2011) New Revision: 2045 Url: http://nuiton.org/repositories/revision/nuiton-utils/2045 Log: fire all events in BeanValidator only after complete result merge + remove validators.xml file from api Removed: trunk/nuiton-validator/src/main/resources/validators.xml Modified: trunk/nuiton-validator/src/main/java/org/nuiton/validator/NuitonValidatorResult.java trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidator.java trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorMessage.java Modified: trunk/nuiton-validator/src/main/java/org/nuiton/validator/NuitonValidatorResult.java =================================================================== --- trunk/nuiton-validator/src/main/java/org/nuiton/validator/NuitonValidatorResult.java 2011-01-24 20:11:37 UTC (rev 2044) +++ trunk/nuiton-validator/src/main/java/org/nuiton/validator/NuitonValidatorResult.java 2011-01-25 07:04:54 UTC (rev 2045) @@ -65,7 +65,8 @@ return result; } - public boolean hasMessagesForScope(String field, NuitonValidatorScope scope) { + public boolean hasMessagesForScope(String field, + NuitonValidatorScope scope) { boolean result = false; if (messages != null) { result = messages.containsKey(scope); @@ -206,7 +207,9 @@ List<String> result = null; if (messages != null) { FieldMap<List<String>> fieldMap = messages.get(scope); - result = new ArrayList<String>(fieldMap.keySet()); + if (fieldMap != null) { + result = new ArrayList<String>(fieldMap.keySet()); + } } if (result == null) { result = Collections.emptyList(); Modified: trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidator.java =================================================================== --- trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidator.java 2011-01-24 20:11:37 UTC (rev 2044) +++ trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidator.java 2011-01-25 07:04:54 UTC (rev 2045) @@ -2,6 +2,7 @@ import org.apache.commons.beanutils.ConversionException; import org.apache.commons.beanutils.Converter; +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.util.beans.BeanUtil; @@ -17,6 +18,7 @@ import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; +import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; @@ -317,7 +319,7 @@ validate(); } setChanged(false); - setValid(!messages.hasFatalMessages() && !messages.hasErrorMessagess()); + setValid(messages == null || messages.isValid()); firePropertyChange(BEAN_PROPERTY, oldBean, bean); } @@ -516,7 +518,7 @@ public void doValidate() { validate(); - setValid(!messages.hasFatalMessages() && !messages.hasErrorMessagess()); + setValid(messages == null || messages.isValid()); setChanged(true); } @@ -621,19 +623,37 @@ pcs.firePropertyChange(propertyName, oldValue, newValue); } + protected BeanValidatorEvent createEvent(String field, + NuitonValidatorScope scope, + String[] toAdd, + String[] toDelete) { + BeanValidatorEvent evt = new BeanValidatorEvent( + this, + field, + scope, + toAdd, + toDelete + ); + return evt; + } + protected void fireFieldChanged(String field, NuitonValidatorScope scope, String[] toAdd, String[] toDelete) { - BeanValidatorEvent evt = new BeanValidatorEvent( - this, + BeanValidatorEvent evt = createEvent( field, scope, toAdd, toDelete ); + fireFieldChanged(evt); + } + + protected void fireFieldChanged(BeanValidatorEvent evt) { + for (BeanValidatorListener listener : listenerList.getListeners(BeanValidatorListener.class)) { listener.onFieldChanged(evt); @@ -651,10 +671,13 @@ Set<NuitonValidatorScope> scopes = getDelegate().getEffectiveScopes(); + // list of events to send after the merge of messages + List<BeanValidatorEvent> events = new ArrayList<BeanValidatorEvent>(); + for (NuitonValidatorScope scope : scopes) { // do the merge at scope level - mergeMessages(scope, newMessages); + mergeMessages(scope, newMessages, events); } @@ -665,10 +688,19 @@ // finally keep the new messages as the current messages messages = newMessages; } + + if (CollectionUtils.isNotEmpty(events)) { + + // send all messages + for (BeanValidatorEvent event : events) { + fireFieldChanged(event); + } + } } protected void mergeMessages(NuitonValidatorScope scope, - NuitonValidatorResult newMessages) { + NuitonValidatorResult newMessages, + List<BeanValidatorEvent> events) { if (newMessages == null) { @@ -678,7 +710,7 @@ for (String field : fieldsForScope) { List<String> messagesForScope = messages.getMessagesForScope(field, scope); - fireFieldChanged(field, scope, null, messagesForScope.toArray(new String[messagesForScope.size()])); + events.add(createEvent(field, scope, null, messagesForScope.toArray(new String[messagesForScope.size()]))); } // suppress all messages for this scope @@ -695,7 +727,7 @@ for (String field : newFields) { List<String> messagesForScope = newMessages.getMessagesForScope(field, scope); - fireFieldChanged(field, scope, messagesForScope.toArray(new String[messagesForScope.size()]), null); + events.add(createEvent(field, scope, messagesForScope.toArray(new String[messagesForScope.size()]), null)); } // nothing else to do @@ -715,7 +747,7 @@ // this fields has now messages but not before : new messages List<String> messagesForScope = newMessages.getMessagesForScope(newField, scope); - fireFieldChanged(newField, scope, messagesForScope.toArray(new String[messagesForScope.size()]), null); + events.add(createEvent(newField, scope, messagesForScope.toArray(new String[messagesForScope.size()]), null)); // treated field itr.remove(); @@ -731,7 +763,7 @@ // this fields has no more messages List<String> messagesForScope = messages.getMessagesForScope(oldField, scope); - fireFieldChanged(oldField, scope, null, messagesForScope.toArray(new String[messagesForScope.size()])); + events.add(createEvent(oldField, scope, null, messagesForScope.toArray(new String[messagesForScope.size()]))); // treated field itr.remove(); @@ -751,12 +783,12 @@ Set<String> toAdd = new HashSet<String>(newMessagesForScope); toAdd.removeAll(oldMessagesForScope); - fireFieldChanged( + events.add(createEvent( field, scope, toAdd.isEmpty() ? null : toAdd.toArray(new String[toAdd.size()]), toDelete.isEmpty() ? null : toDelete.toArray(new String[toDelete.size()]) - ); + )); } Modified: trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorMessage.java =================================================================== --- trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorMessage.java 2011-01-24 20:11:37 UTC (rev 2044) +++ trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorMessage.java 2011-01-25 07:04:54 UTC (rev 2045) @@ -1,23 +1,23 @@ /* * #%L * Nuiton Utils :: Nuiton Validator - * + * * $Id$ * $HeadURL$ * %% * Copyright (C) 2011 CodeLutin, Tony Chemit * %% * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public + * + * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * <http://www.gnu.org/licenses/lgpl-3.0.html>. * #L% @@ -41,7 +41,7 @@ * @author tchemit <chemit@codelutin.com> * @since 2.0 */ -public class BeanValidatorMessage<E extends BeanValidatorMessage<?>> implements Comparable<E> , Serializable{ +public class BeanValidatorMessage<E extends BeanValidatorMessage<?>> implements Comparable<E>, Serializable { private static final long serialVersionUID = 1L; @@ -103,22 +103,22 @@ if (this == o) { return true; } - if (o == null || getClass() != o.getClass()) { + if (!(o instanceof BeanValidatorMessage<?>)) { return false; } BeanValidatorMessage<?> that = (BeanValidatorMessage<?>) o; return field.equals(that.field) && - message.equals(that.message) && + (message != null ? !message.equals(that.message) : that.message == null) && scope == that.scope; } @Override public int hashCode() { int result = field.hashCode(); - result = 31 * result + message.hashCode(); - result = 31 * result + scope.hashCode(); + result = 31 * result + (message != null ? message.hashCode() : 0); + result = 31 * result + (scope != null ? scope.hashCode() : 0); return result; } Deleted: trunk/nuiton-validator/src/main/resources/validators.xml =================================================================== --- trunk/nuiton-validator/src/main/resources/validators.xml 2011-01-24 20:11:37 UTC (rev 2044) +++ trunk/nuiton-validator/src/main/resources/validators.xml 2011-01-25 07:04:54 UTC (rev 2045) @@ -1,48 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - #%L - Nuiton Utils :: Nuiton Validator - - $Id$ - $HeadURL$ - %% - Copyright (C) 2011 CodeLutin, Tony Chemit - %% - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation, either version 3 of the - License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Lesser Public License for more details. - - You should have received a copy of the GNU General Lesser Public - License along with this program. If not, see - <http://www.gnu.org/licenses/lgpl-3.0.html>. - #L% - --> - -<!DOCTYPE validators PUBLIC - "-//OpenSymphony Group//XWork Validator Config 1.0//EN" - "http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd"> - -<!-- START SNIPPET: validators --> -<validators> - <validator name="requiredFile" - class="org.nuiton.validator.field.RequiredFileFieldValidator"/> - <validator name="existingFile" - class="org.nuiton.validator.field.ExistingFileFieldValidator"/> - <validator name="notExistingFile" - class="org.nuiton.validator.field.NotExistingFileFieldValidator"/> - <validator name="existingDirectory" - class="org.nuiton.validator.field.ExistingDirectoryFieldValidator"/> - <validator name="notExistingDirectory" - class="org.nuiton.validator.field.NotExistingDirectoryFieldValidator"/> - <validator name="collectionFieldExpression" - class="org.nuiton.validator.field.CollectionFieldExpressionValidator"/> - <validator name="collectionUniqueKey" - class="org.nuiton.validator.field.CollectionUniqueKeyValidator"/> -</validators> -<!-- END SNIPPET: validators -->