Coverage Report - org.kuali.rice.krad.service.impl.KualiRuleServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
KualiRuleServiceImpl
0%
0/73
0%
0/34
2.833
 
 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.krad.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.krad.bo.AdHocRoutePerson;
 24  
 import org.kuali.rice.krad.bo.AdHocRouteWorkgroup;
 25  
 import org.kuali.rice.krad.document.Document;
 26  
 import org.kuali.rice.krad.document.MaintenanceDocument;
 27  
 import org.kuali.rice.krad.document.TransactionalDocument;
 28  
 import org.kuali.rice.krad.exception.InfrastructureException;
 29  
 import org.kuali.rice.krad.rule.BusinessRule;
 30  
 import org.kuali.rice.krad.rule.event.AddAdHocRoutePersonEvent;
 31  
 import org.kuali.rice.krad.rule.event.AddAdHocRouteWorkgroupEvent;
 32  
 import org.kuali.rice.krad.rule.event.KualiDocumentEvent;
 33  
 import org.kuali.rice.krad.service.DataDictionaryService;
 34  
 import org.kuali.rice.krad.service.DictionaryValidationService;
 35  
 import org.kuali.rice.krad.service.DocumentDictionaryService;
 36  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 37  
 import org.kuali.rice.krad.service.KualiRuleService;
 38  
 import org.kuali.rice.krad.util.GlobalVariables;
 39  
 import org.kuali.rice.krad.util.KRADConstants;
 40  
 import org.kuali.rice.krad.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 DocumentDictionaryService documentDictionaryService;
 51  
     private DictionaryValidationService dictionaryValidationService;
 52  
     private DataDictionaryService dataDictionaryService;
 53  
 
 54  
     /**
 55  
      * @see org.kuali.rice.krad.service.KualiRuleService#applyRules(org.kuali.rice.krad.rule.event.KualiDocumentEvent)
 56  
      */
 57  
     public boolean applyRules(KualiDocumentEvent event) {
 58  0
         if (event == null) {
 59  0
             throw new IllegalArgumentException("invalid (null) event");
 60  
         }
 61  
 
 62  0
         event.validate();
 63  0
         if ( LOG.isDebugEnabled() ) {
 64  0
                 LOG.debug("calling applyRules for event " + event);
 65  
         }
 66  
 
 67  0
         BusinessRule rule = getBusinessRulesInstance(event.getDocument(), event.getRuleInterfaceClass());
 68  
 
 69  0
         boolean success = true;
 70  0
         if (rule != null) {
 71  0
                 if ( LOG.isDebugEnabled() ) {        
 72  0
                         LOG.debug("processing " + event.getName() + " with rule " + rule.getClass().getName());
 73  
                 }
 74  0
             increaseErrorPath(event.getErrorPathPrefix());
 75  
 
 76  
             // get any child events and apply rules
 77  0
             List<KualiDocumentEvent> events = event.generateEvents();
 78  0
             for (KualiDocumentEvent generatedEvent : events) {
 79  0
                 success &= applyRules(generatedEvent);
 80  
             }
 81  
 
 82  
             // now call the event rule method
 83  0
             success &= event.invokeRuleMethod(rule);
 84  
 
 85  0
             decreaseErrorPath(event.getErrorPathPrefix());
 86  
 
 87  
             // report failures
 88  0
             if (!success) {
 89  0
                     if ( LOG.isDebugEnabled() ) { // NO, this is not a type - only log if in debug mode - this is not an error in production
 90  0
                             LOG.debug(event.getName() + " businessRule " + rule.getClass().getName() + " failed");
 91  
                     }
 92  
             }
 93  
             else {
 94  0
                     if ( LOG.isDebugEnabled() ) {
 95  0
                             LOG.debug("processed " + event.getName() + " for rule " + rule.getClass().getName());
 96  
                     }
 97  
             }
 98  
 
 99  
         }
 100  0
         return success;
 101  
     }
 102  
 
 103  
     /**
 104  
      * Builds a list containing AddAdHocRoutePersonEvents since the validation done for an AdHocRouteRecipient is the same for all
 105  
      * events.
 106  
      * 
 107  
      * @see org.kuali.rice.krad.service.KualiRuleService#generateAdHocRoutePersonEvents(org.kuali.rice.krad.document.Document)
 108  
      */
 109  
     public List<AddAdHocRoutePersonEvent> generateAdHocRoutePersonEvents(Document document) {
 110  0
         List<AdHocRoutePerson> adHocRoutePersons = document.getAdHocRoutePersons();
 111  
 
 112  0
         List<AddAdHocRoutePersonEvent> events = new ArrayList<AddAdHocRoutePersonEvent>();
 113  
 
 114  0
         for (int i = 0; i < adHocRoutePersons.size(); i++) {
 115  0
             events.add(new AddAdHocRoutePersonEvent(
 116  
                     KRADConstants.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.krad.service.KualiRuleService#generateAdHocRouteWorkgroupEvents(org.kuali.rice.krad.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(
 135  
                     KRADConstants.EXISTING_AD_HOC_ROUTE_WORKGROUP_PROPERTY_NAME + "[" + i + "]", document, adHocRouteWorkgroups.get(i)));
 136  
         }
 137  
 
 138  0
         return events;
 139  
     }
 140  
     
 141  
 
 142  
 
 143  
 
 144  
 
 145  
 
 146  
     /**
 147  
      * @param document
 148  
      * @param ruleInterface
 149  
      * @return instance of the businessRulesClass for the given document's type, if that businessRulesClass implements the given
 150  
      *         ruleInterface
 151  
      */
 152  
     public BusinessRule getBusinessRulesInstance(Document document, Class<? extends BusinessRule> ruleInterface) {
 153  
         // get the businessRulesClass
 154  0
         Class<? extends BusinessRule> businessRulesClass = null;
 155  0
         if (document instanceof TransactionalDocument) {
 156  0
             TransactionalDocument transactionalDocument = (TransactionalDocument) document;
 157  
 
 158  0
             businessRulesClass = getDocumentDictionaryService().getBusinessRulesClass(transactionalDocument);
 159  0
         }
 160  0
         else if (document instanceof MaintenanceDocument) {
 161  0
             MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document;
 162  
 
 163  0
             businessRulesClass = getDocumentDictionaryService().getBusinessRulesClass(maintenanceDocument);
 164  0
         }
 165  
         else {
 166  0
             LOG.error("unable to get businessRulesClass for unknown document type '" + document.getClass().getName() + "'");
 167  
         }
 168  
 
 169  
         // instantiate and return it if it implements the given ruleInterface
 170  0
         BusinessRule rule = null;
 171  0
         if (businessRulesClass != null) {
 172  
             try {
 173  0
                 if (ruleInterface.isAssignableFrom(businessRulesClass)) {
 174  0
                     rule = businessRulesClass.newInstance();
 175  
                 }
 176  
             }
 177  0
             catch (IllegalAccessException e) {
 178  0
                 throw new InfrastructureException("error processing business rules", e);
 179  
             }
 180  0
             catch (InstantiationException e) {
 181  0
                 throw new InfrastructureException("error processing business rules", e);
 182  0
             }
 183  
         }
 184  
 
 185  0
         return rule;
 186  
     }
 187  
 
 188  
     /**
 189  
      * This method increases the registered error path, so that field highlighting can occur on the appropriate object attribute.
 190  
      * 
 191  
      * @param errorPathPrefix
 192  
      */
 193  
     private void increaseErrorPath(String errorPathPrefix) {
 194  0
         MessageMap errorMap = GlobalVariables.getMessageMap();
 195  
 
 196  0
         if (!StringUtils.isBlank(errorPathPrefix)) {
 197  0
             errorMap.addToErrorPath(errorPathPrefix);
 198  
         }
 199  0
     }
 200  
 
 201  
     /**
 202  
      * This method decreases the registered error path, so that field highlighting can occur on the appropriate object attribute.
 203  
      * 
 204  
      * @param errorPathPrefix
 205  
      */
 206  
     private void decreaseErrorPath(String errorPathPrefix) {
 207  0
         MessageMap errorMap = GlobalVariables.getMessageMap();
 208  
 
 209  0
         if (!StringUtils.isBlank(errorPathPrefix)) {
 210  0
             errorMap.removeFromErrorPath(errorPathPrefix);
 211  
         }
 212  0
     }
 213  
 
 214  
     public DocumentDictionaryService getDocumentDictionaryService() {
 215  0
         if (documentDictionaryService == null) {
 216  0
             this.documentDictionaryService = KRADServiceLocatorWeb.getDocumentDictionaryService();
 217  
         }
 218  0
         return documentDictionaryService;
 219  
     }
 220  
 
 221  
     public void setDocumentDictionaryService(DocumentDictionaryService documentDictionaryService) {
 222  0
         this.documentDictionaryService = documentDictionaryService;
 223  0
     }
 224  
 
 225  
     public DictionaryValidationService getDictionaryValidationService() {
 226  0
         return dictionaryValidationService;
 227  
     }
 228  
 
 229  
     public void setDictionaryValidationService(DictionaryValidationService dictionaryValidationService) {
 230  0
         this.dictionaryValidationService = dictionaryValidationService;
 231  0
     }
 232  
 
 233  
     public DataDictionaryService getDataDictionaryService() {
 234  0
         return dataDictionaryService;
 235  
     }
 236  
 
 237  
     public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
 238  0
         this.dataDictionaryService = dataDictionaryService;
 239  0
     }
 240  
 }