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