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.kew.impl.rule.attribute;
17  
18  import org.apache.commons.collections.CollectionUtils;
19  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
20  import org.kuali.rice.core.api.mo.ModelObjectUtils;
21  import org.kuali.rice.core.api.uif.RemotableAttributeError;
22  import org.kuali.rice.core.api.uif.RemotableAttributeField;
23  import org.kuali.rice.kew.api.KewApiConstants;
24  import org.kuali.rice.kew.api.extension.ExtensionDefinition;
25  import org.kuali.rice.kew.api.extension.ExtensionRepositoryService;
26  import org.kuali.rice.kew.api.extension.ExtensionUtils;
27  import org.kuali.rice.kew.api.rule.RoleName;
28  import org.kuali.rice.kew.framework.rule.attribute.WorkflowRuleAttributeFields;
29  import org.kuali.rice.kew.framework.rule.attribute.WorkflowRuleAttributeHandlerService;
30  import org.kuali.rice.kew.rule.RoleAttribute;
31  import org.kuali.rice.kew.rule.RuleExtensionValue;
32  import org.kuali.rice.kew.rule.WorkflowRuleAttribute;
33  import org.kuali.rice.kew.rule.WorkflowRuleSearchAttribute;
34  import org.kuali.rice.kew.rule.XmlConfiguredAttribute;
35  import org.kuali.rice.kew.rule.bo.RuleAttribute;
36  import org.kuali.rice.kew.rule.xmlrouting.GenericXMLRuleAttribute;
37  import org.kuali.rice.kns.util.FieldUtils;
38  import org.kuali.rice.kns.web.ui.Row;
39  
40  import java.util.Collections;
41  import java.util.HashMap;
42  import java.util.List;
43  import java.util.Map;
44  
45  /**
46   * Reference implementation of {@code WorkflowRuleAttributeHandlerService}.
47   *
48   * @author Kuali Rice Team (rice.collab@kuali.org)
49   */
50  public class WorkflowRuleAttributeHandlerServiceImpl implements WorkflowRuleAttributeHandlerService {
51  
52      private ExtensionRepositoryService extensionRepositoryService;
53  
54      @Override
55      public WorkflowRuleAttributeFields getRuleFields(Map<String, String> parameters,
56                                                       ExtensionDefinition extensionDefinition,
57                                                       boolean required)
58              throws RiceIllegalArgumentException {
59          if (parameters == null) {
60              parameters = new HashMap<String, String>();
61          }
62          WorkflowRuleAttribute attribute = loadAttribute(extensionDefinition);
63          attribute.setRequired(required);
64          List<RemotableAttributeError> errors = attribute.validateRuleData(parameters);
65          List<RemotableAttributeField> fields = FieldUtils.convertRowsToAttributeFields(attribute.getRuleRows());
66          List<RuleExtensionValue> ruleExtensionValues = attribute.getRuleExtensionValues();
67          Map<String, String> ruleExtensionValuesMap = new HashMap<String, String>();
68          for (RuleExtensionValue ruleExtensionValue : ruleExtensionValues) {
69              ruleExtensionValuesMap.put(ruleExtensionValue.getKey(), ruleExtensionValue.getValue());
70          }
71          return WorkflowRuleAttributeFields.create(errors, fields, ruleExtensionValuesMap);
72      }
73  
74      @Override
75      public WorkflowRuleAttributeFields getSearchFields(Map<String, String> parameters,
76                                                         ExtensionDefinition extensionDefinition,
77                                                         boolean required)
78              throws RiceIllegalArgumentException {
79          if (parameters == null) {
80              parameters = new HashMap<String, String>();
81          }
82          WorkflowRuleAttribute attribute = loadAttribute(extensionDefinition);
83          attribute.setRequired(required);
84          List<RemotableAttributeError> errors;
85          List<Row> searchRows;
86          if (attribute instanceof WorkflowRuleSearchAttribute) {
87              errors = ((WorkflowRuleSearchAttribute)attribute).validateSearchData(parameters);
88              searchRows = ((WorkflowRuleSearchAttribute)attribute).getSearchRows();
89          } else {
90              errors = attribute.validateRuleData(parameters);
91              searchRows = attribute.getRuleRows();
92          }
93          List<RemotableAttributeField> fields = FieldUtils.convertRowsToAttributeFields(searchRows);
94          List<RuleExtensionValue> ruleExtensionValues = attribute.getRuleExtensionValues();
95          Map<String, String> ruleExtensionValuesMap = new HashMap<String, String>();
96          for (RuleExtensionValue ruleExtensionValue : ruleExtensionValues) {
97              ruleExtensionValuesMap.put(ruleExtensionValue.getKey(), ruleExtensionValue.getValue());
98          }
99          return WorkflowRuleAttributeFields.create(errors, fields, ruleExtensionValuesMap);
100     }
101 
102     @Override
103     public WorkflowRuleAttributeFields getRoutingDataFields( Map<String, String> parameters,
104                                                     ExtensionDefinition extensionDefinition,
105                                                     boolean required)
106           throws RiceIllegalArgumentException {
107         if (parameters == null) {
108             parameters = new HashMap<String, String>();
109         }
110         WorkflowRuleAttribute attribute = loadAttribute(extensionDefinition);
111         attribute.setRequired(required);
112         List<RemotableAttributeError> allErrors = attribute.validateRoutingData(parameters);
113         List<RemotableAttributeError> ruleDataErrors = attribute.validateRuleData(parameters);
114         for (RemotableAttributeError error : ruleDataErrors) {
115             allErrors.add(error);
116         }
117         List<RemotableAttributeField> fields = FieldUtils.convertRowsToAttributeFields(attribute.getRoutingDataRows());
118         List<RuleExtensionValue> ruleExtensionValues = attribute.getRuleExtensionValues();
119         Map<String, String> ruleExtensionValuesMap = new HashMap<String, String>();
120         for (RuleExtensionValue ruleExtensionValue : ruleExtensionValues) {
121             ruleExtensionValuesMap.put(ruleExtensionValue.getKey(), ruleExtensionValue.getValue());
122         }
123         return WorkflowRuleAttributeFields.create(allErrors, fields, ruleExtensionValuesMap);
124     }
125     
126     @Override
127     public List<RoleName> getRoleNames(ExtensionDefinition extensionDefinition) {
128         WorkflowRuleAttribute attribute = loadAttribute(extensionDefinition);
129         if (attribute instanceof RoleAttribute) {
130             RoleAttribute roleAttribute = (RoleAttribute) attribute;
131             List<RoleName> roleNames = roleAttribute.getRoleNames();
132             if (CollectionUtils.isNotEmpty(roleNames)) {
133                 return ModelObjectUtils.createImmutableCopy(roleNames);
134             }
135       	}
136         return Collections.emptyList();
137     }
138 
139     private WorkflowRuleAttribute loadAttribute(ExtensionDefinition extensionDefinition) {
140         if (extensionDefinition == null) {
141             throw new RiceIllegalArgumentException("extensionDefinition was null or blank");
142         }
143         Object attribute = ExtensionUtils.loadExtension(extensionDefinition);
144         if (attribute == null) {
145             throw new RiceIllegalArgumentException("Failed to load WorkflowRuleAttribute for: " + extensionDefinition);
146         }
147         if (!WorkflowRuleAttribute.class.isAssignableFrom(attribute.getClass())) {
148             throw new RiceIllegalArgumentException("Failed to locate a WorkflowRuleAttribute with the given name: " + extensionDefinition.getName());
149         }
150         if (attribute instanceof XmlConfiguredAttribute) {
151             ((XmlConfiguredAttribute) attribute).setExtensionDefinition(extensionDefinition);
152         }
153         return (WorkflowRuleAttribute) attribute;
154     }
155 
156 
157     protected ExtensionRepositoryService getExtensionRepositoryService() {
158         return extensionRepositoryService;
159     }
160 
161     public void setExtensionRepositoryService(ExtensionRepositoryService extensionRepositoryService) {
162         this.extensionRepositoryService = extensionRepositoryService;
163     }
164 }