1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kew.rule.web; |
17 | |
|
18 | |
import org.apache.commons.beanutils.BeanUtils; |
19 | |
import org.apache.commons.beanutils.PropertyUtils; |
20 | |
import org.apache.commons.lang.ArrayUtils; |
21 | |
import org.apache.commons.lang.StringUtils; |
22 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
23 | |
import org.kuali.rice.kew.api.action.ActionRequestPolicy; |
24 | |
import org.kuali.rice.kew.api.rule.RuleTemplateAttributeContract; |
25 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
26 | |
import org.kuali.rice.kew.rule.GroupRuleResponsibility; |
27 | |
import org.kuali.rice.kew.rule.PersonRuleResponsibility; |
28 | |
import org.kuali.rice.kew.rule.RoleRuleResponsibility; |
29 | |
import org.kuali.rice.kew.rule.RuleBaseValues; |
30 | |
import org.kuali.rice.kew.rule.RuleDelegationBo; |
31 | |
import org.kuali.rice.kew.rule.RuleExtensionBo; |
32 | |
import org.kuali.rice.kew.rule.RuleExtensionValue; |
33 | |
import org.kuali.rice.kew.rule.RuleResponsibilityBo; |
34 | |
import org.kuali.rice.kew.rule.WorkflowRuleAttribute; |
35 | |
import org.kuali.rice.kew.rule.bo.RuleAttribute; |
36 | |
import org.kuali.rice.kew.rule.bo.RuleTemplateAttributeBo; |
37 | |
import org.kuali.rice.kew.rule.bo.RuleTemplateBo; |
38 | |
import org.kuali.rice.kew.rule.service.RuleServiceInternal; |
39 | |
import org.kuali.rice.kew.rule.xmlrouting.GenericXMLRuleAttribute; |
40 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
41 | |
import org.kuali.rice.kew.api.KewApiConstants; |
42 | |
import org.kuali.rice.kim.api.group.Group; |
43 | |
import org.kuali.rice.kim.api.identity.principal.Principal; |
44 | |
import org.kuali.rice.kns.web.ui.Field; |
45 | |
import org.kuali.rice.kns.web.ui.Row; |
46 | |
import org.kuali.rice.kns.web.ui.Section; |
47 | |
|
48 | |
import java.lang.reflect.InvocationTargetException; |
49 | |
import java.util.ArrayList; |
50 | |
import java.util.Collections; |
51 | |
import java.util.HashMap; |
52 | |
import java.util.Iterator; |
53 | |
import java.util.List; |
54 | |
import java.util.Map; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public final class WebRuleUtils { |
62 | |
|
63 | |
public static final String RULE_TEMPLATE_ID_PARAM = "ruleCreationValues.ruleTemplateId"; |
64 | |
public static final String RULE_TEMPLATE_NAME_PARAM = "ruleCreationValues.ruleTemplateName"; |
65 | |
public static final String DOCUMENT_TYPE_NAME_PARAM = "ruleCreationValues.docTypeName"; |
66 | |
public static final String RESPONSIBILITY_ID_PARAM = "ruleCreationValues.responsibilityId"; |
67 | |
|
68 | |
private static final String ID_SEPARATOR = ":"; |
69 | |
private static final String RULE_ATTRIBUTES_SECTION_ID = "RuleAttributes"; |
70 | |
private static final String RULE_ATTRIBUTES_SECTION_TITLE = "Rule Attributes"; |
71 | |
private static final String ROLES_MAINTENANCE_SECTION_ID = "RolesMaintenance"; |
72 | |
|
73 | 0 | private WebRuleUtils() { |
74 | 0 | throw new UnsupportedOperationException("do not call"); |
75 | |
} |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public static WebRuleBaseValues copyRuleOntoExistingDocument(WebRuleBaseValues rule) throws Exception { |
83 | 0 | WebRuleBaseValues ruleCopy = new WebRuleBaseValues(); |
84 | 0 | PropertyUtils.copyProperties(ruleCopy, rule); |
85 | 0 | ruleCopy.setPreviousRuleId(null); |
86 | 0 | ruleCopy.setCurrentInd(null); |
87 | 0 | ruleCopy.setVersionNbr(null); |
88 | |
|
89 | 0 | List responsibilities = new ArrayList(); |
90 | 0 | for (Iterator iter = ruleCopy.getRuleResponsibilities().iterator(); iter.hasNext();) { |
91 | 0 | WebRuleResponsibility responsibility = (WebRuleResponsibility) iter.next(); |
92 | 0 | WebRuleResponsibility responsibilityCopy = new WebRuleResponsibility(); |
93 | 0 | PropertyUtils.copyProperties(responsibilityCopy, responsibility); |
94 | |
|
95 | 0 | responsibilityCopy.setResponsibilityId(null); |
96 | 0 | responsibilityCopy.setId(null); |
97 | |
|
98 | 0 | List delegations = new ArrayList(); |
99 | 0 | for (Iterator iterator = responsibilityCopy.getDelegationRules().iterator(); iterator.hasNext();) { |
100 | 0 | RuleDelegationBo delegation = (RuleDelegationBo) iterator.next(); |
101 | 0 | RuleDelegationBo delegationCopy = new RuleDelegationBo(); |
102 | 0 | PropertyUtils.copyProperties(delegationCopy, delegation); |
103 | |
|
104 | 0 | delegationCopy.setDelegateRuleId(null); |
105 | 0 | delegationCopy.setVersionNumber(null); |
106 | 0 | delegationCopy.setRuleDelegationId(null); |
107 | 0 | delegationCopy.setResponsibilityId(null); |
108 | |
|
109 | 0 | WebRuleBaseValues delegationRule = ((WebRuleBaseValues) delegation.getDelegationRule()); |
110 | 0 | WebRuleBaseValues ruleDelegateCopy = new WebRuleBaseValues(); |
111 | 0 | PropertyUtils.copyProperties(ruleDelegateCopy, delegationRule); |
112 | |
|
113 | 0 | ruleDelegateCopy.setPreviousRuleId(null); |
114 | 0 | ruleDelegateCopy.setCurrentInd(null); |
115 | 0 | ruleDelegateCopy.setVersionNbr(null); |
116 | |
|
117 | 0 | List delegateResps = new ArrayList(); |
118 | 0 | for (Iterator iterator1 = ruleDelegateCopy.getRuleResponsibilities().iterator(); iterator1.hasNext();) { |
119 | 0 | WebRuleResponsibility delegateResp = (WebRuleResponsibility) iterator1.next(); |
120 | 0 | WebRuleResponsibility delegateRespCopy = new WebRuleResponsibility(); |
121 | 0 | PropertyUtils.copyProperties(delegateRespCopy, delegateResp); |
122 | |
|
123 | 0 | delegateRespCopy.setResponsibilityId(null); |
124 | 0 | delegateRespCopy.setId(null); |
125 | 0 | delegateResps.add(delegateRespCopy); |
126 | 0 | } |
127 | 0 | ruleDelegateCopy.setRuleResponsibilities(delegateResps); |
128 | 0 | delegationCopy.setDelegationRule(ruleDelegateCopy); |
129 | 0 | delegations.add(delegationCopy); |
130 | 0 | } |
131 | |
|
132 | 0 | responsibilities.add(responsibilityCopy); |
133 | 0 | } |
134 | 0 | ruleCopy.setRuleResponsibilities(responsibilities); |
135 | 0 | return ruleCopy; |
136 | |
} |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
public static WebRuleBaseValues copyToNewRule(WebRuleBaseValues webRuleBaseValues) throws Exception { |
146 | 0 | WebRuleBaseValues newRule = copyRuleOntoExistingDocument(webRuleBaseValues); |
147 | |
|
148 | 0 | newRule.setDocumentId(null); |
149 | 0 | for (Iterator iterator = newRule.getRuleResponsibilities().iterator(); iterator.hasNext(); ) { |
150 | 0 | RuleResponsibilityBo responsibility = (RuleResponsibilityBo) iterator.next(); |
151 | 0 | for (Iterator iterator2 = responsibility.getDelegationRules().iterator(); iterator2.hasNext(); ) { |
152 | 0 | RuleDelegationBo delegation = (RuleDelegationBo) iterator2.next(); |
153 | 0 | delegation.getDelegationRule().setDocumentId(null); |
154 | 0 | } |
155 | 0 | } |
156 | 0 | return newRule; |
157 | |
} |
158 | |
|
159 | |
public static void validateRuleTemplateAndDocumentType(RuleBaseValues oldRule, RuleBaseValues newRule, Map<String, String[]> parameters) { |
160 | 0 | String[] ruleTemplateIds = parameters.get(RULE_TEMPLATE_ID_PARAM); |
161 | 0 | String[] ruleTemplateNames = parameters.get(RULE_TEMPLATE_NAME_PARAM); |
162 | 0 | String[] documentTypeNames = parameters.get(DOCUMENT_TYPE_NAME_PARAM); |
163 | 0 | if (ArrayUtils.isEmpty(ruleTemplateIds) && ArrayUtils.isEmpty(ruleTemplateNames)) { |
164 | 0 | throw new RiceRuntimeException("Rule document must be initiated with a valid rule template id or rule template name."); |
165 | |
} |
166 | 0 | if (ArrayUtils.isEmpty(documentTypeNames)) { |
167 | 0 | throw new RiceRuntimeException("Rule document must be initiated with a valid document type name."); |
168 | |
} |
169 | 0 | RuleTemplateBo ruleTemplate = null; |
170 | 0 | if (!ArrayUtils.isEmpty(ruleTemplateIds)) { |
171 | 0 | String ruleTemplateId = ruleTemplateIds[0]; |
172 | 0 | ruleTemplate = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateId(ruleTemplateId); |
173 | 0 | if (ruleTemplate == null) { |
174 | 0 | throw new RiceRuntimeException("Failed to load rule template with id '" + ruleTemplateId + "'"); |
175 | |
} |
176 | |
} |
177 | 0 | if (ruleTemplate == null) { |
178 | 0 | String ruleTemplateName = ruleTemplateNames[0]; |
179 | 0 | ruleTemplate = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(ruleTemplateName); |
180 | 0 | if (ruleTemplate == null) { |
181 | 0 | throw new RiceRuntimeException("Failed to load rule template with name '" + ruleTemplateName + "'"); |
182 | |
} |
183 | |
} |
184 | 0 | String documentTypeName = documentTypeNames[0]; |
185 | 0 | DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findByName(documentTypeName); |
186 | 0 | if (documentType == null) { |
187 | 0 | throw new RiceRuntimeException("Failed to locate document type with name '" + documentTypeName + "'"); |
188 | |
} |
189 | |
|
190 | |
|
191 | |
|
192 | 0 | initializeRuleAfterNew(oldRule, ruleTemplate, documentTypeName); |
193 | 0 | initializeRuleAfterNew(newRule, ruleTemplate, documentTypeName); |
194 | 0 | } |
195 | |
|
196 | |
private static void initializeRuleAfterNew(RuleBaseValues rule, RuleTemplateBo ruleTemplate, String documentTypeName) { |
197 | 0 | rule.setRuleTemplate(ruleTemplate); |
198 | 0 | rule.setRuleTemplateId(ruleTemplate.getId()); |
199 | 0 | rule.setDocTypeName(documentTypeName); |
200 | 0 | } |
201 | |
|
202 | |
public static void validateRuleAndResponsibility(RuleDelegationBo oldRuleDelegation, RuleDelegationBo newRuleDelegation, Map<String, String[]> parameters) { |
203 | 0 | String[] responsibilityIds = parameters.get(RESPONSIBILITY_ID_PARAM); |
204 | 0 | if (ArrayUtils.isEmpty(responsibilityIds)) { |
205 | 0 | throw new RiceRuntimeException("Delegation rule document must be initiated with a valid responsibility ID to delegate from."); |
206 | |
} |
207 | 0 | if (!ArrayUtils.isEmpty(responsibilityIds)) { |
208 | 0 | String responsibilityId = responsibilityIds[0]; |
209 | 0 | RuleResponsibilityBo ruleResponsibility = KEWServiceLocator.getRuleService().findRuleResponsibility(responsibilityId); |
210 | 0 | if (ruleResponsibility == null) { |
211 | 0 | throw new RiceRuntimeException("Failed to locate a rule responsibility for responsibility ID " + responsibilityId); |
212 | |
} |
213 | 0 | oldRuleDelegation.setResponsibilityId(responsibilityId); |
214 | 0 | newRuleDelegation.setResponsibilityId(responsibilityId); |
215 | |
} |
216 | |
|
217 | 0 | } |
218 | |
|
219 | |
public static void establishDefaultRuleValues(RuleBaseValues rule) { |
220 | 0 | rule.setActive(true); |
221 | |
|
222 | 0 | RuleBaseValues defaultRule = ((RuleServiceInternal) KEWServiceLocator.getService(KEWServiceLocator.RULE_SERVICE)).findDefaultRuleByRuleTemplateId( |
223 | |
rule.getRuleTemplate().getDelegationTemplateId()); |
224 | 0 | if (defaultRule != null) { |
225 | 0 | defaultRule.setActivationDate(null); |
226 | 0 | defaultRule.setCurrentInd(null); |
227 | 0 | defaultRule.setDeactivationDate(null); |
228 | 0 | defaultRule.setDocTypeName(null); |
229 | 0 | defaultRule.setVersionNumber(null); |
230 | 0 | defaultRule.setId(null); |
231 | 0 | defaultRule.setTemplateRuleInd(Boolean.FALSE); |
232 | 0 | defaultRule.setVersionNbr(null); |
233 | |
try { |
234 | 0 | PropertyUtils.copyProperties(rule, defaultRule); |
235 | 0 | } catch (IllegalAccessException e) { |
236 | 0 | throw new RuntimeException(e); |
237 | 0 | } catch (InvocationTargetException e) { |
238 | 0 | throw new RuntimeException(e); |
239 | 0 | } catch (NoSuchMethodException e) { |
240 | 0 | throw new RuntimeException(e); |
241 | 0 | } |
242 | |
} |
243 | 0 | } |
244 | |
|
245 | |
|
246 | |
public static List customizeSections(RuleBaseValues rule, List<Section> sections, boolean delegateRule) { |
247 | |
|
248 | 0 | List<Section> finalSections = new ArrayList<Section>(); |
249 | 0 | for (Section section : sections) { |
250 | |
|
251 | 0 | if (section.getSectionTitle().equals(RULE_ATTRIBUTES_SECTION_TITLE) || |
252 | |
RULE_ATTRIBUTES_SECTION_ID.equals(section.getSectionId())) { |
253 | 0 | List<Row> ruleTemplateRows = getRuleTemplateRows(rule, delegateRule); |
254 | 0 | if (!ruleTemplateRows.isEmpty()) { |
255 | 0 | section.setRows(ruleTemplateRows); |
256 | 0 | finalSections.add(section); |
257 | |
} |
258 | 0 | } else if (ROLES_MAINTENANCE_SECTION_ID.equals(section.getSectionId())) { |
259 | 0 | if (hasRoles(rule)) { |
260 | 0 | finalSections.add(section); |
261 | |
} |
262 | |
} else { |
263 | 0 | finalSections.add(section); |
264 | |
} |
265 | |
} |
266 | |
|
267 | 0 | return finalSections; |
268 | |
} |
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
public static List<Row> getRuleTemplateRows(RuleBaseValues rule, boolean delegateRule) { |
274 | |
|
275 | 0 | List<Row> rows = new ArrayList<Row>(); |
276 | 0 | RuleTemplateBo ruleTemplate = rule.getRuleTemplate(); |
277 | 0 | Map<String, String> fieldNameMap = new HashMap<String, String>(); |
278 | |
|
279 | 0 | if (ruleTemplate != null){ |
280 | 0 | ruleTemplate = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateId(ruleTemplate.getId()); |
281 | 0 | if (ruleTemplate != null) { |
282 | |
|
283 | 0 | List<RuleTemplateAttributeBo> ruleTemplateAttributes = ruleTemplate.getActiveRuleTemplateAttributes(); |
284 | 0 | Collections.sort(ruleTemplateAttributes); |
285 | |
|
286 | 0 | for (RuleTemplateAttributeBo ruleTemplateAttribute : ruleTemplateAttributes) { |
287 | 0 | if (!ruleTemplateAttribute.isWorkflowAttribute()) { |
288 | 0 | continue; |
289 | |
} |
290 | 0 | WorkflowRuleAttribute workflowAttribute = ruleTemplateAttribute.getWorkflowAttribute(); |
291 | 0 | RuleAttribute ruleAttribute = ruleTemplateAttribute.getRuleAttribute(); |
292 | 0 | if (ruleAttribute.getType().equals(KewApiConstants.RULE_XML_ATTRIBUTE_TYPE)) { |
293 | 0 | ((GenericXMLRuleAttribute) workflowAttribute).setExtensionDefinition(RuleAttribute.to( |
294 | |
ruleAttribute)); |
295 | |
} |
296 | 0 | Map<String, String> parameterMap = getFieldMapForRuleTemplateAttribute(rule, ruleTemplateAttribute); |
297 | 0 | workflowAttribute.validateRuleData(parameterMap); |
298 | 0 | List<Row> attributeRows = transformAndPopulateAttributeRows(workflowAttribute.getRuleRows(), ruleTemplateAttribute, rule, fieldNameMap, delegateRule); |
299 | 0 | rows.addAll(attributeRows); |
300 | |
|
301 | 0 | } |
302 | |
} |
303 | 0 | transformFieldConversions(rows, fieldNameMap); |
304 | |
} |
305 | 0 | return rows; |
306 | |
} |
307 | |
|
308 | |
public static void transformFieldConversions(List<Row> rows, Map<String, String> fieldNameMap) { |
309 | 0 | for (Row row : rows) { |
310 | 0 | Map<String, String> transformedFieldConversions = new HashMap<String, String>(); |
311 | 0 | for (Field field : row.getFields()) { |
312 | 0 | Map<String, String> fieldConversions = field.getFieldConversionMap(); |
313 | 0 | for (String lookupFieldName : fieldConversions.keySet()) { |
314 | 0 | String localFieldName = fieldConversions.get(lookupFieldName); |
315 | 0 | if (fieldNameMap.containsKey(localFieldName)) { |
316 | |
|
317 | 0 | transformedFieldConversions.put(lookupFieldName, fieldNameMap.get(localFieldName)); |
318 | |
} else { |
319 | |
|
320 | 0 | transformedFieldConversions.put(lookupFieldName, fieldConversions.get(lookupFieldName)); |
321 | |
} |
322 | 0 | } |
323 | 0 | field.setFieldConversions(transformedFieldConversions); |
324 | 0 | } |
325 | 0 | } |
326 | 0 | } |
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
private static boolean hasRoles(RuleBaseValues rule) { |
332 | 0 | RuleTemplateBo ruleTemplate = rule.getRuleTemplate(); |
333 | 0 | return !ruleTemplate.getRoles().isEmpty(); |
334 | |
} |
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
public static List<Row> transformAndPopulateAttributeRows(List<Row> attributeRows, RuleTemplateAttributeBo ruleTemplateAttribute, RuleBaseValues rule, Map<String, String> fieldNameMap, boolean delegateRule) { |
342 | |
|
343 | 0 | for (Row row : attributeRows) { |
344 | 0 | for (Field field : row.getFields()) { |
345 | 0 | String fieldName = field.getPropertyName(); |
346 | 0 | if (!StringUtils.isBlank(fieldName)) { |
347 | 0 | String valueKey = ruleTemplateAttribute.getId() + ID_SEPARATOR + fieldName; |
348 | |
|
349 | |
String propertyName; |
350 | |
|
351 | 0 | if (delegateRule) { |
352 | 0 | propertyName = "delegationRuleBaseValues.fieldValues(" + valueKey + ")"; |
353 | |
} else { |
354 | 0 | propertyName = "fieldValues(" + valueKey + ")"; |
355 | |
} |
356 | |
|
357 | 0 | fieldNameMap.put(fieldName, propertyName); |
358 | 0 | field.setPropertyName(propertyName); |
359 | 0 | field.setPropertyValue(rule.getFieldValues().get(valueKey)); |
360 | |
} |
361 | 0 | } |
362 | |
} |
363 | 0 | return attributeRows; |
364 | |
} |
365 | |
|
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
public static void clearKeysForSave(RuleBaseValues rule) { |
372 | 0 | rule.setId(null); |
373 | 0 | rule.setActivationDate(null); |
374 | 0 | rule.setDeactivationDate(null); |
375 | 0 | rule.setCurrentInd(false); |
376 | 0 | rule.setVersionNbr(null); |
377 | 0 | rule.setObjectId(null); |
378 | 0 | rule.setVersionNumber(0L); |
379 | 0 | } |
380 | |
|
381 | |
public static void clearKeysForSave(RuleDelegationBo ruleDelegation) { |
382 | 0 | ruleDelegation.setRuleDelegationId(null); |
383 | 0 | ruleDelegation.setObjectId(null); |
384 | 0 | ruleDelegation.setVersionNumber(0L); |
385 | 0 | clearKeysForSave(ruleDelegation.getDelegationRule()); |
386 | 0 | } |
387 | |
|
388 | |
public static void translateResponsibilitiesForSave(RuleBaseValues rule) { |
389 | 0 | rule.getRuleResponsibilities().clear(); |
390 | 0 | for (PersonRuleResponsibility responsibility : rule.getPersonResponsibilities()) { |
391 | 0 | RuleResponsibilityBo ruleResponsibility = new RuleResponsibilityBo(); |
392 | 0 | ruleResponsibility.setActionRequestedCd(responsibility.getActionRequestedCd()); |
393 | 0 | ruleResponsibility.setPriority(responsibility.getPriority()); |
394 | 0 | ruleResponsibility.setResponsibilityId(responsibility.getResponsibilityId()); |
395 | 0 | if (ruleResponsibility.getResponsibilityId() == null) { |
396 | 0 | ruleResponsibility.setResponsibilityId(KEWServiceLocator.getResponsibilityIdService().getNewResponsibilityId()); |
397 | |
} |
398 | 0 | String principalId = KEWServiceLocator.getIdentityHelperService().getIdForPrincipalName(responsibility.getPrincipalName()); |
399 | 0 | ruleResponsibility.setRuleResponsibilityName(principalId); |
400 | 0 | ruleResponsibility.setRuleResponsibilityType(KewApiConstants.RULE_RESPONSIBILITY_WORKFLOW_ID); |
401 | |
|
402 | 0 | ruleResponsibility.setApprovePolicy(ActionRequestPolicy.FIRST.getCode()); |
403 | 0 | rule.getRuleResponsibilities().add(ruleResponsibility); |
404 | 0 | } |
405 | 0 | for (GroupRuleResponsibility responsibility : rule.getGroupResponsibilities()) { |
406 | 0 | RuleResponsibilityBo ruleResponsibility = new RuleResponsibilityBo(); |
407 | 0 | ruleResponsibility.setActionRequestedCd(responsibility.getActionRequestedCd()); |
408 | 0 | ruleResponsibility.setPriority(responsibility.getPriority()); |
409 | 0 | ruleResponsibility.setResponsibilityId(responsibility.getResponsibilityId()); |
410 | 0 | if (ruleResponsibility.getResponsibilityId() == null) { |
411 | 0 | ruleResponsibility.setResponsibilityId(KEWServiceLocator.getResponsibilityIdService().getNewResponsibilityId()); |
412 | |
} |
413 | 0 | Group group = KEWServiceLocator.getIdentityHelperService().getGroupByName(responsibility.getNamespaceCode(), responsibility.getName()); |
414 | 0 | ruleResponsibility.setRuleResponsibilityName(group.getId()); |
415 | 0 | ruleResponsibility.setRuleResponsibilityType(KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID); |
416 | 0 | ruleResponsibility.setApprovePolicy(ActionRequestPolicy.FIRST.getCode()); |
417 | 0 | rule.getRuleResponsibilities().add(ruleResponsibility); |
418 | 0 | } |
419 | 0 | for (RoleRuleResponsibility responsibility : rule.getRoleResponsibilities()) { |
420 | 0 | RuleResponsibilityBo ruleResponsibility = new RuleResponsibilityBo(); |
421 | 0 | ruleResponsibility.setActionRequestedCd(responsibility.getActionRequestedCd()); |
422 | 0 | ruleResponsibility.setPriority(responsibility.getPriority()); |
423 | 0 | ruleResponsibility.setResponsibilityId(responsibility.getResponsibilityId()); |
424 | 0 | if (ruleResponsibility.getResponsibilityId() == null) { |
425 | 0 | ruleResponsibility.setResponsibilityId(KEWServiceLocator.getResponsibilityIdService().getNewResponsibilityId()); |
426 | |
} |
427 | 0 | ruleResponsibility.setRuleResponsibilityName(responsibility.getRoleName()); |
428 | 0 | ruleResponsibility.setRuleResponsibilityType(KewApiConstants.RULE_RESPONSIBILITY_ROLE_ID); |
429 | 0 | ruleResponsibility.setApprovePolicy(responsibility.getApprovePolicy()); |
430 | 0 | rule.getRuleResponsibilities().add(ruleResponsibility); |
431 | 0 | } |
432 | 0 | } |
433 | |
|
434 | |
public static void translateFieldValuesForSave(RuleBaseValues rule) { |
435 | 0 | RuleTemplateBo ruleTemplate = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateId(rule.getRuleTemplateId()); |
436 | |
|
437 | |
|
438 | 0 | List extensions = new ArrayList(); |
439 | 0 | for (Iterator iterator = ruleTemplate.getActiveRuleTemplateAttributes().iterator(); iterator.hasNext();) { |
440 | 0 | RuleTemplateAttributeBo ruleTemplateAttribute = (RuleTemplateAttributeBo) iterator.next(); |
441 | 0 | if (!ruleTemplateAttribute.isWorkflowAttribute()) { |
442 | 0 | continue; |
443 | |
} |
444 | 0 | WorkflowRuleAttribute workflowAttribute = ruleTemplateAttribute.getWorkflowAttribute(); |
445 | |
|
446 | 0 | RuleAttribute ruleAttribute = ruleTemplateAttribute.getRuleAttribute(); |
447 | 0 | if (ruleAttribute.getType().equals(KewApiConstants.RULE_XML_ATTRIBUTE_TYPE)) { |
448 | 0 | ((GenericXMLRuleAttribute) workflowAttribute).setExtensionDefinition(RuleAttribute.to(ruleAttribute)); |
449 | |
} |
450 | |
|
451 | 0 | Map<String, String> parameterMap = getFieldMapForRuleTemplateAttribute(rule, ruleTemplateAttribute); |
452 | |
|
453 | |
|
454 | 0 | List attValidationErrors = workflowAttribute.validateRuleData(parameterMap); |
455 | |
|
456 | |
|
457 | |
|
458 | 0 | if (attValidationErrors != null && !attValidationErrors.isEmpty()) { |
459 | 0 | throw new RiceRuntimeException("Encountered attribute validation errors when attempting to save the Rule!"); |
460 | |
} |
461 | |
|
462 | 0 | List ruleExtensionValues = workflowAttribute.getRuleExtensionValues(); |
463 | 0 | if (ruleExtensionValues != null && !ruleExtensionValues.isEmpty()) { |
464 | 0 | RuleExtensionBo ruleExtension = new RuleExtensionBo(); |
465 | 0 | ruleExtension.setRuleTemplateAttributeId(ruleTemplateAttribute.getId()); |
466 | |
|
467 | 0 | ruleExtension.setExtensionValues(ruleExtensionValues); |
468 | 0 | extensions.add(ruleExtension); |
469 | |
} |
470 | |
|
471 | 0 | } |
472 | 0 | rule.setRuleExtensions(extensions); |
473 | |
|
474 | 0 | for (Iterator iterator = rule.getRuleExtensions().iterator(); iterator.hasNext();) { |
475 | 0 | RuleExtensionBo ruleExtension = (RuleExtensionBo) iterator.next(); |
476 | 0 | ruleExtension.setRuleBaseValues(rule); |
477 | |
|
478 | 0 | for (Iterator iterator2 = ruleTemplate.getActiveRuleTemplateAttributes().iterator(); iterator2.hasNext();) { |
479 | 0 | RuleTemplateAttributeBo ruleTemplateAttribute = (RuleTemplateAttributeBo) iterator2.next(); |
480 | 0 | if (StringUtils.equals(ruleTemplateAttribute.getId(), ruleExtension.getRuleTemplateAttributeId())) { |
481 | 0 | ruleExtension.setRuleTemplateAttribute(ruleTemplateAttribute); |
482 | 0 | break; |
483 | |
} |
484 | 0 | } |
485 | |
|
486 | 0 | for (Iterator iterator2 = ruleExtension.getExtensionValues().iterator(); iterator2.hasNext();) { |
487 | 0 | RuleExtensionValue ruleExtensionValue = (RuleExtensionValue) iterator2.next(); |
488 | 0 | ruleExtensionValue.setExtension(ruleExtension); |
489 | 0 | } |
490 | 0 | } |
491 | 0 | } |
492 | |
|
493 | |
|
494 | |
|
495 | |
|
496 | |
|
497 | |
public static Map<String, String> getFieldMapForRuleTemplateAttribute(RuleBaseValues rule, RuleTemplateAttributeContract ruleTemplateAttribute) { |
498 | 0 | Map<String, String> fieldMap = new HashMap<String, String>(); |
499 | 0 | for (String fieldKey : rule.getFieldValues().keySet()) { |
500 | 0 | String ruleTemplateAttributeId = fieldKey.substring(0, fieldKey.indexOf(ID_SEPARATOR)); |
501 | 0 | String fieldName = fieldKey.substring(fieldKey.indexOf(ID_SEPARATOR) + 1); |
502 | 0 | if (ruleTemplateAttribute.getId().equals(ruleTemplateAttributeId)) { |
503 | 0 | fieldMap.put(fieldName, rule.getFieldValues().get(fieldKey)); |
504 | |
} |
505 | 0 | } |
506 | 0 | return fieldMap; |
507 | |
} |
508 | |
|
509 | |
public static void processRuleForDelegationSave(RuleDelegationBo ruleDelegation) { |
510 | 0 | RuleBaseValues rule = ruleDelegation.getDelegationRule(); |
511 | 0 | rule.setDelegateRule(true); |
512 | |
|
513 | 0 | for (RuleResponsibilityBo responsibility : rule.getRuleResponsibilities()) { |
514 | 0 | responsibility.setActionRequestedCd(null); |
515 | 0 | responsibility.setPriority(null); |
516 | |
} |
517 | 0 | } |
518 | |
|
519 | |
public static void populateForCopyOrEdit(RuleBaseValues oldRule, RuleBaseValues newRule) { |
520 | 0 | populateRuleMaintenanceFields(oldRule); |
521 | 0 | populateRuleMaintenanceFields(newRule); |
522 | |
|
523 | |
|
524 | 0 | if (newRule.getRuleTemplate().getName() == null) { |
525 | 0 | newRule.getRuleTemplate().setName(oldRule.getRuleTemplate().getName()); |
526 | |
} |
527 | 0 | if (newRule.getDocTypeName() == null) { |
528 | 0 | newRule.setDocTypeName(oldRule.getDocTypeName()); |
529 | |
} |
530 | 0 | } |
531 | |
|
532 | |
|
533 | |
|
534 | |
|
535 | |
|
536 | |
|
537 | |
|
538 | |
public static void populateRuleMaintenanceFields(RuleBaseValues rule) { |
539 | 0 | translateResponsibilitiesForLoad(rule); |
540 | 0 | translateRuleExtensionsForLoad(rule); |
541 | 0 | } |
542 | |
|
543 | |
public static void translateResponsibilitiesForLoad(RuleBaseValues rule) { |
544 | 0 | for (RuleResponsibilityBo responsibility : rule.getRuleResponsibilities()) { |
545 | 0 | if (responsibility.getRuleResponsibilityType().equals(KewApiConstants.RULE_RESPONSIBILITY_WORKFLOW_ID)) { |
546 | 0 | PersonRuleResponsibility personResponsibility = new PersonRuleResponsibility(); |
547 | 0 | copyResponsibility(responsibility, personResponsibility); |
548 | 0 | Principal principal = KEWServiceLocator.getIdentityHelperService().getPrincipal(personResponsibility.getRuleResponsibilityName()); |
549 | 0 | personResponsibility.setPrincipalName(principal.getPrincipalName()); |
550 | 0 | rule.getPersonResponsibilities().add(personResponsibility); |
551 | 0 | } else if (responsibility.getRuleResponsibilityType().equals(KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID)) { |
552 | 0 | GroupRuleResponsibility groupResponsibility = new GroupRuleResponsibility(); |
553 | 0 | copyResponsibility(responsibility, groupResponsibility); |
554 | 0 | Group group = KEWServiceLocator.getIdentityHelperService().getGroup(groupResponsibility.getRuleResponsibilityName()); |
555 | 0 | groupResponsibility.setNamespaceCode(group.getNamespaceCode()); |
556 | 0 | groupResponsibility.setName(group.getName()); |
557 | 0 | rule.getGroupResponsibilities().add(groupResponsibility); |
558 | 0 | } else if (responsibility.getRuleResponsibilityType().equals(KewApiConstants.RULE_RESPONSIBILITY_ROLE_ID)) { |
559 | 0 | RoleRuleResponsibility roleResponsibility = new RoleRuleResponsibility(); |
560 | 0 | copyResponsibility(responsibility, roleResponsibility); |
561 | 0 | rule.getRoleResponsibilities().add(roleResponsibility); |
562 | 0 | } else { |
563 | 0 | throw new RiceRuntimeException("Original responsibility with id '" + responsibility.getId() + "' contained a bad type code of '" + responsibility.getRuleResponsibilityType()); |
564 | |
} |
565 | |
} |
566 | |
|
567 | 0 | rule.getRuleResponsibilities().clear(); |
568 | 0 | } |
569 | |
|
570 | |
public static void copyResponsibility(RuleResponsibilityBo source, RuleResponsibilityBo target) { |
571 | |
try { |
572 | 0 | BeanUtils.copyProperties(target, source); |
573 | 0 | } catch (Exception e) { |
574 | 0 | throw new RiceRuntimeException("Failed to copy properties from source to target responsibility", e); |
575 | 0 | } |
576 | 0 | } |
577 | |
|
578 | |
public static void translateRuleExtensionsForLoad(RuleBaseValues rule) { |
579 | 0 | for (RuleExtensionBo ruleExtension : rule.getRuleExtensions()) { |
580 | 0 | String ruleTemplateAttributeId = ruleExtension.getRuleTemplateAttributeId(); |
581 | 0 | for (RuleExtensionValue ruleExtensionValue : ruleExtension.getExtensionValues()) { |
582 | 0 | String fieldMapKey = ruleTemplateAttributeId + ID_SEPARATOR + ruleExtensionValue.getKey(); |
583 | 0 | rule.getFieldValues().put(fieldMapKey, ruleExtensionValue.getValue()); |
584 | 0 | } |
585 | 0 | } |
586 | |
|
587 | 0 | rule.getRuleExtensions().clear(); |
588 | 0 | } |
589 | |
|
590 | |
public static void processRuleForCopy(String documentNumber, RuleBaseValues oldRule, RuleBaseValues newRule) { |
591 | 0 | WebRuleUtils.populateForCopyOrEdit(oldRule, newRule); |
592 | 0 | clearKeysForCopy(newRule); |
593 | 0 | newRule.setDocumentId(documentNumber); |
594 | 0 | } |
595 | |
|
596 | |
public static void clearKeysForCopy(RuleBaseValues rule) { |
597 | 0 | rule.setId(null); |
598 | 0 | rule.setPreviousRuleId(null); |
599 | 0 | rule.setPreviousVersion(null); |
600 | 0 | rule.setName(null); |
601 | 0 | for (PersonRuleResponsibility responsibility : rule.getPersonResponsibilities()) { |
602 | 0 | clearResponsibilityKeys(responsibility); |
603 | |
} |
604 | 0 | for (GroupRuleResponsibility responsibility : rule.getGroupResponsibilities()) { |
605 | 0 | clearResponsibilityKeys(responsibility); |
606 | |
} |
607 | 0 | for (RoleRuleResponsibility responsibility : rule.getRoleResponsibilities()) { |
608 | 0 | clearResponsibilityKeys(responsibility); |
609 | |
} |
610 | 0 | } |
611 | |
|
612 | |
private static void clearResponsibilityKeys(RuleResponsibilityBo responsibility) { |
613 | 0 | responsibility.setResponsibilityId(null); |
614 | 0 | responsibility.setId(null); |
615 | 0 | responsibility.setRuleBaseValuesId(null); |
616 | 0 | } |
617 | |
|
618 | |
} |