org.springjutsu.validation
Class ValidationManager

java.lang.Object
  extended by org.springframework.validation.beanvalidation.SpringValidatorAdapter
      extended by org.springframework.validation.beanvalidation.CustomValidatorBean
          extended by org.springjutsu.validation.ValidationManager
All Implemented Interfaces:
javax.validation.Validator, org.springframework.beans.factory.InitializingBean, org.springframework.validation.SmartValidator

public class ValidationManager
extends org.springframework.validation.beanvalidation.CustomValidatorBean

Registerable as a JSR-303 @link{CustomValidatorBean}, this ValidationManager class is instead responsible for reading XML-driven nested validation rules. However, it populates a standard Errors object as expected. Logic is divided into two main portions: First context rules are read from the <context-rules> defined for a given path. These rules are loaded based on the current path the user is accessing, with implementation handled by subclasses. Context rules are typically those rules which are specific to a given form: the fields which are required, and also conditional validation logic based on other fields or variables defined in EL or otherwise. These are the conditional per-form validation rules not handled by JSR-303 Second, model rules are read from the <model-rules> defined for a given class. These rules are loaded directly from the per-class definitions provided in the XML rules. Model rules are those rules which do not change for a given class and include type checking, length checks, and so on. These are the more typical JSR-303 type rules.

Author:
Clark Duplichien, Taylor Wicksell
See Also:
CustomValidatorBean

Field Summary
protected static org.apache.commons.logging.Log log
          Validate a lot of requests.
protected  org.springframework.context.MessageSource messageSource
          We'll load error message definitions from the spring message source.
protected  RuleExecutorContainer ruleExecutorContainer
          Holds the implementations of the validation rule executors.
protected  ValidationRulesContainer rulesContainer
          Holds the validation rules which have been parsed from the XML rule sets.
 
Constructor Summary
ValidationManager()
           
 
Method Summary
protected  String appendPath(String path, String suffix)
          Appends two subpath segments together and handles period replacement appropriately.
protected  String buildMessageKey(Class<?> parentType, String fieldPath)
           
protected  void callRules(Object model, org.springframework.validation.Errors errors, List<ValidationRule> modelRules, boolean prelocalized)
          Responsible for delegating each actual model rule to the appropriate @link{RuleExecutor}.
protected  Object getContextArgument(Object model, String expression)
          Responsible for determining the argument to be passed to the rule.
protected  Object getContextModel(Object model, String expression)
          Responsible for discovering the path-described model which is to be validated by the current rule.
 boolean getEnableSuperclassFieldLabelLookup()
           
 String getErrorMessagePrefix()
           
 String getFieldLabelPrefix()
           
protected  String getMessageResolver(Object model, String rulePath, boolean resolveAsModel)
          This method is responsible for getting the the String used to resolve the message that should be recorded as the error message.
protected  String getModelMessageKey(String rawRulePath, Object rootModel)
          If we're trying to resolve the message key for a path on the model, this method will unwrap that message key.
protected  String getMVCFormName()
          Just cleans up a Servlet path URL for rule resolving by the rules container.
protected  String getWebflowFormName()
          Gets a identifier of the current state that needs validating in order to determine what rules to load from the validation definition.
protected  boolean hasEL(String expression)
           
protected  List<Object> inheritedCheckedModels(List<Object> checkedModels)
           
protected  void logError(ValidationRule rule, Object rootModel, org.springframework.validation.Errors errors)
          In the event that a validation rule fails, this method is responsible for recording an error message on the affected path of the Errors object.
protected  boolean passes(ValidationRule rule, Object rootModel)
          Determines if the validation rule passes by calling the rule executor.
protected  Object resolveSPEL(String elContaining, Object model)
          Responsible for resolving a SPEL expression.
 void setEnableSuperclassFieldLabelLookup(boolean enableSuperclassFieldLabelLookup)
           
 void setErrorMessagePrefix(String errorMessagePrefix)
           
 void setFieldLabelPrefix(String fieldLabelPrefix)
           
 boolean supports(Class<?> clazz)
          We delegate to rule container executor, in order to see if rules have been mapped for this class.
 org.springframework.validation.Errors validate(Object target)
          Hook point to perform validation without a web request.
 void validate(Object model, org.springframework.validation.Errors errors)
          We perform actual validation in the order of context rules followed by model rules.
 void validate(Object target, org.springframework.validation.Errors errors, Object... validationHints)
           
protected  void validateModel(Object model, org.springframework.validation.Errors errors, List<Object> checkedModels, String currentForm)
          Responsible for testing all XML-defined per-class rules.
 
Methods inherited from class org.springframework.validation.beanvalidation.CustomValidatorBean
afterPropertiesSet, setMessageInterpolator, setTraversableResolver, setValidatorFactory
 
Methods inherited from class org.springframework.validation.beanvalidation.SpringValidatorAdapter
getArgumentsForConstraint, getConstraintsForClass, processConstraintViolations, unwrap, validate, validateProperty, validateValue
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.validation.Validator
getConstraintsForClass, unwrap, validate, validateProperty, validateValue
 

Field Detail

log

protected static org.apache.commons.logging.Log log
Validate a lot of requests. A log is fine, too.


rulesContainer

@Autowired
protected ValidationRulesContainer rulesContainer
Holds the validation rules which have been parsed from the XML rule sets.

See Also:
ValidationRulesContainer, ValidationRule

ruleExecutorContainer

@Autowired
protected RuleExecutorContainer ruleExecutorContainer
Holds the implementations of the validation rule executors.

See Also:
RuleExecutorContainer, RuleExecutor

messageSource

@Autowired
protected org.springframework.context.MessageSource messageSource
We'll load error message definitions from the spring message source.

See Also:
MessageSource
Constructor Detail

ValidationManager

public ValidationManager()
Method Detail

supports

public boolean supports(Class<?> clazz)
We delegate to rule container executor, in order to see if rules have been mapped for this class. If none have, then we don't support it.

Overrides:
supports in class org.springframework.validation.beanvalidation.SpringValidatorAdapter
See Also:
#RuleExecutorContainer.supportsClass(Class), #javax.validation.Validator.supports(Class)

validate

public org.springframework.validation.Errors validate(Object target)
Hook point to perform validation without a web request. Create and return a BindingModel to allow users to manage errors.


validate

public void validate(Object model,
                     org.springframework.validation.Errors errors)
We perform actual validation in the order of context rules followed by model rules.

Overrides:
validate in class org.springframework.validation.beanvalidation.SpringValidatorAdapter

validate

public void validate(Object target,
                     org.springframework.validation.Errors errors,
                     Object... validationHints)
Specified by:
validate in interface org.springframework.validation.SmartValidator
Overrides:
validate in class org.springframework.validation.beanvalidation.SpringValidatorAdapter

validateModel

protected void validateModel(Object model,
                             org.springframework.validation.Errors errors,
                             List<Object> checkedModels,
                             String currentForm)
Responsible for testing all XML-defined per-class rules. We will check recursively: using a BeanWrapper to get a

Parameters:
model - the model object to validate. May be a recursed sub bean.
errors - standard Errors object to record validation errors to.
checkedModels - A list of model objects we have already validated, in order to prevent unneeded or infinite recursion

callRules

protected void callRules(Object model,
                         org.springframework.validation.Errors errors,
                         List<ValidationRule> modelRules,
                         boolean prelocalized)
Responsible for delegating each actual model rule to the appropriate @link{RuleExecutor}. Errors are recorded if no previous error has been recorded for the given path.

Parameters:
model - The object being validated
errors - Standard errors object to record validation errors.
modelRules - A list of ValidationRules parsed from
prelocalized - indicates if child rules are already localized to the base path of the errors object. the <model-rules> section of the validation XML.

getMVCFormName

protected String getMVCFormName()
Just cleans up a Servlet path URL for rule resolving by the rules container. Restful URL paths may be used, with \{variable} path support. As of 0.6.1, ant paths like * and ** may also be used.


getWebflowFormName

protected String getWebflowFormName()
Gets a identifier of the current state that needs validating in order to determine what rules to load from the validation definition. For webflow, this is the flow ID appended with a colon, and then the state id. For example /accounts/account-creation:basicInformation

Returns:
the context rules associated with this identifier.

passes

protected boolean passes(ValidationRule rule,
                         Object rootModel)
Determines if the validation rule passes by calling the rule executor. Delegates to extract the model and arguments from the sub path defined on the XML rule.

Parameters:
rule - The validation rule to run
rootModel - The model to run the rule on.
Returns:

getContextModel

protected Object getContextModel(Object model,
                                 String expression)
Responsible for discovering the path-described model which is to be validated by the current rule. This path may contain EL, and if it does, we delegate to @link(#resolveEL(String, Object)) to resolve that EL.

Parameters:
model - Object to be validated
expression - The string path expression for the model.
Returns:
the Object to validate.

getContextArgument

protected Object getContextArgument(Object model,
                                    String expression)
Responsible for determining the argument to be passed to the rule. If the argument expression string contains EL, it will be resolved, otherwise, the expression string is taken as a literal argument.

Parameters:
model - Object to be validated
expression - The string path expression for the model.
Returns:
the Object to serve as a rule argument

logError

protected void logError(ValidationRule rule,
                        Object rootModel,
                        org.springframework.validation.Errors errors)
In the event that a validation rule fails, this method is responsible for recording an error message on the affected path of the Errors object. The error message is gathered in three parts: First the base message, if not provided is based on the rule executor class. This is a message like "\{0} should be longer than \{1} chars." Next, the first argument \{0} is the model descriptor. This will resolve to a label for the path that failed, based on second to last path subBean and the field that failed. So, if "account.accountOwner.username" had an error, it would look for a message based on the class name of accountOwner, and the field username: like "user.username". If the message files contained a "user.username=User name", then the message would now read something like "User name should be longer than \{1} chars." Finally, the argument is resolved. If the argument is just a flat string, like "16", then you would get "User name should be longer than 16 chars." If the argument contained EL that resolved on the model, it would perform the same model lookup detailed above, so you could potentially have something like "User name should be longer than First name", which is a bit weird, but has its uses. For either the model or argument lookup, if EL is used in the path which resolves off the model, the literal value of the evaluated EL expression is used.

Parameters:
rule - the rule which failed
rootModel - the root model (not failed bean)
errors - standard Errors object to record error on.

getMessageResolver

protected String getMessageResolver(Object model,
                                    String rulePath,
                                    boolean resolveAsModel)
This method is responsible for getting the the String used to resolve the message that should be recorded as the error message. This proceeds as described in the logError message: If EL is utilized, and the EL path resolves on the bean, use a string like owningClassName.fieldName to resolve a message. If EL is utilized, and the EL path does not resolve on the bean, use the literal value of the evaluated EL. IF EL is not utilized, and we're evaluating for the model, use the model field path like owningClassName.fieldName to resolve message. If EL is not utilized, and we're evaluating for the argument, use the literal string that's passed in as the argument.

Parameters:
model - The root model on which the path describes the error location.
rulePath - The path which was given to the rule. The path that was validated using the rule and failed.
resolveAsModel - if true, use the behavior to resolve the model. Otherwise, use the behavior to resolve the argument.
Returns:
A string used to look up the message to resolve as the model or argument of a failed validation rule, as determined by resolveAsModel.

getModelMessageKey

protected String getModelMessageKey(String rawRulePath,
                                    Object rootModel)
If we're trying to resolve the message key for a path on the model, this method will unwrap that message key. For instance, consider our model is a Account instance, which has a field accountOwner of type User, and that User object has a username field of type String: If rulePath was "accountOwner.username", then it would return a message key of "user.username", which is the simple classname of the owning object of the failed validation path, and the field name. This is so we can display the label of the field that failed validation in the error message. For instance "User Name must be 8 chars" instead of something cryptic like "accountOwner.username must be 8 chars".

Parameters:
rulePath - Validation rule path to the failed field.
rootModel - The root model owning the field that failed.
Returns:
A message key used to resolve a message describing the field that failed.

buildMessageKey

protected String buildMessageKey(Class<?> parentType,
                                 String fieldPath)

hasEL

protected boolean hasEL(String expression)
Parameters:
expression - A string expression
Returns:
returns true if the expression string is EL.

resolveSPEL

protected Object resolveSPEL(String elContaining,
                             Object model)
Responsible for resolving a SPEL expression. Unwraps the EL string, creates an instance of a

Parameters:
el - The EL expression to resolve.
model - The model on which the EL-described field MAY lie.
Returns:
The object described by the EL expression.

appendPath

protected String appendPath(String path,
                            String suffix)
Appends two subpath segments together and handles period replacement appropriately.

Parameters:
path - A string path.
suffix - A string path to add to the prior path.
Returns:
A combined path.

getErrorMessagePrefix

public String getErrorMessagePrefix()

setErrorMessagePrefix

public void setErrorMessagePrefix(String errorMessagePrefix)

getFieldLabelPrefix

public String getFieldLabelPrefix()

setFieldLabelPrefix

public void setFieldLabelPrefix(String fieldLabelPrefix)

getEnableSuperclassFieldLabelLookup

public boolean getEnableSuperclassFieldLabelLookup()

setEnableSuperclassFieldLabelLookup

public void setEnableSuperclassFieldLabelLookup(boolean enableSuperclassFieldLabelLookup)

inheritedCheckedModels

protected List<Object> inheritedCheckedModels(List<Object> checkedModels)


Copyright © 2013 Spring Jutsu. All Rights Reserved.