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