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