View Javadoc

1   package org.mod4j.runtime.domain;
2   
3   import java.net.BindException;
4   import java.util.LinkedList;
5   import java.util.List;
6   
7   import org.mod4j.runtime.exception.BusinessRuleException;
8   import org.mod4j.runtime.validation.BusinessRuleValidationTemplate;
9   import org.springframework.validation.Validator;
10  
11  /**
12   * The first reason to introduce this class is to provide a generic {@link #validate} method for all domain objects.
13   * 
14   * @author Eric Jan Malotaux
15   * 
16   */
17  public abstract class AbstractDomainObject {
18      protected List<Validator> validators = new LinkedList<Validator>();
19  
20      protected final BusinessRuleValidationTemplate validationTemplate = new BusinessRuleValidationTemplate(this);
21  
22      /**
23       * Add the given <code>validator</code> to the collection of validators that will be executed on this object when
24       * the {@link #validate()} method is called. The validators will be invoked in the order they were added.
25       * 
26       * @param validator -
27       *            the validator being added.
28       */
29      public void addValidator(Validator validator) {
30          if (!validators.contains(validator)) {
31              validators.add(validator);
32          }
33      }
34  
35      /**
36       * Validate this object.
37       * 
38       * Validation is performed by calling every {@link Validator} added by the {@link #addValidator(Validator)} method,
39       * in the order they were added.
40       * 
41       * @throws BusinessRuleException -
42       *             thrown when one or more errors are found. The exception thrown has as its cause a
43       *             {@link BindException} containing information on all errors found.
44       */
45      protected void validate() throws BusinessRuleException {
46          validationTemplate.invokeValidators(validators);
47      }
48  
49  }