View Javadoc

1   /**
2    * Copyright 2005-2012 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  public class RuleDAOJpaImpl implements RuleDAO {
45  
46  	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  		if(ruleBaseValues.getId()==null){
63  			entityManager.persist(ruleBaseValues);
64  		}else{
65  			OrmUtils.merge(entityManager, ruleBaseValues);
66  		}
67  	}
68  
69  	public List<RuleBaseValues> fetchAllCurrentRulesForTemplateDocCombination(String ruleTemplateId, List documentTypes) {
70  		Criteria crit = new Criteria(RuleBaseValues.class.getName());
71  		crit.in("docTypeName", documentTypes);
72  		crit.eq("ruleTemplateId", ruleTemplateId);
73  		crit.eq("currentInd", Boolean.TRUE);
74  		crit.eq("active", Boolean.TRUE);
75  		crit.eq("delegateRule", Boolean.FALSE);
76  		crit.eq("templateRuleInd", Boolean.FALSE);
77  
78  		crit.and(generateFromToDateCriteria(new Date()));
79  		return (List) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
80  	}
81  
82  	public List<RuleBaseValues> fetchAllCurrentRulesForTemplateDocCombination(String ruleTemplateId, List documentTypes, Timestamp effectiveDate) {
83  		Criteria crit = new Criteria(RuleBaseValues.class.getName());
84  		crit.in("docTypeName", documentTypes);
85  		crit.eq("ruleTemplateId", ruleTemplateId);
86  		crit.eq("active", Boolean.TRUE);
87  		crit.eq("delegateRule", Boolean.FALSE);
88  		crit.eq("templateRuleInd", Boolean.FALSE);
89  		if (effectiveDate != null) {
90  			crit.lte("activationDate", effectiveDate);
91  			crit.gte("deactivationDate", effectiveDate);
92  		}
93  
94  		crit.and(generateFromToDateCriteria(new Date()));
95  		return (List) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
96  	}
97  
98  	public Criteria generateFromToDateCriteria(Date date) {
99  		Criteria crit = new Criteria(RuleBaseValues.class.getName());
100 
101 		Criteria fromCrit = new Criteria(RuleBaseValues.class.getName());
102 		Criteria fromNullCrit = new Criteria(RuleBaseValues.class.getName());
103 		fromNullCrit.isNull("fromDateValue");
104 		Criteria fromLessOrEqualCrit = new Criteria(RuleBaseValues.class.getName());
105 		fromLessOrEqualCrit.lte("fromDateValue", new Timestamp(date.getTime()));
106 		fromCrit.or(fromNullCrit);
107 		fromCrit.or(fromLessOrEqualCrit);
108 
109 		Criteria toCrit = new Criteria(RuleBaseValues.class.getName());
110 		Criteria toNullCrit = new Criteria(RuleBaseValues.class.getName());
111 		toNullCrit.isNull("toDateValue");
112 		Criteria toGreaterOrEqualCrit = new Criteria(RuleBaseValues.class.getName());
113 		toGreaterOrEqualCrit.gte("toDateValue", new Timestamp(date.getTime()));
114 		toCrit.or(toNullCrit);
115 		toCrit.or(toGreaterOrEqualCrit);
116 
117 		crit.and(fromCrit);
118 		crit.and(toCrit);
119 
120 		return crit;
121 	}
122 
123 	public List<RuleBaseValues> fetchAllRules(boolean currentRules) {
124 		Criteria crit = new Criteria(RuleBaseValues.class.getName());
125 		crit.eq("currentInd", new Boolean(currentRules));
126 		crit.eq("templateRuleInd", Boolean.FALSE);
127 		crit.orderBy("activationDate", false);
128 
129 		QueryByCriteria query = new QueryByCriteria(entityManager, crit);
130 
131 		return (List) query.toQuery().getResultList();
132 	}
133 
134 	public void delete(String ruleBaseValuesId) {
135 		entityManager.remove(entityManager.find(RuleBaseValues.class, ruleBaseValuesId));
136 	}
137 
138 	public List<RuleBaseValues> findByDocumentId(String documentId) {
139 		Criteria crit = new Criteria(RuleBaseValues.class.getName());
140 		crit.eq("documentId", documentId);
141 		return (List) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
142 	}
143 
144     public RuleBaseValues findRuleBaseValuesByName(String name) {
145         Criteria crit = new Criteria(RuleBaseValues.class.getName());
146         if (name == null) {
147         	return null;
148         }
149         crit.eq("name", name);
150         crit.eq("currentInd", Boolean.TRUE);
151        	return (RuleBaseValues) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
152     }
153 
154 	public RuleBaseValues findRuleBaseValuesById(String ruleBaseValuesId) {
155 		if (ruleBaseValuesId == null) {
156 			return null;
157 		}
158 		Criteria crit = new Criteria(RuleBaseValues.class.getName());
159 		crit.eq("id", ruleBaseValuesId);
160 		return (RuleBaseValues) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
161 	}
162 
163 	public List<RuleBaseValues> findRuleBaseValuesByResponsibilityReviewer(String reviewerName, String type) {
164 		Criteria crit = new Criteria(RuleResponsibilityBo.class.getName());
165 		crit.eq("ruleResponsibilityName", reviewerName);
166 		crit.eq("ruleResponsibilityType", type);
167 
168 		List responsibilities = (List) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
169 		List rules = new ArrayList();
170 
171 		for (Iterator iter = responsibilities.iterator(); iter.hasNext();) {
172 			RuleResponsibilityBo responsibility = (RuleResponsibilityBo) iter.next();
173 			RuleBaseValues rule = responsibility.getRuleBaseValues();
174 			if (rule != null && rule.getCurrentInd() != null && rule.getCurrentInd().booleanValue()) {
175 				rules.add(rule);
176 			}
177 		}
178 		return rules;
179 	}
180 
181 	public List<RuleBaseValues> findRuleBaseValuesByResponsibilityReviewerTemplateDoc(String ruleTemplateName, String documentType, String reviewerName, String type) {
182 	    Criteria crit = new Criteria(RuleResponsibilityBo.class.getName());
183 		crit.eq("ruleResponsibilityName", reviewerName);
184 		crit.eq("ruleResponsibilityType", type);
185 		crit.eq("ruleBaseValues.currentInd", Boolean.TRUE);
186 		if (!StringUtils.isBlank(ruleTemplateName)) {
187 		    crit.like("ruleBaseValues.ruleTemplate.name", ruleTemplateName.replace("*", "%").concat("%"));
188 		}
189 		if (!StringUtils.isBlank(documentType)) {
190 		    crit.like("ruleBaseValues.docTypeName", documentType.replace("*", "%").concat("%"));
191 		}
192 
193 		List responsibilities = (List) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
194 		List rules = new ArrayList();
195 
196 		for (Iterator iter = responsibilities.iterator(); iter.hasNext();) {
197 			RuleResponsibilityBo responsibility = (RuleResponsibilityBo) iter.next();
198 			RuleBaseValues rule = responsibility.getRuleBaseValues();
199 			if (rule != null && rule.getCurrentInd() != null && rule.getCurrentInd().booleanValue()) {
200 				rules.add(rule);
201 			}
202 		}
203 		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 		Criteria crit = new Criteria(RuleResponsibilityBo.class.getName());
215 		crit.eq("responsibilityId", responsibilityId);
216 		Collection responsibilities = new QueryByCriteria(entityManager, crit).toQuery().getResultList();
217 		for (Iterator iterator = responsibilities.iterator(); iterator.hasNext();) {
218 			RuleResponsibilityBo responsibility = (RuleResponsibilityBo) iterator.next();
219 			if (responsibility.getRuleBaseValues().getCurrentInd().booleanValue()) {
220 				return responsibility;
221 			}
222 		}
223 		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         Criteria crit = getSearchCriteria(docTypeName, ruleTemplateId, ruleDescription, delegateRule, activeInd, extensionValues);
228         if (ruleId != null) {
229             crit.eq("id", ruleId);
230         }
231         if (groupId != null) {
232             //crit.in("responsibilities.ruleBaseValuesId", getResponsibilitySubQuery(groupId), "ruleBaseValuesId");
233         	addResponsibilityCriteria(crit, groupId);
234         }
235         Collection<String> kimGroupIds = new HashSet<String>();
236         Boolean searchUser = Boolean.FALSE;
237         Boolean searchUserInWorkgroups = Boolean.FALSE;
238         
239         if ("group".equals(workflowIdDirective)) {
240             searchUserInWorkgroups = Boolean.TRUE;
241         } else if (StringUtils.isBlank(workflowIdDirective)) {
242             searchUser = Boolean.TRUE;
243             searchUserInWorkgroups = Boolean.TRUE;
244         } else {
245             searchUser = Boolean.TRUE;
246         }
247         
248         if (!org.apache.commons.lang.StringUtils.isEmpty(principalId) && searchUserInWorkgroups) {
249             Principal principal = null;
250 
251             principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
252 
253             if (principal == null)
254             {
255             	throw new RiceRuntimeException("Failed to locate user for the given principal id: " + principalId);
256             }
257             kimGroupIds = KimApiServiceLocator.getGroupService().getGroupIdsByPrincipalId(principalId);
258         }
259         addResponsibilityCriteria(crit, kimGroupIds, principalId, searchUser, searchUserInWorkgroups);
260         //if (responsibilityCrit != null) {
261         	//crit.in("responsibilities.ruleBaseValuesId", responsibilityCrit,"ruleBaseValuesId");
262         //}
263         crit.distinct(true);
264 		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         Criteria crit = getSearchCriteria(docTypeName, ruleTemplateId, ruleDescription, delegateRule, activeInd, extensionValues);
269         addResponsibilityCriteria(crit, workgroupIds, workflowId, actionRequestCodes, (workflowId != null), ((workgroupIds != null) && !workgroupIds.isEmpty()));
270         //if (responsibilityCrit != null) {
271         	//crit.in("responsibilities.ruleBaseValuesId", responsibilityCrit, "ruleBaseValuesId");
272         //}
273         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         Collection<String> workgroupIdStrings = new ArrayList<String>();
278         for (String workgroupId : kimGroupIds) {
279             workgroupIdStrings.add(workgroupId.toString());
280         }
281         addResponsibilityCriteria(parentCrit, workgroupIdStrings,principalId,new ArrayList<String>(), searchUser, searchUserInWorkgroups);
282     }
283     
284     private void addResponsibilityCriteria(Criteria parentCrit, Collection<String> workgroupIds, String workflowId, Collection actionRequestCodes, Boolean searchUser, Boolean searchUserInWorkgroups) {
285         Criteria responsibilityCrit = null;
286 
287         Criteria ruleResponsibilityNameCrit = null;
288         
289         if ( (actionRequestCodes != null) && (!actionRequestCodes.isEmpty()) ) {
290         	responsibilityCrit = new Criteria(RuleBaseValues.class.getName(), false);
291             responsibilityCrit.in("__JPA_ALIAS[['rr']]__.actionRequestedCd", new ArrayList(actionRequestCodes));
292         }
293         
294         if (!org.apache.commons.lang.StringUtils.isEmpty(workflowId)) {
295             // workflow user id exists
296             if (searchUser != null && searchUser) {
297                 // searching user wishes to search for rules specific to user
298                 ruleResponsibilityNameCrit = new Criteria(RuleBaseValues.class.getName(), false);
299                 ruleResponsibilityNameCrit.like("__JPA_ALIAS[['rr']]__.ruleResponsibilityName", workflowId);
300                 ruleResponsibilityNameCrit.eq("__JPA_ALIAS[['rr']]__.ruleResponsibilityType", KewApiConstants.RULE_RESPONSIBILITY_WORKFLOW_ID);
301             }
302             if ( (searchUserInWorkgroups != null && searchUserInWorkgroups) && (workgroupIds != null) && (!workgroupIds.isEmpty()) ) {
303                 // at least one workgroup id exists and user wishes to search on workgroups
304                 if (ruleResponsibilityNameCrit == null) {
305                     ruleResponsibilityNameCrit = new Criteria(RuleBaseValues.class.getName(), false);
306                 }
307                 Criteria workgroupCrit = new Criteria(RuleBaseValues.class.getName(), false);
308                 workgroupCrit.in("__JPA_ALIAS[['rr']]__.ruleResponsibilityName", new ArrayList<String>(workgroupIds));
309                 workgroupCrit.eq("__JPA_ALIAS[['rr']]__.ruleResponsibilityType", KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID);
310                 ruleResponsibilityNameCrit.or(workgroupCrit);
311             }
312         } else if ( (workgroupIds != null) && (workgroupIds.size() == 1) ) {
313             // no user and one workgroup id
314             ruleResponsibilityNameCrit = new Criteria(RuleBaseValues.class.getName(), false);
315             ruleResponsibilityNameCrit.like("__JPA_ALIAS[['rr']]__.ruleResponsibilityName", workgroupIds.iterator().next());
316             ruleResponsibilityNameCrit.eq("__JPA_ALIAS[['rr']]__.ruleResponsibilityType", KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID);
317         } else if ( (workgroupIds != null) && (workgroupIds.size() > 1) ) {
318             // no user and more than one workgroup id
319             ruleResponsibilityNameCrit = new Criteria(RuleBaseValues.class.getName(), false);
320             ruleResponsibilityNameCrit.in("__JPA_ALIAS[['rr']]__.ruleResponsibilityName",  new ArrayList<String>(workgroupIds));
321             ruleResponsibilityNameCrit.eq("__JPA_ALIAS[['rr']]__.ruleResponsibilityType", KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID);
322         }
323 
324         if (ruleResponsibilityNameCrit != null) {
325         	if (responsibilityCrit == null) {
326         		responsibilityCrit = new Criteria(RuleBaseValues.class.getName(), false);
327         	}
328             responsibilityCrit.and(ruleResponsibilityNameCrit);
329         }
330 
331         if (responsibilityCrit != null) {
332         	if (parentCrit.getAliasIndex("rr") == -1) {
333     			parentCrit.join("responsibilities", "rr", false, true);
334     		}
335         	parentCrit.and(responsibilityCrit);
336         }
337         //return responsibilityCrit;
338     }
339 
340     private Criteria getSearchCriteria(String docTypeName, String ruleTemplateId, String ruleDescription, Boolean delegateRule, Boolean activeInd, Map extensionValues) {
341         Criteria crit = new Criteria(RuleBaseValues.class.getName());
342         crit.eq("currentInd", Boolean.TRUE);
343         crit.eq("templateRuleInd", Boolean.FALSE);
344         if (activeInd != null) {
345             crit.eq("active", activeInd);
346         }
347         if (docTypeName != null) {
348             crit.like("UPPER(__JPA_ALIAS[[0]]__.docTypeName)", docTypeName.toUpperCase());
349         }
350         if (ruleDescription != null && !ruleDescription.trim().equals("")) {
351             crit.like("UPPER(__JPA_ALIAS[[0]]__.description)", ruleDescription.toUpperCase());
352         }
353         if (ruleTemplateId != null) {
354             crit.eq("ruleTemplateId", ruleTemplateId);
355         }
356         if (delegateRule != null) {
357             crit.eq("delegateRule", delegateRule);
358         }
359         if (extensionValues != null && !extensionValues.isEmpty()) {
360             for (Iterator iter2 = extensionValues.entrySet().iterator(); iter2.hasNext();) {
361                 Map.Entry entry = (Map.Entry) iter2.next();
362                 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                     Criteria extensionCrit2 = new Criteria(RuleExtensionBo.class.getName());
370                     extensionCrit2.distinct(true);
371                     extensionCrit2.join("extensionValues", "extval", false, true);
372                     extensionCrit2.eq("__JPA_ALIAS[['extval']]__.key", entry.getKey());
373                     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                     extensionCrit2.memberOf("__JPA_ALIAS[[0]]__", "__JPA_ALIAS[[-1]]__.ruleExtensions");
384                     crit.exists(extensionCrit2);
385                 }
386             }
387         }
388         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 		if (parentCrit.getAliasIndex("rr") == -1) {
408 			parentCrit.join("responsibilities", "rr", false, true);
409 		}
410 		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 	}
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 		Criteria crit = new Criteria(RuleBaseValues.class.getName());
431 		crit.eq("previousRuleId", previousRuleId);
432 		return (List) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
433 	}
434 
435 	public RuleBaseValues findDefaultRuleByRuleTemplateId(String ruleTemplateId) {
436 		Criteria crit = new Criteria(RuleBaseValues.class.getName());
437 		crit.eq("ruleTemplateId", ruleTemplateId);
438 		crit.eq("templateRuleInd", Boolean.TRUE);
439 		List rules = (List) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
440 		if (rules != null && !rules.isEmpty()) {
441 			return (RuleBaseValues) rules.get(0);
442 		}
443 		return null;
444 	}
445 
446 	public void retrieveAllReferences(RuleBaseValues rule) {
447 		// getPersistenceBroker().retrieveAllReferences(rule);
448 	}
449 
450 	public RuleBaseValues getParentRule(String ruleBaseValuesId) {
451 		Criteria criteria = new Criteria(RuleBaseValues.class.getName());
452 		criteria.eq("currentInd", Boolean.TRUE);
453 		criteria.eq("responsibilities.delegationRules.delegateRuleId", ruleBaseValuesId);
454 		Collection rules = new QueryByCriteria(entityManager, criteria).toQuery().getResultList();
455 		RuleBaseValues rule = null;
456 		for (Iterator iterator = rules.iterator(); iterator.hasNext();) {
457 			RuleBaseValues currentRule = (RuleBaseValues) iterator.next();
458 			if (rule == null || currentRule.getVersionNbr().intValue() > rule.getVersionNbr().intValue()) {
459 				rule = currentRule;
460 			}
461 		}
462 		return rule;
463 	}
464 
465 	public List findOldDelegations(final RuleBaseValues oldRule, final RuleBaseValues newRule) {
466 
467 		Query q = entityManager.createNativeQuery(OLD_DELEGATIONS_SQL);
468 		q.setParameter(1, oldRule.getId());
469 		q.setParameter(2, newRule.getId());
470 		List oldDelegations = new ArrayList();
471 		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 			oldDelegations.add(findRuleBaseValuesById(String.valueOf(l)));
474 		}
475 		return oldDelegations;
476 
477 	}
478 	
479 	public String findResponsibilityIdForRule(String ruleName, String ruleResponsibilityName, String ruleResponsibilityType) {
480 		Criteria crit = new Criteria(RuleResponsibilityBo.class.getName());
481 		crit.eq("ruleResponsibilityName", ruleResponsibilityName);
482 		crit.eq("ruleResponsibilityType", ruleResponsibilityType);
483 		crit.eq("ruleBaseValues.currentInd", Boolean.TRUE);
484 		crit.eq("ruleBaseValues.name", ruleName);
485 		Collection responsibilities = new QueryByCriteria(entityManager, crit).toQuery().getResultList();
486 		if (responsibilities != null) {
487 			for (Iterator iter = responsibilities.iterator(); iter.hasNext();) {
488 				RuleResponsibilityBo responsibility = (RuleResponsibilityBo) iter.next();
489 				return responsibility.getResponsibilityId();
490 			}
491 		}
492 		return null;
493 	}
494 
495     public EntityManager getEntityManager() {
496         return this.entityManager;
497     }
498 
499     public void setEntityManager(EntityManager entityManager) {
500         this.entityManager = entityManager;
501     }
502 
503 }