View Javadoc

1   /**
2    * Copyright 2005-2011 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.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
21  import org.kuali.rice.core.api.uif.RemotableAttributeField;
22  import org.kuali.rice.kew.api.extension.ExtensionDefinition;
23  import org.kuali.rice.kew.api.extension.ExtensionRepositoryService;
24  import org.kuali.rice.kew.api.extension.ExtensionUtils;
25  import org.kuali.rice.kew.api.rule.RoleName;
26  import org.kuali.rice.kew.api.validation.ValidationResults;
27  import org.kuali.rice.kew.exception.WorkflowServiceError;
28  import org.kuali.rice.kew.framework.rule.attribute.WorkflowRuleAttributeHandlerService;
29  import org.kuali.rice.kew.rule.RoleAttribute;
30  import org.kuali.rice.kew.rule.WorkflowRuleAttribute;
31  import org.kuali.rice.kew.rule.WorkflowRuleSearchAttribute;
32  import org.kuali.rice.kew.rule.XmlConfiguredAttribute;
33  import org.kuali.rice.kns.util.FieldUtils;
34  import org.kuali.rice.kns.web.ui.Row;
35  
36  import javax.jws.WebParam;
37  import java.util.ArrayList;
38  import java.util.List;
39  import java.util.Map;
40  
41  public class WorkflowRuleAttributeHandlerServiceImpl implements WorkflowRuleAttributeHandlerService {
42      private static final Logger LOG = Logger.getLogger(WorkflowRuleAttributeHandlerServiceImpl.class);
43  
44      private ExtensionRepositoryService extensionRepositoryService;
45  
46      @Override
47      public List<RemotableAttributeField> getSearchRows(String attributeName) {
48          if (StringUtils.isBlank(attributeName)) {
49              throw new RiceIllegalArgumentException("attributeName was null or blank");
50          }
51          Object searchAttribute = loadAttribute(attributeName);
52          List<Row> rows = null;
53  
54          if (WorkflowRuleSearchAttribute.class.isAssignableFrom(searchAttribute.getClass())) {
55              rows = ((WorkflowRuleSearchAttribute)searchAttribute).getSearchRows();
56          } else {
57              rows = ((WorkflowRuleAttribute)searchAttribute).getRuleRows();
58          }
59  
60          return FieldUtils.convertRowsToAttributeFields(rows);
61      }
62  
63      @Override
64      public boolean isWorkflowRuleAttribute(String attributeName) {
65          if (StringUtils.isBlank(attributeName)) {
66              throw new RiceIllegalArgumentException("attributeName was null or blank");
67          }
68          Object workflowRuleAttribute = loadAttribute(attributeName);
69          return workflowRuleAttribute instanceof WorkflowRuleAttribute;
70      }
71  
72      @Override
73      public List<RemotableAttributeField> getRuleRows(String attributeName) {
74          if (StringUtils.isBlank(attributeName)) {
75              throw new RiceIllegalArgumentException("attributeName was null or blank");
76          }
77          WorkflowRuleAttribute attribute = loadAttribute(attributeName);
78          List<Row> rows = attribute.getRuleRows();
79  
80          return FieldUtils.convertRowsToAttributeFields(rows);
81      }
82  
83      @Override
84      public List<RemotableAttributeField> getRoutingDataRows(String attributeName) {
85          if (StringUtils.isBlank(attributeName)) {
86              throw new RiceIllegalArgumentException("attributeName was null or blank");
87          }
88          WorkflowRuleAttribute attribute = loadAttribute(attributeName);
89          List<Row> rows = attribute.getRoutingDataRows();
90  
91          return FieldUtils.convertRowsToAttributeFields(rows);
92      }
93  
94      @Override
95      public ValidationResults validateRoutingData(@WebParam(name = "attributeName") String attributeName, Map<String, String> paramMap) {
96          if (StringUtils.isBlank(attributeName)) {
97              throw new RiceIllegalArgumentException("attributeName was null or blank");
98          }
99          WorkflowRuleAttribute attribute = loadAttribute(attributeName);
100         List<WorkflowServiceError> errors = attribute.validateRoutingData(paramMap);
101         ValidationResults.Builder builder = ValidationResults.Builder.create();
102         for (WorkflowServiceError error : errors) {
103             builder.addError(error.getArg1(), error.getMessage());
104         }
105         return builder.build();
106     }
107 
108     @Override
109     public ValidationResults validateSearchData(@WebParam(name = "attributeName") String attributeName,  Map<String, String> paramMap) {
110         if (StringUtils.isBlank(attributeName)) {
111             throw new RiceIllegalArgumentException("attributeName was null or blank");
112         }
113         WorkflowRuleAttribute attribute = loadAttribute(attributeName);
114         List<WorkflowServiceError> errors = attribute.validateRoutingData(paramMap);
115         ValidationResults.Builder builder = ValidationResults.Builder.create();
116         for (WorkflowServiceError error : errors) {
117             builder.addError(error.getArg1(), error.getMessage());
118         }
119         return builder.build();
120     }
121 
122     @Override
123     public ValidationResults validateRuleData(@WebParam(name = "attributeName") String attributeName, Map<String, String> paramMap) {
124         if (StringUtils.isBlank(attributeName)) {
125             throw new RiceIllegalArgumentException("customizerName was null or blank");
126         }
127         List<WorkflowServiceError> errors = new ArrayList<WorkflowServiceError>();
128         Object attribute = loadAttribute(attributeName);
129         if (WorkflowRuleSearchAttribute.class.isAssignableFrom(attribute.getClass())) {
130             errors = ((WorkflowRuleSearchAttribute)attribute).validateSearchData(paramMap);
131         } else {
132             errors = ((WorkflowRuleAttribute)attribute).validateRoutingData(paramMap);
133         }
134         ValidationResults.Builder builder = ValidationResults.Builder.create();
135         for (WorkflowServiceError error : errors) {
136             builder.addError(error.getArg1(), error.getMessage());
137         }
138         return builder.build();
139     }
140 
141     @Override
142     public List<RoleName> getRoleNames(String attributeName) {
143         Object roleAttribute = loadAttribute(attributeName);
144         if (!RoleAttribute.class.isAssignableFrom(roleAttribute.getClass())) {
145             throw new RiceIllegalArgumentException("Failed to locate a RoleAttribute with the given name: " + attributeName);
146         }
147         return ((RoleAttribute)roleAttribute).getRoleNames();
148     }
149 
150     private WorkflowRuleAttribute loadAttribute(String attributeName) {
151         ExtensionDefinition extensionDefinition = getExtensionRepositoryService().getExtensionByName(attributeName);
152         if (extensionDefinition == null) {
153             throw new RiceIllegalArgumentException("Failed to locate a WorkflowRuleAttribute with the given name: " + attributeName);
154         }
155         Object attribute = ExtensionUtils.loadExtension(extensionDefinition);
156         if (attribute == null) {
157             throw new RiceIllegalArgumentException("Failed to load WorkflowRuleAttribute for: " + extensionDefinition);
158         }
159         if (!WorkflowRuleAttribute.class.isAssignableFrom(attribute.getClass())) {
160             throw new RiceIllegalArgumentException("Failed to locate a WorkflowRuleAttribute with the given name: " + attributeName);
161         }
162         if (attribute instanceof XmlConfiguredAttribute) {
163             ((XmlConfiguredAttribute)attribute).setExtensionDefinition(extensionDefinition);
164         }
165 
166         return (WorkflowRuleAttribute)attribute;
167     }
168 
169 
170     protected ExtensionRepositoryService getExtensionRepositoryService() {
171         return extensionRepositoryService;
172     }
173 
174     public void setExtensionRepositoryService(ExtensionRepositoryService extensionRepositoryService) {
175         this.extensionRepositoryService = extensionRepositoryService;
176     }
177 }