Coverage Report - org.kuali.rice.kew.rule.dao.impl.RuleDAOOjbImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleDAOOjbImpl
0%
0/265
0%
0/124
3.448
RuleDAOOjbImpl$1
0%
0/23
0%
0/6
3.448
 
 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.PersistenceBroker;
 21  
 import org.apache.ojb.broker.query.Criteria;
 22  
 import org.apache.ojb.broker.query.QueryByCriteria;
 23  
 import org.apache.ojb.broker.query.QueryFactory;
 24  
 import org.apache.ojb.broker.query.ReportQueryByCriteria;
 25  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 26  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 27  
 import org.kuali.rice.kew.rule.RuleBaseValues;
 28  
 import org.kuali.rice.kew.rule.RuleExtension;
 29  
 import org.kuali.rice.kew.rule.RuleResponsibility;
 30  
 import org.kuali.rice.kew.rule.dao.RuleDAO;
 31  
 import org.kuali.rice.kew.util.KEWConstants;
 32  
 import org.kuali.rice.kim.api.identity.principal.Principal;
 33  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 34  
 import org.springmodules.orm.ojb.PersistenceBrokerCallback;
 35  
 import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
 36  
 
 37  
 import java.sql.PreparedStatement;
 38  
 import java.sql.ResultSet;
 39  
 import java.sql.Timestamp;
 40  
 import java.util.ArrayList;
 41  
 import java.util.Collection;
 42  
 import java.util.Date;
 43  
 import java.util.HashSet;
 44  
 import java.util.Iterator;
 45  
 import java.util.List;
 46  
 import java.util.Map;
 47  
 import java.util.Set;
 48  
 
 49  
 
 50  0
 public class RuleDAOOjbImpl extends PersistenceBrokerDaoSupport implements RuleDAO {
 51  
 
 52  0
         private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RuleDAOOjbImpl.class);
 53  
 
 54  
         private static final String OLD_DELEGATIONS_SQL =
 55  
                 "select oldDel.dlgn_rule_id "+
 56  
                 "from krew_rule_rsp_t oldRsp, krew_dlgn_rsp_t oldDel "+
 57  
                 "where oldRsp.rule_id=? and "+
 58  
                 "oldRsp.rule_rsp_id=oldDel.rule_rsp_id and "+
 59  
                 "oldDel.dlgn_rule_base_val_id not in "+
 60  
                 "(select newDel.dlgn_rule_base_val_id from krew_rule_rsp_t newRsp, krew_dlgn_rsp_t newDel "+
 61  
                 "where newRsp.rule_id=? and "+
 62  
                 "newRsp.rule_rsp_id=newDel.rule_rsp_id)";
 63  
 
 64  
         public void save(RuleBaseValues ruleBaseValues) {
 65  0
                 this.getPersistenceBrokerTemplate().store(ruleBaseValues);
 66  0
         }
 67  
 
 68  
         public List<RuleBaseValues> fetchAllCurrentRulesForTemplateDocCombination(String ruleTemplateId, List documentTypes) {
 69  0
                 Criteria crit = new Criteria();
 70  0
                 crit.addIn("docTypeName", documentTypes);
 71  0
                 crit.addEqualTo("ruleTemplateId", ruleTemplateId);
 72  0
                 crit.addEqualTo("currentInd", Boolean.TRUE);
 73  0
                 crit.addEqualTo("activeInd", Boolean.TRUE);
 74  0
                 crit.addEqualTo("delegateRule", Boolean.FALSE);
 75  0
                 crit.addEqualTo("templateRuleInd", Boolean.FALSE);
 76  
 
 77  0
                 crit.addAndCriteria(generateFromToDateCriteria(new Date()));
 78  0
                 return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleBaseValues.class, crit));
 79  
         }
 80  
 
 81  
         public List<RuleBaseValues> fetchAllCurrentRulesForTemplateDocCombination(String ruleTemplateId, List documentTypes, Timestamp effectiveDate) {
 82  0
                 Criteria crit = new Criteria();
 83  0
                 crit.addIn("docTypeName", documentTypes);
 84  0
                 crit.addEqualTo("ruleTemplateId", ruleTemplateId);
 85  0
                 crit.addEqualTo("activeInd", Boolean.TRUE);
 86  0
                 crit.addEqualTo("delegateRule", Boolean.FALSE);
 87  0
                 crit.addEqualTo("templateRuleInd", Boolean.FALSE);
 88  0
                 if (effectiveDate != null) {
 89  0
                         crit.addLessOrEqualThan("activationDate", effectiveDate);
 90  0
                         crit.addGreaterThan("deactivationDate", effectiveDate);
 91  
                 }
 92  
 
 93  0
                 crit.addAndCriteria(generateFromToDateCriteria(new Date()));
 94  0
                 return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleBaseValues.class, crit));
 95  
         }
 96  
 
 97  
         public Criteria generateFromToDateCriteria(Date date) {
 98  0
                 Criteria crit = new Criteria();
 99  
 
 100  0
                 Criteria fromCrit = new Criteria();
 101  0
                 Criteria fromNullCrit = new Criteria();
 102  0
                 fromNullCrit.addIsNull("fromDate");
 103  0
                 Criteria fromLessOrEqualCrit = new Criteria();
 104  0
                 fromLessOrEqualCrit.addLessOrEqualThan("fromDate", new Timestamp(date.getTime()));
 105  0
                 fromCrit.addOrCriteria(fromNullCrit);
 106  0
                 fromCrit.addOrCriteria(fromLessOrEqualCrit);
 107  
 
 108  0
                 Criteria toCrit = new Criteria();
 109  0
                 Criteria toNullCrit = new Criteria();
 110  0
                 toNullCrit.addIsNull("toDate");
 111  0
                 Criteria toGreaterOrEqualCrit = new Criteria();
 112  0
                 toGreaterOrEqualCrit.addGreaterOrEqualThan("toDate", new Timestamp(date.getTime()));
 113  0
                 toCrit.addOrCriteria(toNullCrit);
 114  0
                 toCrit.addOrCriteria(toGreaterOrEqualCrit);
 115  
 
 116  0
                 crit.addAndCriteria(fromCrit);
 117  0
                 crit.addAndCriteria(toCrit);
 118  
 
 119  0
                 return crit;
 120  
         }
 121  
 
 122  
         public List<RuleBaseValues> fetchAllRules(boolean currentRules) {
 123  0
                 Criteria crit = new Criteria();
 124  0
                 crit.addEqualTo("currentInd", new Boolean(currentRules));
 125  0
                 crit.addEqualTo("templateRuleInd", Boolean.FALSE);
 126  
                 // crit.addEqualTo("delegateRule", Boolean.FALSE);
 127  
 
 128  0
                 QueryByCriteria query = new QueryByCriteria(RuleBaseValues.class, crit);
 129  0
                 query.addOrderByDescending("activationDate");
 130  
 
 131  0
                 return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
 132  
         }
 133  
 
 134  
         public void delete(String ruleBaseValuesId) {
 135  0
                 this.getPersistenceBrokerTemplate().delete(findRuleBaseValuesById(ruleBaseValuesId));
 136  0
         }
 137  
 
 138  
         public List<RuleBaseValues> findByDocumentId(String documentId) {
 139  0
                 Criteria crit = new Criteria();
 140  0
                 crit.addEqualTo("documentId", documentId);
 141  0
                 return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleBaseValues.class, crit));
 142  
         }
 143  
 
 144  
     public RuleBaseValues findRuleBaseValuesByName(String name) {
 145  0
         Criteria crit = new Criteria();
 146  0
         crit.addEqualTo("name", name);
 147  0
         crit.addEqualTo("currentInd", Boolean.TRUE);
 148  0
         return (RuleBaseValues) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(RuleBaseValues.class, crit));
 149  
     }
 150  
 
 151  
         public RuleBaseValues findRuleBaseValuesById(String ruleBaseValuesId) {
 152  0
                 Criteria crit = new Criteria();
 153  0
                 crit.addEqualTo("ruleBaseValuesId", ruleBaseValuesId);
 154  
                 // crit.addEqualTo("currentInd", new Boolean(true));
 155  0
                 return (RuleBaseValues) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(RuleBaseValues.class, crit));
 156  
         }
 157  
 
 158  
         public List<RuleBaseValues> findRuleBaseValuesByResponsibilityReviewer(String reviewerName, String type) {
 159  0
                 Criteria crit = new Criteria();
 160  0
                 crit.addEqualTo("ruleResponsibilityName", reviewerName);
 161  0
                 crit.addEqualTo("ruleResponsibilityType", type);
 162  
 
 163  0
                 List responsibilities = (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleResponsibility.class, crit));
 164  0
                 List rules = new ArrayList();
 165  
 
 166  0
                 for (Iterator iter = responsibilities.iterator(); iter.hasNext();) {
 167  0
                         RuleResponsibility responsibility = (RuleResponsibility) iter.next();
 168  0
                         RuleBaseValues rule = responsibility.getRuleBaseValues();
 169  0
                         if (rule != null && rule.getCurrentInd() != null && rule.getCurrentInd().booleanValue()) {
 170  0
                                 rules.add(rule);
 171  
                         }
 172  0
                 }
 173  0
                 return rules;
 174  
         }
 175  
 
 176  
         public List<RuleBaseValues> findRuleBaseValuesByResponsibilityReviewerTemplateDoc(String ruleTemplateName, String documentType, String reviewerName, String type) {
 177  0
             Criteria crit = new Criteria();
 178  0
                 crit.addEqualTo("ruleResponsibilityName", reviewerName);
 179  0
                 crit.addEqualTo("ruleResponsibilityType", type);
 180  0
                 crit.addEqualTo("ruleBaseValues.currentInd", Boolean.TRUE);
 181  0
                 if (!StringUtils.isBlank(ruleTemplateName)) {
 182  0
                     crit.addLike("ruleBaseValues.ruleTemplate.name", ruleTemplateName.replace("*", "%").concat("%"));
 183  
                 }
 184  0
                 if (!StringUtils.isBlank(documentType)) {
 185  0
                     crit.addLike("ruleBaseValues.docTypeName", documentType.replace("*", "%").concat("%"));
 186  
                 }
 187  
 
 188  0
                 List responsibilities = (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleResponsibility.class, crit));
 189  0
                 List rules = new ArrayList();
 190  
 
 191  0
                 for (Iterator iter = responsibilities.iterator(); iter.hasNext();) {
 192  0
                         RuleResponsibility responsibility = (RuleResponsibility) iter.next();
 193  0
                         RuleBaseValues rule = responsibility.getRuleBaseValues();
 194  0
                         if (rule != null && rule.getCurrentInd() != null && rule.getCurrentInd().booleanValue()) {
 195  0
                                 rules.add(rule);
 196  
                         }
 197  0
                 }
 198  0
                 return rules;
 199  
         }
 200  
 
 201  
         public List findRuleBaseValuesByObjectGraph(RuleBaseValues ruleBaseValues) {
 202  0
                 ruleBaseValues.setCurrentInd(Boolean.TRUE);
 203  0
                 ruleBaseValues.setTemplateRuleInd(Boolean.FALSE);
 204  0
                 return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ruleBaseValues));
 205  
         }
 206  
 
 207  
         public RuleResponsibility findRuleResponsibility(String responsibilityId) {
 208  
                                 
 209  
                 ReportQueryByCriteria subQuery;
 210  0
                 Criteria subCrit = new Criteria();
 211  0
                 Criteria crit2 = new Criteria();
 212  0
                 subCrit.addLike("responsibilityId", responsibilityId);
 213  0
                 subQuery = QueryFactory.newReportQuery(RuleResponsibility.class, subCrit);
 214  0
                 subQuery.setAttributes(new String[] {"RULE_ID"});
 215  0
                 crit2.addIn("ruleBaseValuesId", subQuery);
 216  0
                 crit2.addEqualTo("currentInd", 1);
 217  0
                 QueryByCriteria outerQuery = QueryFactory.newQuery(RuleBaseValues.class, crit2);
 218  0
                 RuleBaseValues rbv = (RuleBaseValues)this.getPersistenceBrokerTemplate().getObjectByQuery(outerQuery);
 219  0
                 Criteria finalCrit = new Criteria();
 220  0
                 finalCrit.addEqualTo("responsibilityId", responsibilityId);
 221  0
                 finalCrit.addEqualTo("ruleBaseValuesId", rbv.getRuleBaseValuesId());
 222  0
                 RuleResponsibility rResp = (RuleResponsibility)this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(RuleResponsibility.class, finalCrit));
 223  
                 
 224  0
                 if(rResp != null){
 225  0
                         if(rResp.getRuleBaseValuesId().equals(rbv.getRuleBaseValuesId())){
 226  0
                                 return rResp;
 227  
                         }
 228  
                 }        
 229  
         
 230  0
                 return null;
 231  
         }
 232  
 
 233  
         public List<RuleBaseValues> search(String docTypeName, String ruleId, String ruleTemplateId, String ruleDescription, String workgroupId, String principalId, Boolean delegateRule, Boolean activeInd, Map extensionValues, String workflowIdDirective) {
 234  0
         Criteria crit = getSearchCriteria(docTypeName, ruleTemplateId, ruleDescription, delegateRule, activeInd, extensionValues);
 235  0
         if (ruleId != null) {
 236  0
             crit.addEqualTo("ruleBaseValuesId", ruleId);
 237  
         }
 238  0
         if (workgroupId != null) {
 239  0
             crit.addIn("responsibilities.ruleBaseValuesId", getResponsibilitySubQuery(workgroupId));
 240  
         }
 241  0
         List<String> workgroupIds = new ArrayList<String>();
 242  0
         Boolean searchUser = Boolean.FALSE;
 243  0
         Boolean searchUserInWorkgroups = Boolean.FALSE;
 244  
         
 245  0
         if ("group".equals(workflowIdDirective)) {
 246  0
             searchUserInWorkgroups = Boolean.TRUE;
 247  0
         } else if (StringUtils.isBlank(workflowIdDirective)) {
 248  0
             searchUser = Boolean.TRUE;
 249  0
             searchUserInWorkgroups = Boolean.TRUE;
 250  
         } else {
 251  0
             searchUser = Boolean.TRUE;
 252  
         }
 253  
         
 254  0
         if (!org.apache.commons.lang.StringUtils.isEmpty(principalId) && searchUserInWorkgroups)
 255  
         {
 256  0
             Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
 257  
 
 258  0
             if (principal == null)
 259  
             {
 260  0
                     throw new RiceRuntimeException("Failed to locate user for the given workflow id: " + principalId);
 261  
             }
 262  0
             workgroupIds = KimApiServiceLocator.getGroupService().getGroupIdsForPrincipal(principalId);
 263  
         }
 264  0
         ReportQueryByCriteria query = getResponsibilitySubQuery(workgroupIds, principalId, searchUser, searchUserInWorkgroups);
 265  0
         if (query != null) {
 266  0
                 crit.addIn("responsibilities.ruleBaseValuesId", query);
 267  
         }
 268  
 
 269  0
                 return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleBaseValues.class, crit, true));
 270  
         }
 271  
 
 272  
     public List<RuleBaseValues> search(String docTypeName, String ruleTemplateId, String ruleDescription, Collection<String> workgroupIds, String workflowId, Boolean delegateRule, Boolean activeInd, Map extensionValues, Collection actionRequestCodes) {
 273  0
             List results = null;
 274  0
         Criteria crit = getSearchCriteria(docTypeName, ruleTemplateId, ruleDescription, delegateRule, activeInd, extensionValues);
 275  0
         ReportQueryByCriteria query = getResponsibilitySubQuery(workgroupIds, workflowId, actionRequestCodes, (workflowId != null), ((workgroupIds != null) && !workgroupIds.isEmpty()));
 276  0
         if (query != null) {
 277  0
                 crit.addIn("responsibilities.ruleBaseValuesId", query);
 278  
         }
 279  0
         results = (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleBaseValues.class, crit, true));
 280  0
         return results;
 281  
     }
 282  
 
 283  
     private ReportQueryByCriteria getResponsibilitySubQuery(List<String> workgroupIds, String workflowId, Boolean searchUser, Boolean searchUserInWorkgroups) {
 284  0
         Collection<String> workgroupIdStrings = new ArrayList<String>();
 285  0
         for (String workgroupId : workgroupIds) {
 286  0
             workgroupIdStrings.add(workgroupId);
 287  
         }
 288  0
         return getResponsibilitySubQuery(workgroupIdStrings,workflowId,new ArrayList<String>(), searchUser, searchUserInWorkgroups);
 289  
     }
 290  
     
 291  
     private ReportQueryByCriteria getResponsibilitySubQuery(Collection<String> workgroupIds, String workflowId, Collection actionRequestCodes, Boolean searchUser, Boolean searchUserInWorkgroups) {
 292  0
         Criteria responsibilityCrit = null;
 293  0
         if ( (actionRequestCodes != null) && (!actionRequestCodes.isEmpty()) ) {
 294  0
                 responsibilityCrit = new Criteria();
 295  0
             responsibilityCrit.addIn("actionRequestedCd", actionRequestCodes);
 296  
         }
 297  
 
 298  0
         ReportQueryByCriteria query = null;
 299  0
         Criteria ruleResponsibilityNameCrit = null;
 300  0
         if (!org.apache.commons.lang.StringUtils.isEmpty(workflowId)) {
 301  
             // workflow user id exists
 302  0
             if (searchUser != null && searchUser) {
 303  
                 // searching user wishes to search for rules specific to user
 304  0
                 ruleResponsibilityNameCrit = new Criteria();
 305  0
                 ruleResponsibilityNameCrit.addLike("ruleResponsibilityName", workflowId);
 306  0
                 ruleResponsibilityNameCrit.addEqualTo("ruleResponsibilityType", KEWConstants.RULE_RESPONSIBILITY_WORKFLOW_ID);
 307  
             }
 308  0
             if ( (searchUserInWorkgroups != null && searchUserInWorkgroups) && (workgroupIds != null) && (!workgroupIds.isEmpty()) ) {
 309  
                 // at least one workgroup id exists and user wishes to search on workgroups
 310  0
                 if (ruleResponsibilityNameCrit == null) {
 311  0
                     ruleResponsibilityNameCrit = new Criteria();
 312  
                 }
 313  0
                 Criteria workgroupCrit = new Criteria();
 314  0
                 workgroupCrit.addIn("ruleResponsibilityName", workgroupIds);
 315  0
                 workgroupCrit.addEqualTo("ruleResponsibilityType", KEWConstants.RULE_RESPONSIBILITY_GROUP_ID);
 316  0
                 ruleResponsibilityNameCrit.addOrCriteria(workgroupCrit);
 317  0
             }
 318  0
         } else if ( (workgroupIds != null) && (workgroupIds.size() == 1) ) {
 319  
             // no user and one workgroup id
 320  0
             ruleResponsibilityNameCrit = new Criteria();
 321  0
             ruleResponsibilityNameCrit.addLike("ruleResponsibilityName", workgroupIds.iterator().next());
 322  0
             ruleResponsibilityNameCrit.addEqualTo("ruleResponsibilityType", KEWConstants.RULE_RESPONSIBILITY_GROUP_ID);
 323  0
         } else if ( (workgroupIds != null) && (workgroupIds.size() > 1) ) {
 324  
             // no user and more than one workgroup id
 325  0
             ruleResponsibilityNameCrit = new Criteria();
 326  0
             ruleResponsibilityNameCrit.addIn("ruleResponsibilityName", workgroupIds);
 327  0
             ruleResponsibilityNameCrit.addEqualTo("ruleResponsibilityType", KEWConstants.RULE_RESPONSIBILITY_GROUP_ID);
 328  
         }
 329  0
         if (ruleResponsibilityNameCrit != null) {
 330  0
                 if (responsibilityCrit == null) {
 331  0
                         responsibilityCrit = new Criteria();
 332  
                 }
 333  0
             responsibilityCrit.addAndCriteria(ruleResponsibilityNameCrit);
 334  
         }
 335  0
         if (responsibilityCrit != null) {
 336  0
                 query = QueryFactory.newReportQuery(RuleResponsibility.class, responsibilityCrit);
 337  0
                 query.setAttributes(new String[] { "ruleBaseValuesId" });
 338  
         }
 339  0
         return query;
 340  
     }
 341  
 
 342  
     private Criteria getSearchCriteria(String docTypeName, String ruleTemplateId, String ruleDescription, Boolean delegateRule, Boolean activeInd, Map extensionValues) {
 343  0
         Criteria crit = new Criteria();
 344  0
         crit.addEqualTo("currentInd", Boolean.TRUE);
 345  0
         crit.addEqualTo("templateRuleInd", Boolean.FALSE);
 346  0
         if (activeInd != null) {
 347  0
             crit.addEqualTo("activeInd", activeInd);
 348  
         }
 349  0
         if (docTypeName != null) {
 350  0
             crit.addLike("UPPER(docTypeName)", docTypeName.toUpperCase());
 351  
         }
 352  0
         if (ruleDescription != null && !ruleDescription.trim().equals("")) {
 353  0
             crit.addLike("UPPER(description)", ruleDescription.toUpperCase());
 354  
         }
 355  0
         if (ruleTemplateId != null) {
 356  0
             crit.addEqualTo("ruleTemplateId", ruleTemplateId);
 357  
         }
 358  0
         if (delegateRule != null) {
 359  0
             crit.addEqualTo("delegateRule", delegateRule);
 360  
         }
 361  0
         if (extensionValues != null && !extensionValues.isEmpty()) {
 362  0
             for (Iterator iter2 = extensionValues.entrySet().iterator(); iter2.hasNext();) {
 363  0
                 Map.Entry entry = (Map.Entry) iter2.next();
 364  0
                 if (!org.apache.commons.lang.StringUtils.isEmpty((String) entry.getValue())) {
 365  
                     // Criteria extensionCrit = new Criteria();
 366  
                     // extensionCrit.addEqualTo("extensionValues.key",
 367  
                     // entry.getKey());
 368  
                     // extensionCrit.addLike("extensionValues.value",
 369  
                     // "%"+(String) entry.getValue()+"%");
 370  
 
 371  0
                     Criteria extensionCrit2 = new Criteria();
 372  0
                     extensionCrit2.addEqualTo("extensionValues.key", entry.getKey());
 373  0
                     extensionCrit2.addLike("UPPER(extensionValues.value)", ("%" + (String) entry.getValue() + "%").toUpperCase());
 374  
 
 375  
                     // Criteria extensionCrit3 = new Criteria();
 376  
                     // extensionCrit3.addEqualTo("extensionValues.key",
 377  
                     // entry.getKey());
 378  
                     // extensionCrit3.addLike("extensionValues.value",
 379  
                     // ("%"+(String) entry.getValue()+"%").toLowerCase());
 380  
 
 381  
                     // extensionCrit.addOrCriteria(extensionCrit2);
 382  
                     // extensionCrit.addOrCriteria(extensionCrit3);
 383  0
                     ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleExtension.class, extensionCrit2);
 384  0
                     query.setAttributes(new String[] { "ruleBaseValuesId" });
 385  0
                     crit.addIn("ruleExtensions.ruleBaseValuesId", query);
 386  
                 }
 387  0
             }
 388  
         }
 389  0
         return crit;
 390  
     }
 391  
 
 392  
     private Criteria getWorkgroupOrCriteria(Collection workgroupIds) {
 393  0
         Criteria responsibilityCrit = new Criteria();
 394  0
         for (Iterator iter = workgroupIds.iterator(); iter.hasNext();) {
 395  0
             String workgroupIdFromList = (String) iter.next();
 396  0
             Criteria orCriteria = new Criteria();
 397  0
             orCriteria.addLike("ruleResponsibilityName", workgroupIdFromList);
 398  0
             responsibilityCrit.addOrCriteria(orCriteria);
 399  0
         }
 400  0
         ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibility.class, responsibilityCrit);
 401  0
         query.setAttributes(new String[] { "ruleBaseValuesId" });
 402  0
         Criteria crit = new Criteria();
 403  0
         crit.addIn("responsibilities.ruleBaseValuesId", query);
 404  0
         return crit;
 405  
     }
 406  
 
 407  
         private ReportQueryByCriteria getResponsibilitySubQuery(String ruleResponsibilityName) {
 408  0
                 Criteria responsibilityCrit = new Criteria();
 409  0
                 responsibilityCrit.addLike("ruleResponsibilityName", ruleResponsibilityName);
 410  0
                 ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibility.class, responsibilityCrit);
 411  0
                 query.setAttributes(new String[] { "ruleBaseValuesId" });
 412  0
                 return query;
 413  
         }
 414  
 
 415  
         private ReportQueryByCriteria getWorkgroupResponsibilitySubQuery(Set<Long> workgroupIds) {
 416  0
                     Set<String> workgroupIdStrings = new HashSet<String>();
 417  0
                     for (Long workgroupId : workgroupIds) {
 418  0
                         workgroupIdStrings.add(workgroupId.toString());
 419  
                     }
 420  0
                 Criteria responsibilityCrit = new Criteria();
 421  0
                 responsibilityCrit.addIn("ruleResponsibilityName", workgroupIds);
 422  0
                 responsibilityCrit.addEqualTo("ruleResponsibilityType", KEWConstants.RULE_RESPONSIBILITY_GROUP_ID);
 423  0
                 ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibility.class, responsibilityCrit);
 424  0
                 query.setAttributes(new String[] { "ruleBaseValuesId" });
 425  0
                 return query;
 426  
         }
 427  
 
 428  
         public List<RuleBaseValues> findByPreviousVersionId(String previousVersionId) {
 429  0
                 Criteria crit = new Criteria();
 430  0
                 crit.addEqualTo("previousVersionId", previousVersionId);
 431  0
                 return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleBaseValues.class, crit));
 432  
         }
 433  
 
 434  
         public RuleBaseValues findDefaultRuleByRuleTemplateId(String ruleTemplateId) {
 435  0
                 Criteria crit = new Criteria();
 436  0
                 crit.addEqualTo("ruleTemplateId", ruleTemplateId);
 437  0
                 crit.addEqualTo("templateRuleInd", Boolean.TRUE);
 438  0
                 List rules = (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleBaseValues.class, crit));
 439  0
                 if (rules != null && !rules.isEmpty()) {
 440  0
                         return (RuleBaseValues) rules.get(0);
 441  
                 }
 442  0
                 return null;
 443  
         }
 444  
 
 445  
         public void clearCache() {
 446  0
                 this.getPersistenceBroker(false).clearCache();
 447  0
         }
 448  
 
 449  
         public void retrieveAllReferences(RuleBaseValues rule) {
 450  
                 // getPersistenceBroker().retrieveAllReferences(rule);
 451  0
         }
 452  
 
 453  
         public RuleBaseValues getParentRule(String ruleBaseValuesId) {
 454  0
                 Criteria criteria = new Criteria();
 455  0
                 criteria.addEqualTo("currentInd", Boolean.TRUE);
 456  0
                 criteria.addEqualTo("responsibilities.delegationRules.delegateRuleId", ruleBaseValuesId);
 457  0
                 Collection rules = this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleBaseValues.class, criteria));
 458  0
                 RuleBaseValues rule = null;
 459  0
                 for (Iterator iterator = rules.iterator(); iterator.hasNext();) {
 460  0
                         RuleBaseValues currentRule = (RuleBaseValues) iterator.next();
 461  0
                         if (rule == null || currentRule.getVersionNbr().intValue() > rule.getVersionNbr().intValue()) {
 462  0
                                 rule = currentRule;
 463  
                         }
 464  0
                 }
 465  0
                 return rule;
 466  
         }
 467  
 
 468  
         public List findOldDelegations(final RuleBaseValues oldRule, final RuleBaseValues newRule) {
 469  0
                 return (List)this.getPersistenceBrokerTemplate().execute(new PersistenceBrokerCallback() {
 470  
                         public Object doInPersistenceBroker(PersistenceBroker pb) {
 471  0
                                 List oldDelegations = new ArrayList();
 472  0
                                 PreparedStatement ps = null;
 473  0
                                 ResultSet rs = null;
 474  
                                 try {
 475  0
                                         ps = pb.serviceConnectionManager().getConnection().prepareStatement(OLD_DELEGATIONS_SQL);
 476  0
                                         ps.setString(1, oldRule.getRuleBaseValuesId());
 477  0
                                         ps.setString(2, newRule.getRuleBaseValuesId());
 478  0
                                         rs = ps.executeQuery();
 479  0
                                         while (rs.next()) {
 480  0
                                                 oldDelegations.add(findRuleBaseValuesById(rs.getString(1)));
 481  
                                         }
 482  0
                                 } catch (Exception e) {
 483  0
                                         throw new WorkflowRuntimeException("error saving deactivation date", e);
 484  
                                 } finally {
 485  0
                                         if (rs != null) {
 486  
                                                 try {
 487  0
                                                         rs.close();
 488  0
                                                 } catch (Exception e) {
 489  0
                                                         LOG.error("error closing result set", e);
 490  0
                                                 }
 491  
                                         }
 492  0
                                         if (ps != null) {
 493  
                                                 try {
 494  0
                                                         ps.close();
 495  0
                                                 } catch (Exception e) {
 496  0
                                                         LOG.error("error closing preparedstatement", e);
 497  0
                                                 }
 498  
                                         }
 499  
                                 }
 500  0
                                 return oldDelegations;
 501  
                         }
 502  
                 });
 503  
         }
 504  
         
 505  
         public String findResponsibilityIdForRule(String ruleName, String ruleResponsibilityName, String ruleResponsibilityType) {
 506  0
                 Criteria crit = new Criteria();
 507  0
                 crit.addEqualTo("ruleResponsibilityName", ruleResponsibilityName);
 508  0
                 crit.addEqualTo("ruleResponsibilityType", ruleResponsibilityType);
 509  0
                 crit.addEqualTo("ruleBaseValues.currentInd", Boolean.TRUE);
 510  0
                 crit.addEqualTo("ruleBaseValues.name", ruleName);
 511  0
                 List responsibilities = (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleResponsibility.class, crit));
 512  0
                 if (responsibilities != null) {
 513  0
                         for (Iterator iter = responsibilities.iterator(); iter.hasNext();) {
 514  0
                                 RuleResponsibility responsibility = (RuleResponsibility) iter.next();
 515  0
                                 return responsibility.getResponsibilityId();
 516  
                         }
 517  
                 }
 518  0
                 return null;
 519  
         }
 520  
 
 521  
 }