Coverage Report - org.kuali.rice.kew.rule.service.impl.RuleDelegationServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleDelegationServiceImpl
0%
0/71
0%
0/42
2.667
 
 1  
 /*
 2  
  * Copyright 2005-2009 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.rule.service.impl;
 18  
 
 19  
 import java.io.InputStream;
 20  
 import java.util.ArrayList;
 21  
 import java.util.Collection;
 22  
 import java.util.Collections;
 23  
 import java.util.List;
 24  
 import java.util.Map;
 25  
 
 26  
 import org.apache.commons.lang.StringUtils;
 27  
 import org.jdom.Element;
 28  
 import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
 29  
 import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
 30  
 import org.kuali.rice.kew.export.ExportDataSet;
 31  
 import org.kuali.rice.kew.rule.RuleDelegation;
 32  
 import org.kuali.rice.kew.rule.bo.RuleTemplate;
 33  
 import org.kuali.rice.kew.rule.dao.RuleDelegationDAO;
 34  
 import org.kuali.rice.kew.rule.service.RuleDelegationService;
 35  
 import org.kuali.rice.kew.rule.service.RuleTemplateService;
 36  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 37  
 import org.kuali.rice.kew.util.KEWConstants;
 38  
 import org.kuali.rice.kew.util.PerformanceLogger;
 39  
 import org.kuali.rice.kew.util.Utilities;
 40  
 import org.kuali.rice.kew.xml.RuleXmlParser;
 41  
 import org.kuali.rice.kew.xml.export.RuleDelegationXmlExporter;
 42  
 import org.kuali.rice.kim.bo.Group;
 43  
 import org.kuali.rice.kim.service.IdentityManagementService;
 44  
 import org.kuali.rice.kim.service.KIMServiceLocator;
 45  
 import org.kuali.rice.kns.util.KNSConstants;
 46  
 
 47  
 
 48  
 /**
 49  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 50  
  */
 51  0
 public class RuleDelegationServiceImpl implements RuleDelegationService {
 52  
         
 53  
 
 54  
     private static final String USING_RULE_DLGN_CACHE_IND = "CACHING_IND";
 55  
     private static final String RULE_DLGN_GROUP_CACHE = "org.kuali.rice.kew.rule.RuleDlgnCache";
 56  
 
 57  
 
 58  
     
 59  0
         private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
 60  
                         .getLogger(RuleDelegationServiceImpl.class);
 61  
         
 62  
         private static final String XML_PARSE_ERROR = "general.error.parsexml";
 63  
         
 64  
     private RuleDelegationDAO dao;
 65  
 
 66  
     public List findByDelegateRuleId(Long ruleId) {
 67  0
         if (ruleId == null) return Collections.EMPTY_LIST;
 68  0
         return dao.findByDelegateRuleId(ruleId);
 69  
     }
 70  
 
 71  
     public void save(RuleDelegation ruleDelegation) {
 72  0
         dao.save(ruleDelegation);
 73  0
     }
 74  
 
 75  
     public void setRuleDelegationDAO(RuleDelegationDAO dao) {
 76  0
         this.dao = dao;
 77  0
     }
 78  
     public List findAllCurrentRuleDelegations(){
 79  0
         return dao.findAllCurrentRuleDelegations();
 80  
     }
 81  
     public void delete(Long ruleDelegationId){
 82  0
         dao.delete(ruleDelegationId);
 83  0
     }
 84  
 
 85  
     public RuleDelegation findByRuleDelegationId(Long ruleDelegationId){
 86  0
         return dao.findByRuleDelegationId(ruleDelegationId);
 87  
     }
 88  
 
 89  
     public List<RuleDelegation> findByResponsibilityId(Long responsibilityId) {
 90  
             //return dao.findByResponsibilityIdWithCurrentRule(responsibilityId);
 91  0
             return findByResponsibilityId(responsibilityId, false);
 92  
     }
 93  
 
 94  
     public List<RuleDelegation> search(String parentRuleBaseVaueId, String parentResponsibilityId,  String docTypeName, Long ruleId, Long ruleTemplateId, String ruleDescription, String groupId, String principalId,
 95  
             String delegationType, Boolean activeInd, Map extensionValues, String workflowIdDirective) {
 96  0
         return dao.search(parentRuleBaseVaueId, parentResponsibilityId, docTypeName, ruleId, ruleTemplateId, ruleDescription, groupId, principalId, delegationType,
 97  
                 activeInd, extensionValues, workflowIdDirective);
 98  
     }
 99  
 
 100  
     public List<RuleDelegation> search(String parentRuleBaseVaueId, String parentResponsibilityId,  String docTypeName, String ruleTemplateName, String ruleDescription, String groupId, String principalId,
 101  
             Boolean workgroupMember, String delegationType, Boolean activeInd, Map extensionValues, Collection<String> actionRequestCodes) {
 102  
 
 103  0
         if ( (StringUtils.isEmpty(docTypeName)) &&
 104  
                 (StringUtils.isEmpty(ruleTemplateName)) &&
 105  
                 (StringUtils.isEmpty(ruleDescription)) &&
 106  
                 (StringUtils.isEmpty(groupId)) &&
 107  
                 (StringUtils.isEmpty(principalId)) &&
 108  
                 (extensionValues.isEmpty()) &&
 109  
                 (actionRequestCodes.isEmpty()) ) {
 110  
             // all fields are empty
 111  0
             throw new IllegalArgumentException("At least one criterion must be sent");
 112  
         }
 113  
 
 114  0
         RuleTemplate ruleTemplate = getRuleTemplateService().findByRuleTemplateName(ruleTemplateName);
 115  0
         Long ruleTemplateId = null;
 116  0
         if (ruleTemplate != null) {
 117  0
             ruleTemplateId = ruleTemplate.getRuleTemplateId();
 118  
         }
 119  
 
 120  0
         if ( ( (extensionValues != null) && (!extensionValues.isEmpty()) ) &&
 121  
                 (ruleTemplateId == null) ) {
 122  
             // cannot have extensions without a correct template
 123  0
             throw new IllegalArgumentException("A Rule Template Name must be given if using Rule Extension values");
 124  
         }
 125  
 
 126  0
         Collection<String> workgroupIds = new ArrayList<String>();
 127  0
         if (principalId != null) {
 128  0
             if ( (workgroupMember == null) || (workgroupMember.booleanValue()) ) {
 129  0
                 workgroupIds = getIdentityManagementService().getGroupIdsForPrincipal(principalId);
 130  
             } else {
 131  
                 // user was passed but workgroups should not be parsed... do nothing
 132  
             }
 133  0
         } else if (groupId != null) {
 134  0
             Group group = KEWServiceLocator.getIdentityHelperService().getGroup(groupId);
 135  0
             if (group == null) {
 136  0
                 throw new IllegalArgumentException("Group does not exist in for given group id: " + groupId);
 137  
             } else  {
 138  0
                 workgroupIds.add(group.getGroupId());
 139  
             }
 140  
         }
 141  
 
 142  0
         return dao.search(parentRuleBaseVaueId, parentResponsibilityId, docTypeName, ruleTemplateId, ruleDescription, workgroupIds, principalId,
 143  
                 delegationType,activeInd, extensionValues, actionRequestCodes);
 144  
     }
 145  
     
 146  
     public void loadXml(InputStream inputStream, String principalId) {
 147  0
             RuleXmlParser parser = new RuleXmlParser();
 148  
         try {
 149  0
             parser.parseRuleDelegations(inputStream);
 150  0
         } catch (Exception e) { //any other exception
 151  0
             LOG.error("Error loading xml file", e);
 152  0
             WorkflowServiceErrorException wsee = new WorkflowServiceErrorException("Error loading xml file", new WorkflowServiceErrorImpl("Error loading xml file", XML_PARSE_ERROR));
 153  0
             wsee.initCause(e);
 154  0
             throw wsee;
 155  0
         }
 156  0
         }
 157  
 
 158  
         public Element export(ExportDataSet dataSet) {
 159  0
                 RuleDelegationXmlExporter exporter = new RuleDelegationXmlExporter();
 160  0
                 return exporter.export(dataSet);
 161  
         }
 162  
 
 163  
         private IdentityManagementService getIdentityManagementService() {
 164  0
         return (IdentityManagementService)KIMServiceLocator.getIdentityManagementService();
 165  
     }
 166  
 
 167  
     private RuleTemplateService getRuleTemplateService() {
 168  0
         return (RuleTemplateService)KEWServiceLocator.getRuleTemplateService();
 169  
     }
 170  
     
 171  
 
 172  
     
 173  
     public List findByResponsibilityId(Long responsibilityId, boolean ignoreCache) {
 174  0
             if ( responsibilityId != null ) {
 175  0
                     PerformanceLogger performanceLogger = new PerformanceLogger();
 176  0
                     Boolean cachingRules = Boolean.valueOf(Utilities.getKNSParameterBooleanValue(KEWConstants.KEW_NAMESPACE, KNSConstants.DetailTypes.RULE_DETAIL_TYPE, USING_RULE_DLGN_CACHE_IND));
 177  0
                     if (cachingRules.booleanValue()) {
 178  0
                             List<RuleDelegation> rules = getListFromCache(responsibilityId);
 179  0
                             if (rules != null && !ignoreCache) {
 180  0
                                     performanceLogger.log("Time to fetchDelegationRules by responsibility Id " + responsibilityId + " cached.");
 181  0
                                     return rules;
 182  
                             }
 183  
             
 184  0
                             rules = dao.findByResponsibilityIdWithCurrentRule(responsibilityId);
 185  0
                             putListInCache(responsibilityId, rules);
 186  0
                             performanceLogger.log("Time to fetchDlgnRules by responsibilityId " + responsibilityId + " cache refreshed.");
 187  0
                             return rules;
 188  
 
 189  
                     } else {
 190  0
                             performanceLogger.log("Time to fetchDelegationRules by responsibility Id " + responsibilityId + " not caching.");
 191  0
                             return dao.findByResponsibilityIdWithCurrentRule(responsibilityId);
 192  
                     }
 193  
             } else {
 194  0
                     return dao.findByResponsibilityIdWithCurrentRule(responsibilityId);
 195  
             }
 196  
     }
 197  
     
 198  
     protected void putListInCache(Long responsibilityId, List<RuleDelegation> rules) {
 199  0
             String responsibilityIdStr = responsibilityId.toString();
 200  0
         LOG.info("Caching " + rules.size() + " rules for responsibilityId=" + responsibilityIdStr );
 201  
 
 202  0
         KEWServiceLocator.getCacheAdministrator().putInCache(getRuleDlgnCacheKey(responsibilityIdStr), rules, RULE_DLGN_GROUP_CACHE);
 203  
 
 204  0
     }
 205  
     
 206  
     protected List<RuleDelegation> getListFromCache(Long responsibilityId) {
 207  0
             String responsibilityIdStr = responsibilityId.toString();
 208  0
         LOG.debug("Retrieving List of Delegation Rules from cache for responsibilityId = " + responsibilityIdStr );
 209  0
         return (List) KEWServiceLocator.getCacheAdministrator().getFromCache(getRuleDlgnCacheKey(responsibilityIdStr));
 210  
     }
 211  
         
 212  
     protected String getRuleDlgnCacheKey(String responsibilityIdStr) {
 213  0
         return "RuleDlgnCache:" + responsibilityIdStr;
 214  
     }
 215  
     
 216  
    
 217  
     public void flushRuleDlgnCache() {
 218  0
         LOG.info("Flushing entire Rule Delegation Cache.");
 219  0
         KEWServiceLocator.getCacheAdministrator().flushGroup(RULE_DLGN_GROUP_CACHE);
 220  0
     }
 221  
     
 222  
 
 223  
     
 224  
 }