Coverage Report - org.kuali.rice.kew.document.RoutingRuleMaintainableBusRule
 
Classes in this File Line Coverage Branch Coverage Complexity
RoutingRuleMaintainableBusRule
0%
0/107
0%
0/64
3.692
 
 1  
 /*
 2  
  * Copyright 2007-2009 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kew.document;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Iterator;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 
 23  
 import org.kuali.rice.kew.doctype.service.DocumentTypeService;
 24  
 import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
 25  
 import org.kuali.rice.kew.rule.GroupRuleResponsibility;
 26  
 import org.kuali.rice.kew.rule.PersonRuleResponsibility;
 27  
 import org.kuali.rice.kew.rule.RuleBaseValues;
 28  
 import org.kuali.rice.kew.rule.RuleDelegation;
 29  
 import org.kuali.rice.kew.rule.RuleResponsibility;
 30  
 import org.kuali.rice.kew.rule.WorkflowAttribute;
 31  
 import org.kuali.rice.kew.rule.bo.RuleAttribute;
 32  
 import org.kuali.rice.kew.rule.bo.RuleTemplate;
 33  
 import org.kuali.rice.kew.rule.bo.RuleTemplateAttribute;
 34  
 import org.kuali.rice.kew.rule.web.WebRuleUtils;
 35  
 import org.kuali.rice.kew.rule.xmlrouting.GenericXMLRuleAttribute;
 36  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 37  
 import org.kuali.rice.kew.util.KEWConstants;
 38  
 import org.kuali.rice.kew.util.KEWPropertyConstants;
 39  
 import org.kuali.rice.kns.bo.PersistableBusinessObject;
 40  
 import org.kuali.rice.kns.document.MaintenanceDocument;
 41  
 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
 42  
 
 43  
 
 44  
 
 45  
 /**
 46  
  * This is a description of what this class does - Garey don't forget to fill this in.
 47  
  *
 48  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 49  
  *
 50  
  */
 51  0
 public class RoutingRuleMaintainableBusRule extends MaintenanceDocumentRuleBase {
 52  
 
 53  
         /**
 54  
          * This overridden method ...
 55  
          *
 56  
          * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
 57  
          */
 58  
         @Override
 59  
         protected boolean processCustomSaveDocumentBusinessRules(
 60  
                         MaintenanceDocument document) {
 61  
 
 62  0
                 boolean isValid = true;
 63  
 
 64  0
                 RuleBaseValues ruleBaseValues = this.getRuleBaseValues(document);
 65  0
                 RuleBaseValues oldRuleBaseValues = this.getOldRuleBaseValues(document);
 66  
                 
 67  0
                 if (oldRuleBaseValues != null) {
 68  0
                         ruleBaseValues.setPreviousVersionId(oldRuleBaseValues.getRuleBaseValuesId());
 69  
         }
 70  0
                 isValid &= this.populateErrorMap(ruleBaseValues);
 71  
 
 72  
 
 73  0
                 return isValid;
 74  
         }
 75  
 
 76  
         protected RuleBaseValues getRuleBaseValues(MaintenanceDocument document){
 77  0
                 return (RuleBaseValues)document.getNewMaintainableObject().getBusinessObject();
 78  
         }
 79  
         
 80  
         protected RuleBaseValues getOldRuleBaseValues(MaintenanceDocument document){
 81  0
                 return (RuleBaseValues)document.getOldMaintainableObject().getBusinessObject();
 82  
         }
 83  
         
 84  
 
 85  
         protected void populateErrorMap(Map<String,String> errorMap){
 86  0
                 for(Map.Entry<String, String> entry : errorMap.entrySet()){
 87  0
                         this.putFieldError(entry.getKey(), entry.getValue());
 88  
                 }
 89  0
         }
 90  
 
 91  
         /**
 92  
          * This overridden method ...
 93  
          *
 94  
          * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomAddCollectionLineBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument, java.lang.String, org.kuali.rice.kns.bo.PersistableBusinessObject)
 95  
          */
 96  
         @Override
 97  
         public boolean processCustomAddCollectionLineBusinessRules(
 98  
                         MaintenanceDocument document, String collectionName,
 99  
                         PersistableBusinessObject line) {
 100  
 
 101  0
                 boolean isValid = true;
 102  
 
 103  0
                 if(getPersonSectionName().equals(collectionName)){
 104  0
                         PersonRuleResponsibility pr = (PersonRuleResponsibility)line;
 105  0
                         String name = pr.getPrincipalName();
 106  
 
 107  0
                         if(!personExists(name)){
 108  0
                                 isValid &= false;
 109  0
                                 this.putFieldError(getPersonSectionName(), "error.document.personResponsibilities.principleDoesNotExist");
 110  
                         }
 111  0
                 }else if(getGroupSectionName().equals(collectionName)){
 112  0
                         GroupRuleResponsibility gr = (GroupRuleResponsibility)line;
 113  0
                         if(!groupExists(gr.getNamespaceCode(), gr.getName())){
 114  0
                                 isValid &= false;
 115  0
                                 this.putFieldError(getGroupSectionName(), "error.document.personResponsibilities.groupDoesNotExist");
 116  
                         }
 117  
                 }
 118  
 
 119  0
                 return isValid;
 120  
         }
 121  
 
 122  
         protected String getPersonSectionName(){
 123  0
                 return KEWPropertyConstants.PERSON_RESP_SECTION;
 124  
         }
 125  
         protected String getGroupSectionName(){
 126  0
                 return KEWPropertyConstants.GROUP_RESP_SECTION;
 127  
         }
 128  
 
 129  
         protected boolean personExists(String principalName){
 130  0
                 boolean bRet = false;
 131  
                 try{
 132  0
                         KEWServiceLocator.getIdentityHelperService().getIdForPrincipalName(principalName);
 133  0
                         bRet = true;
 134  0
                 }catch(Exception ex){
 135  0
                         bRet = false;
 136  
                         //ex.printStackTrace();
 137  0
                 }
 138  
 
 139  0
                 return bRet;
 140  
         }
 141  
 
 142  
         protected boolean groupExists(String namespaceCode, String groupName){
 143  0
                 boolean bRet = false;
 144  
                 try{
 145  0
                         KEWServiceLocator.getIdentityHelperService().getGroupByName(namespaceCode, groupName);
 146  0
                         bRet = true;
 147  0
                 }catch(Exception ex){
 148  0
                         bRet = false;
 149  
                         //ex.printStackTrace();
 150  0
                 }
 151  0
                 return bRet;
 152  
         }
 153  
 
 154  
         protected boolean populateErrorMap(RuleBaseValues ruleBaseValues){
 155  
 
 156  0
                 boolean isValid = true;
 157  
 
 158  0
                 if (getDocumentTypeService().findByName(ruleBaseValues.getDocTypeName()) == null) {
 159  0
             this.putFieldError("docTypeName", "doctype.documenttypeservice.doctypename.required");
 160  0
             isValid &= false;
 161  
         }
 162  0
         if (ruleBaseValues.getActiveInd() == null) {
 163  0
                 this.putFieldError("activeInd", "routetemplate.ruleservice.activeind.required");
 164  0
                 isValid &= false;
 165  
         }
 166  0
         if(ruleBaseValues.getName() != null){
 167  0
                 if(ruleExists(ruleBaseValues)){
 168  0
                         this.putFieldError("name", "routetemplate.ruleservice.name.unique");
 169  0
                     isValid &= false;
 170  
                 }
 171  
         }
 172  
 
 173  
         /*
 174  
          * Logic: If both from and to dates exist, make sure toDate is after fromDate
 175  
          */
 176  0
         if(ruleBaseValues.getToDate() != null && ruleBaseValues.getFromDate() != null){
 177  0
                 if (ruleBaseValues.getToDate().before(ruleBaseValues.getFromDate())) {
 178  0
                             this.putFieldError("toDate", "error.document.maintainableItems.toDate");
 179  0
                             isValid &= false;
 180  
             }
 181  
         }
 182  
 
 183  0
                 if (ruleBaseValues.getForceAction() == null) {
 184  0
                         this.putFieldError("forceAction", "routetemplate.ruleservice.forceAction.required");
 185  0
                         isValid &= false;
 186  
         }
 187  
 
 188  
 
 189  0
                 if(!setRuleAttributeErrors(ruleBaseValues)){
 190  0
                         isValid &= false;
 191  
                 }
 192  
 
 193  
                 // This doesn't map directly to a single field. It's either the person or the group tab
 194  0
         if (ruleBaseValues.getResponsibilities().isEmpty()) {
 195  0
                 this.putFieldError("Responsibilities", "error.document.responsibility.required");
 196  0
                 isValid &= false;
 197  
         } else {
 198  0
             for (Iterator<RuleResponsibility> iter = ruleBaseValues.getResponsibilities().iterator(); iter.hasNext();) {
 199  0
                 RuleResponsibility responsibility = iter.next();
 200  0
                 if (responsibility.getRuleResponsibilityName() != null && KEWConstants.RULE_RESPONSIBILITY_GROUP_ID.equals(responsibility.getRuleResponsibilityType())) {
 201  0
                     if (getIdentityManagementService().getGroup(responsibility.getRuleResponsibilityName()) == null) {
 202  0
                             this.putFieldError("Groups", "routetemplate.ruleservice.workgroup.invalid");
 203  0
                             isValid &= false;
 204  
                     }
 205  0
                 } else if (responsibility.getPrincipal() == null && responsibility.getRole() == null) {
 206  0
                         this.putFieldError("Persons", "routetemplate.ruleservice.user.invalid");
 207  0
                         isValid &= false;
 208  
                 }
 209  0
             }
 210  
         }
 211  
 
 212  0
         return isValid;
 213  
         }
 214  
 
 215  
         protected boolean ruleExists(RuleBaseValues rule){
 216  0
                 boolean bRet = false;
 217  
 
 218  0
                 RuleBaseValues tmp = KEWServiceLocator.getRuleService().getRuleByName(rule.getName());
 219  
 
 220  0
                 if(tmp != null) {
 221  0
                     if ((rule.getPreviousVersionId() == null) 
 222  
                          || (rule.getPreviousVersionId() != null
 223  
                             && !rule.getPreviousVersionId().equals(tmp.getRuleBaseValuesId()))) {
 224  0
                             bRet = true;
 225  
                     }
 226  
                 }
 227  
 
 228  0
                 return bRet;
 229  
         }
 230  
 
 231  
         protected DocumentTypeService getDocumentTypeService() {
 232  0
         return (DocumentTypeService) KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE);
 233  
     }
 234  
 
 235  
 
 236  
         protected boolean setRuleAttributeErrors(RuleBaseValues rule){
 237  
 
 238  0
                 boolean isValid = true;
 239  
 
 240  0
                 RuleTemplate ruleTemplate = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateId(rule.getRuleTemplateId());
 241  
 
 242  
                 /** Populate rule extension values * */
 243  0
                 List extensions = new ArrayList();
 244  0
                 for (Iterator iterator = ruleTemplate.getActiveRuleTemplateAttributes().iterator(); iterator.hasNext();) {
 245  0
                         RuleTemplateAttribute ruleTemplateAttribute = (RuleTemplateAttribute) iterator.next();
 246  0
                         if (!ruleTemplateAttribute.isWorkflowAttribute()) {
 247  0
                                 continue;
 248  
                         }
 249  0
                         WorkflowAttribute workflowAttribute = ruleTemplateAttribute.getWorkflowAttribute();
 250  
 
 251  0
                         RuleAttribute ruleAttribute = ruleTemplateAttribute.getRuleAttribute();
 252  0
                         if (ruleAttribute.getType().equals(KEWConstants.RULE_XML_ATTRIBUTE_TYPE)) {
 253  0
                                 ((GenericXMLRuleAttribute) workflowAttribute).setRuleAttribute(ruleAttribute);
 254  
                         }
 255  
 
 256  0
                         Map<String, String> parameterMap = WebRuleUtils.getFieldMapForRuleTemplateAttribute(rule, ruleTemplateAttribute);
 257  
 
 258  
                         // validate rule data populates the rule extension values for us
 259  0
                         List<WorkflowServiceErrorImpl> attValidationErrors = null;
 260  
                         try{
 261  0
                                 attValidationErrors = workflowAttribute.validateRuleData(parameterMap);
 262  0
                         }catch(Exception ex){
 263  0
                                 isValid = false;
 264  0
                                 this.putFieldError("RuleAttributes", "routetemplate.xmlattribute.required.error");
 265  
                                 //ex.printStackTrace();
 266  0
                         }
 267  
                         // TODO hook validation of rule data into PreRules
 268  0
                          if (attValidationErrors != null && !attValidationErrors.isEmpty()) {
 269  0
                                  isValid = false;
 270  0
                                  for(WorkflowServiceErrorImpl error: attValidationErrors){
 271  0
                                          this.putFieldError("RuleAttributes", error.getKey(), error.getArg1());
 272  
                                  }
 273  
                          }
 274  0
                 }
 275  0
                 return isValid;
 276  
 
 277  
         }
 278  
 
 279  
 }