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