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 field
context.
A such validator is also a JavaBean and you can listen his states
modifications via the classic java bean api.
Note: The SimpleBeanValidator should 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 java.lang.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, java.lang.Class<O> beanClass, java.lang.String context)SimpleBeanValidator(NuitonValidatorProvider validatorProvider, java.lang.Class<O> beanClass, java.lang.String context, NuitonValidatorScope... scopes) -
Method Summary
Modifier and Type Method Description voidaddSimpleBeanValidatorListener(SimpleBeanValidatorListener listener)<T> Tconvert(java.lang.String fieldName, java.lang.String value, java.lang.Class<T> valueClass)Convert a value.<T> Tconvert(O bean, java.lang.String fieldName, java.lang.String value, java.lang.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(java.lang.String field)SimpleBeanValidator<?>getParentValidator()SimpleBeanValidatorListener[]getSimpleBeanValidatorListeners()booleanhasErrors()booleanhasFatalErrors()booleanhasInfos()booleanhasWarnings()booleanisValid(java.lang.String fieldName)protected voidmergeMessages(NuitonValidatorResult newMessages)static <O> SimpleBeanValidator<O>newValidator(java.lang.Class<O> type, java.lang.String context, NuitonValidatorScope... scopes)Obtain a newSimpleBeanValidatorfor the given parameters.static <O> SimpleBeanValidator<O>newValidator(java.lang.String providerName, java.lang.Class<O> type, java.lang.String context, NuitonValidatorScope... scopes)Obtain a newSimpleBeanValidatorfor the given parameters.protected voidrebuildDelegateValidator(java.lang.Class<O> beanType, java.lang.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 Details
-
BEAN_PROPERTY
public static final java.lang.String BEAN_PROPERTYName of the bounded propertybean.- See Also:
getBean(),setBean(Object), Constant Field Values
-
context
Context of the registred bean to validate.- Since:
- 2.5.2
-
parentValidator
To chain to another validator (acting as parent of this one).- Since:
- 2.5.2
-
-
Constructor Details
-
SimpleBeanValidator
public SimpleBeanValidator(NuitonValidatorProvider validatorProvider, java.lang.Class<O> beanClass, java.lang.String context) -
SimpleBeanValidator
public SimpleBeanValidator(NuitonValidatorProvider validatorProvider, java.lang.Class<O> beanClass, java.lang.String context, NuitonValidatorScope... scopes)
-
-
Method Details
-
newValidator
public static <O> SimpleBeanValidator<O> newValidator(java.lang.Class<O> type, java.lang.String context, NuitonValidatorScope... scopes) throws java.lang.NullPointerExceptionObtain 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:
java.lang.NullPointerException- if type isnull- See Also:
NuitonValidatorFactory.getDefaultProviderName()
-
newValidator
public static <O> SimpleBeanValidator<O> newValidator(java.lang.String providerName, java.lang.Class<O> type, java.lang.String context, NuitonValidatorScope... scopes) throws java.lang.NullPointerExceptionObtain 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:
java.lang.NullPointerException- if type isnull- See Also:
NuitonValidatorFactory.getProvider(String)
-
getBean
Obtain the actual bean attached to the validator.- Returns:
- the bean attached to the validor or
nullif no bean is attached
-
setBean
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
-
setParentValidator
-
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(java.lang.String fieldName)- Specified by:
isValidin classAbstractValidator<O>
-
getHighestScope
- Specified by:
getHighestScopein classAbstractValidator<O>
-
convert
public <T> T convert(O bean, java.lang.String fieldName, java.lang.String value, java.lang.Class<T> valueClass)- Specified by:
convertin classAbstractValidator<O>
-
doValidate
public void doValidate()- Specified by:
doValidatein classAbstractValidator<O>
-
convert
public <T> T convert(java.lang.String fieldName, java.lang.String value, java.lang.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
-
removeSimpleBeanValidatorListener
-
getSimpleBeanValidatorListeners
-
doValidate
- Specified by:
doValidatein classAbstractValidator<O>
-
getDelegate
- Specified by:
getDelegatein classAbstractValidator<O>
-
rebuildDelegateValidator
protected void rebuildDelegateValidator(java.lang.Class<O> beanType, java.lang.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
-
mergeMessages
-