|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
     | 
  |  17 |     | 
   package org.kuali.rice.kew.rule.service.impl;  | 
  |  18 |     | 
     | 
  |  19 |     | 
   import org.apache.commons.lang.StringUtils;  | 
  |  20 |     | 
   import org.jdom.Element;  | 
  |  21 |     | 
   import org.kuali.rice.core.api.impex.ExportDataSet;  | 
  |  22 |     | 
   import org.kuali.rice.core.framework.services.CoreFrameworkServiceLocator;  | 
  |  23 |     | 
   import org.kuali.rice.kew.exception.WorkflowServiceErrorException;  | 
  |  24 |     | 
   import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;  | 
  |  25 |     | 
   import org.kuali.rice.kew.rule.RuleDelegation;  | 
  |  26 |     | 
   import org.kuali.rice.kew.rule.bo.RuleTemplate;  | 
  |  27 |     | 
   import org.kuali.rice.kew.rule.dao.RuleDelegationDAO;  | 
  |  28 |     | 
   import org.kuali.rice.kew.rule.service.RuleDelegationService;  | 
  |  29 |     | 
   import org.kuali.rice.kew.rule.service.RuleTemplateService;  | 
  |  30 |     | 
   import org.kuali.rice.kew.service.KEWServiceLocator;  | 
  |  31 |     | 
   import org.kuali.rice.kew.util.KEWConstants;  | 
  |  32 |     | 
   import org.kuali.rice.kew.util.PerformanceLogger;  | 
  |  33 |     | 
   import org.kuali.rice.kew.xml.RuleXmlParser;  | 
  |  34 |     | 
   import org.kuali.rice.kew.xml.export.RuleDelegationXmlExporter;  | 
  |  35 |     | 
   import org.kuali.rice.kim.api.group.Group;  | 
  |  36 |     | 
   import org.kuali.rice.kim.api.group.GroupService;  | 
  |  37 |     | 
   import org.kuali.rice.kim.api.services.KimApiServiceLocator;  | 
  |  38 |     | 
   import org.kuali.rice.krad.util.KRADConstants;  | 
  |  39 |     | 
   import org.kuali.rice.ksb.api.KsbApiServiceLocator;  | 
  |  40 |     | 
     | 
  |  41 |     | 
   import java.io.InputStream;  | 
  |  42 |     | 
   import java.util.ArrayList;  | 
  |  43 |     | 
   import java.util.Collection;  | 
  |  44 |     | 
   import java.util.Collections;  | 
  |  45 |     | 
   import java.util.List;  | 
  |  46 |     | 
   import java.util.Map;  | 
  |  47 |     | 
     | 
  |  48 |     | 
     | 
  |  49 |     | 
     | 
  |  50 |     | 
     | 
  |  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<RuleDelegation> findByDelegateRuleId(String 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<RuleDelegation> findAllCurrentRuleDelegations(){ | 
  |  80 |    0 |            return dao.findAllCurrentRuleDelegations();  | 
  |  81 |     | 
       }  | 
  |  82 |     | 
       public void delete(String ruleDelegationId){ | 
  |  83 |    0 |            dao.delete(ruleDelegationId);  | 
  |  84 |    0 |        }  | 
  |  85 |     | 
     | 
  |  86 |     | 
       public RuleDelegation findByRuleDelegationId(String ruleDelegationId){ | 
  |  87 |    0 |            return dao.findByRuleDelegationId(ruleDelegationId);  | 
  |  88 |     | 
       }  | 
  |  89 |     | 
     | 
  |  90 |     | 
       public List<RuleDelegation> findByResponsibilityId(String responsibilityId) { | 
  |  91 |     | 
                 | 
  |  92 |    0 |                return findByResponsibilityId(responsibilityId, false);  | 
  |  93 |     | 
       }  | 
  |  94 |     | 
     | 
  |  95 |     | 
       public List<RuleDelegation> search(String parentRuleBaseVaueId, String parentResponsibilityId,  String docTypeName, String ruleId, String 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> searchByTemplate(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 |     | 
                 | 
  |  112 |    0 |                throw new IllegalArgumentException("At least one criterion must be sent"); | 
  |  113 |     | 
           }  | 
  |  114 |     | 
     | 
  |  115 |    0 |            RuleTemplate ruleTemplate = getRuleTemplateService().findByRuleTemplateName(ruleTemplateName);  | 
  |  116 |    0 |            String 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 |     | 
                 | 
  |  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 = getGroupService().getGroupIdsForPrincipal(principalId);  | 
  |  131 |     | 
               } else { | 
  |  132 |     | 
                     | 
  |  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) {  | 
  |  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 GroupService getGroupService() { | 
  |  170 |    0 |            return KimApiServiceLocator.getGroupService();  | 
  |  171 |     | 
       }  | 
  |  172 |     | 
     | 
  |  173 |     | 
       private RuleTemplateService getRuleTemplateService() { | 
  |  174 |    0 |            return KEWServiceLocator.getRuleTemplateService();  | 
  |  175 |     | 
       }  | 
  |  176 |     | 
         | 
  |  177 |     | 
     | 
  |  178 |     | 
         | 
  |  179 |     | 
       public List findByResponsibilityId(String responsibilityId, boolean ignoreCache) { | 
  |  180 |    0 |                if ( responsibilityId != null ) { | 
  |  181 |    0 |                        PerformanceLogger performanceLogger = new PerformanceLogger();  | 
  |  182 |    0 |                        Boolean cachingRules = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsBoolean(KEWConstants.KEW_NAMESPACE, KRADConstants.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(String 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(String 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 |     | 
   }  |