View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.datadictionary.validation.constraint.provider;
17  
18  import org.kuali.rice.krad.datadictionary.validation.capability.Constrainable;
19  import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint;
20  import org.kuali.rice.krad.datadictionary.validation.constraint.resolver.ConstraintResolver;
21  
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  /**
27   * BaseConstraintProvider implements a simple in memory storage map of constraint resolvers
28   *
29   * <p>This provides a convenient base class
30   * from which other constraint providers can be derived.</p>
31   *
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   * @since 1.1
34   */
35  public abstract class BaseConstraintProvider<T extends Constrainable> implements ConstraintProvider<T> {
36  
37      protected Map<String, ConstraintResolver<T>> resolverMap;
38  
39      /**
40       * initializes the constraints
41       *
42       * <p>By doing initialization here, and not in a constructor, constraints are only placed in memory when they are
43       * utilized.</p>
44       */
45      public void init() {
46          if (resolverMap == null) {
47              resolverMap = new HashMap<String, ConstraintResolver<T>>();
48          }
49  
50      }
51  
52      /**
53       * @see org.kuali.rice.krad.datadictionary.validation.constraint.provider.ConstraintProvider#getConstraints(org.kuali.rice.krad.datadictionary.validation.capability.Constrainable,
54       *      java.lang.Class)
55       */
56      @Override
57      public List<Constraint> getConstraints(T definition, Class<? extends Constraint> constraintType) {
58          if (resolverMap == null) {
59              init();
60          }
61  
62          ConstraintResolver<T> resolver = resolverMap.get(constraintType.getName());
63  
64          if (resolver == null) {
65              return null;
66          }
67  
68          return resolver.resolve(definition);
69      }
70  
71      /**
72       * @return the resolverMap
73       */
74      public Map<String, ConstraintResolver<T>> getResolverMap() {
75          return this.resolverMap;
76      }
77  
78      /**
79       * @param resolverMap the resolverMap to set
80       */
81      public void setResolverMap(Map<String, ConstraintResolver<T>> resolverMap) {
82          this.resolverMap = resolverMap;
83      }
84  
85  }