Coverage Report - org.kuali.rice.kns.service.impl.KualiRuleServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
KualiRuleServiceImpl
0%
0/74
0%
0/32
2.5
 
 1  
 /*
 2  
  * Copyright 2005-2008 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.kns.service.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import org.apache.commons.lang.StringUtils;
 22  
 import org.apache.log4j.Logger;
 23  
 import org.kuali.rice.kns.bo.AdHocRoutePerson;
 24  
 import org.kuali.rice.kns.bo.AdHocRouteWorkgroup;
 25  
 import org.kuali.rice.kns.document.Document;
 26  
 import org.kuali.rice.kns.document.MaintenanceDocument;
 27  
 import org.kuali.rice.kns.document.TransactionalDocument;
 28  
 import org.kuali.rice.kns.exception.InfrastructureException;
 29  
 import org.kuali.rice.kns.rule.BusinessRule;
 30  
 import org.kuali.rice.kns.rule.event.AddAdHocRoutePersonEvent;
 31  
 import org.kuali.rice.kns.rule.event.AddAdHocRouteWorkgroupEvent;
 32  
 import org.kuali.rice.kns.rule.event.KualiDocumentEvent;
 33  
 import org.kuali.rice.kns.service.DataDictionaryService;
 34  
 import org.kuali.rice.kns.service.DictionaryValidationService;
 35  
 import org.kuali.rice.kns.service.KualiRuleService;
 36  
 import org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService;
 37  
 import org.kuali.rice.kns.service.TransactionalDocumentDictionaryService;
 38  
 import org.kuali.rice.kns.util.GlobalVariables;
 39  
 import org.kuali.rice.kns.util.KNSConstants;
 40  
 import org.kuali.rice.kns.util.MessageMap;
 41  
 
 42  
 /**
 43  
  * This class represents a rule evaluator for Kuali. This class is to be used for evaluating business rule checks. The class defines
 44  
  * one method right now - applyRules() which takes in a Document and a DocumentEvent and does the proper business rule checks based
 45  
  * on the context of the event and the document type.
 46  
  */
 47  0
 public class KualiRuleServiceImpl implements KualiRuleService {
 48  0
     private static final Logger LOG = Logger.getLogger(KualiRuleServiceImpl.class);
 49  
 
 50  
     private TransactionalDocumentDictionaryService transactionalDocumentDictionaryService;
 51  
     private MaintenanceDocumentDictionaryService maintenanceDocumentDictionaryService;
 52  
     private DictionaryValidationService dictionaryValidationService;
 53  
     private DataDictionaryService dataDictionaryService;
 54  
 
 55  
     /**
 56  
      * @see org.kuali.rice.kns.service.KualiRuleService#applyRules(org.kuali.rice.kns.rule.event.KualiDocumentEvent)
 57  
      */
 58  
     public boolean applyRules(KualiDocumentEvent event) {
 59  0
         if (event == null) {
 60  0
             throw new IllegalArgumentException("invalid (null) event");
 61  
         }
 62  
 
 63  0
         event.validate();
 64  0
         if ( LOG.isDebugEnabled() ) {
 65  0
                 LOG.debug("calling applyRules for event " + event);
 66  
         }
 67  
 
 68  0
         BusinessRule rule = getBusinessRulesInstance(event.getDocument(), event.getRuleInterfaceClass());
 69  
 
 70  0
         boolean success = true;
 71  0
         if (rule != null) {
 72  0
                 if ( LOG.isDebugEnabled() ) {        
 73  0
                         LOG.debug("processing " + event.getName() + " with rule " + rule.getClass().getName());
 74  
                 }
 75  0
             increaseErrorPath(event.getErrorPathPrefix());
 76  
 
 77  
             // get any child events and apply rules
 78  0
             List<KualiDocumentEvent> events = event.generateEvents();
 79  0
             for (KualiDocumentEvent generatedEvent : events) {
 80  0
                 success &= applyRules(generatedEvent);
 81  
             }
 82  
 
 83  
             // now call the event rule method
 84  0
             success &= event.invokeRuleMethod(rule);
 85  
 
 86  0
             decreaseErrorPath(event.getErrorPathPrefix());
 87  
 
 88  
             // report failures
 89  0
             if (!success) {
 90  0
                     if ( LOG.isDebugEnabled() ) { // NO, this is not a type - only log if in debug mode - this is not an error in production
 91  0
                             LOG.error(event.getName() + " businessRule " + rule.getClass().getName() + " failed");
 92  
                     }
 93  
             }
 94  
             else {
 95  0
                     if ( LOG.isDebugEnabled() ) {
 96  0
                             LOG.debug("processed " + event.getName() + " for rule " + rule.getClass().getName());
 97  
                     }
 98  
             }
 99  
 
 100  
         }
 101  0
         return success;
 102  
     }
 103  
 
 104  
     /**
 105  
      * Builds a list containing AddAdHocRoutePersonEvents since the validation done for an AdHocRouteRecipient is the same for all
 106  
      * events.
 107  
      * 
 108  
      * @see org.kuali.rice.kns.service.KualiRuleService#generateAdHocRoutePersonEvents(org.kuali.rice.kns.document.Document)
 109  
      */
 110  
     public List<AddAdHocRoutePersonEvent> generateAdHocRoutePersonEvents(Document document) {
 111  0
         List<AdHocRoutePerson> adHocRoutePersons = document.getAdHocRoutePersons();
 112  
 
 113  0
         List<AddAdHocRoutePersonEvent> events = new ArrayList<AddAdHocRoutePersonEvent>();
 114  
 
 115  0
         for (int i = 0; i < adHocRoutePersons.size(); i++) {
 116  0
             events.add(new AddAdHocRoutePersonEvent(KNSConstants.EXISTING_AD_HOC_ROUTE_PERSON_PROPERTY_NAME + "[" + i + "]", document, adHocRoutePersons.get(i)));
 117  
         }
 118  
 
 119  0
         return events;
 120  
     }
 121  
 
 122  
     /**
 123  
      * Builds a list containing AddAdHocRoutePersonEvents since the validation done for an AdHocRouteRecipient is the same for all
 124  
      * events.
 125  
      * 
 126  
      * @see org.kuali.rice.kns.service.KualiRuleService#generateAdHocRouteWorkgroupEvents(org.kuali.rice.kns.document.Document)
 127  
      */
 128  
     public List<AddAdHocRouteWorkgroupEvent> generateAdHocRouteWorkgroupEvents(Document document) {
 129  0
         List<AdHocRouteWorkgroup> adHocRouteWorkgroups = document.getAdHocRouteWorkgroups();
 130  
 
 131  0
         List<AddAdHocRouteWorkgroupEvent> events = new ArrayList<AddAdHocRouteWorkgroupEvent>();
 132  
 
 133  0
         for (int i = 0; i < adHocRouteWorkgroups.size(); i++) {
 134  0
             events.add(new AddAdHocRouteWorkgroupEvent(KNSConstants.EXISTING_AD_HOC_ROUTE_WORKGROUP_PROPERTY_NAME + "[" + i + "]", document, adHocRouteWorkgroups.get(i)));
 135  
         }
 136  
 
 137  0
         return events;
 138  
     }
 139  
     
 140  
 
 141  
 
 142  
 
 143  
 
 144  
 
 145  
     /**
 146  
      * @param document
 147  
      * @param ruleInterface
 148  
      * @return instance of the businessRulesClass for the given document's type, if that businessRulesClass implements the given
 149  
      *         ruleInterface
 150  
      */
 151  
     public BusinessRule getBusinessRulesInstance(Document document, Class<? extends BusinessRule> ruleInterface) {
 152  
         // get the businessRulesClass
 153  0
         Class<? extends BusinessRule> businessRulesClass = null;
 154  0
         if (document instanceof TransactionalDocument) {
 155  0
             TransactionalDocument transactionalDocument = (TransactionalDocument) document;
 156  
 
 157  0
             businessRulesClass = transactionalDocumentDictionaryService.getBusinessRulesClass(transactionalDocument);
 158  0
         }
 159  0
         else if (document instanceof MaintenanceDocument) {
 160  0
             MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document;
 161  
 
 162  0
             businessRulesClass = maintenanceDocumentDictionaryService.getBusinessRulesClass(maintenanceDocument);
 163  0
         }
 164  
         else {
 165  0
             LOG.error("unable to get businessRulesClass for unknown document type '" + document.getClass().getName() + "'");
 166  
         }
 167  
 
 168  
         // instantiate and return it if it implements the given ruleInterface
 169  0
         BusinessRule rule = null;
 170  0
         if (businessRulesClass != null) {
 171  
             try {
 172  0
                 if (ruleInterface.isAssignableFrom(businessRulesClass)) {
 173  0
                     rule = businessRulesClass.newInstance();
 174  
                 }
 175  
             }
 176  0
             catch (IllegalAccessException e) {
 177  0
                 throw new InfrastructureException("error processing business rules", e);
 178  
             }
 179  0
             catch (InstantiationException e) {
 180  0
                 throw new InfrastructureException("error processing business rules", e);
 181  0
             }
 182  
         }
 183  
 
 184  0
         return rule;
 185  
     }
 186  
 
 187  
     /**
 188  
      * This method increases the registered error path, so that field highlighting can occur on the appropriate object attribute.
 189  
      * 
 190  
      * @param errorPathPrefix
 191  
      */
 192  
     private void increaseErrorPath(String errorPathPrefix) {
 193  0
         MessageMap errorMap = GlobalVariables.getMessageMap();
 194  
 
 195  0
         if (!StringUtils.isBlank(errorPathPrefix)) {
 196  0
             errorMap.addToErrorPath(errorPathPrefix);
 197  
         }
 198  0
     }
 199  
 
 200  
     /**
 201  
      * This method decreases the registered error path, so that field highlighting can occur on the appropriate object attribute.
 202  
      * 
 203  
      * @param errorPathPrefix
 204  
      */
 205  
     private void decreaseErrorPath(String errorPathPrefix) {
 206  0
         MessageMap errorMap = GlobalVariables.getMessageMap();
 207  
 
 208  0
         if (!StringUtils.isBlank(errorPathPrefix)) {
 209  0
             errorMap.removeFromErrorPath(errorPathPrefix);
 210  
         }
 211  0
     }
 212  
 
 213  
     /* Spring service injection */
 214  
 
 215  
     /**
 216  
      * @param maintenanceDocumentDictionaryService
 217  
      */
 218  
     public void setMaintenanceDocumentDictionaryService(MaintenanceDocumentDictionaryService maintenanceDocumentDictionaryService) {
 219  0
         this.maintenanceDocumentDictionaryService = maintenanceDocumentDictionaryService;
 220  0
     }
 221  
 
 222  
     /**
 223  
      * @return MaintenanceDocumentDictionaryService
 224  
      */
 225  
     public MaintenanceDocumentDictionaryService getMaintenanceDocumentDictionaryService() {
 226  0
         return maintenanceDocumentDictionaryService;
 227  
     }
 228  
 
 229  
     /**
 230  
      * @param transactionalDocumentDictionaryService
 231  
      */
 232  
     public void setTransactionalDocumentDictionaryService(TransactionalDocumentDictionaryService transactionalDocumentDictionaryService) {
 233  0
         this.transactionalDocumentDictionaryService = transactionalDocumentDictionaryService;
 234  0
     }
 235  
 
 236  
     /**
 237  
      * @return TransactionalDocumentDictionaryService
 238  
      */
 239  
     public TransactionalDocumentDictionaryService getTransactionalDocumentDictionaryService() {
 240  0
         return transactionalDocumentDictionaryService;
 241  
     }
 242  
 
 243  
     /**
 244  
      * @return DictionaryValidationService
 245  
      */
 246  
     public DictionaryValidationService getDictionaryValidationService() {
 247  0
         return dictionaryValidationService;
 248  
     }
 249  
 
 250  
     /**
 251  
      * @param dictionaryValidationService
 252  
      */
 253  
     public void setDictionaryValidationService(DictionaryValidationService dictionaryValidationService) {
 254  0
         this.dictionaryValidationService = dictionaryValidationService;
 255  0
     }
 256  
 
 257  
     /**
 258  
      * @return DataDictionaryService
 259  
      */
 260  
     public DataDictionaryService getDataDictionaryService() {
 261  0
         return dataDictionaryService;
 262  
     }
 263  
 
 264  
     /**
 265  
      * @param dataDictionaryService
 266  
      */
 267  
     public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
 268  0
         this.dataDictionaryService = dataDictionaryService;
 269  0
     }
 270  
 }