View Javadoc

1   package org.mod4j.runtime.validation;
2   
3   import java.util.List;
4   
5   import org.mod4j.runtime.exception.BusinessRuleException;
6   import org.springframework.util.ClassUtils;
7   import org.springframework.validation.BindException;
8   import org.springframework.validation.Errors;
9   import org.springframework.validation.ValidationUtils;
10  import org.springframework.validation.Validator;
11  
12  /**
13   * Template class that simplifies business rule validation. The central method is {@link #execute}, supporting
14   * validations implementing the {@link BusinessRuleValidationCallback} interface. It provides for the creation of an
15   * {@link Errors} object for storing all errors found while validating fields of a particular object in the
16   * {@link BusinessRuleValidationCallback#doValidate(Object, Errors)} method. After all validations are executed and the
17   * results collected in the {@link Errors} object and if one or more errors are found, a {@link BusinessRuleException}
18   * is thrown that wraps the the {@link BindException} object that implements the {@link Errors} object is thrown as an
19   * exception.
20   * 
21   * @author Eric Jan Malotaux
22   */
23  public class BusinessRuleValidationTemplate {
24      private Object object;
25  
26      /**
27       * Creates a new BusinessRuleValidationTemplate instance. Field validations are performed on named fields of the
28       * given <code>object</code>.
29       * 
30       * @param object
31       */
32      public BusinessRuleValidationTemplate(Object object) {
33          this.object = object;
34      }
35  
36      /**
37       * Invoke the given <code>validator</code> on the current object.
38       * 
39       * @param validator
40       * @throws BusinessRuleException
41       *             when the validator finds an error.
42       */
43      public void invokeValidator(final Validator validator) {
44          execute(new BusinessRuleValidationCallback() {
45              public void doValidate(Object object, Errors errors) {
46                  ValidationUtils.invokeValidator(validator, object, errors);
47              }
48          });
49      }
50  
51      /**
52       * Invoke the given <code>validators</code> on the current object.
53       * 
54       * @param validators -
55       *            the validators to invoke.
56       * @throws BusinessRuleException -
57       *             thrown when one of the validators finds an error.
58       */
59      public void invokeValidators(final List<Validator> validators) {
60          execute(new BusinessRuleValidationCallback() {
61              public void doValidate(Object object, Errors errors) {
62                  for (Validator validator : validators) {
63                      ValidationUtils.invokeValidator(validator, object, errors);
64                  }
65              }
66          });
67      }
68  
69      /**
70       * Execute the {@link BusinessRuleValidationCallback#doValidate(Object, Errors)} method of the given
71       * <code>action</code> on the current object.
72       * 
73       * @param action -
74       *            the {@link BusinessRuleValidationCallback} to invoke its
75       *            {@link BusinessRuleValidationCallback#doValidate(Object, Errors)} method on.
76       * @throws BusinessRuleException -
77       *             thrown when the {@link BusinessRuleValidationCallback#doValidate(Object, Errors)} method added one
78       *             ore more errors to its <code>errors</code> object.
79       */
80      public void execute(BusinessRuleValidationCallback action) {
81          BindException errors = new BindException(object, ClassUtils.getShortNameAsProperty(object.getClass()));
82          action.doValidate(object, errors);
83          if (errors.hasErrors()) {
84              throw new BusinessRuleException(errors);
85          }
86      }
87  }