Package org.nuiton.validator.bean.list
Class BeanListValidator<O>
- java.lang.Object
-
- org.nuiton.validator.bean.AbstractValidator<O>
-
- org.nuiton.validator.bean.list.BeanListValidator<O>
-
public class BeanListValidator<O> extends AbstractValidator<O>
To validate a list of beans. Each bean of the list will be associated with aBeanListValidator.NuitonValidatorContext
.- Since:
- 2.5.2
- Author:
- Tony Chemit - chemit@codelutin.com
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BeanListValidator.NuitonValidatorContext<O>
-
Field Summary
Fields Modifier and Type Field Description protected Map<O,BeanListValidator.NuitonValidatorContext<O>>
contexts
Context for each bean registred.protected NuitonValidator<O>
delegate
The delegate validator used to validate the bean.-
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 BeanListValidator(NuitonValidatorProvider validatorProvider, Class<O> beanClass, String context)
BeanListValidator(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
addAllBeans(Collection<O> beansToAdd)
void
addBean(O bean)
Add a bean to validate.void
addBeanListValidatorListener(BeanListValidatorListener listener)
<T> T
convert(O bean, String fieldName, String value, Class<T> valueClass)
Convert a value.void
doValidate()
protected void
doValidate(O bean)
protected void
fireFieldChanged(BeanListValidatorEvent evt)
BeanListValidatorListener[]
getBeanListValidatorListeners()
Set<O>
getBeans()
BeanListValidator.NuitonValidatorContext<O>
getContext(O bean)
protected NuitonValidator<O>
getDelegate()
NuitonValidatorScope
getHighestScope(String field)
boolean
hasErrors()
boolean
hasFatalErrors()
boolean
hasInfos()
boolean
hasWarnings()
boolean
isValid(String fieldName)
protected boolean
isValid0()
protected void
mergeMessages(BeanListValidator.NuitonValidatorContext<O> context, NuitonValidatorResult newMessages)
static <O> BeanListValidator<O>
newValidator(Class<O> type, String context, NuitonValidatorScope... scopes)
Obtain a newBeanListValidator
for the given parameters.static <O> BeanListValidator<O>
newValidator(String providerName, Class<O> type, String context, NuitonValidatorScope... scopes)
Obtain a newBeanListValidator
for the given parameters.protected void
rebuildDelegateValidator(Class<O> beanType, String context, NuitonValidatorScope... scopes)
void
removeAllBeans()
Shortcut method to unregister all previously registred beans from this validator.void
removeAllBeans(Collection<O> beansToRemove)
Remove all the given beans fro this validator.void
removeBean(O bean)
Remove the given bean from the validaotr.void
removeBeanListValidatorListener(BeanListValidatorListener listener)
protected void
validate()
il faut eviter le code re-intrant (durant une validation, une autre est demandee).protected void
validate(O bean)
-
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
-
contexts
protected final Map<O,BeanListValidator.NuitonValidatorContext<O>> contexts
Context for each bean registred.- Since:
- 2.5.2
-
delegate
protected NuitonValidator<O> delegate
The delegate validator used to validate the bean.- Since:
- 2.5.2
-
-
Constructor Detail
-
BeanListValidator
public BeanListValidator(NuitonValidatorProvider validatorProvider, Class<O> beanClass, String context)
-
BeanListValidator
public BeanListValidator(NuitonValidatorProvider validatorProvider, Class<O> beanClass, String context, NuitonValidatorScope... scopes)
-
-
Method Detail
-
newValidator
public static <O> BeanListValidator<O> newValidator(Class<O> type, String context, NuitonValidatorScope... scopes) throws NullPointerException
Obtain a newBeanListValidator
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
BeanListValidator
. - Throws:
NullPointerException
- if type isnull
- See Also:
NuitonValidatorFactory.getDefaultProviderName()
-
newValidator
public static <O> BeanListValidator<O> newValidator(String providerName, Class<O> type, String context, NuitonValidatorScope... scopes) throws NullPointerException
Obtain a newBeanListValidator
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
BeanListValidator
. - Throws:
NullPointerException
- if type isnull
- See Also:
NuitonValidatorFactory.getProvider(String)
-
addBean
public void addBean(O bean)
Add a bean to validate. The bean can not be null, nor registered twice in the validator.- Parameters:
bean
- the bean to attach (can not benull
).
-
addAllBeans
public void addAllBeans(Collection<O> beansToAdd)
-
removeBean
public void removeBean(O bean)
Remove the given bean from the validaotr. The bean can not benull
, nor not has been previously registred in this validator.- Parameters:
bean
- the bean to unregister
-
removeAllBeans
public void removeAllBeans(Collection<O> beansToRemove)
Remove all the given beans fro this validator. Like in methodremoveBean(Object)
, each bean must be notnull
and has been previously registred in this validator.- Parameters:
beansToRemove
- beans to remove from this validator
-
removeAllBeans
public void removeAllBeans()
Shortcut method to unregister all previously registred beans from this validator.
-
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>
-
doValidate
public void doValidate()
- Specified by:
doValidate
in classAbstractValidator<O>
-
convert
public <T> T convert(O bean, String fieldName, String value, Class<T> valueClass)
Convert a value. If an error occurs, then add an error in validator.- Specified by:
convert
in classAbstractValidator<O>
- 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
-
addBeanListValidatorListener
public void addBeanListValidatorListener(BeanListValidatorListener listener)
-
removeBeanListValidatorListener
public void removeBeanListValidatorListener(BeanListValidatorListener listener)
-
getBeanListValidatorListeners
public BeanListValidatorListener[] getBeanListValidatorListeners()
-
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).
-
validate
protected void validate(O bean)
-
isValid0
protected boolean isValid0()
-
mergeMessages
protected void mergeMessages(BeanListValidator.NuitonValidatorContext<O> context, NuitonValidatorResult newMessages)
-
fireFieldChanged
protected void fireFieldChanged(BeanListValidatorEvent evt)
-
getContext
public BeanListValidator.NuitonValidatorContext<O> getContext(O bean)
-
-