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