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