Coverage Report - org.kuali.rice.kew.rule.web.WebRuleUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
WebRuleUtils
0%
0/348
0%
0/136
4.269
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  * 
 4  
  * 
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  * 
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.rule.web;
 18  
 
 19  
 import java.lang.reflect.InvocationTargetException;
 20  
 import java.util.ArrayList;
 21  
 import java.util.Collections;
 22  
 import java.util.HashMap;
 23  
 import java.util.Iterator;
 24  
 import java.util.List;
 25  
 import java.util.Map;
 26  
 
 27  
 import org.apache.commons.beanutils.BeanUtils;
 28  
 import org.apache.commons.beanutils.PropertyUtils;
 29  
 import org.apache.commons.lang.ArrayUtils;
 30  
 import org.apache.commons.lang.StringUtils;
 31  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 32  
 import org.kuali.rice.kew.api.action.ActionRequestPolicy;
 33  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 34  
 import org.kuali.rice.kew.rule.GroupRuleResponsibility;
 35  
 import org.kuali.rice.kew.rule.PersonRuleResponsibility;
 36  
 import org.kuali.rice.kew.rule.RoleRuleResponsibility;
 37  
 import org.kuali.rice.kew.rule.RuleBaseValues;
 38  
 import org.kuali.rice.kew.rule.RuleDelegation;
 39  
 import org.kuali.rice.kew.rule.RuleExtension;
 40  
 import org.kuali.rice.kew.rule.RuleExtensionValue;
 41  
 import org.kuali.rice.kew.rule.RuleResponsibility;
 42  
 import org.kuali.rice.kew.rule.WorkflowAttribute;
 43  
 import org.kuali.rice.kew.rule.bo.RuleAttribute;
 44  
 import org.kuali.rice.kew.rule.bo.RuleTemplate;
 45  
 import org.kuali.rice.kew.rule.bo.RuleTemplateAttribute;
 46  
 import org.kuali.rice.kew.rule.service.RuleService;
 47  
 import org.kuali.rice.kew.rule.xmlrouting.GenericXMLRuleAttribute;
 48  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 49  
 import org.kuali.rice.kew.util.KEWConstants;
 50  
 import org.kuali.rice.kim.api.identity.principal.Principal;
 51  
 import org.kuali.rice.kim.api.group.Group;
 52  
 import org.kuali.rice.kns.web.ui.Field;
 53  
 import org.kuali.rice.kns.web.ui.Row;
 54  
 import org.kuali.rice.kns.web.ui.Section;
 55  
 
 56  
 /**
 57  
  * Some utilities which are utilized by the {@link RuleAction}.
 58  
  *
 59  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 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  
          * Copies the existing rule onto the current document.  This is used within the web-based rule GUI to make a
 79  
          * copy of a rule on the existing document.  Essentially, this method makes a copy of the rule and all
 80  
          * delegates but preserves the document ID of the original rule.
 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.setPreviousVersionId(null);
 86  0
         ruleCopy.setCurrentInd(null);
 87  0
         ruleCopy.setVersionNbr(null);
 88  
 
 89  0
         List responsibilities = new ArrayList();
 90  0
         for (Iterator iter = ruleCopy.getResponsibilities().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.setRuleResponsibilityKey(null);
 97  
             
 98  0
             List delegations = new ArrayList();
 99  0
             for (Iterator iterator = responsibilityCopy.getDelegationRules().iterator(); iterator.hasNext();) {
 100  0
                 RuleDelegation delegation = (RuleDelegation) iterator.next();
 101  0
                 RuleDelegation delegationCopy = new RuleDelegation();
 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.getDelegationRuleBaseValues());
 110  0
                 WebRuleBaseValues ruleDelegateCopy = new WebRuleBaseValues();
 111  0
                 PropertyUtils.copyProperties(ruleDelegateCopy, delegationRule);
 112  
 
 113  0
                 ruleDelegateCopy.setPreviousVersionId(null);
 114  0
                 ruleDelegateCopy.setCurrentInd(null);
 115  0
                 ruleDelegateCopy.setVersionNbr(null);
 116  
 
 117  0
                 List delegateResps = new ArrayList();
 118  0
                 for (Iterator iterator1 = ruleDelegateCopy.getResponsibilities().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.setRuleResponsibilityKey(null);
 125  0
                     delegateResps.add(delegateRespCopy);
 126  0
                 }
 127  0
                 ruleDelegateCopy.setResponsibilities(delegateResps);
 128  0
                 delegationCopy.setDelegationRuleBaseValues(ruleDelegateCopy);
 129  0
                 delegations.add(delegationCopy);
 130  0
             }
 131  
             //responsibilityCopy.setDelegationRules(delegations);
 132  0
             responsibilities.add(responsibilityCopy);
 133  0
         }
 134  0
         ruleCopy.setResponsibilities(responsibilities);
 135  0
         return ruleCopy;
 136  
     }
 137  
     
 138  
     /**
 139  
      * Makes a copy of the rule and clears the document id on the rule and any of its delegates.
 140  
      * This method is used for making a copy of a rule for a new document.  It essentially calls
 141  
      * the copyRuleOntoExistingDocument method and then clears out the document IDs.
 142  
      * 
 143  
      * @param webRuleBaseValues
 144  
      */
 145  
     public static WebRuleBaseValues copyToNewRule(WebRuleBaseValues webRuleBaseValues) throws Exception {
 146  0
             WebRuleBaseValues newRule = copyRuleOntoExistingDocument(webRuleBaseValues);
 147  
             // clear out all document IDs on the rule and it's delegates
 148  0
             newRule.setDocumentId(null);
 149  0
             for (Iterator iterator = newRule.getResponsibilities().iterator(); iterator.hasNext(); ) {
 150  0
                         RuleResponsibility responsibility = (RuleResponsibility) iterator.next();
 151  0
                         for (Iterator iterator2 = responsibility.getDelegationRules().iterator(); iterator2.hasNext(); ) {
 152  0
                                 RuleDelegation delegation = (RuleDelegation) iterator2.next();
 153  0
                                 delegation.getDelegationRuleBaseValues().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
                 RuleTemplate 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  
                 // it appears that there is always an old maintainable, even in the case of a new document creation,
 191  
                 // if we don't initialize both the old and new versions we get errors during meshSections
 192  0
                 initializeRuleAfterNew(oldRule, ruleTemplate, documentTypeName);
 193  0
                 initializeRuleAfterNew(newRule, ruleTemplate, documentTypeName);
 194  0
         }
 195  
     
 196  
         private static void initializeRuleAfterNew(RuleBaseValues rule, RuleTemplate ruleTemplate, String documentTypeName) {
 197  0
                 rule.setRuleTemplate(ruleTemplate);
 198  0
                 rule.setRuleTemplateId(ruleTemplate.getRuleTemplateId());
 199  0
                 rule.setDocTypeName(documentTypeName);
 200  0
         }
 201  
         
 202  
         public static void validateRuleAndResponsibility(RuleDelegation oldRuleDelegation, RuleDelegation 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
                         RuleResponsibility 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.setActiveInd(true);
 221  
 
 222  0
         RuleBaseValues defaultRule = ((RuleService) 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.setRuleBaseValuesId(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  
                         // unfortunately, in the case of an inquiry the sectionId will always be null so we have to check section title
 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
                 RuleTemplate ruleTemplate = rule.getRuleTemplate();
 277  0
                 Map<String, String> fieldNameMap = new HashMap<String, String>();
 278  
                 // refetch rule template from service because after persistence in KNS, it comes back without any rule template attributes
 279  0
                 if (ruleTemplate != null){
 280  0
                         ruleTemplate = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateId(ruleTemplate.getRuleTemplateId());
 281  0
                         if (ruleTemplate != null) {
 282  
                                 
 283  0
                                 List<RuleTemplateAttribute> ruleTemplateAttributes = ruleTemplate.getActiveRuleTemplateAttributes();
 284  0
                                 Collections.sort(ruleTemplateAttributes);
 285  
 
 286  0
                                 for (RuleTemplateAttribute ruleTemplateAttribute : ruleTemplateAttributes) {
 287  0
                                         if (!ruleTemplateAttribute.isWorkflowAttribute()) {
 288  0
                                                 continue;
 289  
                                         }
 290  0
                                         WorkflowAttribute workflowAttribute = ruleTemplateAttribute.getWorkflowAttribute();
 291  0
                                         RuleAttribute ruleAttribute = ruleTemplateAttribute.getRuleAttribute();
 292  0
                                         if (ruleAttribute.getType().equals(KEWConstants.RULE_XML_ATTRIBUTE_TYPE)) {
 293  0
                                                 ((GenericXMLRuleAttribute) workflowAttribute).setRuleAttribute(ruleAttribute);
 294  
                                         }
 295  0
                                         Map<String, String> parameterMap = getFieldMapForRuleTemplateAttribute(rule, ruleTemplateAttribute);
 296  0
                                         workflowAttribute.validateRuleData(parameterMap);
 297  0
                                         List<Row> attributeRows = transformAndPopulateAttributeRows(workflowAttribute.getRuleRows(), ruleTemplateAttribute, rule, fieldNameMap, delegateRule);
 298  0
                                         rows.addAll(attributeRows);
 299  
 
 300  0
                                 }
 301  
                         }
 302  0
                         transformFieldConversions(rows, fieldNameMap);
 303  
                 }
 304  0
                 return rows;
 305  
         }
 306  
         
 307  
         public static void transformFieldConversions(List<Row> rows, Map<String, String> fieldNameMap) {
 308  0
                 for (Row row : rows) {
 309  0
                         Map<String, String> transformedFieldConversions = new HashMap<String, String>();
 310  0
                         for (Field field : row.getFields()) {
 311  0
                                 Map<String, String> fieldConversions = field.getFieldConversionMap();
 312  0
                                 for (String lookupFieldName : fieldConversions.keySet()) {
 313  0
                                         String localFieldName = fieldConversions.get(lookupFieldName);
 314  0
                                         if (fieldNameMap.containsKey(localFieldName)) {
 315  
                                                 // set the transformed value
 316  0
                                                 transformedFieldConversions.put(lookupFieldName, fieldNameMap.get(localFieldName));
 317  
                                         } else {
 318  
                                                 // set the original value (not sure if this case will happen, but just in case)
 319  0
                                                 transformedFieldConversions.put(lookupFieldName, fieldConversions.get(lookupFieldName));
 320  
                                         }
 321  0
                                 }
 322  0
                                 field.setFieldConversions(transformedFieldConversions);
 323  0
                         }
 324  0
                 }
 325  0
         }
 326  
         
 327  
 
 328  
 
 329  
         
 330  
         private static boolean hasRoles(RuleBaseValues rule) {
 331  0
                 RuleTemplate ruleTemplate = rule.getRuleTemplate();
 332  0
                 return !ruleTemplate.getRoles().isEmpty();
 333  
         }
 334  
         
 335  
         /**
 336  
          * Processes the Fields on the various attributes Rows to assign an appropriate field name to them so that the
 337  
          * field name rendered in the maintenance HTML will properly assign the value to RuleBaseValues.fieldValues.
 338  
          */
 339  
         
 340  
         public static List<Row> transformAndPopulateAttributeRows(List<Row> attributeRows, RuleTemplateAttribute ruleTemplateAttribute, RuleBaseValues rule, Map<String, String> fieldNameMap, boolean delegateRule) {
 341  
 
 342  0
                 for (Row row : attributeRows) {
 343  0
                         for (Field field : row.getFields()) {
 344  0
                                 String fieldName = field.getPropertyName();
 345  0
                                 if (!StringUtils.isBlank(fieldName)) {
 346  0
                                         String valueKey = ruleTemplateAttribute.getRuleTemplateAttributeId() + ID_SEPARATOR + fieldName;
 347  
 
 348  
                                         String propertyName;
 349  
                                         
 350  0
                                         if (delegateRule) {
 351  0
                                                 propertyName = "delegationRuleBaseValues.fieldValues(" + valueKey + ")"; 
 352  
                                         } else {
 353  0
                                                 propertyName = "fieldValues(" + valueKey + ")"; 
 354  
                                         }
 355  
 
 356  0
                                         fieldNameMap.put(fieldName, propertyName);
 357  0
                                         field.setPropertyName(propertyName);
 358  0
                                         field.setPropertyValue(rule.getFieldValues().get(valueKey));
 359  
                                 }
 360  0
                         }
 361  
                 }
 362  0
                 return attributeRows;
 363  
         }
 364  
         
 365  
         /**
 366  
          * Since editing of a Rule should actually result in a rule with a new ID and new
 367  
          * entries in the rule and rule responsibility tables, we need to clear out
 368  
          * the primary keys of the rule and related objects.
 369  
          */
 370  
         public static void clearKeysForSave(RuleBaseValues rule) {
 371  0
                 rule.setRuleBaseValuesId(null);
 372  0
                 rule.setActivationDate(null);
 373  0
                 rule.setDeactivationDate(null);
 374  0
                 rule.setCurrentInd(false);
 375  0
                 rule.setVersionNbr(null);
 376  0
                 rule.setObjectId(null);
 377  0
                 rule.setVersionNumber(0L);
 378  0
         }
 379  
         
 380  
         public static void clearKeysForSave(RuleDelegation ruleDelegation) {
 381  0
                 ruleDelegation.setRuleDelegationId(null);
 382  0
                 ruleDelegation.setObjectId(null);
 383  0
                 ruleDelegation.setVersionNumber(0L);
 384  0
                 clearKeysForSave(ruleDelegation.getDelegationRuleBaseValues());
 385  0
         }
 386  
         
 387  
     public static void translateResponsibilitiesForSave(RuleBaseValues rule) {
 388  0
                 rule.getResponsibilities().clear();
 389  0
                 for (PersonRuleResponsibility responsibility : rule.getPersonResponsibilities()) {
 390  0
                         RuleResponsibility ruleResponsibility = new RuleResponsibility();
 391  0
                         ruleResponsibility.setActionRequestedCd(responsibility.getActionRequestedCd());
 392  0
                         ruleResponsibility.setPriority(responsibility.getPriority());
 393  0
                         ruleResponsibility.setResponsibilityId(responsibility.getResponsibilityId());
 394  0
                         if (ruleResponsibility.getResponsibilityId() == null) {
 395  0
                                 ruleResponsibility.setResponsibilityId(KEWServiceLocator.getResponsibilityIdService().getNewResponsibilityId());
 396  
                         }
 397  0
                         String principalId = KEWServiceLocator.getIdentityHelperService().getIdForPrincipalName(responsibility.getPrincipalName());
 398  0
                         ruleResponsibility.setRuleResponsibilityName(principalId);
 399  0
                         ruleResponsibility.setRuleResponsibilityType(KEWConstants.RULE_RESPONSIBILITY_WORKFLOW_ID);
 400  
                         // default the approve policy to First Approve
 401  0
                         ruleResponsibility.setApprovePolicy(ActionRequestPolicy.FIRST.getCode());
 402  0
                         rule.getResponsibilities().add(ruleResponsibility);
 403  0
                 }
 404  0
                 for (GroupRuleResponsibility responsibility : rule.getGroupResponsibilities()) {
 405  0
                         RuleResponsibility ruleResponsibility = new RuleResponsibility();
 406  0
                         ruleResponsibility.setActionRequestedCd(responsibility.getActionRequestedCd());
 407  0
                         ruleResponsibility.setPriority(responsibility.getPriority());
 408  0
                         ruleResponsibility.setResponsibilityId(responsibility.getResponsibilityId());
 409  0
                         if (ruleResponsibility.getResponsibilityId() == null) {
 410  0
                                 ruleResponsibility.setResponsibilityId(KEWServiceLocator.getResponsibilityIdService().getNewResponsibilityId());
 411  
                         }
 412  0
                         Group group = KEWServiceLocator.getIdentityHelperService().getGroupByName(responsibility.getNamespaceCode(), responsibility.getName());
 413  0
                         ruleResponsibility.setRuleResponsibilityName(group.getId());
 414  0
                         ruleResponsibility.setRuleResponsibilityType(KEWConstants.RULE_RESPONSIBILITY_GROUP_ID);
 415  0
                         ruleResponsibility.setApprovePolicy(ActionRequestPolicy.FIRST.getCode());
 416  0
                         rule.getResponsibilities().add(ruleResponsibility);
 417  0
                 }
 418  0
                 for (RoleRuleResponsibility responsibility : rule.getRoleResponsibilities()) {
 419  0
                         RuleResponsibility ruleResponsibility = new RuleResponsibility();
 420  0
                         ruleResponsibility.setActionRequestedCd(responsibility.getActionRequestedCd());
 421  0
                         ruleResponsibility.setPriority(responsibility.getPriority());
 422  0
                         ruleResponsibility.setResponsibilityId(responsibility.getResponsibilityId());
 423  0
                         if (ruleResponsibility.getResponsibilityId() == null) {
 424  0
                                 ruleResponsibility.setResponsibilityId(KEWServiceLocator.getResponsibilityIdService().getNewResponsibilityId());
 425  
                         }
 426  0
                         ruleResponsibility.setRuleResponsibilityName(responsibility.getRoleName());
 427  0
                         ruleResponsibility.setRuleResponsibilityType(KEWConstants.RULE_RESPONSIBILITY_ROLE_ID);
 428  0
                         ruleResponsibility.setApprovePolicy(responsibility.getApprovePolicy());
 429  0
                         rule.getResponsibilities().add(ruleResponsibility);
 430  0
                 }
 431  0
         }
 432  
     
 433  
     public static void translateFieldValuesForSave(RuleBaseValues rule) {
 434  0
             RuleTemplate ruleTemplate = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateId(rule.getRuleTemplateId());
 435  
 
 436  
                 /** Populate rule extension values * */
 437  0
                 List extensions = new ArrayList();
 438  0
                 for (Iterator iterator = ruleTemplate.getActiveRuleTemplateAttributes().iterator(); iterator.hasNext();) {
 439  0
                         RuleTemplateAttribute ruleTemplateAttribute = (RuleTemplateAttribute) iterator.next();
 440  0
                         if (!ruleTemplateAttribute.isWorkflowAttribute()) {
 441  0
                                 continue;
 442  
                         }
 443  0
                         WorkflowAttribute workflowAttribute = ruleTemplateAttribute.getWorkflowAttribute();
 444  
 
 445  0
                         RuleAttribute ruleAttribute = ruleTemplateAttribute.getRuleAttribute();
 446  0
                         if (ruleAttribute.getType().equals(KEWConstants.RULE_XML_ATTRIBUTE_TYPE)) {
 447  0
                                 ((GenericXMLRuleAttribute) workflowAttribute).setRuleAttribute(ruleAttribute);
 448  
                         }
 449  
 
 450  0
                         Map<String, String> parameterMap = getFieldMapForRuleTemplateAttribute(rule, ruleTemplateAttribute);
 451  
                                                 
 452  
                         // validate rule data populates the rule extension values for us
 453  0
                         List attValidationErrors = workflowAttribute.validateRuleData(parameterMap);
 454  
 
 455  
                         // because validation should be handled by business rules now, if we encounter a validation error at this point in
 456  
                         // time, let's throw an exception
 457  0
                         if (attValidationErrors != null && !attValidationErrors.isEmpty()) {
 458  0
                                 throw new RiceRuntimeException("Encountered attribute validation errors when attempting to save the Rule!");
 459  
                         }
 460  
                         
 461  0
                         List ruleExtensionValues = workflowAttribute.getRuleExtensionValues();
 462  0
                         if (ruleExtensionValues != null && !ruleExtensionValues.isEmpty()) {
 463  0
                                 RuleExtension ruleExtension = new RuleExtension();
 464  0
                                 ruleExtension.setRuleTemplateAttributeId(ruleTemplateAttribute.getRuleTemplateAttributeId());
 465  
 
 466  0
                                 ruleExtension.setExtensionValues(ruleExtensionValues);
 467  0
                                 extensions.add(ruleExtension);
 468  
                         }
 469  
                                 
 470  0
                 }
 471  0
                 rule.setRuleExtensions(extensions);
 472  
 
 473  0
                 for (Iterator iterator = rule.getRuleExtensions().iterator(); iterator.hasNext();) {
 474  0
                         RuleExtension ruleExtension = (RuleExtension) iterator.next();
 475  0
                         ruleExtension.setRuleBaseValues(rule);
 476  
 
 477  0
                         for (Iterator iterator2 = ruleTemplate.getActiveRuleTemplateAttributes().iterator(); iterator2.hasNext();) {
 478  0
                                 RuleTemplateAttribute ruleTemplateAttribute = (RuleTemplateAttribute) iterator2.next();
 479  0
                                 if (StringUtils.equals(ruleTemplateAttribute.getRuleTemplateAttributeId(), ruleExtension.getRuleTemplateAttributeId())) {
 480  0
                                         ruleExtension.setRuleTemplateAttribute(ruleTemplateAttribute);
 481  0
                                         break;
 482  
                                 }
 483  0
                         }
 484  
 
 485  0
                         for (Iterator iterator2 = ruleExtension.getExtensionValues().iterator(); iterator2.hasNext();) {
 486  0
                                 RuleExtensionValue ruleExtensionValue = (RuleExtensionValue) iterator2.next();
 487  0
                                 ruleExtensionValue.setExtension(ruleExtension);
 488  0
                         }
 489  0
                 }
 490  0
     }
 491  
 
 492  
     /**
 493  
      * Based on original logic implemented in Rule system.  Essentially constructs a Map of field values related
 494  
      * to the given RuleTemplateAttribute.
 495  
      */
 496  
     public static Map<String, String> getFieldMapForRuleTemplateAttribute(RuleBaseValues rule, RuleTemplateAttribute ruleTemplateAttribute) {
 497  0
             Map<String, String> fieldMap = new HashMap<String, String>();
 498  0
             for (String fieldKey : rule.getFieldValues().keySet()) {
 499  0
                     String ruleTemplateAttributeId = fieldKey.substring(0, fieldKey.indexOf(ID_SEPARATOR));
 500  0
                     String fieldName = fieldKey.substring(fieldKey.indexOf(ID_SEPARATOR) + 1);
 501  0
                     if (ruleTemplateAttribute.getRuleTemplateAttributeId().toString().equals(ruleTemplateAttributeId)) {
 502  0
                             fieldMap.put(fieldName, rule.getFieldValues().get(fieldKey));
 503  
                     }
 504  0
             }
 505  0
             return fieldMap;
 506  
     }
 507  
     
 508  
     public static void processRuleForDelegationSave(RuleDelegation ruleDelegation) {
 509  0
             RuleBaseValues rule = ruleDelegation.getDelegationRuleBaseValues();
 510  0
             rule.setDelegateRule(true);
 511  
             // certain items on a delegated rule responsibility are inherited from parent responsibility, set them to null
 512  0
             for (RuleResponsibility responsibility : rule.getResponsibilities()) {
 513  0
                     responsibility.setActionRequestedCd(null);
 514  0
                     responsibility.setPriority(null);
 515  
             }
 516  0
     }
 517  
     
 518  
     public static void populateForCopyOrEdit(RuleBaseValues oldRule, RuleBaseValues newRule) {
 519  0
                 populateRuleMaintenanceFields(oldRule);
 520  0
                 populateRuleMaintenanceFields(newRule);
 521  
                 // in the case of copy, our fields which are marked read only are cleared, this includes the rule template
 522  
                 // name and the document type name but we don't want these cleared
 523  0
                 if (newRule.getRuleTemplate().getName() == null) {
 524  0
                         newRule.getRuleTemplate().setName(oldRule.getRuleTemplate().getName());
 525  
                 }
 526  0
                 if (newRule.getDocTypeName() == null) {
 527  0
                         newRule.setDocTypeName(oldRule.getDocTypeName());
 528  
                 }
 529  0
         }
 530  
     
 531  
     /**
 532  
          * This method populates fields on RuleBaseValues which are used only for
 533  
          * maintenance purposes.  In otherwords, it populates the non-persistent fields
 534  
          * on the RuleBaseValues which the maintenance document needs to function
 535  
          * (such as the extension field values and responsibilities).
 536  
          */
 537  
         public static void populateRuleMaintenanceFields(RuleBaseValues rule) {
 538  0
                 translateResponsibilitiesForLoad(rule);
 539  0
                 translateRuleExtensionsForLoad(rule);
 540  0
         }
 541  
         
 542  
         public static void translateResponsibilitiesForLoad(RuleBaseValues rule) {
 543  0
                 for (RuleResponsibility responsibility : rule.getResponsibilities()) {
 544  0
                         if (responsibility.getRuleResponsibilityType().equals(KEWConstants.RULE_RESPONSIBILITY_WORKFLOW_ID)) {
 545  0
                                 PersonRuleResponsibility personResponsibility = new PersonRuleResponsibility();
 546  0
                                 copyResponsibility(responsibility, personResponsibility);
 547  0
                                 Principal principal = KEWServiceLocator.getIdentityHelperService().getPrincipal(personResponsibility.getRuleResponsibilityName());
 548  0
                                 personResponsibility.setPrincipalName(principal.getPrincipalName());
 549  0
                                 rule.getPersonResponsibilities().add(personResponsibility);
 550  0
                         } else if (responsibility.getRuleResponsibilityType().equals(KEWConstants.RULE_RESPONSIBILITY_GROUP_ID)) {
 551  0
                                 GroupRuleResponsibility groupResponsibility = new GroupRuleResponsibility();
 552  0
                                 copyResponsibility(responsibility, groupResponsibility);
 553  0
                                 Group group = KEWServiceLocator.getIdentityHelperService().getGroup(groupResponsibility.getRuleResponsibilityName());
 554  0
                                 groupResponsibility.setNamespaceCode(group.getNamespaceCode());
 555  0
                                 groupResponsibility.setName(group.getName());
 556  0
                                 rule.getGroupResponsibilities().add(groupResponsibility);
 557  0
                         } else if (responsibility.getRuleResponsibilityType().equals(KEWConstants.RULE_RESPONSIBILITY_ROLE_ID)) {
 558  0
                                 RoleRuleResponsibility roleResponsibility = new RoleRuleResponsibility();
 559  0
                                 copyResponsibility(responsibility, roleResponsibility);
 560  0
                                 rule.getRoleResponsibilities().add(roleResponsibility);
 561  0
                         } else {
 562  0
                                 throw new RiceRuntimeException("Original responsibility with id '" + responsibility.getRuleResponsibilityKey() + "' contained a bad type code of '" + responsibility.getRuleResponsibilityType());
 563  
                         }
 564  
                 }
 565  
                 // since we've loaded the responsibilities, let's clear the originals so they don't get serialized to the maint doc XML
 566  0
                 rule.getResponsibilities().clear();
 567  0
         }
 568  
         
 569  
         public static void copyResponsibility(RuleResponsibility source, RuleResponsibility target) {
 570  
                 try {
 571  0
                         BeanUtils.copyProperties(target, source);
 572  0
                 } catch (Exception e) {
 573  0
                         throw new RiceRuntimeException("Failed to copy properties from source to target responsibility", e);
 574  0
                 }
 575  0
         }
 576  
         
 577  
         public static void translateRuleExtensionsForLoad(RuleBaseValues rule) {
 578  0
                 for (RuleExtension ruleExtension : rule.getRuleExtensions()) {
 579  0
                         String ruleTemplateAttributeId = ruleExtension.getRuleTemplateAttributeId();
 580  0
                         for (RuleExtensionValue ruleExtensionValue : ruleExtension.getExtensionValues()) {
 581  0
                                 String fieldMapKey = ruleTemplateAttributeId + ID_SEPARATOR + ruleExtensionValue.getKey();
 582  0
                                 rule.getFieldValues().put(fieldMapKey, ruleExtensionValue.getValue());
 583  0
                         }
 584  0
                 }
 585  
                 // since we've loaded the extensions, let's clear the originals so that they don't get serialized to the maint doc XML
 586  0
                 rule.getRuleExtensions().clear();
 587  0
         }
 588  
         
 589  
         public static void processRuleForCopy(String documentNumber, RuleBaseValues oldRule, RuleBaseValues newRule) {
 590  0
                 WebRuleUtils.populateForCopyOrEdit(oldRule, newRule);
 591  0
                 clearKeysForCopy(newRule);
 592  0
                 newRule.setDocumentId(documentNumber);
 593  0
         }
 594  
         
 595  
         public static void clearKeysForCopy(RuleBaseValues rule) {            
 596  0
             rule.setRuleBaseValuesId(null);
 597  0
             rule.setPreviousVersionId(null);
 598  0
             rule.setPreviousVersion(null);
 599  0
             rule.setName(null);
 600  0
             for (PersonRuleResponsibility responsibility : rule.getPersonResponsibilities()) {
 601  0
                     clearResponsibilityKeys(responsibility);
 602  
             }
 603  0
             for (GroupRuleResponsibility responsibility : rule.getGroupResponsibilities()) {
 604  0
                     clearResponsibilityKeys(responsibility);
 605  
             }
 606  0
             for (RoleRuleResponsibility responsibility : rule.getRoleResponsibilities()) {
 607  0
                     clearResponsibilityKeys(responsibility);
 608  
             }
 609  0
     }
 610  
 
 611  
     private static void clearResponsibilityKeys(RuleResponsibility responsibility) {
 612  0
                 responsibility.setResponsibilityId(null);
 613  0
                 responsibility.setRuleResponsibilityKey(null);
 614  0
                 responsibility.setRuleBaseValuesId(null);
 615  0
     }
 616  
     
 617  
 }