001    package org.kuali.rice.krms.service.impl;
002    
003    import org.kuali.rice.krms.framework.engine.expression.ComparisonOperator;
004    import org.kuali.rice.krms.builder.ComponentBuilder;
005    import org.kuali.rice.krms.service.TemplateRegistry;
006    import org.kuali.rice.krms.dto.TemplateInfo;
007    
008    import java.util.HashMap;
009    import java.util.Map;
010    
011    /**
012     * @author Christoff
013     * Date: 2013/01/10
014     */
015    public class TemplateRegistryImpl implements TemplateRegistry {
016    
017        private Map<String, TemplateInfo> templatesMap;
018        private Map<String, ComponentBuilder> componentBuilderMap = new HashMap<String, ComponentBuilder>();
019    
020        public void setTemplatesMap(Map<String, TemplateInfo> templatesMap) {
021            this.templatesMap = templatesMap;
022        }
023    
024        public TemplateInfo getTemplateForType(String type) {
025            TemplateInfo template = templatesMap.get(type);
026            return (template != null ? template : null);
027        }
028    
029        public String getTermSpecNameForType(String type) {
030            TemplateInfo template = templatesMap.get(type);
031            return (template != null ? template.getTermSpecName() : null);
032        }
033    
034        public String getOperationForType(String type) {
035            TemplateInfo template = templatesMap.get(type);
036            if (template == null) {
037                return null;
038            }
039            ComparisonOperator operator = ComparisonOperator.fromCode(template.getOperator());
040            return (operator != null ? operator.getCode() : null);
041        }
042    
043        public String getValueForType(String type) {
044            TemplateInfo template = templatesMap.get(type);
045            return (template != null ? template.getValue() : null);
046        }
047    
048        public ComponentBuilder getComponentBuilderForType(String type){
049            TemplateInfo template = templatesMap.get(type);
050            if ((template == null) || (template.getComponentBuilderClass() == null)){
051                return null;
052            }
053    
054            ComponentBuilder builder = componentBuilderMap.get(template.getComponentBuilderClass());
055            if (builder == null){
056                try {
057                    builder = (ComponentBuilder) Class.forName(template.getComponentBuilderClass()).newInstance();
058                    componentBuilderMap.put(template.getComponentBuilderClass(), builder);
059                } catch (InstantiationException e) {
060                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
061                } catch (IllegalAccessException e) {
062                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
063                } catch (ClassNotFoundException e) {
064                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
065                }
066            }
067    
068            return builder;
069        }
070    
071    }