Coverage Report - org.kuali.rice.kew.document.RoutingRuleMaintainableBusRule
 
Classes in this File Line Coverage Branch Coverage Complexity
RoutingRuleMaintainableBusRule
0%
0/90
0%
0/56
3.308
 
 1  
 /**
 2  
  * Copyright 2005-2011 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.api.KEWPropertyConstants;
 19  
 import org.kuali.rice.kew.api.KewApiServiceLocator;
 20  
 import org.kuali.rice.kew.api.rule.RuleTemplate;
 21  
 import org.kuali.rice.kew.api.rule.RuleTemplateAttribute;
 22  
 import org.kuali.rice.kew.api.validation.ValidationResults;
 23  
 import org.kuali.rice.kew.doctype.service.DocumentTypeService;
 24  
 import org.kuali.rice.kew.framework.KewFrameworkServiceLocator;
 25  
 import org.kuali.rice.kew.framework.rule.attribute.WorkflowRuleAttributeHandlerService;
 26  
 import org.kuali.rice.kew.rule.GroupRuleResponsibility;
 27  
 import org.kuali.rice.kew.rule.PersonRuleResponsibility;
 28  
 import org.kuali.rice.kew.rule.RuleBaseValues;
 29  
 import org.kuali.rice.kew.rule.RuleResponsibilityBo;
 30  
 import org.kuali.rice.kew.rule.web.WebRuleUtils;
 31  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 32  
 import org.kuali.rice.kew.api.KewApiConstants;
 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.Map;
 38  
 
 39  
 /**
 40  
  * This is a description of what this class does - Garey don't forget to fill this in.
 41  
  *
 42  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 43  
  *
 44  
  */
 45  0
 public class RoutingRuleMaintainableBusRule extends MaintenanceDocumentRuleBase {
 46  
 
 47  
         /**
 48  
          * This overridden method ...
 49  
          *
 50  
          * @see org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.rice.krad.document.MaintenanceDocument)
 51  
          */
 52  
         @Override
 53  
         protected boolean processCustomSaveDocumentBusinessRules(
 54  
                         MaintenanceDocument document) {
 55  
 
 56  0
                 boolean isValid = true;
 57  
 
 58  0
                 RuleBaseValues ruleBaseValues = this.getRuleBaseValues(document);
 59  0
                 RuleBaseValues oldRuleBaseValues = this.getOldRuleBaseValues(document);
 60  
                 
 61  0
                 if (oldRuleBaseValues != null) {
 62  0
                         ruleBaseValues.setPreviousRuleId(oldRuleBaseValues.getId());
 63  
         }
 64  0
                 isValid &= this.populateErrorMap(ruleBaseValues);
 65  
 
 66  
 
 67  0
                 return isValid;
 68  
         }
 69  
 
 70  
         protected RuleBaseValues getRuleBaseValues(MaintenanceDocument document){
 71  0
                 return (RuleBaseValues)document.getNewMaintainableObject().getBusinessObject();
 72  
         }
 73  
         
 74  
         protected RuleBaseValues getOldRuleBaseValues(MaintenanceDocument document){
 75  0
                 return (RuleBaseValues)document.getOldMaintainableObject().getBusinessObject();
 76  
         }
 77  
         
 78  
 
 79  
         protected void populateErrorMap(Map<String,String> errorMap){
 80  0
                 for(Map.Entry<String, String> entry : errorMap.entrySet()){
 81  0
                         this.putFieldError(entry.getKey(), entry.getValue());
 82  
                 }
 83  0
         }
 84  
 
 85  
         /**
 86  
          * This overridden method ...
 87  
          *
 88  
          * @see org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase#processCustomAddCollectionLineBusinessRules(org.kuali.rice.krad.document.MaintenanceDocument, java.lang.String, org.kuali.rice.krad.bo.PersistableBusinessObject)
 89  
          */
 90  
         @Override
 91  
         public boolean processCustomAddCollectionLineBusinessRules(
 92  
                         MaintenanceDocument document, String collectionName,
 93  
                         PersistableBusinessObject line) {
 94  
 
 95  0
                 boolean isValid = true;
 96  
 
 97  0
                 if(getPersonSectionName().equals(collectionName)){
 98  0
                         PersonRuleResponsibility pr = (PersonRuleResponsibility)line;
 99  0
                         String name = pr.getPrincipalName();
 100  
 
 101  0
                         if(!personExists(name)){
 102  0
                                 isValid &= false;
 103  0
                                 this.putFieldError(getPersonSectionName(), "error.document.personResponsibilities.principleDoesNotExist");
 104  
                         }
 105  0
                 }else if(getGroupSectionName().equals(collectionName)){
 106  0
                         GroupRuleResponsibility gr = (GroupRuleResponsibility)line;
 107  0
                         if(!groupExists(gr.getNamespaceCode(), gr.getName())){
 108  0
                                 isValid &= false;
 109  0
                                 this.putFieldError(getGroupSectionName(), "error.document.personResponsibilities.groupDoesNotExist");
 110  
                         }
 111  
                 }
 112  
 
 113  0
                 return isValid;
 114  
         }
 115  
 
 116  
         protected String getPersonSectionName(){
 117  0
                 return KEWPropertyConstants.PERSON_RESP_SECTION;
 118  
         }
 119  
         protected String getGroupSectionName(){
 120  0
                 return KEWPropertyConstants.GROUP_RESP_SECTION;
 121  
         }
 122  
 
 123  
         protected boolean personExists(String principalName){
 124  0
                 boolean bRet = false;
 125  
                 try{
 126  0
                         KEWServiceLocator.getIdentityHelperService().getIdForPrincipalName(principalName);
 127  0
                         bRet = true;
 128  0
                 }catch(Exception ex){
 129  0
                         bRet = false;
 130  
                         //ex.printStackTrace();
 131  0
                 }
 132  
 
 133  0
                 return bRet;
 134  
         }
 135  
 
 136  
         protected boolean groupExists(String namespaceCode, String groupName){
 137  0
                 boolean bRet = false;
 138  
                 try{
 139  0
                         KEWServiceLocator.getIdentityHelperService().getGroupByName(namespaceCode, groupName);
 140  0
                         bRet = true;
 141  0
                 }catch(Exception ex){
 142  0
                         bRet = false;
 143  
                         //ex.printStackTrace();
 144  0
                 }
 145  0
                 return bRet;
 146  
         }
 147  
 
 148  
         protected boolean populateErrorMap(RuleBaseValues ruleBaseValues){
 149  
 
 150  0
                 boolean isValid = true;
 151  
 
 152  0
                 if (getDocumentTypeService().findByName(ruleBaseValues.getDocTypeName()) == null) {
 153  0
             this.putFieldError("docTypeName", "doctype.documenttypeservice.doctypename.required");
 154  0
             isValid &= false;
 155  
         }
 156  0
         if(ruleBaseValues.getName() != null){
 157  0
                 if(ruleExists(ruleBaseValues)){
 158  0
                         this.putFieldError("name", "routetemplate.ruleservice.name.unique");
 159  0
                     isValid &= false;
 160  
                 }
 161  
         }
 162  
 
 163  
         /*
 164  
          * Logic: If both from and to dates exist, make sure toDate is after fromDate
 165  
          */
 166  0
         if(ruleBaseValues.getToDateValue() != null && ruleBaseValues.getFromDateValue() != null){
 167  0
                 if (ruleBaseValues.getToDateValue().before(ruleBaseValues.getFromDateValue())) {
 168  0
                             this.putFieldError("toDate", "error.document.maintainableItems.toDate");
 169  0
                             isValid &= false;
 170  
             }
 171  
         }
 172  
 
 173  0
                 if(!setRuleAttributeErrors(ruleBaseValues)){
 174  0
                         isValid &= false;
 175  
                 }
 176  
 
 177  
                 // This doesn't map directly to a single field. It's either the person or the group tab
 178  0
         if (ruleBaseValues.getRuleResponsibilities().isEmpty()) {
 179  0
                 this.putFieldError("Responsibilities", "error.document.responsibility.required");
 180  0
                 isValid &= false;
 181  
         } else {
 182  0
             for (RuleResponsibilityBo responsibility : ruleBaseValues.getRuleResponsibilities()) {
 183  0
                 if (responsibility.getRuleResponsibilityName() != null && KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID.equals(responsibility.getRuleResponsibilityType())) {
 184  0
                     if (getGroupService().getGroup(responsibility.getRuleResponsibilityName()) == null) {
 185  0
                             this.putFieldError("Groups", "routetemplate.ruleservice.workgroup.invalid");
 186  0
                             isValid &= false;
 187  
                     }
 188  0
                 } else if (responsibility.getPrincipal() == null && responsibility.getRole() == null) {
 189  0
                         this.putFieldError("Persons", "routetemplate.ruleservice.user.invalid");
 190  0
                         isValid &= false;
 191  
                 }
 192  
             }
 193  
         }
 194  
 
 195  0
         return isValid;
 196  
         }
 197  
 
 198  
         protected boolean ruleExists(RuleBaseValues rule){
 199  0
                 boolean bRet = false;
 200  
 
 201  0
                 RuleBaseValues tmp = KEWServiceLocator.getRuleService().getRuleByName(rule.getName());
 202  
 
 203  0
                 if(tmp != null) {
 204  0
                     if ((rule.getPreviousRuleId() == null)
 205  
                          || (rule.getPreviousRuleId() != null
 206  
                             && !rule.getPreviousRuleId().equals(tmp.getId()))) {
 207  0
                             bRet = true;
 208  
                     }
 209  
                 }
 210  
 
 211  0
                 return bRet;
 212  
         }
 213  
 
 214  
         protected DocumentTypeService getDocumentTypeService() {
 215  0
         return (DocumentTypeService) KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE);
 216  
     }
 217  
 
 218  
 
 219  
         protected boolean setRuleAttributeErrors(RuleBaseValues rule){
 220  
 
 221  0
                 boolean isValid = true;
 222  
 
 223  0
                 RuleTemplate ruleTemplate = KewApiServiceLocator.getRuleService().getRuleTemplate(rule.getRuleTemplateId());
 224  
 
 225  
                 /** Populate rule extension values * */
 226  0
                 for (RuleTemplateAttribute ruleTemplateAttribute : ruleTemplate.getActiveRuleTemplateAttributes()) {
 227  0
             WorkflowRuleAttributeHandlerService wrahs = KewFrameworkServiceLocator.getWorkflowRuleAttributeHandlerService();
 228  
 
 229  0
                         Map<String, String> parameterMap = WebRuleUtils.getFieldMapForRuleTemplateAttribute(rule, ruleTemplateAttribute);
 230  
 
 231  0
                     ValidationResults validationResults = wrahs.validateRuleData(ruleTemplateAttribute.getRuleAttribute().getName(), parameterMap);
 232  
 
 233  0
             if (!validationResults.getErrors().isEmpty()) {
 234  0
                                 isValid = false;
 235  0
                                 this.putFieldError("RuleAttributes", "routetemplate.xmlattribute.required.error");
 236  
                         }
 237  
                         // TODO hook validation of rule data into PreRules
 238  0
                         if (validationResults.getErrors().isEmpty()) {
 239  0
                                 isValid = false;
 240  0
                                 for(Map.Entry<String, String> error: validationResults.getErrors().entrySet()){
 241  0
                                     this.putFieldError("RuleAttributes", error.getValue(), error.getKey());
 242  
                                 }
 243  
                         }
 244  0
                 }
 245  0
                 return isValid;
 246  
 
 247  
         }
 248  
 
 249  
 }