View Javadoc

1   /**
2    * Copyright 2005-2013 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.krms.service.impl;
17  
18  import org.kuali.rice.krms.framework.engine.expression.ComparisonOperator;
19  import org.kuali.rice.krms.builder.ComponentBuilder;
20  import org.kuali.rice.krms.service.TemplateRegistry;
21  import org.kuali.rice.krms.dto.TemplateInfo;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  /**
27   * Provides a hashmap implementation that contains rule statement (proposition) types to
28   * template information relationships.
29   *
30   * @author Kuali Student Team
31   */
32  public class TemplateRegistryImpl implements TemplateRegistry {
33  
34      private Map<String, TemplateInfo> templatesMap;
35      private Map<String, ComponentBuilder> componentBuilderMap = new HashMap<String, ComponentBuilder>();
36  
37      public void setTemplatesMap(Map<String, TemplateInfo> templatesMap) {
38          this.templatesMap = templatesMap;
39      }
40  
41      public TemplateInfo getTemplateForType(String type) {
42          TemplateInfo template = templatesMap.get(type);
43          return (template != null ? template : null);
44      }
45  
46      public String getTermSpecNameForType(String type) {
47          TemplateInfo template = templatesMap.get(type);
48          return (template != null ? template.getTermSpecName() : null);
49      }
50  
51      public String getOperationForType(String type) {
52          TemplateInfo template = templatesMap.get(type);
53          if (template == null) {
54              return null;
55          }
56          ComparisonOperator operator = ComparisonOperator.fromCode(template.getOperator());
57          return (operator != null ? operator.getCode() : null);
58      }
59  
60      public String getValueForType(String type) {
61          TemplateInfo template = templatesMap.get(type);
62          return (template != null ? template.getValue() : null);
63      }
64  
65      public ComponentBuilder getComponentBuilderForType(String type){
66          TemplateInfo template = templatesMap.get(type);
67          if ((template == null) || (template.getComponentBuilderClass() == null)){
68              return null;
69          }
70  
71          ComponentBuilder builder = componentBuilderMap.get(template.getComponentBuilderClass());
72          if (builder == null){
73              try {
74                  builder = (ComponentBuilder) Class.forName(template.getComponentBuilderClass()).newInstance();
75                  componentBuilderMap.put(template.getComponentBuilderClass(), builder);
76              } catch (Exception e) {
77                  throw new RuntimeException("Unable to locate component builder for class " + template.getComponentBuilderClass());
78              }
79          }
80  
81          return builder;
82      }
83  
84  }