View Javadoc
1   /**
2    * Copyright 2005-2014 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.kew.rule;
17  
18  import org.kuali.rice.kew.api.extension.ExtensionDefinition;
19  import org.kuali.rice.kew.api.rule.RoleName;
20  import org.kuali.rice.kew.api.rule.RuleTemplateAttribute;
21  import org.kuali.rice.kew.framework.KewFrameworkServiceLocator;
22  import org.kuali.rice.kew.framework.rule.attribute.WorkflowRuleAttributeFields;
23  import org.kuali.rice.kew.framework.rule.attribute.WorkflowRuleAttributeHandlerService;
24  import org.kuali.rice.kew.rule.bo.RuleAttribute;
25  import org.kuali.rice.kew.rule.bo.RuleTemplateAttributeBo;
26  
27  import java.util.Collections;
28  import java.util.List;
29  import java.util.Map;
30  
31  /**
32   * Reference implementation of {@code WorkflowRuleAttributeMediator}.
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  public class WorkflowRuleAttributeMediatorImpl implements WorkflowRuleAttributeMediator {
37  
38      @Override
39      public WorkflowRuleAttributeRows getRuleRows(Map<String, String> parameters, RuleTemplateAttributeBo ruleTemplateAttribute) {
40          required(ruleTemplateAttribute, "ruleTemplateAttribute");
41          if (parameters == null) {
42              parameters = Collections.emptyMap();
43          }
44          RuleAttribute ruleAttribute = ruleTemplateAttribute.getRuleAttribute();
45          ExtensionDefinition extensionDefinition = RuleAttribute.to(ruleAttribute);
46          WorkflowRuleAttributeHandlerService handler = getHandler(extensionDefinition);
47          WorkflowRuleAttributeFields fields = handler.getRuleFields(parameters, extensionDefinition, ruleTemplateAttribute.isRequired());
48          return new WorkflowRuleAttributeRows(fields);
49      }
50  
51      @Override
52      public WorkflowRuleAttributeRows getRuleRows(Map<String, String> parameters, RuleTemplateAttribute ruleTemplateAttribute) {
53          required(ruleTemplateAttribute, "ruleTemplateAttribute");
54          if (parameters == null) {
55              parameters = Collections.emptyMap();
56          }
57          ExtensionDefinition extensionDefinition = ruleTemplateAttribute.getRuleAttribute();
58          WorkflowRuleAttributeHandlerService handler = getHandler(extensionDefinition);
59          WorkflowRuleAttributeFields fields = handler.getRuleFields(parameters, extensionDefinition, ruleTemplateAttribute.isRequired());
60          return new WorkflowRuleAttributeRows(fields);
61      }
62      
63      @Override
64      public WorkflowRuleAttributeRows getRoutingDataRows(Map<String, String> parameters, RuleTemplateAttributeBo ruleTemplateAttribute) {
65          required(ruleTemplateAttribute, "ruleTemplateAttribute");
66          if (parameters == null) {
67              parameters = Collections.emptyMap();
68          }
69          RuleAttribute ruleAttribute = ruleTemplateAttribute.getRuleAttribute();
70          ExtensionDefinition extensionDefinition = RuleAttribute.to(ruleAttribute);
71          WorkflowRuleAttributeHandlerService handler = getHandler(extensionDefinition);
72          WorkflowRuleAttributeFields fields = handler.getRoutingDataFields(parameters, extensionDefinition, ruleTemplateAttribute.isRequired());
73          return new WorkflowRuleAttributeRows(fields);
74      }
75      
76      @Override
77      public WorkflowRuleAttributeRows getRoutingDataRows(Map<String, String> parameters, RuleTemplateAttribute ruleTemplateAttribute) {
78          required(ruleTemplateAttribute, "ruleTemplateAttribute");
79          if (parameters == null) {
80              parameters = Collections.emptyMap();
81          }
82          ExtensionDefinition extensionDefinition = ruleTemplateAttribute.getRuleAttribute();
83          WorkflowRuleAttributeHandlerService handler = getHandler(extensionDefinition);
84          WorkflowRuleAttributeFields fields = handler.getRoutingDataFields(parameters, extensionDefinition, ruleTemplateAttribute.isRequired());
85          return new WorkflowRuleAttributeRows(fields);
86      }
87  
88      @Override
89      public WorkflowRuleAttributeRows getSearchRows(Map<String, String> parameters, RuleTemplateAttribute ruleTemplateAttribute) {
90          required(ruleTemplateAttribute, "ruleTemplateAttribute");
91          if (parameters == null) {
92              parameters = Collections.emptyMap();
93          }
94          ExtensionDefinition extensionDefinition = ruleTemplateAttribute.getRuleAttribute();
95          WorkflowRuleAttributeHandlerService handler = getHandler(extensionDefinition);
96          WorkflowRuleAttributeFields fields = handler.getSearchFields(parameters, extensionDefinition, ruleTemplateAttribute.isRequired());
97          return new WorkflowRuleAttributeRows(fields);
98  
99      }
100 
101     @Override
102     public WorkflowRuleAttributeRows getSearchRows(Map<String, String> parameters, RuleTemplateAttribute ruleTemplateAttribute, boolean required) {
103         required(ruleTemplateAttribute, "ruleTemplateAttribute");
104         if (parameters == null) {
105             parameters = Collections.emptyMap();
106         }
107         ExtensionDefinition extensionDefinition = ruleTemplateAttribute.getRuleAttribute();
108         WorkflowRuleAttributeHandlerService handler = getHandler(extensionDefinition);
109         WorkflowRuleAttributeFields fields = handler.getSearchFields(parameters, extensionDefinition, required);
110         return new WorkflowRuleAttributeRows(fields);
111 
112     }
113 
114     @Override
115     public List<RoleName> getRoleNames(RuleTemplateAttributeBo ruleTemplateAttribute) {
116         required(ruleTemplateAttribute, "ruleTemplateAttribute");
117         RuleAttribute ruleAttribute = ruleTemplateAttribute.getRuleAttribute();
118         ExtensionDefinition extensionDefinition = RuleAttribute.to(ruleAttribute);
119         WorkflowRuleAttributeHandlerService handler = getHandler(extensionDefinition);
120         return handler.getRoleNames(extensionDefinition);
121     }
122 
123     private void required(Object object, String name) {
124         if (object == null) {
125             throw new IllegalArgumentException(name + " must not be null");
126         }
127     }
128 
129     /**
130      * Retrieves the WorkflowRuleAttributeHandlerService which can be used to handle the given ExtensionDefinition. If
131      * such a service cannot be loaded, an IllegalStateException is thrown.
132      *
133      * @param extensionDefinition the definition of the extension which will be accessed through the handler service
134      *
135      * @return a reference to a handler service which can be used to interact with the given extension definition
136      *
137      * @throws IllegalStateException if the handler cannot be found
138      */
139     protected WorkflowRuleAttributeHandlerService getHandler(ExtensionDefinition extensionDefinition) {
140         WorkflowRuleAttributeHandlerService handler =
141                 KewFrameworkServiceLocator.getWorkflowRuleAttributeHandlerService(extensionDefinition.getApplicationId());
142         if (handler == null) {
143             throw new IllegalStateException("Failed to locate a WorkflowRuleAttributeHandlerService for the given ExtensionDefinition: " + extensionDefinition);
144         }
145         return handler;
146     }
147 
148 
149 }