View Javadoc

1   /**
2    * Copyright 2005-2012 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   * A class that implements a simple in memory storage map of constraint resolvers. This provides a convenient base class
28   * from which other constraint providers can be derived. 
29   * 
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   * @since 1.1
32   */
33  public abstract class BaseConstraintProvider<T extends Constrainable> implements ConstraintProvider<T> {
34  	
35  	
36  	protected Map<String, ConstraintResolver<T>> resolverMap;
37  	
38  	public void init() {
39  		if (resolverMap == null)
40  			resolverMap = new HashMap<String, ConstraintResolver<T>>();
41  
42  	}
43  	
44  	/**
45  	 * @see org.kuali.rice.krad.datadictionary.validation.constraint.provider.ConstraintProvider#getConstraints(org.kuali.rice.krad.datadictionary.validation.capability.Constrainable, java.lang.Class)
46  	 */
47  	@Override
48  	public List<Constraint> getConstraints(T definition, Class<? extends Constraint> constraintType) {
49  		if (resolverMap == null)
50  			init();
51  		
52  		ConstraintResolver<T> resolver = resolverMap.get(constraintType.getName());
53  
54  		if (resolver == null)
55  			return null;
56  		
57  		return resolver.resolve(definition);
58  	}
59  
60  	/**
61  	 * @return the resolverMap
62  	 */
63  	public Map<String, ConstraintResolver<T>> getResolverMap() {
64  		return this.resolverMap;
65  	}
66  
67  	/**
68  	 * @param resolverMap the resolverMap to set
69  	 */
70  	public void setResolverMap(Map<String, ConstraintResolver<T>> resolverMap) {
71  		this.resolverMap = resolverMap;
72  	}
73  	
74  }