001/**
002 * Copyright 2005-2014 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 */
016package org.kuali.rice.krad.datadictionary.validation.constraint.provider;
017
018import org.kuali.rice.krad.datadictionary.validation.capability.Constrainable;
019import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint;
020import org.kuali.rice.krad.datadictionary.validation.constraint.resolver.ConstraintResolver;
021
022import java.util.HashMap;
023import java.util.List;
024import 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 * @param <T> constrainable type
034 * @since 1.1
035 */
036public abstract class BaseConstraintProvider<T extends Constrainable> implements ConstraintProvider<T> {
037
038    protected Map<String, ConstraintResolver<T>> resolverMap;
039
040    /**
041     * initializes the constraints
042     *
043     * <p>By doing initialization here, and not in a constructor, constraints are only placed in memory when they are
044     * utilized.</p>
045     */
046    public void init() {
047        if (resolverMap == null) {
048            resolverMap = new HashMap<String, ConstraintResolver<T>>();
049        }
050
051    }
052
053    /**
054     * @see org.kuali.rice.krad.datadictionary.validation.constraint.provider.ConstraintProvider#getConstraints(org.kuali.rice.krad.datadictionary.validation.capability.Constrainable,
055     *      java.lang.Class)
056     */
057    @Override
058    public List<Constraint> getConstraints(T definition, Class<? extends Constraint> constraintType) {
059        if (resolverMap == null) {
060            init();
061        }
062
063        ConstraintResolver<T> resolver = resolverMap.get(constraintType.getName());
064
065        if (resolver == null) {
066            return null;
067        }
068
069        return resolver.resolve(definition);
070    }
071
072    /**
073     * @return the resolverMap
074     */
075    public Map<String, ConstraintResolver<T>> getResolverMap() {
076        return this.resolverMap;
077    }
078
079    /**
080     * @param resolverMap the resolverMap to set
081     */
082    public void setResolverMap(Map<String, ConstraintResolver<T>> resolverMap) {
083        this.resolverMap = resolverMap;
084    }
085
086}