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