Coverage Report - org.kuali.rice.kew.rule.dao.impl.RuleDelegationDAOOjbImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleDelegationDAOOjbImpl
0%
0/144
0%
0/98
4
 
 1  
 /*
 2  
  * Copyright 2005-2007 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.dao.impl;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.Collection;
 21  
 import java.util.HashSet;
 22  
 import java.util.Iterator;
 23  
 import java.util.List;
 24  
 import java.util.Map;
 25  
 import java.util.Set;
 26  
 
 27  
 import org.apache.commons.lang.StringUtils;
 28  
 import org.apache.ojb.broker.query.Criteria;
 29  
 import org.apache.ojb.broker.query.QueryByCriteria;
 30  
 import org.apache.ojb.broker.query.QueryFactory;
 31  
 import org.apache.ojb.broker.query.ReportQueryByCriteria;
 32  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 33  
 import org.kuali.rice.kew.rule.RuleBaseValues;
 34  
 import org.kuali.rice.kew.rule.RuleDelegation;
 35  
 import org.kuali.rice.kew.rule.RuleExtension;
 36  
 import org.kuali.rice.kew.rule.RuleResponsibility;
 37  
 import org.kuali.rice.kew.rule.dao.RuleDelegationDAO;
 38  
 import org.kuali.rice.kew.util.KEWConstants;
 39  
 import org.kuali.rice.kim.api.entity.principal.Principal;
 40  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 41  
 
 42  
 import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
 43  
 
 44  
 
 45  0
 public class RuleDelegationDAOOjbImpl extends PersistenceBrokerDaoSupport implements RuleDelegationDAO {
 46  
 
 47  
     public List findByDelegateRuleId(Long ruleId) {
 48  0
         Criteria crit = new Criteria();
 49  0
         crit.addEqualTo("delegateRuleId", ruleId);
 50  0
         return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleDelegation.class, crit));
 51  
     }
 52  
 
 53  
     public void save(RuleDelegation ruleDelegation) {
 54  0
             this.getPersistenceBrokerTemplate().store(ruleDelegation);
 55  0
     }
 56  
     public List findAllCurrentRuleDelegations(){
 57  0
         Criteria crit = new Criteria();
 58  0
         crit.addEqualTo("delegationRuleBaseValues.currentInd", true);
 59  0
         return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleDelegation.class, crit));
 60  
     }
 61  
 
 62  
     public RuleDelegation findByRuleDelegationId(Long ruleDelegationId){
 63  0
         Criteria crit = new Criteria();
 64  0
         crit.addEqualTo("ruleDelegationId", ruleDelegationId);
 65  0
         return (RuleDelegation) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(RuleDelegation.class, crit));
 66  
 
 67  
     }
 68  
     public void delete(Long ruleDelegationId){
 69  0
             this.getPersistenceBrokerTemplate().delete(findByRuleDelegationId(ruleDelegationId));
 70  0
     }
 71  
 
 72  
     public List<RuleDelegation> findByResponsibilityIdWithCurrentRule(Long responsibilityId) {
 73  0
             Criteria crit = new Criteria();
 74  0
             crit.addEqualTo("responsibilityId", responsibilityId);
 75  0
             crit.addEqualTo("delegationRuleBaseValues.currentInd", true);
 76  0
             Collection delegations = getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleDelegation.class, crit));
 77  0
             return new ArrayList<RuleDelegation>(delegations);
 78  
     }
 79  
 
 80  
     /**
 81  
      * This overridden method ...
 82  
      *
 83  
      * @see org.kuali.rice.kew.rule.dao.RuleDelegationDAO#search(java.lang.String, java.lang.Long, java.lang.Long, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.Boolean, java.util.Map, java.lang.String)
 84  
      */
 85  
     public List<RuleDelegation> search(String parentRuleBaseVaueId, String parentResponsibilityId, String docTypeName, Long ruleId,
 86  
             Long ruleTemplateId, String ruleDescription, String workgroupId,
 87  
             String principalId, String delegationType, Boolean activeInd,
 88  
             Map extensionValues, String workflowIdDirective) {
 89  0
         Criteria crit = new Criteria(); //getSearchCriteria(docTypeName, ruleTemplateId, ruleDescription, delegationType, activeInd, extensionValues);
 90  
 
 91  0
         if (StringUtils.isNotBlank(delegationType) && !delegationType.equals(KEWConstants.DELEGATION_BOTH)) {
 92  0
                 crit.addEqualTo("delegationType", delegationType);
 93  
         }
 94  
         
 95  0
         if (StringUtils.isNotBlank(parentRuleBaseVaueId) && StringUtils.isNumeric(parentRuleBaseVaueId)) {
 96  0
             crit.addIn("responsibilityId", this.getRuleResponsibilitySubQuery(new Long(parentRuleBaseVaueId)));
 97  
         }
 98  
 
 99  0
         if (StringUtils.isNotBlank(parentResponsibilityId) && StringUtils.isNumeric(parentResponsibilityId)) {
 100  0
             crit.addEqualTo("responsibilityId", parentResponsibilityId);
 101  
         }
 102  
 
 103  0
         crit.addIn("delegateRuleId", getRuleBaseValuesSubQuery(docTypeName, ruleId,
 104  
                                                                ruleTemplateId, ruleDescription, workgroupId,
 105  
                                                                principalId, activeInd,
 106  
                                                                extensionValues, workflowIdDirective));
 107  
 
 108  0
         return (List<RuleDelegation>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleDelegation.class, crit, true));
 109  
     }
 110  
 
 111  
     /**
 112  
      * This overridden method ...
 113  
      *
 114  
      * @see org.kuali.rice.kew.rule.dao.RuleDelegationDAO#search(java.lang.String, java.lang.Long, java.lang.String, java.util.Collection, java.lang.String, java.lang.String, java.lang.Boolean, java.util.Map, java.util.Collection)
 115  
      */
 116  
     public List<RuleDelegation> search(String parentRuleBaseVaueId, String parentResponsibilityId, String docTypeName, Long ruleTemplateId,
 117  
             String ruleDescription, Collection<String> workgroupIds,
 118  
             String workflowId, String delegationType, Boolean activeInd,
 119  
             Map extensionValues, Collection actionRequestCodes) {
 120  0
         Criteria crit = new Criteria();
 121  
         
 122  0
         if (StringUtils.isNotBlank(delegationType) && !delegationType.equals(KEWConstants.DELEGATION_BOTH)) {
 123  0
                 crit.addEqualTo("delegationType", delegationType);
 124  
         }
 125  
         
 126  0
         if (StringUtils.isNotBlank(parentRuleBaseVaueId) && StringUtils.isNumeric(parentRuleBaseVaueId)) {
 127  0
             crit.addIn("responsibilityId", this.getRuleResponsibilitySubQuery(new Long(parentRuleBaseVaueId)));
 128  
         }
 129  
 
 130  0
         if (StringUtils.isNotBlank(parentResponsibilityId) && StringUtils.isNumeric(parentResponsibilityId)) {
 131  0
             crit.addEqualTo("responsibilityId", parentResponsibilityId);
 132  
         }
 133  
 
 134  0
         crit.addIn("delegateRuleId", getRuleBaseValuesSubQuery(docTypeName, ruleTemplateId,
 135  
                                                                ruleDescription, workgroupIds,
 136  
                                                                workflowId, activeInd,
 137  
                                                                extensionValues, actionRequestCodes));
 138  0
        return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleDelegation.class, crit, true));
 139  
     }
 140  
 
 141  
     private ReportQueryByCriteria getResponsibilitySubQuery(String ruleResponsibilityName) {
 142  0
         Criteria responsibilityCrit = new Criteria();
 143  0
         responsibilityCrit.addLike("ruleResponsibilityName", ruleResponsibilityName);
 144  0
         ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibility.class, responsibilityCrit);
 145  0
         query.setAttributes(new String[] { "ruleBaseValuesId" });
 146  0
         return query;
 147  
     }
 148  
 
 149  
     private ReportQueryByCriteria getWorkgroupResponsibilitySubQuery(Set<Long> workgroupIds) {
 150  0
             Set<String> workgroupIdStrings = new HashSet<String>();
 151  0
             for (Long workgroupId : workgroupIds) {
 152  0
                 workgroupIdStrings.add(workgroupId.toString());
 153  
             }
 154  0
         Criteria responsibilityCrit = new Criteria();
 155  0
         responsibilityCrit.addIn("ruleResponsibilityName", workgroupIds);
 156  0
         responsibilityCrit.addEqualTo("ruleResponsibilityType", KEWConstants.RULE_RESPONSIBILITY_GROUP_ID);
 157  0
         ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibility.class, responsibilityCrit);
 158  0
         query.setAttributes(new String[] { "ruleBaseValuesId" });
 159  0
         return query;
 160  
     }
 161  
 
 162  
     private ReportQueryByCriteria getRuleBaseValuesSubQuery(String docTypeName, Long ruleTemplateId,
 163  
             String ruleDescription, Collection<String> workgroupIds,
 164  
             String workflowId, Boolean activeInd,
 165  
             Map extensionValues, Collection actionRequestCodes) {
 166  0
         Criteria crit = getSearchCriteria(docTypeName, ruleTemplateId, ruleDescription, activeInd, extensionValues);
 167  0
         crit.addIn("responsibilities.ruleBaseValuesId", getResponsibilitySubQuery(workgroupIds, workflowId, actionRequestCodes, (workflowId != null), ((workgroupIds != null) && !workgroupIds.isEmpty())));
 168  0
         crit.addEqualTo("delegateRule", 1);
 169  0
         ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleBaseValues.class, crit);
 170  0
         query.setAttributes(new String[] { "ruleBaseValuesId" });
 171  0
         return query;
 172  
     }
 173  
 
 174  
     private ReportQueryByCriteria getRuleBaseValuesSubQuery(String docTypeName, Long ruleId,
 175  
             Long ruleTemplateId, String ruleDescription, String workgroupId,
 176  
             String principalId, Boolean activeInd,
 177  
             Map extensionValues, String workflowIdDirective) {
 178  0
         Criteria crit = getSearchCriteria(docTypeName, ruleTemplateId, ruleDescription, activeInd, extensionValues);
 179  0
         if (ruleId != null) {
 180  0
             crit.addEqualTo("ruleBaseValuesId", ruleId);
 181  
         }
 182  0
         if (workgroupId != null) {
 183  0
             crit.addIn("responsibilities.ruleBaseValuesId", getResponsibilitySubQuery(workgroupId));
 184  
         }
 185  0
         List<String> workgroupIds = new ArrayList<String>();
 186  0
         Boolean searchUser = Boolean.FALSE;
 187  0
         Boolean searchUserInWorkgroups = Boolean.FALSE;
 188  0
         if (workflowIdDirective != null) {/** IU patch EN-1552 */
 189  0
             if ("group".equals(workflowIdDirective)) {
 190  0
                 searchUserInWorkgroups = Boolean.TRUE;
 191  0
             } else if ("".equals(workflowIdDirective)) {
 192  0
                 searchUser = Boolean.TRUE;
 193  0
                 searchUserInWorkgroups = Boolean.TRUE;
 194  
             } else {
 195  0
                 searchUser = Boolean.TRUE;
 196  
             }
 197  
         }
 198  0
         if (!org.apache.commons.lang.StringUtils.isEmpty(principalId) && searchUserInWorkgroups)
 199  
         {
 200  0
             Principal principal = KimApiServiceLocator.getIdentityManagementService().getPrincipal(principalId);
 201  
 
 202  0
             if (principal == null)
 203  
             {
 204  0
                 throw new RiceRuntimeException("Failed to locate user for the given workflow id: " + principalId);
 205  
             }
 206  0
             workgroupIds = KimApiServiceLocator.getIdentityManagementService().getGroupIdsForPrincipal(principalId);
 207  
         }
 208  0
         crit.addIn("responsibilities.ruleBaseValuesId", getResponsibilitySubQuery(workgroupIds, principalId, searchUser, searchUserInWorkgroups));
 209  0
         crit.addEqualTo("delegateRule", 1);
 210  0
         ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleBaseValues.class, crit);
 211  0
         query.setAttributes(new String[] { "ruleBaseValuesId" });
 212  0
         return query;
 213  
         //return (List<RuleDelegation>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleDelegation.class, crit, true));
 214  
     }
 215  
 
 216  
     private ReportQueryByCriteria getRuleResponsibilitySubQuery(Long ruleBaseValuesId) {
 217  0
         Criteria crit = new Criteria();
 218  0
         crit.addEqualTo("ruleBaseValuesId", ruleBaseValuesId);
 219  0
         ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibility.class, crit);
 220  0
         query.setAttributes(new String[] { "responsibilityId" });
 221  0
         return query;
 222  
         //return getResponsibilitySubQuery(workgroupIdStrings,workflowId,new ArrayList<String>(), searchUser, searchUserInWorkgroups);
 223  
     }
 224  
 
 225  
     private ReportQueryByCriteria getResponsibilitySubQuery(List<String> workgroupIds, String workflowId, Boolean searchUser, Boolean searchUserInWorkgroups) {
 226  0
         Collection<String> workgroupIdStrings = new ArrayList<String>();
 227  0
         for (String workgroupId : workgroupIds) {
 228  0
             workgroupIdStrings.add(workgroupId);
 229  
         }
 230  0
         return getResponsibilitySubQuery(workgroupIdStrings,workflowId,new ArrayList<String>(), searchUser, searchUserInWorkgroups);
 231  
     }
 232  
 
 233  
     private ReportQueryByCriteria getResponsibilitySubQuery(Collection<String> workgroupIds, String workflowId, Collection actionRequestCodes, Boolean searchUser, Boolean searchUserInWorkgroups) {
 234  0
         Criteria responsibilityCrit = new Criteria();
 235  0
         if ( (actionRequestCodes != null) && (!actionRequestCodes.isEmpty()) ) {
 236  0
             responsibilityCrit.addIn("actionRequestedCd", actionRequestCodes);
 237  
         }
 238  
 
 239  0
         Criteria ruleResponsibilityNameCrit = null;
 240  0
         if (!org.apache.commons.lang.StringUtils.isEmpty(workflowId)) {
 241  
             // workflow user id exists
 242  0
             if (searchUser != null && searchUser) {
 243  
                 // searching user wishes to search for rules specific to user
 244  0
                 ruleResponsibilityNameCrit = new Criteria();
 245  0
                 ruleResponsibilityNameCrit.addLike("ruleResponsibilityName", workflowId);
 246  0
                 ruleResponsibilityNameCrit.addEqualTo("ruleResponsibilityType", KEWConstants.RULE_RESPONSIBILITY_WORKFLOW_ID);
 247  
             }
 248  0
             if ( (searchUserInWorkgroups != null && searchUserInWorkgroups) && (workgroupIds != null) && (!workgroupIds.isEmpty()) ) {
 249  
                 // at least one workgroup id exists and user wishes to search on workgroups
 250  0
                 if (ruleResponsibilityNameCrit == null) {
 251  0
                     ruleResponsibilityNameCrit = new Criteria();
 252  
                 }
 253  0
                 Criteria workgroupCrit = new Criteria();
 254  0
                 workgroupCrit.addIn("ruleResponsibilityName", workgroupIds);
 255  0
                 workgroupCrit.addEqualTo("ruleResponsibilityType", KEWConstants.RULE_RESPONSIBILITY_GROUP_ID);
 256  0
                 ruleResponsibilityNameCrit.addOrCriteria(workgroupCrit);
 257  0
             }
 258  0
         } else if ( (workgroupIds != null) && (workgroupIds.size() == 1) ) {
 259  
             // no user and one workgroup id
 260  0
             ruleResponsibilityNameCrit = new Criteria();
 261  0
             ruleResponsibilityNameCrit.addLike("ruleResponsibilityName", workgroupIds.iterator().next());
 262  0
             ruleResponsibilityNameCrit.addEqualTo("ruleResponsibilityType", KEWConstants.RULE_RESPONSIBILITY_GROUP_ID);
 263  0
         } else if ( (workgroupIds != null) && (workgroupIds.size() > 1) ) {
 264  
             // no user and more than one workgroup id
 265  0
             ruleResponsibilityNameCrit = new Criteria();
 266  0
             ruleResponsibilityNameCrit.addIn("ruleResponsibilityName", workgroupIds);
 267  0
             ruleResponsibilityNameCrit.addEqualTo("ruleResponsibilityType", KEWConstants.RULE_RESPONSIBILITY_GROUP_ID);
 268  
         }
 269  0
         if (ruleResponsibilityNameCrit != null) {
 270  0
             responsibilityCrit.addAndCriteria(ruleResponsibilityNameCrit);
 271  
         }
 272  
 
 273  0
         ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibility.class, responsibilityCrit);
 274  0
         query.setAttributes(new String[] { "ruleBaseValuesId" });
 275  0
         return query;
 276  
     }
 277  
 
 278  
 
 279  
     private Criteria getSearchCriteria(String docTypeName, Long ruleTemplateId, String ruleDescription, Boolean activeInd, Map extensionValues) {
 280  0
         Criteria crit = new Criteria();
 281  0
         crit.addEqualTo("currentInd", Boolean.TRUE);
 282  0
         crit.addEqualTo("templateRuleInd", Boolean.FALSE);
 283  0
         if (activeInd != null) {
 284  0
             crit.addEqualTo("activeInd", activeInd);
 285  
         }
 286  0
         if (docTypeName != null) {
 287  0
             crit.addLike("UPPER(docTypeName)", docTypeName.toUpperCase());
 288  
         }
 289  0
         if (ruleDescription != null && !ruleDescription.trim().equals("")) {
 290  0
             crit.addLike("UPPER(description)", ruleDescription.toUpperCase());
 291  
         }
 292  0
         if (ruleTemplateId != null) {
 293  0
             crit.addEqualTo("ruleTemplateId", ruleTemplateId);
 294  
         }
 295  0
         if (extensionValues != null && !extensionValues.isEmpty()) {
 296  0
             for (Iterator iter2 = extensionValues.entrySet().iterator(); iter2.hasNext();) {
 297  0
                 Map.Entry entry = (Map.Entry) iter2.next();
 298  0
                 if (!org.apache.commons.lang.StringUtils.isEmpty((String) entry.getValue())) {
 299  
                     // Criteria extensionCrit = new Criteria();
 300  
                     // extensionCrit.addEqualTo("extensionValues.key",
 301  
                     // entry.getKey());
 302  
                     // extensionCrit.addLike("extensionValues.value",
 303  
                     // "%"+(String) entry.getValue()+"%");
 304  
 
 305  0
                     Criteria extensionCrit2 = new Criteria();
 306  0
                     extensionCrit2.addEqualTo("extensionValues.key", entry.getKey());
 307  0
                     extensionCrit2.addLike("UPPER(extensionValues.value)", ("%" + (String) entry.getValue() + "%").toUpperCase());
 308  
 
 309  
                     // Criteria extensionCrit3 = new Criteria();
 310  
                     // extensionCrit3.addEqualTo("extensionValues.key",
 311  
                     // entry.getKey());
 312  
                     // extensionCrit3.addLike("extensionValues.value",
 313  
                     // ("%"+(String) entry.getValue()+"%").toLowerCase());
 314  
 
 315  
                     // extensionCrit.addOrCriteria(extensionCrit2);
 316  
                     // extensionCrit.addOrCriteria(extensionCrit3);
 317  0
                     ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleExtension.class, extensionCrit2);
 318  0
                     query.setAttributes(new String[] { "ruleBaseValuesId" });
 319  0
                     crit.addIn("ruleExtensions.ruleBaseValuesId", query);
 320  
                 }
 321  0
             }
 322  
         }
 323  0
         return crit;
 324  
     }
 325  
 }