001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krad.datadictionary.validation.constraint.provider;
017    
018    import org.kuali.rice.krad.datadictionary.validation.capability.Constrainable;
019    import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint;
020    import org.kuali.rice.krad.datadictionary.validation.constraint.resolver.ConstraintResolver;
021    
022    import java.util.HashMap;
023    import java.util.List;
024    import java.util.Map;
025    
026    /**
027     * BaseConstraintProvider implements a simple in memory storage map of constraint resolvers
028     *
029     * <p>This provides a convenient base class
030     * from which other constraint providers can be derived.</p>
031     *
032     * @author Kuali Rice Team (rice.collab@kuali.org)
033     * @since 1.1
034     */
035    public abstract class BaseConstraintProvider<T extends Constrainable> implements ConstraintProvider<T> {
036    
037        protected Map<String, ConstraintResolver<T>> resolverMap;
038    
039        /**
040         * initializes the constraints
041         *
042         * <p>By doing initialization here, and not in a constructor, constraints are only placed in memory when they are
043         * utilized.</p>
044         */
045        public void init() {
046            if (resolverMap == null) {
047                resolverMap = new HashMap<String, ConstraintResolver<T>>();
048            }
049    
050        }
051    
052        /**
053         * @see org.kuali.rice.krad.datadictionary.validation.constraint.provider.ConstraintProvider#getConstraints(org.kuali.rice.krad.datadictionary.validation.capability.Constrainable,
054         *      java.lang.Class)
055         */
056        @Override
057        public List<Constraint> getConstraints(T definition, Class<? extends Constraint> constraintType) {
058            if (resolverMap == null) {
059                init();
060            }
061    
062            ConstraintResolver<T> resolver = resolverMap.get(constraintType.getName());
063    
064            if (resolver == null) {
065                return null;
066            }
067    
068            return resolver.resolve(definition);
069        }
070    
071        /**
072         * @return the resolverMap
073         */
074        public Map<String, ConstraintResolver<T>> getResolverMap() {
075            return this.resolverMap;
076        }
077    
078        /**
079         * @param resolverMap the resolverMap to set
080         */
081        public void setResolverMap(Map<String, ConstraintResolver<T>> resolverMap) {
082            this.resolverMap = resolverMap;
083        }
084    
085    }