Coverage Report - org.kuali.rice.kew.rule.service.impl.RuleDelegationServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleDelegationServiceImpl
0%
0/72
0%
0/42
2.579
 
 1  
 /*
 2  
  * Copyright 2006-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  
 
 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.core.api.impex.ExportDataSet;
 29  
 import org.kuali.rice.core.framework.services.CoreFrameworkServiceLocator;
 30  
 import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
 31  
 import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
 32  
 import org.kuali.rice.kew.rule.RuleDelegation;
 33  
 import org.kuali.rice.kew.rule.bo.RuleTemplate;
 34  
 import org.kuali.rice.kew.rule.dao.RuleDelegationDAO;
 35  
 import org.kuali.rice.kew.rule.service.RuleDelegationService;
 36  
 import org.kuali.rice.kew.rule.service.RuleTemplateService;
 37  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 38  
 import org.kuali.rice.kew.util.KEWConstants;
 39  
 import org.kuali.rice.kew.util.PerformanceLogger;
 40  
 import org.kuali.rice.kew.xml.RuleXmlParser;
 41  
 import org.kuali.rice.kew.xml.export.RuleDelegationXmlExporter;
 42  
 import org.kuali.rice.kim.api.services.IdentityManagementService;
 43  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 44  
 import org.kuali.rice.kim.api.group.Group;
 45  
 import org.kuali.rice.kns.util.KNSConstants;
 46  
 import org.kuali.rice.ksb.api.KsbApiServiceLocator;
 47  
 
 48  
 
 49  
 /**
 50  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 51  
  */
 52  0
 public class RuleDelegationServiceImpl implements RuleDelegationService {
 53  
         
 54  
 
 55  
     private static final String USING_RULE_DLGN_CACHE_IND = "CACHING_IND";
 56  
     private static final String RULE_DLGN_GROUP_CACHE = "org.kuali.rice.kew.rule.RuleDlgnCache";
 57  
 
 58  
 
 59  
     
 60  0
         private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
 61  
                         .getLogger(RuleDelegationServiceImpl.class);
 62  
         
 63  
         private static final String XML_PARSE_ERROR = "general.error.parsexml";
 64  
         
 65  
     private RuleDelegationDAO dao;
 66  
 
 67  
     public List findByDelegateRuleId(Long ruleId) {
 68  0
         if (ruleId == null) return Collections.EMPTY_LIST;
 69  0
         return dao.findByDelegateRuleId(ruleId);
 70  
     }
 71  
 
 72  
     public void save(RuleDelegation ruleDelegation) {
 73  0
         dao.save(ruleDelegation);
 74  0
     }
 75  
 
 76  
     public void setRuleDelegationDAO(RuleDelegationDAO dao) {
 77  0
         this.dao = dao;
 78  0
     }
 79  
     public List findAllCurrentRuleDelegations(){
 80  0
         return dao.findAllCurrentRuleDelegations();
 81  
     }
 82  
     public void delete(Long ruleDelegationId){
 83  0
         dao.delete(ruleDelegationId);
 84  0
     }
 85  
 
 86  
     public RuleDelegation findByRuleDelegationId(Long ruleDelegationId){
 87  0
         return dao.findByRuleDelegationId(ruleDelegationId);
 88  
     }
 89  
 
 90  
     public List<RuleDelegation> findByResponsibilityId(Long responsibilityId) {
 91  
             //return dao.findByResponsibilityIdWithCurrentRule(responsibilityId);
 92  0
             return findByResponsibilityId(responsibilityId, false);
 93  
     }
 94  
 
 95  
     public List<RuleDelegation> search(String parentRuleBaseVaueId, String parentResponsibilityId,  String docTypeName, Long ruleId, Long ruleTemplateId, String ruleDescription, String groupId, String principalId,
 96  
             String delegationType, Boolean activeInd, Map extensionValues, String workflowIdDirective) {
 97  0
         return dao.search(parentRuleBaseVaueId, parentResponsibilityId, docTypeName, ruleId, ruleTemplateId, ruleDescription, groupId, principalId, delegationType,
 98  
                 activeInd, extensionValues, workflowIdDirective);
 99  
     }
 100  
 
 101  
     public List<RuleDelegation> search(String parentRuleBaseVaueId, String parentResponsibilityId,  String docTypeName, String ruleTemplateName, String ruleDescription, String groupId, String principalId,
 102  
             Boolean workgroupMember, String delegationType, Boolean activeInd, Map extensionValues, Collection<String> actionRequestCodes) {
 103  
 
 104  0
         if ( (StringUtils.isEmpty(docTypeName)) &&
 105  
                 (StringUtils.isEmpty(ruleTemplateName)) &&
 106  
                 (StringUtils.isEmpty(ruleDescription)) &&
 107  
                 (StringUtils.isEmpty(groupId)) &&
 108  
                 (StringUtils.isEmpty(principalId)) &&
 109  
                 (extensionValues.isEmpty()) &&
 110  
                 (actionRequestCodes.isEmpty()) ) {
 111  
             // all fields are empty
 112  0
             throw new IllegalArgumentException("At least one criterion must be sent");
 113  
         }
 114  
 
 115  0
         RuleTemplate ruleTemplate = getRuleTemplateService().findByRuleTemplateName(ruleTemplateName);
 116  0
         Long ruleTemplateId = null;
 117  0
         if (ruleTemplate != null) {
 118  0
             ruleTemplateId = ruleTemplate.getRuleTemplateId();
 119  
         }
 120  
 
 121  0
         if ( ( (extensionValues != null) && (!extensionValues.isEmpty()) ) &&
 122  
                 (ruleTemplateId == null) ) {
 123  
             // cannot have extensions without a correct template
 124  0
             throw new IllegalArgumentException("A Rule Template Name must be given if using Rule Extension values");
 125  
         }
 126  
 
 127  0
         Collection<String> workgroupIds = new ArrayList<String>();
 128  0
         if (principalId != null) {
 129  0
             if ( (workgroupMember == null) || (workgroupMember.booleanValue()) ) {
 130  0
                 workgroupIds = getIdentityManagementService().getGroupIdsForPrincipal(principalId);
 131  
             } else {
 132  
                 // user was passed but workgroups should not be parsed... do nothing
 133  
             }
 134  0
         } else if (groupId != null) {
 135  0
             Group group = KEWServiceLocator.getIdentityHelperService().getGroup(groupId);
 136  0
             if (group == null) {
 137  0
                 throw new IllegalArgumentException("Group does not exist in for given group id: " + groupId);
 138  
             } else  {
 139  0
                 workgroupIds.add(group.getId());
 140  
             }
 141  
         }
 142  
 
 143  0
         return dao.search(parentRuleBaseVaueId, parentResponsibilityId, docTypeName, ruleTemplateId, ruleDescription, workgroupIds, principalId,
 144  
                 delegationType,activeInd, extensionValues, actionRequestCodes);
 145  
     }
 146  
     
 147  
     public void loadXml(InputStream inputStream, String principalId) {
 148  0
             RuleXmlParser parser = new RuleXmlParser();
 149  
         try {
 150  0
             parser.parseRuleDelegations(inputStream);
 151  0
         } catch (Exception e) { //any other exception
 152  0
             LOG.error("Error loading xml file", e);
 153  0
             WorkflowServiceErrorException wsee = new WorkflowServiceErrorException("Error loading xml file", new WorkflowServiceErrorImpl("Error loading xml file", XML_PARSE_ERROR));
 154  0
             wsee.initCause(e);
 155  0
             throw wsee;
 156  0
         }
 157  0
         }
 158  
 
 159  
         public Element export(ExportDataSet dataSet) {
 160  0
                 RuleDelegationXmlExporter exporter = new RuleDelegationXmlExporter();
 161  0
                 return exporter.export(dataSet);
 162  
         }
 163  
         
 164  
         @Override
 165  
         public boolean supportPrettyPrint() {
 166  0
                 return true;
 167  
         }
 168  
 
 169  
         private IdentityManagementService getIdentityManagementService() {
 170  0
         return (IdentityManagementService) KimApiServiceLocator.getIdentityManagementService();
 171  
     }
 172  
 
 173  
     private RuleTemplateService getRuleTemplateService() {
 174  0
         return (RuleTemplateService)KEWServiceLocator.getRuleTemplateService();
 175  
     }
 176  
     
 177  
 
 178  
     
 179  
     public List findByResponsibilityId(Long responsibilityId, boolean ignoreCache) {
 180  0
             if ( responsibilityId != null ) {
 181  0
                     PerformanceLogger performanceLogger = new PerformanceLogger();
 182  0
                     Boolean cachingRules = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsBoolean(KEWConstants.KEW_NAMESPACE, KNSConstants.DetailTypes.RULE_DETAIL_TYPE, USING_RULE_DLGN_CACHE_IND);
 183  0
                     if (cachingRules.booleanValue()) {
 184  0
                             List<RuleDelegation> rules = getListFromCache(responsibilityId);
 185  0
                             if (rules != null && !ignoreCache) {
 186  0
                                     performanceLogger.log("Time to fetchDelegationRules by responsibility Id " + responsibilityId + " cached.");
 187  0
                                     return rules;
 188  
                             }
 189  
             
 190  0
                             rules = dao.findByResponsibilityIdWithCurrentRule(responsibilityId);
 191  0
                             putListInCache(responsibilityId, rules);
 192  0
                             performanceLogger.log("Time to fetchDlgnRules by responsibilityId " + responsibilityId + " cache refreshed.");
 193  0
                             return rules;
 194  
 
 195  
                     } else {
 196  0
                             performanceLogger.log("Time to fetchDelegationRules by responsibility Id " + responsibilityId + " not caching.");
 197  0
                             return dao.findByResponsibilityIdWithCurrentRule(responsibilityId);
 198  
                     }
 199  
             } else {
 200  0
                     return dao.findByResponsibilityIdWithCurrentRule(responsibilityId);
 201  
             }
 202  
     }
 203  
     
 204  
     protected void putListInCache(Long responsibilityId, List<RuleDelegation> rules) {
 205  0
             String responsibilityIdStr = responsibilityId.toString();
 206  0
         LOG.info("Caching " + rules.size() + " rules for responsibilityId=" + responsibilityIdStr );
 207  
 
 208  0
         KsbApiServiceLocator.getCacheAdministrator().putInCache(getRuleDlgnCacheKey(responsibilityIdStr), rules, RULE_DLGN_GROUP_CACHE);
 209  
 
 210  0
     }
 211  
     
 212  
     protected List<RuleDelegation> getListFromCache(Long responsibilityId) {
 213  0
             String responsibilityIdStr = responsibilityId.toString();
 214  0
         LOG.debug("Retrieving List of Delegation Rules from cache for responsibilityId = " + responsibilityIdStr );
 215  0
         return (List) KsbApiServiceLocator.getCacheAdministrator().getFromCache(getRuleDlgnCacheKey(responsibilityIdStr));
 216  
     }
 217  
         
 218  
     protected String getRuleDlgnCacheKey(String responsibilityIdStr) {
 219  0
         return "RuleDlgnCache:" + responsibilityIdStr;
 220  
     }
 221  
     
 222  
    
 223  
     public void flushRuleDlgnCache() {
 224  0
         LOG.info("Flushing entire Rule Delegation Cache.");
 225  0
         KsbApiServiceLocator.getCacheAdministrator().flushGroup(RULE_DLGN_GROUP_CACHE);
 226  0
     }
 227  
     
 228  
 
 229  
     
 230  
 }