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