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 */
016package org.kuali.rice.krms.service.impl;
017
018import org.kuali.rice.krms.framework.engine.expression.ComparisonOperator;
019import org.kuali.rice.krms.builder.ComponentBuilder;
020import org.kuali.rice.krms.service.TemplateRegistry;
021import org.kuali.rice.krms.dto.TemplateInfo;
022
023import java.util.HashMap;
024import java.util.Map;
025
026/**
027 * Provides a hashmap implementation that contains rule statement (proposition) types to
028 * template information relationships.
029 *
030 * @author Kuali Student Team
031 */
032public class TemplateRegistryImpl implements TemplateRegistry {
033
034    private Map<String, TemplateInfo> templatesMap;
035    private Map<String, ComponentBuilder> componentBuilderMap = new HashMap<String, ComponentBuilder>();
036
037    public void setTemplatesMap(Map<String, TemplateInfo> templatesMap) {
038        this.templatesMap = templatesMap;
039    }
040
041    public TemplateInfo getTemplateForType(String type) {
042        TemplateInfo template = templatesMap.get(type);
043        return (template != null ? template : null);
044    }
045
046    public String getTermSpecNameForType(String type) {
047        TemplateInfo template = templatesMap.get(type);
048        return (template != null ? template.getTermSpecName() : null);
049    }
050
051    public String getOperationForType(String type) {
052        TemplateInfo template = templatesMap.get(type);
053        if (template == null) {
054            return null;
055        }
056        ComparisonOperator operator = ComparisonOperator.fromCode(template.getOperator());
057        return (operator != null ? operator.getCode() : null);
058    }
059
060    public String getValueForType(String type) {
061        TemplateInfo template = templatesMap.get(type);
062        return (template != null ? template.getValue() : null);
063    }
064
065    public ComponentBuilder getComponentBuilderForType(String type){
066        TemplateInfo template = templatesMap.get(type);
067        if ((template == null) || (template.getComponentBuilderClass() == null)){
068            return null;
069        }
070
071        ComponentBuilder builder = componentBuilderMap.get(template.getComponentBuilderClass());
072        if (builder == null){
073            try {
074                builder = (ComponentBuilder) Class.forName(template.getComponentBuilderClass()).newInstance();
075                componentBuilderMap.put(template.getComponentBuilderClass(), builder);
076            } catch (Exception e) {
077                throw new RuntimeException("Unable to locate component builder for class " + template.getComponentBuilderClass());
078            }
079        }
080
081        return builder;
082    }
083
084}