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: TheSimpleBeanValidator
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 class
SimpleBeanValidator.NuitonValidatorContext<O>
-
Field Summary
Fields Modifier and Type Field Description static String
BEAN_PROPERTY
Name of the bounded propertybean
.protected SimpleBeanValidator.NuitonValidatorContext<O>
context
Context of the registred bean to validate.protected SimpleBeanValidator<?>
parentValidator
To 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 void
addSimpleBeanValidatorListener(SimpleBeanValidatorListener listener)
<T> T
convert(String fieldName, String value, Class<T> valueClass)
Convert a value.<T> T
convert(O bean, String fieldName, String value, Class<T> valueClass)
void
doValidate()
protected void
doValidate(O bean)
protected void
fireFieldChanged(SimpleBeanValidatorEvent evt)
O
getBean()
Obtain the actual bean attached to the validator.protected NuitonValidator<O>
getDelegate()
NuitonValidatorScope
getHighestScope(String field)
SimpleBeanValidator<?>
getParentValidator()
SimpleBeanValidatorListener[]
getSimpleBeanValidatorListeners()
boolean
hasErrors()
boolean
hasFatalErrors()
boolean
hasInfos()
boolean
hasWarnings()
boolean
isValid(String fieldName)
protected void
mergeMessages(NuitonValidatorResult newMessages)
static <O> SimpleBeanValidator<O>
newValidator(Class<O> type, String context, NuitonValidatorScope... scopes)
Obtain a newSimpleBeanValidator
for the given parameters.static <O> SimpleBeanValidator<O>
newValidator(String providerName, Class<O> type, String context, NuitonValidatorScope... scopes)
Obtain a newSimpleBeanValidator
for the given parameters.protected void
rebuildDelegateValidator(Class<O> beanType, String context, NuitonValidatorScope... scopes)
void
removeSimpleBeanValidatorListener(SimpleBeanValidatorListener listener)
void
setBean(O bean)
Change the attached bean.void
setParentValidator(SimpleBeanValidator<?> parentValidator)
protected void
validate()
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 newSimpleBeanValidator
for 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 newSimpleBeanValidator
for the given parameters. Note: It will use the provider ofNuitonValidator
defined by theproviderName
.- Type Parameters:
O
- type of bean to validate- Parameters:
providerName
- name ofNuitonValidator
to 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
null
if no bean is attached
-
setBean
public void setBean(O bean)
Change the attached bean. As a side effect, the internalAbstractNuitonValidatorContext.messages
will be reset.- Parameters:
bean
- the bean to attach (can benull
to reset the validator).
-
getParentValidator
public SimpleBeanValidator<?> getParentValidator()
-
setParentValidator
public void setParentValidator(SimpleBeanValidator<?> parentValidator)
-
hasFatalErrors
public boolean hasFatalErrors()
- Specified by:
hasFatalErrors
in classAbstractValidator<O>
-
hasErrors
public boolean hasErrors()
- Specified by:
hasErrors
in classAbstractValidator<O>
-
hasWarnings
public boolean hasWarnings()
- Specified by:
hasWarnings
in classAbstractValidator<O>
-
hasInfos
public boolean hasInfos()
- Specified by:
hasInfos
in classAbstractValidator<O>
-
isValid
public boolean isValid(String fieldName)
- Specified by:
isValid
in classAbstractValidator<O>
-
getHighestScope
public NuitonValidatorScope getHighestScope(String field)
- Specified by:
getHighestScope
in classAbstractValidator<O>
-
convert
public <T> T convert(O bean, String fieldName, String value, Class<T> valueClass)
- Specified by:
convert
in classAbstractValidator<O>
-
doValidate
public void doValidate()
- Specified by:
doValidate
in 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:
doValidate
in classAbstractValidator<O>
-
getDelegate
protected NuitonValidator<O> getDelegate()
- Specified by:
getDelegate
in classAbstractValidator<O>
-
rebuildDelegateValidator
protected void rebuildDelegateValidator(Class<O> beanType, String context, NuitonValidatorScope... scopes)
- Specified by:
rebuildDelegateValidator
in 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 lesPropertyChangeListener
Note: 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)
-
-