View Javadoc

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