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