org.nuiton.validator.bean
Class BeanValidator<O>

java.lang.Object
  extended by org.nuiton.validator.bean.BeanValidator<O>

public class BeanValidator<O>
extends Object

Validator for a javaBean object.

A such validator is designed to validate to keep the validation of a bean, means the bean is attached to the validator (field bean.

A such validator is also a JavaBean and you can listen his states modifications via the classic java bean api.

Note: The BeanValidator should never be used for validation in a service approch since it needs to keep a reference to the bean to validate.

Since:
2.0
Author:
tchemit
See Also:
BeanValidatorListener

Field Summary
protected  O bean
          The bean to validate.
static String BEAN_PROPERTY
          Name of the bounded property bean.
protected  boolean canValidate
          State to know if the validator can be used (we keep this state for performance reasons : do not want to compute this value each time a validation is asked...).
protected  boolean changed
          State to indicate that validator has changed since the last time bean was setted.
static String CHANGED_PROPERTY
          Name of the bounded property changed.
static String CONTEXT_PROPERTY
          Name of the bounded property context.
protected  Map<String,String> conversionErrors
          map of conversion errors detected by this validator
protected  NuitonValidator<O> delegate
          The delegate validator used to validate the bean.
protected  PropertyChangeListener l
          Listener that listens on bean modification.
protected  EventListenerList listenerList
          A list of event listeners for this validators
protected static org.apache.commons.logging.Log log
          Logger.
protected  NuitonValidatorResult messages
          State of validation (keep all messages of validation for the filled bean).
protected  BeanValidator<?> parentValidator
          To chain to another validator (acting as parent of this one).
protected  PropertyChangeSupport pcs
          delegate property change support
static String SCOPES_PROPERTY
          Name of the bounded property scopes.
protected  boolean valid
          State of the validator (is true if no errors of error scope is found).
static String VALID_PROPERTY
          Name of the bounded property valid.
protected  NuitonValidatorProvider validatorProvider
          The provider of delegate validators.
 
Constructor Summary
BeanValidator(NuitonValidatorProvider validatorProvider, Class<O> beanClass, String context)
           
BeanValidator(NuitonValidatorProvider validatorProvider, Class<O> beanClass, String context, NuitonValidatorScope... scopes)
           
 
Method Summary
 void addBeanValidatorListener(BeanValidatorListener listener)
           
 void addPropertyChangeListener(PropertyChangeListener listener)
           
 void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
           
 boolean containsField(String fieldName)
          Test a the validator contains the field given his name
<T> T
convert(String fieldName, String value, Class<T> valueClass)
          Convert a value.
protected  BeanValidatorEvent createEvent(String field, NuitonValidatorScope scope, String[] toAdd, String[] toDelete)
           
 void doValidate()
           
protected  void fireFieldChanged(BeanValidatorEvent evt)
           
protected  void fireFieldChanged(String field, NuitonValidatorScope scope, String[] toAdd, String[] toDelete)
           
 void firePropertyChange(String propertyName, Object oldValue, Object newValue)
           
 O getBean()
          Obtain the actual bean attached to the validator.
 BeanValidatorListener[] getBeanValidatorListeners()
           
 String getContext()
           
protected  NuitonValidator<O> getDelegate()
           
 Set<String> getEffectiveFields()
           
 Set<String> getEffectiveFields(NuitonValidatorScope scope)
           
 Set<NuitonValidatorScope> getEffectiveScopes()
           
 NuitonValidatorScope getHighestScope(String field)
           
 BeanValidator<?> getParentValidator()
           
 Set<NuitonValidatorScope> getScopes()
           
 Class<O> getType()
           
 boolean hasErrors()
           
 boolean hasFatalErrors()
           
 boolean hasInfos()
           
 boolean hasWarnings()
           
 boolean isCanValidate()
           
 boolean isChanged()
          Obtain the changed property value.
 boolean isValid()
          Obtain the valid property value.
 boolean isValid(String fieldName)
           
protected  void mergeMessages(NuitonValidatorResult newMessages)
           
protected  void mergeMessages(NuitonValidatorScope scope, NuitonValidatorResult newMessages, List<BeanValidatorEvent> events)
           
protected  void rebuildDelegateValidator(Class<O> beanType, String context, NuitonValidatorScope... scopes)
           
 void removeBeanValidatorListener(BeanValidatorListener listener)
           
 void removePropertyChangeListener(PropertyChangeListener listener)
           
 void removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
           
 void setBean(O bean)
          Change the attached bean.
 void setCanValidate(boolean canValidate)
           
 void setChanged(boolean changed)
          To force the value of the property changed.
 void setContext(String context)
           
 void setParentValidator(BeanValidator<?> parentValidator)
           
 void setScopes(NuitonValidatorScope... scopes)
           
 void setValid(boolean valid)
          Change the value of the valid property.
 String toString()
           
 void validate()
          il faut eviter le code re-intrant (durant une validation, une autre est demandee).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

BEAN_PROPERTY

public static final String BEAN_PROPERTY
Name of the bounded property bean.

See Also:
bean, getBean(), setBean(Object), Constant Field Values

CONTEXT_PROPERTY

public static final String CONTEXT_PROPERTY
Name of the bounded property context.

See Also:
getContext(), setContext(String), Constant Field Values

SCOPES_PROPERTY

public static final String SCOPES_PROPERTY
Name of the bounded property scopes.

See Also:
getScopes(), setScopes(NuitonValidatorScope...), Constant Field Values

VALID_PROPERTY

public static final String VALID_PROPERTY
Name of the bounded property valid.

See Also:
valid, isValid(), setValid(boolean), Constant Field Values

CHANGED_PROPERTY

public static final String CHANGED_PROPERTY
Name of the bounded property changed.

See Also:
changed, isChanged(), setChanged(boolean), Constant Field Values

log

protected static final org.apache.commons.logging.Log log
Logger.


bean

protected O bean
The bean to validate.


parentValidator

protected BeanValidator<?> parentValidator
To chain to another validator (acting as parent of this one).


delegate

protected NuitonValidator<O> delegate
The delegate validator used to validate the bean.


messages

protected NuitonValidatorResult messages
State of validation (keep all messages of validation for the filled bean).


changed

protected boolean changed
State to indicate that validator has changed since the last time bean was setted.


valid

protected boolean valid
State of the validator (is true if no errors of error scope is found).


canValidate

protected boolean canValidate
State to know if the validator can be used (we keep this state for performance reasons : do not want to compute this value each time a validation is asked...).


conversionErrors

protected Map<String,String> conversionErrors
map of conversion errors detected by this validator


l

protected PropertyChangeListener l
Listener that listens on bean modification.


pcs

protected PropertyChangeSupport pcs
delegate property change support


listenerList

protected EventListenerList listenerList
A list of event listeners for this validators


validatorProvider

protected final NuitonValidatorProvider validatorProvider
The provider of delegate validators.

It will also produce validator model.

See Also:
NuitonValidatorProvider
Constructor Detail

BeanValidator

public BeanValidator(NuitonValidatorProvider validatorProvider,
                     Class<O> beanClass,
                     String context)

BeanValidator

public BeanValidator(NuitonValidatorProvider validatorProvider,
                     Class<O> beanClass,
                     String context,
                     NuitonValidatorScope... scopes)
Method Detail

isChanged

public boolean isChanged()
Obtain the changed property value.

Returns true if bean was modified since last time a bean was attached.

Returns:
true if bean was modified since last attachement of a bean.

setChanged

public void setChanged(boolean changed)
To force the value of the property changed.

Parameters:
changed - flag to force reset of property changed

isCanValidate

public boolean isCanValidate()

setCanValidate

public void setCanValidate(boolean canValidate)

isValid

public boolean isValid()
Obtain the valid property value.

Returns:
true if attached bean is valid (no error or fatal messages)

setValid

public void setValid(boolean valid)
Change the value of the valid property.

Parameters:
valid - the new value of the property

getBean

public O getBean()
Obtain the actual bean attached to the validator.

Returns:
the bean attached to the validor or null if no bean is attached

setBean

public void setBean(O bean)
Change the attached bean.

As a side effect, the internal messages will be reset.

Parameters:
bean - the bean to attach (can be null to reset the validator).

getContext

public String getContext()

setContext

public void setContext(String context)

getScopes

public Set<NuitonValidatorScope> getScopes()

getEffectiveScopes

public Set<NuitonValidatorScope> getEffectiveScopes()

getEffectiveFields

public Set<String> getEffectiveFields()

getEffectiveFields

public Set<String> getEffectiveFields(NuitonValidatorScope scope)

setScopes

public void setScopes(NuitonValidatorScope... scopes)

getType

public Class<O> getType()

rebuildDelegateValidator

protected void rebuildDelegateValidator(Class<O> beanType,
                                        String context,
                                        NuitonValidatorScope... scopes)

getParentValidator

public BeanValidator<?> getParentValidator()

setParentValidator

public void setParentValidator(BeanValidator<?> parentValidator)

hasFatalErrors

public boolean hasFatalErrors()

hasErrors

public boolean hasErrors()

hasWarnings

public boolean hasWarnings()

hasInfos

public boolean hasInfos()

containsField

public boolean containsField(String fieldName)
Test a the validator contains the field given his name

Parameters:
fieldName - the name of the searched field
Returns:
true if validator contaisn this field, false otherwise

isValid

public boolean isValid(String fieldName)

getHighestScope

public NuitonValidatorScope getHighestScope(String field)

convert

public <T> T convert(String fieldName,
                     String value,
                     Class<T> valueClass)
Convert a value.

If an error occurs, then add an error in validator.

Type Parameters:
T - the type of conversion
Parameters:
fieldName - the name of the bean property
value - the value to convert
valueClass - the type of converted value
Returns:
the converted value, or null if conversion was not ok

doValidate

public void doValidate()

validate

public void validate()
il faut eviter le code re-intrant (durant une validation, une autre est demandee). Pour cela on fait la validation dans un thread, et tant que la premiere validation n'est pas fini, on ne repond pas aux solicitations. Cette method est public pour permettre de force une validation par programmation, ce qui est utile par exemple si le bean ne supporte pas les PropertyChangeListener

Note: la methode est protected et on utilise la methode doValidate() car la méthode ne modifie pas les etats internes et cela en rend son utilisation delicate (le validateur entre dans un etat incoherent par rapport aux messages envoyés).


toString

public String toString()
Overrides:
toString in class Object

addBeanValidatorListener

public void addBeanValidatorListener(BeanValidatorListener listener)

removeBeanValidatorListener

public void removeBeanValidatorListener(BeanValidatorListener listener)

getBeanValidatorListeners

public BeanValidatorListener[] getBeanValidatorListeners()

addPropertyChangeListener

public void addPropertyChangeListener(PropertyChangeListener listener)

addPropertyChangeListener

public void addPropertyChangeListener(String propertyName,
                                      PropertyChangeListener listener)

removePropertyChangeListener

public void removePropertyChangeListener(PropertyChangeListener listener)

removePropertyChangeListener

public void removePropertyChangeListener(String propertyName,
                                         PropertyChangeListener listener)

firePropertyChange

public void firePropertyChange(String propertyName,
                               Object oldValue,
                               Object newValue)

createEvent

protected BeanValidatorEvent createEvent(String field,
                                         NuitonValidatorScope scope,
                                         String[] toAdd,
                                         String[] toDelete)

fireFieldChanged

protected void fireFieldChanged(String field,
                                NuitonValidatorScope scope,
                                String[] toAdd,
                                String[] toDelete)

fireFieldChanged

protected void fireFieldChanged(BeanValidatorEvent evt)

mergeMessages

protected void mergeMessages(NuitonValidatorResult newMessages)

mergeMessages

protected void mergeMessages(NuitonValidatorScope scope,
                             NuitonValidatorResult newMessages,
                             List<BeanValidatorEvent> events)

getDelegate

protected NuitonValidator<O> getDelegate()


Copyright © 2011 CodeLutin. All Rights Reserved.