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.doctype.bo.DocumentType;
 33  
 import org.kuali.rice.kew.rule.GroupRuleResponsibility;
 34  
 import org.kuali.rice.kew.rule.PersonRuleResponsibility;
 35  
 import org.kuali.rice.kew.rule.RoleRuleResponsibility;
 36  
 import org.kuali.rice.kew.rule.RuleBaseValues;
 37  
 import org.kuali.rice.kew.rule.RuleDelegation;
 38  
 import org.kuali.rice.kew.rule.RuleExtension;
 39  
 import org.kuali.rice.kew.rule.RuleExtensionValue;
 40  
 import org.kuali.rice.kew.rule.RuleResponsibility;
 41  
 import org.kuali.rice.kew.rule.WorkflowAttribute;
 42  
 import org.kuali.rice.kew.rule.bo.RuleAttribute;
 43  
 import org.kuali.rice.kew.rule.bo.RuleTemplate;
 44  
 import org.kuali.rice.kew.rule.bo.RuleTemplateAttribute;
 45  
 import org.kuali.rice.kew.rule.service.RuleService;
 46  
 import org.kuali.rice.kew.rule.xmlrouting.GenericXMLRuleAttribute;
 47  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 48  
 import org.kuali.rice.kew.util.KEWConstants;
 49  
 import org.kuali.rice.kim.api.entity.principal.Principal;
 50  
 import org.kuali.rice.kim.api.group.Group;
 51  
 
 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  
 /**
 58  
  * Some utilities which are utilized by the {@link RuleAction}.
 59  
  *
 60  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 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  0
         private WebRuleUtils() {
 75  0
                 throw new UnsupportedOperationException("do not call");
 76  
         }
 77  
         
 78  
         /**
 79  
          * Copies the existing rule onto the current document.  This is used within the web-based rule GUI to make a
 80  
          * copy of a rule on the existing document.  Essentially, this method makes a copy of the rule and all
 81  
          * delegates but preserves the document ID of the original rule.
 82  
          */
 83  
     public static WebRuleBaseValues copyRuleOntoExistingDocument(WebRuleBaseValues rule) throws Exception {
 84  0
         WebRuleBaseValues ruleCopy = new WebRuleBaseValues();
 85  0
         PropertyUtils.copyProperties(ruleCopy, rule);
 86  0
         ruleCopy.setPreviousVersionId(null);
 87  0
         ruleCopy.setCurrentInd(null);
 88  0
         ruleCopy.setVersionNbr(null);
 89  
 
 90  0
         List responsibilities = new ArrayList();
 91  0
         for (Iterator iter = ruleCopy.getResponsibilities().iterator(); iter.hasNext();) {
 92  0
             WebRuleResponsibility responsibility = (WebRuleResponsibility) iter.next();
 93  0
             WebRuleResponsibility responsibilityCopy = new WebRuleResponsibility();
 94  0
             PropertyUtils.copyProperties(responsibilityCopy, responsibility);
 95  
 
 96  0
             responsibilityCopy.setResponsibilityId(null);
 97  0
             responsibilityCopy.setRuleResponsibilityKey(null);
 98  
             
 99  0
             List delegations = new ArrayList();
 100  0
             for (Iterator iterator = responsibilityCopy.getDelegationRules().iterator(); iterator.hasNext();) {
 101  0
                 RuleDelegation delegation = (RuleDelegation) iterator.next();
 102  0
                 RuleDelegation delegationCopy = new RuleDelegation();
 103  0
                 PropertyUtils.copyProperties(delegationCopy, delegation);
 104  
 
 105  0
                 delegationCopy.setDelegateRuleId(null);
 106  0
                 delegationCopy.setVersionNumber(null);
 107  0
                 delegationCopy.setRuleDelegationId(null);
 108  0
                 delegationCopy.setResponsibilityId(null);
 109  
 
 110  0
                 WebRuleBaseValues delegationRule = ((WebRuleBaseValues) delegation.getDelegationRuleBaseValues());
 111  0
                 WebRuleBaseValues ruleDelegateCopy = new WebRuleBaseValues();
 112  0
                 PropertyUtils.copyProperties(ruleDelegateCopy, delegationRule);
 113  
 
 114  0
                 ruleDelegateCopy.setPreviousVersionId(null);
 115  0
                 ruleDelegateCopy.setCurrentInd(null);
 116  0
                 ruleDelegateCopy.setVersionNbr(null);
 117  
 
 118  0
                 List delegateResps = new ArrayList();
 119  0
                 for (Iterator iterator1 = ruleDelegateCopy.getResponsibilities().iterator(); iterator1.hasNext();) {
 120  0
                     WebRuleResponsibility delegateResp = (WebRuleResponsibility) iterator1.next();
 121  0
                     WebRuleResponsibility delegateRespCopy = new WebRuleResponsibility();
 122  0
                     PropertyUtils.copyProperties(delegateRespCopy, delegateResp);
 123  
 
 124  0
                     delegateRespCopy.setResponsibilityId(null);
 125  0
                     delegateRespCopy.setRuleResponsibilityKey(null);
 126  0
                     delegateResps.add(delegateRespCopy);
 127  0
                 }
 128  0
                 ruleDelegateCopy.setResponsibilities(delegateResps);
 129  0
                 delegationCopy.setDelegationRuleBaseValues(ruleDelegateCopy);
 130  0
                 delegations.add(delegationCopy);
 131  0
             }
 132  
             //responsibilityCopy.setDelegationRules(delegations);
 133  0
             responsibilities.add(responsibilityCopy);
 134  0
         }
 135  0
         ruleCopy.setResponsibilities(responsibilities);
 136  0
         return ruleCopy;
 137  
     }
 138  
     
 139  
     /**
 140  
      * Makes a copy of the rule and clears the document id on the rule and any of its delegates.
 141  
      * This method is used for making a copy of a rule for a new document.  It essentially calls
 142  
      * the copyRuleOntoExistingDocument method and then clears out the document IDs.
 143  
      * 
 144  
      * @param webRuleBaseValues
 145  
      */
 146  
     public static WebRuleBaseValues copyToNewRule(WebRuleBaseValues webRuleBaseValues) throws Exception {
 147  0
             WebRuleBaseValues newRule = copyRuleOntoExistingDocument(webRuleBaseValues);
 148  
             // clear out all document IDs on the rule and it's delegates
 149  0
             newRule.setDocumentId(null);
 150  0
             for (Iterator iterator = newRule.getResponsibilities().iterator(); iterator.hasNext(); ) {
 151  0
                         RuleResponsibility responsibility = (RuleResponsibility) iterator.next();
 152  0
                         for (Iterator iterator2 = responsibility.getDelegationRules().iterator(); iterator2.hasNext(); ) {
 153  0
                                 RuleDelegation delegation = (RuleDelegation) iterator2.next();
 154  0
                                 delegation.getDelegationRuleBaseValues().setDocumentId(null);
 155  0
                         }
 156  0
                 }
 157  0
             return newRule;
 158  
     }
 159  
 
 160  
     public static void validateRuleTemplateAndDocumentType(RuleBaseValues oldRule, RuleBaseValues newRule, Map<String, String[]> parameters) {
 161  0
                 String[] ruleTemplateIds = parameters.get(RULE_TEMPLATE_ID_PARAM);
 162  0
                 String[] ruleTemplateNames = parameters.get(RULE_TEMPLATE_NAME_PARAM);
 163  0
                 String[] documentTypeNames = parameters.get(DOCUMENT_TYPE_NAME_PARAM);
 164  0
                 if (ArrayUtils.isEmpty(ruleTemplateIds) && ArrayUtils.isEmpty(ruleTemplateNames)) {
 165  0
                         throw new RiceRuntimeException("Rule document must be initiated with a valid rule template id or rule template name.");
 166  
                 }
 167  0
                 if (ArrayUtils.isEmpty(documentTypeNames)) {
 168  0
                         throw new RiceRuntimeException("Rule document must be initiated with a valid document type name.");
 169  
                 }
 170  0
                 RuleTemplate ruleTemplate = null;
 171  0
                 if (!ArrayUtils.isEmpty(ruleTemplateIds)) {
 172  0
                         String ruleTemplateId = ruleTemplateIds[0];
 173  0
                         ruleTemplate = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateId(new Long(ruleTemplateId));
 174  0
                         if (ruleTemplate == null) {
 175  0
                                 throw new RiceRuntimeException("Failed to load rule template with id '" + ruleTemplateId + "'");
 176  
                         }
 177  
                 }
 178  0
                 if (ruleTemplate == null) {
 179  0
                         String ruleTemplateName = ruleTemplateNames[0];
 180  0
                         ruleTemplate = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(ruleTemplateName);
 181  0
                         if (ruleTemplate == null) {
 182  0
                                 throw new RiceRuntimeException("Failed to load rule template with name '" + ruleTemplateName + "'");
 183  
                         }
 184  
                 }
 185  0
                 String documentTypeName = documentTypeNames[0];
 186  0
                 DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findByName(documentTypeName);
 187  0
                 if (documentType == null) {
 188  0
                         throw new RiceRuntimeException("Failed to locate document type with name '" + documentTypeName + "'");
 189  
                 }
 190  
                 
 191  
                 // it appears that there is always an old maintainable, even in the case of a new document creation,
 192  
                 // if we don't initialize both the old and new versions we get errors during meshSections
 193  0
                 initializeRuleAfterNew(oldRule, ruleTemplate, documentTypeName);
 194  0
                 initializeRuleAfterNew(newRule, ruleTemplate, documentTypeName);
 195  0
         }
 196  
     
 197  
         private static void initializeRuleAfterNew(RuleBaseValues rule, RuleTemplate ruleTemplate, String documentTypeName) {
 198  0
                 rule.setRuleTemplate(ruleTemplate);
 199  0
                 rule.setRuleTemplateId(ruleTemplate.getRuleTemplateId());
 200  0
                 rule.setDocTypeName(documentTypeName);
 201  0
         }
 202  
         
 203  
         public static void validateRuleAndResponsibility(RuleDelegation oldRuleDelegation, RuleDelegation newRuleDelegation, Map<String, String[]> parameters) {
 204  0
                 String[] responsibilityIds = parameters.get(RESPONSIBILITY_ID_PARAM);
 205  0
                 if (ArrayUtils.isEmpty(responsibilityIds)) {
 206  0
                         throw new RiceRuntimeException("Delegation rule document must be initiated with a valid responsibility ID to delegate from.");
 207  
                 }
 208  0
                 if (!ArrayUtils.isEmpty(responsibilityIds)) {
 209  0
                         Long responsibilityId = new Long(responsibilityIds[0]);
 210  0
                         RuleResponsibility ruleResponsibility = KEWServiceLocator.getRuleService().findRuleResponsibility(responsibilityId);
 211  0
                         if (ruleResponsibility == null) {
 212  0
                                 throw new RiceRuntimeException("Failed to locate a rule responsibility for responsibility ID " + responsibilityId);
 213  
                         }
 214  0
                         oldRuleDelegation.setResponsibilityId(responsibilityId);
 215  0
                         newRuleDelegation.setResponsibilityId(responsibilityId);
 216  
                 }
 217  
                 
 218  0
         }
 219  
 
 220  
         public static void establishDefaultRuleValues(RuleBaseValues rule) {
 221  0
                 rule.setActiveInd(true);
 222  
 
 223  0
         RuleBaseValues defaultRule = ((RuleService) KEWServiceLocator.getService(KEWServiceLocator.RULE_SERVICE)).findDefaultRuleByRuleTemplateId(
 224  
                         rule.getRuleTemplate().getDelegationTemplateId());
 225  0
         if (defaultRule != null) {
 226  0
             defaultRule.setActivationDate(null);
 227  0
             defaultRule.setCurrentInd(null);
 228  0
             defaultRule.setDeactivationDate(null);
 229  0
             defaultRule.setDocTypeName(null);
 230  0
             defaultRule.setVersionNumber(null);
 231  0
             defaultRule.setRuleBaseValuesId(null);
 232  0
             defaultRule.setTemplateRuleInd(Boolean.FALSE);
 233  0
             defaultRule.setVersionNbr(null);
 234  
             try {
 235  0
                                 PropertyUtils.copyProperties(rule, defaultRule);
 236  0
                         } catch (IllegalAccessException e) {
 237  0
                                 throw new RuntimeException(e);
 238  0
                         } catch (InvocationTargetException e) {
 239  0
                                 throw new RuntimeException(e);
 240  0
                         } catch (NoSuchMethodException e) {
 241  0
                                 throw new RuntimeException(e);
 242  0
                         }
 243  
         }
 244  0
         }
 245  
         
 246  
 
 247  
         public static List customizeSections(RuleBaseValues rule, List<Section> sections, boolean delegateRule) {
 248  
 
 249  0
                 List<Section> finalSections = new ArrayList<Section>();
 250  0
                 for (Section section : sections) {
 251  
                         // unfortunately, in the case of an inquiry the sectionId will always be null so we have to check section title
 252  0
                         if (section.getSectionTitle().equals(RULE_ATTRIBUTES_SECTION_TITLE) || 
 253  
                                         RULE_ATTRIBUTES_SECTION_ID.equals(section.getSectionId())) {
 254  0
                                 List<Row> ruleTemplateRows = getRuleTemplateRows(rule, delegateRule);
 255  0
                                 if (!ruleTemplateRows.isEmpty()) {
 256  0
                                         section.setRows(ruleTemplateRows);
 257  0
                                         finalSections.add(section);
 258  
                                 }
 259  0
                         } else if (ROLES_MAINTENANCE_SECTION_ID.equals(section.getSectionId())) {
 260  0
                                 if (hasRoles(rule)) {
 261  0
                                         finalSections.add(section);
 262  
                                 }
 263  
                         } else {
 264  0
                                 finalSections.add(section);
 265  
                         }
 266  
                 }
 267  
                 
 268  0
                 return finalSections;
 269  
         }
 270  
         
 271  
         
 272  
         
 273  
 
 274  
         public static List<Row> getRuleTemplateRows(RuleBaseValues rule, boolean delegateRule) {
 275  
 
 276  0
                 List<Row> rows = new ArrayList<Row>();
 277  0
                 RuleTemplate ruleTemplate = rule.getRuleTemplate();
 278  0
                 Map<String, String> fieldNameMap = new HashMap<String, String>();
 279  
                 // refetch rule template from service because after persistence in KNS, it comes back without any rule template attributes
 280  0
                 if (ruleTemplate != null){
 281  0
                         ruleTemplate = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateId(ruleTemplate.getRuleTemplateId());
 282  0
                         if (ruleTemplate != null) {
 283  
                                 
 284  0
                                 List<RuleTemplateAttribute> ruleTemplateAttributes = ruleTemplate.getActiveRuleTemplateAttributes();
 285  0
                                 Collections.sort(ruleTemplateAttributes);
 286  
 
 287  0
                                 for (RuleTemplateAttribute ruleTemplateAttribute : ruleTemplateAttributes) {
 288  0
                                         if (!ruleTemplateAttribute.isWorkflowAttribute()) {
 289  0
                                                 continue;
 290  
                                         }
 291  0
                                         WorkflowAttribute workflowAttribute = ruleTemplateAttribute.getWorkflowAttribute();
 292  0
                                         RuleAttribute ruleAttribute = ruleTemplateAttribute.getRuleAttribute();
 293  0
                                         if (ruleAttribute.getType().equals(KEWConstants.RULE_XML_ATTRIBUTE_TYPE)) {
 294  0
                                                 ((GenericXMLRuleAttribute) workflowAttribute).setRuleAttribute(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  
                                                 // set the transformed value
 317  0
                                                 transformedFieldConversions.put(lookupFieldName, fieldNameMap.get(localFieldName));
 318  
                                         } else {
 319  
                                                 // set the original value (not sure if this case will happen, but just in case)
 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
                 RuleTemplate ruleTemplate = rule.getRuleTemplate();
 333  0
                 return !ruleTemplate.getRoles().isEmpty();
 334  
         }
 335  
         
 336  
         /**
 337  
          * Processes the Fields on the various attributes Rows to assign an appropriate field name to them so that the
 338  
          * field name rendered in the maintenance HTML will properly assign the value to RuleBaseValues.fieldValues.
 339  
          */
 340  
         
 341  
         public static List<Row> transformAndPopulateAttributeRows(List<Row> attributeRows, RuleTemplateAttribute 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.getRuleTemplateAttributeId() + 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  
          * Since editing of a Rule should actually result in a rule with a new ID and new
 368  
          * entries in the rule and rule responsibility tables, we need to clear out
 369  
          * the primary keys of the rule and related objects.
 370  
          */
 371  
         public static void clearKeysForSave(RuleBaseValues rule) {
 372  0
                 rule.setRuleBaseValuesId(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(RuleDelegation ruleDelegation) {
 382  0
                 ruleDelegation.setRuleDelegationId(null);
 383  0
                 ruleDelegation.setObjectId(null);
 384  0
                 ruleDelegation.setVersionNumber(0L);
 385  0
                 clearKeysForSave(ruleDelegation.getDelegationRuleBaseValues());
 386  0
         }
 387  
         
 388  
     public static void translateResponsibilitiesForSave(RuleBaseValues rule) {
 389  0
                 rule.getResponsibilities().clear();
 390  0
                 for (PersonRuleResponsibility responsibility : rule.getPersonResponsibilities()) {
 391  0
                         RuleResponsibility ruleResponsibility = new RuleResponsibility();
 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(KEWConstants.RULE_RESPONSIBILITY_WORKFLOW_ID);
 401  
                         // default the approve policy to First Approve
 402  0
                         ruleResponsibility.setApprovePolicy(KEWConstants.APPROVE_POLICY_FIRST_APPROVE);
 403  0
                         rule.getResponsibilities().add(ruleResponsibility);
 404  0
                 }
 405  0
                 for (GroupRuleResponsibility responsibility : rule.getGroupResponsibilities()) {
 406  0
                         RuleResponsibility ruleResponsibility = new RuleResponsibility();
 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(KEWConstants.RULE_RESPONSIBILITY_GROUP_ID);
 416  0
                         ruleResponsibility.setApprovePolicy(KEWConstants.APPROVE_POLICY_FIRST_APPROVE);
 417  0
                         rule.getResponsibilities().add(ruleResponsibility);
 418  0
                 }
 419  0
                 for (RoleRuleResponsibility responsibility : rule.getRoleResponsibilities()) {
 420  0
                         RuleResponsibility ruleResponsibility = new RuleResponsibility();
 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(KEWConstants.RULE_RESPONSIBILITY_ROLE_ID);
 429  0
                         ruleResponsibility.setApprovePolicy(responsibility.getApprovePolicy());
 430  0
                         rule.getResponsibilities().add(ruleResponsibility);
 431  0
                 }
 432  0
         }
 433  
     
 434  
     public static void translateFieldValuesForSave(RuleBaseValues rule) {
 435  0
             RuleTemplate ruleTemplate = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateId(rule.getRuleTemplateId());
 436  
 
 437  
                 /** Populate rule extension values * */
 438  0
                 List extensions = new ArrayList();
 439  0
                 for (Iterator iterator = ruleTemplate.getActiveRuleTemplateAttributes().iterator(); iterator.hasNext();) {
 440  0
                         RuleTemplateAttribute ruleTemplateAttribute = (RuleTemplateAttribute) iterator.next();
 441  0
                         if (!ruleTemplateAttribute.isWorkflowAttribute()) {
 442  0
                                 continue;
 443  
                         }
 444  0
                         WorkflowAttribute workflowAttribute = ruleTemplateAttribute.getWorkflowAttribute();
 445  
 
 446  0
                         RuleAttribute ruleAttribute = ruleTemplateAttribute.getRuleAttribute();
 447  0
                         if (ruleAttribute.getType().equals(KEWConstants.RULE_XML_ATTRIBUTE_TYPE)) {
 448  0
                                 ((GenericXMLRuleAttribute) workflowAttribute).setRuleAttribute(ruleAttribute);
 449  
                         }
 450  
 
 451  0
                         Map<String, String> parameterMap = getFieldMapForRuleTemplateAttribute(rule, ruleTemplateAttribute);
 452  
                                                 
 453  
                         // validate rule data populates the rule extension values for us
 454  0
                         List attValidationErrors = workflowAttribute.validateRuleData(parameterMap);
 455  
 
 456  
                         // because validation should be handled by business rules now, if we encounter a validation error at this point in
 457  
                         // time, let's throw an exception
 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
                                 RuleExtension ruleExtension = new RuleExtension();
 465  0
                                 ruleExtension.setRuleTemplateAttributeId(ruleTemplateAttribute.getRuleTemplateAttributeId());
 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
                         RuleExtension ruleExtension = (RuleExtension) iterator.next();
 476  0
                         ruleExtension.setRuleBaseValues(rule);
 477  
 
 478  0
                         for (Iterator iterator2 = ruleTemplate.getActiveRuleTemplateAttributes().iterator(); iterator2.hasNext();) {
 479  0
                                 RuleTemplateAttribute ruleTemplateAttribute = (RuleTemplateAttribute) iterator2.next();
 480  0
                                 if (ruleTemplateAttribute.getRuleTemplateAttributeId().longValue() == ruleExtension.getRuleTemplateAttributeId().longValue()) {
 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  
      * Based on original logic implemented in Rule system.  Essentially constructs a Map of field values related
 495  
      * to the given RuleTemplateAttribute.
 496  
      */
 497  
     public static Map<String, String> getFieldMapForRuleTemplateAttribute(RuleBaseValues rule, RuleTemplateAttribute 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.getRuleTemplateAttributeId().toString().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(RuleDelegation ruleDelegation) {
 510  0
             RuleBaseValues rule = ruleDelegation.getDelegationRuleBaseValues();
 511  0
             rule.setDelegateRule(true);
 512  
             // certain items on a delegated rule responsibility are inherited from parent responsibility, set them to null
 513  0
             for (RuleResponsibility responsibility : rule.getResponsibilities()) {
 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  
                 // in the case of copy, our fields which are marked read only are cleared, this includes the rule template
 523  
                 // name and the document type name but we don't want these cleared
 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  
          * This method populates fields on RuleBaseValues which are used only for
 534  
          * maintenance purposes.  In otherwords, it populates the non-persistent fields
 535  
          * on the RuleBaseValues which the maintenance document needs to function
 536  
          * (such as the extension field values and responsibilities).
 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 (RuleResponsibility responsibility : rule.getResponsibilities()) {
 545  0
                         if (responsibility.getRuleResponsibilityType().equals(KEWConstants.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(KEWConstants.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(KEWConstants.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.getRuleResponsibilityKey() + "' contained a bad type code of '" + responsibility.getRuleResponsibilityType());
 564  
                         }
 565  
                 }
 566  
                 // since we've loaded the responsibilities, let's clear the originals so they don't get serialized to the maint doc XML
 567  0
                 rule.getResponsibilities().clear();
 568  0
         }
 569  
         
 570  
         public static void copyResponsibility(RuleResponsibility source, RuleResponsibility 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 (RuleExtension ruleExtension : rule.getRuleExtensions()) {
 580  0
                         Long 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  
                 // since we've loaded the extensions, let's clear the originals so that they don't get serialized to the maint doc XML
 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.setRuleBaseValuesId(null);
 598  0
             rule.setPreviousVersionId(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(RuleResponsibility responsibility) {
 613  0
                 responsibility.setResponsibilityId(null);
 614  0
                 responsibility.setRuleResponsibilityKey(null);
 615  0
                 responsibility.setRuleBaseValuesId(null);
 616  0
     }
 617  
     
 618  
 }