Package org.nuiton.validator.bean.simple
Class SimpleBeanValidator<O>
- java.lang.Object
-
- org.nuiton.validator.bean.AbstractValidator<O>
-
- org.nuiton.validator.bean.simple.SimpleBeanValidator<O>
-
public class SimpleBeanValidator<O> extends AbstractValidator<O>
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 (via the context fieldcontext. A such validator is also a JavaBean and you can listen his states modifications via the classic java bean api. Note: TheSimpleBeanValidatorshould never be used for validation in a service approch since it needs to keep a reference to the bean to validate.- Since:
- 2.5.2
- Author:
- Tony Chemit - chemit@codelutin.com
- See Also:
SimpleBeanValidatorListener
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classSimpleBeanValidator.NuitonValidatorContext<O>
-
Field Summary
Fields Modifier and Type Field Description static StringBEAN_PROPERTYName of the bounded propertybean.protected SimpleBeanValidator.NuitonValidatorContext<O>contextContext of the registred bean to validate.protected SimpleBeanValidator<?>parentValidatorTo chain to another validator (acting as parent of this one).-
Fields inherited from class org.nuiton.validator.bean.AbstractValidator
canValidate, changed, CHANGED_PROPERTY, CONTEXT_PROPERTY, l, listenerList, pcs, SCOPES_PROPERTY, valid, VALID_PROPERTY, validatorProvider
-
-
Constructor Summary
Constructors Constructor Description SimpleBeanValidator(NuitonValidatorProvider validatorProvider, Class<O> beanClass, String context)SimpleBeanValidator(NuitonValidatorProvider validatorProvider, Class<O> beanClass, String context, NuitonValidatorScope... scopes)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddSimpleBeanValidatorListener(SimpleBeanValidatorListener listener)<T> Tconvert(String fieldName, String value, Class<T> valueClass)Convert a value.<T> Tconvert(O bean, String fieldName, String value, Class<T> valueClass)voiddoValidate()protected voiddoValidate(O bean)protected voidfireFieldChanged(SimpleBeanValidatorEvent evt)OgetBean()Obtain the actual bean attached to the validator.protected NuitonValidator<O>getDelegate()NuitonValidatorScopegetHighestScope(String field)SimpleBeanValidator<?>getParentValidator()SimpleBeanValidatorListener[]getSimpleBeanValidatorListeners()booleanhasErrors()booleanhasFatalErrors()booleanhasInfos()booleanhasWarnings()booleanisValid(String fieldName)protected voidmergeMessages(NuitonValidatorResult newMessages)static <O> SimpleBeanValidator<O>newValidator(Class<O> type, String context, NuitonValidatorScope... scopes)Obtain a newSimpleBeanValidatorfor the given parameters.static <O> SimpleBeanValidator<O>newValidator(String providerName, Class<O> type, String context, NuitonValidatorScope... scopes)Obtain a newSimpleBeanValidatorfor the given parameters.protected voidrebuildDelegateValidator(Class<O> beanType, String context, NuitonValidatorScope... scopes)voidremoveSimpleBeanValidatorListener(SimpleBeanValidatorListener listener)voidsetBean(O bean)Change the attached bean.voidsetParentValidator(SimpleBeanValidator<?> parentValidator)protected voidvalidate()il faut eviter le code re-intrant (durant une validation, une autre est demandee).-
Methods inherited from class org.nuiton.validator.bean.AbstractValidator
addPropertyChangeListener, addPropertyChangeListener, containsField, firePropertyChange, getContext, getEffectiveFields, getEffectiveFields, getEffectiveScopes, getModel, getScopes, getType, isCanValidate, isChanged, isValid, removePropertyChangeListener, removePropertyChangeListener, setCanValidate, setChanged, setContext, setScopes, setValid
-
-
-
-
Field Detail
-
BEAN_PROPERTY
public static final String BEAN_PROPERTY
Name of the bounded propertybean.- See Also:
getBean(),setBean(Object), Constant Field Values
-
context
protected final SimpleBeanValidator.NuitonValidatorContext<O> context
Context of the registred bean to validate.- Since:
- 2.5.2
-
parentValidator
protected SimpleBeanValidator<?> parentValidator
To chain to another validator (acting as parent of this one).- Since:
- 2.5.2
-
-
Constructor Detail
-
SimpleBeanValidator
public SimpleBeanValidator(NuitonValidatorProvider validatorProvider, Class<O> beanClass, String context)
-
SimpleBeanValidator
public SimpleBeanValidator(NuitonValidatorProvider validatorProvider, Class<O> beanClass, String context, NuitonValidatorScope... scopes)
-
-
Method Detail
-
newValidator
public static <O> SimpleBeanValidator<O> newValidator(Class<O> type, String context, NuitonValidatorScope... scopes) throws NullPointerException
Obtain a newSimpleBeanValidatorfor the given parameters. Note: It will use the default provider ofNuitonValidator- Type Parameters:
O- type of bean to validate- Parameters:
type- type of bean to validatecontext- context of validationscopes- authorized scopes (ifnull, will use all scopes)- Returns:
- the new instanciated
SimpleBeanValidator. - Throws:
NullPointerException- if type isnull- See Also:
NuitonValidatorFactory.getDefaultProviderName()
-
newValidator
public static <O> SimpleBeanValidator<O> newValidator(String providerName, Class<O> type, String context, NuitonValidatorScope... scopes) throws NullPointerException
Obtain a newSimpleBeanValidatorfor the given parameters. Note: It will use the provider ofNuitonValidatordefined by theproviderName.- Type Parameters:
O- type of bean to validate- Parameters:
providerName- name ofNuitonValidatorto usetype- type of bean to validatecontext- context of validationscopes- authorized scopes (ifnull, will use all scopes)- Returns:
- the new instanciated
SimpleBeanValidator. - Throws:
NullPointerException- if type isnull- See Also:
NuitonValidatorFactory.getProvider(String)
-
getBean
public O getBean()
Obtain the actual bean attached to the validator.- Returns:
- the bean attached to the validor or
nullif no bean is attached
-
setBean
public void setBean(O bean)
Change the attached bean. As a side effect, the internalAbstractNuitonValidatorContext.messageswill be reset.- Parameters:
bean- the bean to attach (can benullto reset the validator).
-
getParentValidator
public SimpleBeanValidator<?> getParentValidator()
-
setParentValidator
public void setParentValidator(SimpleBeanValidator<?> parentValidator)
-
hasFatalErrors
public boolean hasFatalErrors()
- Specified by:
hasFatalErrorsin classAbstractValidator<O>
-
hasErrors
public boolean hasErrors()
- Specified by:
hasErrorsin classAbstractValidator<O>
-
hasWarnings
public boolean hasWarnings()
- Specified by:
hasWarningsin classAbstractValidator<O>
-
hasInfos
public boolean hasInfos()
- Specified by:
hasInfosin classAbstractValidator<O>
-
isValid
public boolean isValid(String fieldName)
- Specified by:
isValidin classAbstractValidator<O>
-
getHighestScope
public NuitonValidatorScope getHighestScope(String field)
- Specified by:
getHighestScopein classAbstractValidator<O>
-
convert
public <T> T convert(O bean, String fieldName, String value, Class<T> valueClass)
- Specified by:
convertin classAbstractValidator<O>
-
doValidate
public void doValidate()
- Specified by:
doValidatein classAbstractValidator<O>
-
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 propertyvalue- the value to convertvalueClass- the type of converted value- Returns:
- the converted value, or null if conversion was not ok
-
addSimpleBeanValidatorListener
public void addSimpleBeanValidatorListener(SimpleBeanValidatorListener listener)
-
removeSimpleBeanValidatorListener
public void removeSimpleBeanValidatorListener(SimpleBeanValidatorListener listener)
-
getSimpleBeanValidatorListeners
public SimpleBeanValidatorListener[] getSimpleBeanValidatorListeners()
-
doValidate
protected void doValidate(O bean)
- Specified by:
doValidatein classAbstractValidator<O>
-
getDelegate
protected NuitonValidator<O> getDelegate()
- Specified by:
getDelegatein classAbstractValidator<O>
-
rebuildDelegateValidator
protected void rebuildDelegateValidator(Class<O> beanType, String context, NuitonValidatorScope... scopes)
- Specified by:
rebuildDelegateValidatorin classAbstractValidator<O>
-
validate
protected 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 lesPropertyChangeListenerNote: la methode est protected et on utilise la methodedoValidate()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).
-
fireFieldChanged
protected void fireFieldChanged(SimpleBeanValidatorEvent evt)
-
mergeMessages
protected void mergeMessages(NuitonValidatorResult newMessages)
-
-