View Javadoc

1   /*
2    * Copyright 2005-2007 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.PreparedStatement;
20  import java.sql.ResultSet;
21  import java.sql.Timestamp;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Date;
25  import java.util.HashSet;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Set;
30  
31  import org.apache.commons.lang.StringUtils;
32  import org.apache.ojb.broker.PersistenceBroker;
33  import org.apache.ojb.broker.query.Criteria;
34  import org.apache.ojb.broker.query.QueryByCriteria;
35  import org.apache.ojb.broker.query.QueryFactory;
36  import org.apache.ojb.broker.query.ReportQueryByCriteria;
37  import org.kuali.rice.core.api.exception.RiceRuntimeException;
38  import org.kuali.rice.kew.exception.WorkflowRuntimeException;
39  import org.kuali.rice.kew.rule.RuleBaseValues;
40  import org.kuali.rice.kew.rule.RuleExtension;
41  import org.kuali.rice.kew.rule.RuleResponsibility;
42  import org.kuali.rice.kew.rule.dao.RuleDAO;
43  import org.kuali.rice.kew.util.KEWConstants;
44  import org.kuali.rice.kim.api.entity.principal.Principal;
45  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
46  
47  import org.springmodules.orm.ojb.PersistenceBrokerCallback;
48  import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
49  
50  
51  public class RuleDAOOjbImpl extends PersistenceBrokerDaoSupport implements RuleDAO {
52  
53  	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RuleDAOOjbImpl.class);
54  
55  	private static final String OLD_DELEGATIONS_SQL =
56  		"select oldDel.dlgn_rule_id "+
57  		"from krew_rule_rsp_t oldRsp, krew_dlgn_rsp_t oldDel "+
58  		"where oldRsp.rule_id=? and "+
59  		"oldRsp.rule_rsp_id=oldDel.rule_rsp_id and "+
60  		"oldDel.dlgn_rule_base_val_id not in "+
61  		"(select newDel.dlgn_rule_base_val_id from krew_rule_rsp_t newRsp, krew_dlgn_rsp_t newDel "+
62  		"where newRsp.rule_id=? and "+
63  		"newRsp.rule_rsp_id=newDel.rule_rsp_id)";
64  
65  	public void save(RuleBaseValues ruleBaseValues) {
66  		this.getPersistenceBrokerTemplate().store(ruleBaseValues);
67  	}
68  
69  	public List fetchAllCurrentRulesForTemplateDocCombination(Long ruleTemplateId, List documentTypes) {
70  		Criteria crit = new Criteria();
71  		crit.addIn("docTypeName", documentTypes);
72  		crit.addEqualTo("ruleTemplateId", ruleTemplateId);
73  		crit.addEqualTo("currentInd", Boolean.TRUE);
74  		crit.addEqualTo("activeInd", Boolean.TRUE);
75  		crit.addEqualTo("delegateRule", Boolean.FALSE);
76  		crit.addEqualTo("templateRuleInd", Boolean.FALSE);
77  
78  		crit.addAndCriteria(generateFromToDateCriteria(new Date()));
79  		return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleBaseValues.class, crit));
80  	}
81  
82  	public List fetchAllCurrentRulesForTemplateDocCombination(Long ruleTemplateId, List documentTypes, Timestamp effectiveDate) {
83  		Criteria crit = new Criteria();
84  		crit.addIn("docTypeName", documentTypes);
85  		crit.addEqualTo("ruleTemplateId", ruleTemplateId);
86  		crit.addEqualTo("activeInd", Boolean.TRUE);
87  		crit.addEqualTo("delegateRule", Boolean.FALSE);
88  		crit.addEqualTo("templateRuleInd", Boolean.FALSE);
89  		if (effectiveDate != null) {
90  			crit.addLessOrEqualThan("activationDate", effectiveDate);
91  			crit.addGreaterThan("deactivationDate", effectiveDate);
92  		}
93  
94  		crit.addAndCriteria(generateFromToDateCriteria(new Date()));
95  		return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleBaseValues.class, crit));
96  	}
97  
98  	public Criteria generateFromToDateCriteria(Date date) {
99  		Criteria crit = new Criteria();
100 
101 		Criteria fromCrit = new Criteria();
102 		Criteria fromNullCrit = new Criteria();
103 		fromNullCrit.addIsNull("fromDate");
104 		Criteria fromLessOrEqualCrit = new Criteria();
105 		fromLessOrEqualCrit.addLessOrEqualThan("fromDate", new Timestamp(date.getTime()));
106 		fromCrit.addOrCriteria(fromNullCrit);
107 		fromCrit.addOrCriteria(fromLessOrEqualCrit);
108 
109 		Criteria toCrit = new Criteria();
110 		Criteria toNullCrit = new Criteria();
111 		toNullCrit.addIsNull("toDate");
112 		Criteria toGreaterOrEqualCrit = new Criteria();
113 		toGreaterOrEqualCrit.addGreaterOrEqualThan("toDate", new Timestamp(date.getTime()));
114 		toCrit.addOrCriteria(toNullCrit);
115 		toCrit.addOrCriteria(toGreaterOrEqualCrit);
116 
117 		crit.addAndCriteria(fromCrit);
118 		crit.addAndCriteria(toCrit);
119 
120 		return crit;
121 	}
122 
123 	public List fetchAllRules(boolean currentRules) {
124 		Criteria crit = new Criteria();
125 		crit.addEqualTo("currentInd", new Boolean(currentRules));
126 		crit.addEqualTo("templateRuleInd", Boolean.FALSE);
127 		// crit.addEqualTo("delegateRule", Boolean.FALSE);
128 
129 		QueryByCriteria query = new QueryByCriteria(RuleBaseValues.class, crit);
130 		query.addOrderByDescending("activationDate");
131 
132 		return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
133 	}
134 
135 	public void delete(Long ruleBaseValuesId) {
136 		this.getPersistenceBrokerTemplate().delete(findRuleBaseValuesById(ruleBaseValuesId));
137 	}
138 
139 	public List findByDocumentId(String documentId) {
140 		Criteria crit = new Criteria();
141 		crit.addEqualTo("documentId", documentId);
142 		return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleBaseValues.class, crit));
143 	}
144 
145     public RuleBaseValues findRuleBaseValuesByName(String name) {
146         Criteria crit = new Criteria();
147         crit.addEqualTo("name", name);
148         crit.addEqualTo("currentInd", Boolean.TRUE);
149         return (RuleBaseValues) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(RuleBaseValues.class, crit));
150     }
151 
152 	public RuleBaseValues findRuleBaseValuesById(Long ruleBaseValuesId) {
153 		Criteria crit = new Criteria();
154 		crit.addEqualTo("ruleBaseValuesId", ruleBaseValuesId);
155 		// crit.addEqualTo("currentInd", new Boolean(true));
156 		return (RuleBaseValues) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(RuleBaseValues.class, crit));
157 	}
158 
159 	public List findRuleBaseValuesByResponsibilityReviewer(String reviewerName, String type) {
160 		Criteria crit = new Criteria();
161 		crit.addEqualTo("ruleResponsibilityName", reviewerName);
162 		crit.addEqualTo("ruleResponsibilityType", type);
163 
164 		List responsibilities = (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleResponsibility.class, crit));
165 		List rules = new ArrayList();
166 
167 		for (Iterator iter = responsibilities.iterator(); iter.hasNext();) {
168 			RuleResponsibility responsibility = (RuleResponsibility) iter.next();
169 			RuleBaseValues rule = responsibility.getRuleBaseValues();
170 			if (rule != null && rule.getCurrentInd() != null && rule.getCurrentInd().booleanValue()) {
171 				rules.add(rule);
172 			}
173 		}
174 		return rules;
175 	}
176 
177 	public List findRuleBaseValuesByResponsibilityReviewerTemplateDoc(String ruleTemplateName, String documentType, String reviewerName, String type) {
178 	    Criteria crit = new Criteria();
179 		crit.addEqualTo("ruleResponsibilityName", reviewerName);
180 		crit.addEqualTo("ruleResponsibilityType", type);
181 		crit.addEqualTo("ruleBaseValues.currentInd", Boolean.TRUE);
182 		if (!StringUtils.isBlank(ruleTemplateName)) {
183 		    crit.addLike("ruleBaseValues.ruleTemplate.name", ruleTemplateName.replace("*", "%").concat("%"));
184 		}
185 		if (!StringUtils.isBlank(documentType)) {
186 		    crit.addLike("ruleBaseValues.docTypeName", documentType.replace("*", "%").concat("%"));
187 		}
188 
189 		List responsibilities = (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleResponsibility.class, crit));
190 		List rules = new ArrayList();
191 
192 		for (Iterator iter = responsibilities.iterator(); iter.hasNext();) {
193 			RuleResponsibility responsibility = (RuleResponsibility) iter.next();
194 			RuleBaseValues rule = responsibility.getRuleBaseValues();
195 			if (rule != null && rule.getCurrentInd() != null && rule.getCurrentInd().booleanValue()) {
196 				rules.add(rule);
197 			}
198 		}
199 		return rules;
200 	}
201 
202 	public List findRuleBaseValuesByObjectGraph(RuleBaseValues ruleBaseValues) {
203 		ruleBaseValues.setCurrentInd(Boolean.TRUE);
204 		ruleBaseValues.setTemplateRuleInd(Boolean.FALSE);
205 		return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ruleBaseValues));
206 	}
207 
208 	public RuleResponsibility findRuleResponsibility(Long responsibilityId) {
209 				
210 		ReportQueryByCriteria subQuery;
211 		Criteria subCrit = new Criteria();
212 		Criteria crit2 = new Criteria();
213 		subCrit.addLike("responsibilityId", responsibilityId);
214 		subQuery = QueryFactory.newReportQuery(RuleResponsibility.class, subCrit);
215 		subQuery.setAttributes(new String[] {"RULE_ID"});
216 		crit2.addIn("ruleBaseValuesId", subQuery);
217 		crit2.addEqualTo("currentInd", 1);
218 		QueryByCriteria outerQuery = QueryFactory.newQuery(RuleBaseValues.class, crit2);
219 		RuleBaseValues rbv = (RuleBaseValues)this.getPersistenceBrokerTemplate().getObjectByQuery(outerQuery);
220 		Criteria finalCrit = new Criteria();
221 		finalCrit.addEqualTo("responsibilityId", responsibilityId);
222 		finalCrit.addEqualTo("ruleBaseValuesId", rbv.getRuleBaseValuesId());
223 		RuleResponsibility rResp = (RuleResponsibility)this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(RuleResponsibility.class, finalCrit));
224 		
225 		if(rResp != null){
226 			if(rResp.getRuleBaseValuesId().equals(rbv.getRuleBaseValuesId())){
227 				return rResp;
228 			}
229 		}	
230 	
231 		return null;
232 	}
233 
234 	public List search(String docTypeName, Long ruleId, Long ruleTemplateId, String ruleDescription, String workgroupId, String principalId, Boolean delegateRule, Boolean activeInd, Map extensionValues, String workflowIdDirective) {
235         Criteria crit = getSearchCriteria(docTypeName, ruleTemplateId, ruleDescription, delegateRule, activeInd, extensionValues);
236         if (ruleId != null) {
237             crit.addEqualTo("ruleBaseValuesId", ruleId);
238         }
239         if (workgroupId != null) {
240             crit.addIn("responsibilities.ruleBaseValuesId", getResponsibilitySubQuery(workgroupId));
241         }
242         List<String> workgroupIds = new ArrayList<String>();
243         Boolean searchUser = Boolean.FALSE;
244         Boolean searchUserInWorkgroups = Boolean.FALSE;
245         
246         if ("group".equals(workflowIdDirective)) {
247             searchUserInWorkgroups = Boolean.TRUE;
248         } else if (StringUtils.isBlank(workflowIdDirective)) {
249             searchUser = Boolean.TRUE;
250             searchUserInWorkgroups = Boolean.TRUE;
251         } else {
252             searchUser = Boolean.TRUE;
253         }
254         
255         if (!org.apache.commons.lang.StringUtils.isEmpty(principalId) && searchUserInWorkgroups)
256         {
257             Principal principal = KimApiServiceLocator.getIdentityManagementService().getPrincipal(principalId);
258 
259             if (principal == null)
260             {
261             	throw new RiceRuntimeException("Failed to locate user for the given workflow id: " + principalId);
262             }
263             workgroupIds = KimApiServiceLocator.getIdentityManagementService().getGroupIdsForPrincipal(principalId);
264         }
265         ReportQueryByCriteria query = getResponsibilitySubQuery(workgroupIds, principalId, searchUser, searchUserInWorkgroups);
266         if (query != null) {
267         	crit.addIn("responsibilities.ruleBaseValuesId", query);
268         }
269 
270 		return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleBaseValues.class, crit, true));
271 	}
272 
273     public List search(String docTypeName, Long ruleTemplateId, String ruleDescription, Collection<String> workgroupIds, String workflowId, Boolean delegateRule, Boolean activeInd, Map extensionValues, Collection actionRequestCodes) {
274     	List results = null;
275         Criteria crit = getSearchCriteria(docTypeName, ruleTemplateId, ruleDescription, delegateRule, activeInd, extensionValues);
276         ReportQueryByCriteria query = getResponsibilitySubQuery(workgroupIds, workflowId, actionRequestCodes, (workflowId != null), ((workgroupIds != null) && !workgroupIds.isEmpty()));
277         if (query != null) {
278         	crit.addIn("responsibilities.ruleBaseValuesId", query);
279         }
280         results = (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleBaseValues.class, crit, true));
281         return results;
282     }
283 
284     private ReportQueryByCriteria getResponsibilitySubQuery(List<String> workgroupIds, String workflowId, Boolean searchUser, Boolean searchUserInWorkgroups) {
285         Collection<String> workgroupIdStrings = new ArrayList<String>();
286         for (String workgroupId : workgroupIds) {
287             workgroupIdStrings.add(workgroupId);
288         }
289         return getResponsibilitySubQuery(workgroupIdStrings,workflowId,new ArrayList<String>(), searchUser, searchUserInWorkgroups);
290     }
291     
292     private ReportQueryByCriteria getResponsibilitySubQuery(Collection<String> workgroupIds, String workflowId, Collection actionRequestCodes, Boolean searchUser, Boolean searchUserInWorkgroups) {
293         Criteria responsibilityCrit = null;
294         if ( (actionRequestCodes != null) && (!actionRequestCodes.isEmpty()) ) {
295         	responsibilityCrit = new Criteria();
296             responsibilityCrit.addIn("actionRequestedCd", actionRequestCodes);
297         }
298 
299         ReportQueryByCriteria query = null;
300         Criteria ruleResponsibilityNameCrit = null;
301         if (!org.apache.commons.lang.StringUtils.isEmpty(workflowId)) {
302             // workflow user id exists
303             if (searchUser != null && searchUser) {
304                 // searching user wishes to search for rules specific to user
305                 ruleResponsibilityNameCrit = new Criteria();
306                 ruleResponsibilityNameCrit.addLike("ruleResponsibilityName", workflowId);
307                 ruleResponsibilityNameCrit.addEqualTo("ruleResponsibilityType", KEWConstants.RULE_RESPONSIBILITY_WORKFLOW_ID);
308             }
309             if ( (searchUserInWorkgroups != null && searchUserInWorkgroups) && (workgroupIds != null) && (!workgroupIds.isEmpty()) ) {
310                 // at least one workgroup id exists and user wishes to search on workgroups
311                 if (ruleResponsibilityNameCrit == null) {
312                     ruleResponsibilityNameCrit = new Criteria();
313                 }
314                 Criteria workgroupCrit = new Criteria();
315                 workgroupCrit.addIn("ruleResponsibilityName", workgroupIds);
316                 workgroupCrit.addEqualTo("ruleResponsibilityType", KEWConstants.RULE_RESPONSIBILITY_GROUP_ID);
317                 ruleResponsibilityNameCrit.addOrCriteria(workgroupCrit);
318             }
319         } else if ( (workgroupIds != null) && (workgroupIds.size() == 1) ) {
320             // no user and one workgroup id
321             ruleResponsibilityNameCrit = new Criteria();
322             ruleResponsibilityNameCrit.addLike("ruleResponsibilityName", workgroupIds.iterator().next());
323             ruleResponsibilityNameCrit.addEqualTo("ruleResponsibilityType", KEWConstants.RULE_RESPONSIBILITY_GROUP_ID);
324         } else if ( (workgroupIds != null) && (workgroupIds.size() > 1) ) {
325             // no user and more than one workgroup id
326             ruleResponsibilityNameCrit = new Criteria();
327             ruleResponsibilityNameCrit.addIn("ruleResponsibilityName", workgroupIds);
328             ruleResponsibilityNameCrit.addEqualTo("ruleResponsibilityType", KEWConstants.RULE_RESPONSIBILITY_GROUP_ID);
329         }
330         if (ruleResponsibilityNameCrit != null) {
331         	if (responsibilityCrit == null) {
332         		responsibilityCrit = new Criteria();
333         	}
334             responsibilityCrit.addAndCriteria(ruleResponsibilityNameCrit);
335         }
336         if (responsibilityCrit != null) {
337         	query = QueryFactory.newReportQuery(RuleResponsibility.class, responsibilityCrit);
338         	query.setAttributes(new String[] { "ruleBaseValuesId" });
339         }
340         return query;
341     }
342 
343     private Criteria getSearchCriteria(String docTypeName, Long ruleTemplateId, String ruleDescription, Boolean delegateRule, Boolean activeInd, Map extensionValues) {
344         Criteria crit = new Criteria();
345         crit.addEqualTo("currentInd", Boolean.TRUE);
346         crit.addEqualTo("templateRuleInd", Boolean.FALSE);
347         if (activeInd != null) {
348             crit.addEqualTo("activeInd", activeInd);
349         }
350         if (docTypeName != null) {
351             crit.addLike("UPPER(docTypeName)", docTypeName.toUpperCase());
352         }
353         if (ruleDescription != null && !ruleDescription.trim().equals("")) {
354             crit.addLike("UPPER(description)", ruleDescription.toUpperCase());
355         }
356         if (ruleTemplateId != null) {
357             crit.addEqualTo("ruleTemplateId", ruleTemplateId);
358         }
359         if (delegateRule != null) {
360             crit.addEqualTo("delegateRule", delegateRule);
361         }
362         if (extensionValues != null && !extensionValues.isEmpty()) {
363             for (Iterator iter2 = extensionValues.entrySet().iterator(); iter2.hasNext();) {
364                 Map.Entry entry = (Map.Entry) iter2.next();
365                 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                     Criteria extensionCrit2 = new Criteria();
373                     extensionCrit2.addEqualTo("extensionValues.key", entry.getKey());
374                     extensionCrit2.addLike("UPPER(extensionValues.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                     ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleExtension.class, extensionCrit2);
385                     query.setAttributes(new String[] { "ruleBaseValuesId" });
386                     crit.addIn("ruleExtensions.ruleBaseValuesId", query);
387                 }
388             }
389         }
390         return crit;
391     }
392 
393     private Criteria getWorkgroupOrCriteria(Collection workgroupIds) {
394         Criteria responsibilityCrit = new Criteria();
395         for (Iterator iter = workgroupIds.iterator(); iter.hasNext();) {
396             String workgroupIdFromList = (String) iter.next();
397             Criteria orCriteria = new Criteria();
398             orCriteria.addLike("ruleResponsibilityName", workgroupIdFromList);
399             responsibilityCrit.addOrCriteria(orCriteria);
400         }
401         ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibility.class, responsibilityCrit);
402         query.setAttributes(new String[] { "ruleBaseValuesId" });
403         Criteria crit = new Criteria();
404         crit.addIn("responsibilities.ruleBaseValuesId", query);
405         return crit;
406     }
407 
408 	private ReportQueryByCriteria getResponsibilitySubQuery(String ruleResponsibilityName) {
409 		Criteria responsibilityCrit = new Criteria();
410 		responsibilityCrit.addLike("ruleResponsibilityName", ruleResponsibilityName);
411 		ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibility.class, responsibilityCrit);
412 		query.setAttributes(new String[] { "ruleBaseValuesId" });
413 		return query;
414 	}
415 
416 	private ReportQueryByCriteria 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();
422 		responsibilityCrit.addIn("ruleResponsibilityName", workgroupIds);
423 		responsibilityCrit.addEqualTo("ruleResponsibilityType", KEWConstants.RULE_RESPONSIBILITY_GROUP_ID);
424 		ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibility.class, responsibilityCrit);
425 		query.setAttributes(new String[] { "ruleBaseValuesId" });
426 		return query;
427 	}
428 
429 	public List findByPreviousVersionId(Long previousVersionId) {
430 		Criteria crit = new Criteria();
431 		crit.addEqualTo("previousVersionId", previousVersionId);
432 		return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleBaseValues.class, crit));
433 	}
434 
435 	public RuleBaseValues findDefaultRuleByRuleTemplateId(Long ruleTemplateId) {
436 		Criteria crit = new Criteria();
437 		crit.addEqualTo("ruleTemplateId", ruleTemplateId);
438 		crit.addEqualTo("templateRuleInd", Boolean.TRUE);
439 		List rules = (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleBaseValues.class, crit));
440 		if (rules != null && !rules.isEmpty()) {
441 			return (RuleBaseValues) rules.get(0);
442 		}
443 		return null;
444 	}
445 
446 	public void clearCache() {
447 		this.getPersistenceBroker(false).clearCache();
448 	}
449 
450 	public void retrieveAllReferences(RuleBaseValues rule) {
451 		// getPersistenceBroker().retrieveAllReferences(rule);
452 	}
453 
454 	public RuleBaseValues getParentRule(Long ruleBaseValuesId) {
455 		Criteria criteria = new Criteria();
456 		criteria.addEqualTo("currentInd", Boolean.TRUE);
457 		criteria.addEqualTo("responsibilities.delegationRules.delegateRuleId", ruleBaseValuesId);
458 		Collection rules = this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleBaseValues.class, criteria));
459 		RuleBaseValues rule = null;
460 		for (Iterator iterator = rules.iterator(); iterator.hasNext();) {
461 			RuleBaseValues currentRule = (RuleBaseValues) iterator.next();
462 			if (rule == null || currentRule.getVersionNbr().intValue() > rule.getVersionNbr().intValue()) {
463 				rule = currentRule;
464 			}
465 		}
466 		return rule;
467 	}
468 
469 	public List findOldDelegations(final RuleBaseValues oldRule, final RuleBaseValues newRule) {
470 		return (List)this.getPersistenceBrokerTemplate().execute(new PersistenceBrokerCallback() {
471 			public Object doInPersistenceBroker(PersistenceBroker pb) {
472 				List oldDelegations = new ArrayList();
473 				PreparedStatement ps = null;
474 				ResultSet rs = null;
475 				try {
476 					ps = pb.serviceConnectionManager().getConnection().prepareStatement(OLD_DELEGATIONS_SQL);
477 					ps.setLong(1, oldRule.getRuleBaseValuesId().longValue());
478 					ps.setLong(2, newRule.getRuleBaseValuesId().longValue());
479 					rs = ps.executeQuery();
480 					while (rs.next()) {
481 						oldDelegations.add(findRuleBaseValuesById(new Long(rs.getLong(1))));
482 					}
483 				} catch (Exception e) {
484 					throw new WorkflowRuntimeException("error saving deactivation date", e);
485 				} finally {
486 					if (rs != null) {
487 						try {
488 							rs.close();
489 						} catch (Exception e) {
490 							LOG.error("error closing result set", e);
491 						}
492 					}
493 					if (ps != null) {
494 						try {
495 							ps.close();
496 						} catch (Exception e) {
497 							LOG.error("error closing preparedstatement", e);
498 						}
499 					}
500 				}
501 				return oldDelegations;
502 			}
503 		});
504 	}
505 	
506 	public Long findResponsibilityIdForRule(String ruleName, String ruleResponsibilityName, String ruleResponsibilityType) {
507 		Criteria crit = new Criteria();
508 		crit.addEqualTo("ruleResponsibilityName", ruleResponsibilityName);
509 		crit.addEqualTo("ruleResponsibilityType", ruleResponsibilityType);
510 		crit.addEqualTo("ruleBaseValues.currentInd", Boolean.TRUE);
511 		crit.addEqualTo("ruleBaseValues.name", ruleName);
512 		List responsibilities = (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleResponsibility.class, crit));
513 		if (responsibilities != null) {
514 			for (Iterator iter = responsibilities.iterator(); iter.hasNext();) {
515 				RuleResponsibility responsibility = (RuleResponsibility) iter.next();
516 				return responsibility.getResponsibilityId();
517 			}
518 		}
519 		return null;
520 	}
521 
522 }