View Javadoc

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.apache.ojb.broker.PersistenceBroker;
20  import org.apache.ojb.broker.query.Criteria;
21  import org.apache.ojb.broker.query.QueryByCriteria;
22  import org.apache.ojb.broker.query.QueryFactory;
23  import org.apache.ojb.broker.query.ReportQueryByCriteria;
24  import org.kuali.rice.core.api.exception.RiceRuntimeException;
25  import org.kuali.rice.kew.api.WorkflowRuntimeException;
26  import org.kuali.rice.kew.rule.RuleBaseValues;
27  import org.kuali.rice.kew.rule.RuleExtensionBo;
28  import org.kuali.rice.kew.rule.RuleResponsibilityBo;
29  import org.kuali.rice.kew.rule.dao.RuleDAO;
30  import org.kuali.rice.kew.api.KewApiConstants;
31  import org.kuali.rice.kim.api.identity.principal.Principal;
32  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
33  import org.kuali.rice.krad.service.KRADServiceLocator;
34  import org.springmodules.orm.ojb.PersistenceBrokerCallback;
35  import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
36  
37  import java.sql.PreparedStatement;
38  import java.sql.ResultSet;
39  import java.sql.Timestamp;
40  import java.util.ArrayList;
41  import java.util.Collection;
42  import java.util.Date;
43  import java.util.HashMap;
44  import java.util.HashSet;
45  import java.util.Iterator;
46  import java.util.List;
47  import java.util.Map;
48  import java.util.Set;
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<RuleBaseValues> fetchAllCurrentRulesForTemplateDocCombination(String 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("active", 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<RuleBaseValues> fetchAllCurrentRulesForTemplateDocCombination(String ruleTemplateId, List documentTypes, Timestamp effectiveDate) {
83  		Criteria crit = new Criteria();
84  		crit.addIn("docTypeName", documentTypes);
85  		crit.addEqualTo("ruleTemplateId", ruleTemplateId);
86  		crit.addEqualTo("active", 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("fromDateValue");
104 		Criteria fromLessOrEqualCrit = new Criteria();
105 		fromLessOrEqualCrit.addLessOrEqualThan("fromDateValue", 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("toDateValue");
112 		Criteria toGreaterOrEqualCrit = new Criteria();
113 		toGreaterOrEqualCrit.addGreaterOrEqualThan("toDateValue", 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<RuleBaseValues> 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(String ruleBaseValuesId) {
136 		this.getPersistenceBrokerTemplate().delete(findRuleBaseValuesById(ruleBaseValuesId));
137 	}
138 
139 	public List<RuleBaseValues> 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(String ruleBaseValuesId) {
153 		Criteria crit = new Criteria();
154 		crit.addEqualTo("id", ruleBaseValuesId);
155         Map<String, String> criteria = new HashMap<String, String>();
156         criteria.put("id", ruleBaseValuesId);
157 		// crit.addEqualTo("currentInd", new Boolean(true));
158         return KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(RuleBaseValues.class, criteria);
159 		//return (RuleBaseValues) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(RuleBaseValues.class, crit));
160 	}
161 
162 	public List<RuleBaseValues> findRuleBaseValuesByResponsibilityReviewer(String reviewerName, String type) {
163 		Criteria crit = new Criteria();
164 		crit.addEqualTo("ruleResponsibilityName", reviewerName);
165 		crit.addEqualTo("ruleResponsibilityType", type);
166 
167 		List responsibilities = (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleResponsibilityBo.class, crit));
168 		List rules = new ArrayList();
169 
170 		for (Iterator iter = responsibilities.iterator(); iter.hasNext();) {
171 			RuleResponsibilityBo responsibility = (RuleResponsibilityBo) iter.next();
172 			RuleBaseValues rule = responsibility.getRuleBaseValues();
173 			if (rule != null && rule.getCurrentInd() != null && rule.getCurrentInd().booleanValue()) {
174 				rules.add(rule);
175 			}
176 		}
177 		return rules;
178 	}
179 
180 	public List<RuleBaseValues> findRuleBaseValuesByResponsibilityReviewerTemplateDoc(String ruleTemplateName, String documentType, String reviewerName, String type) {
181 	    Criteria crit = new Criteria();
182 		crit.addEqualTo("ruleResponsibilityName", reviewerName);
183 		crit.addEqualTo("ruleResponsibilityType", type);
184 		crit.addEqualTo("ruleBaseValues.currentInd", Boolean.TRUE);
185 		if (!StringUtils.isBlank(ruleTemplateName)) {
186 		    crit.addLike("ruleBaseValues.ruleTemplate.name", ruleTemplateName.replace("*", "%").concat("%"));
187 		}
188 		if (!StringUtils.isBlank(documentType)) {
189 		    crit.addLike("ruleBaseValues.docTypeName", documentType.replace("*", "%").concat("%"));
190 		}
191 
192 		List responsibilities = (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleResponsibilityBo.class, crit));
193 		List rules = new ArrayList();
194 
195 		for (Iterator iter = responsibilities.iterator(); iter.hasNext();) {
196 			RuleResponsibilityBo responsibility = (RuleResponsibilityBo) iter.next();
197 			RuleBaseValues rule = responsibility.getRuleBaseValues();
198 			if (rule != null && rule.getCurrentInd() != null && rule.getCurrentInd().booleanValue()) {
199 				rules.add(rule);
200 			}
201 		}
202 		return rules;
203 	}
204 
205 	public List findRuleBaseValuesByObjectGraph(RuleBaseValues ruleBaseValues) {
206 		ruleBaseValues.setCurrentInd(Boolean.TRUE);
207 		ruleBaseValues.setTemplateRuleInd(Boolean.FALSE);
208 		return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ruleBaseValues));
209 	}
210 
211 	public RuleResponsibilityBo findRuleResponsibility(String responsibilityId) {
212 				
213 		ReportQueryByCriteria subQuery;
214 		Criteria subCrit = new Criteria();
215 		Criteria crit2 = new Criteria();
216 		subCrit.addLike("responsibilityId", responsibilityId);
217 		subQuery = QueryFactory.newReportQuery(RuleResponsibilityBo.class, subCrit);
218 		subQuery.setAttributes(new String[] {"RULE_ID"});
219 		crit2.addIn("id", subQuery);
220 		crit2.addEqualTo("currentInd", 1);
221 		QueryByCriteria outerQuery = QueryFactory.newQuery(RuleBaseValues.class, crit2);
222 		RuleBaseValues rbv = (RuleBaseValues)this.getPersistenceBrokerTemplate().getObjectByQuery(outerQuery);
223 		Criteria finalCrit = new Criteria();
224 		finalCrit.addEqualTo("responsibilityId", responsibilityId);
225 		finalCrit.addEqualTo("ruleBaseValuesId", rbv.getId());
226 		RuleResponsibilityBo
227                 rResp = (RuleResponsibilityBo)this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(RuleResponsibilityBo.class, finalCrit));
228 		
229 		if(rResp != null){
230 			if(rResp.getRuleBaseValuesId().equals(rbv.getId())){
231 				return rResp;
232 			}
233 		}	
234 	
235 		return null;
236 	}
237 
238 	public List<RuleBaseValues> search(String docTypeName, String ruleId, String ruleTemplateId, String ruleDescription, String workgroupId, String principalId, Boolean delegateRule, Boolean activeInd, Map extensionValues, String workflowIdDirective) {
239         Criteria crit = getSearchCriteria(docTypeName, ruleTemplateId, ruleDescription, delegateRule, activeInd, extensionValues);
240         if (ruleId != null) {
241             crit.addEqualTo("id", ruleId);
242         }
243         if (workgroupId != null) {
244             crit.addIn("id", getResponsibilitySubQuery(workgroupId));
245         }
246         List<String> workgroupIds = new ArrayList<String>();
247         Boolean searchUser = Boolean.FALSE;
248         Boolean searchUserInWorkgroups = Boolean.FALSE;
249         
250         if ("group".equals(workflowIdDirective)) {
251             searchUserInWorkgroups = Boolean.TRUE;
252         } else if (StringUtils.isBlank(workflowIdDirective)) {
253             searchUser = Boolean.TRUE;
254             searchUserInWorkgroups = Boolean.TRUE;
255         } else {
256             searchUser = Boolean.TRUE;
257         }
258         
259         if (!org.apache.commons.lang.StringUtils.isEmpty(principalId) && searchUserInWorkgroups)
260         {
261             Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
262 
263             if (principal == null)
264             {
265             	throw new RiceRuntimeException("Failed to locate user for the given workflow id: " + principalId);
266             }
267             workgroupIds = KimApiServiceLocator.getGroupService().getGroupIdsByPrincipalId(principalId);
268         }
269         ReportQueryByCriteria query = getResponsibilitySubQuery(workgroupIds, principalId, searchUser, searchUserInWorkgroups);
270         if (query != null) {
271         	crit.addIn("id", query);
272         }
273 
274 		return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleBaseValues.class, crit, true));
275 	}
276 
277     public List<RuleBaseValues> search(String docTypeName, String ruleTemplateId, String ruleDescription, Collection<String> workgroupIds, String workflowId, Boolean delegateRule, Boolean activeInd, Map extensionValues, Collection actionRequestCodes) {
278     	List results = null;
279         Criteria crit = getSearchCriteria(docTypeName, ruleTemplateId, ruleDescription, delegateRule, activeInd, extensionValues);
280         ReportQueryByCriteria query = getResponsibilitySubQuery(workgroupIds, workflowId, actionRequestCodes, (workflowId != null), ((workgroupIds != null) && !workgroupIds.isEmpty()));
281         if (query != null) {
282         	crit.addIn("ruleResponsibilities.ruleBaseValuesId", query);
283         }
284         results = (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleBaseValues.class, crit, true));
285         return results;
286     }
287 
288     private ReportQueryByCriteria getResponsibilitySubQuery(List<String> workgroupIds, String workflowId, Boolean searchUser, Boolean searchUserInWorkgroups) {
289         Collection<String> workgroupIdStrings = new ArrayList<String>();
290         for (String workgroupId : workgroupIds) {
291             workgroupIdStrings.add(workgroupId);
292         }
293         return getResponsibilitySubQuery(workgroupIdStrings,workflowId,new ArrayList<String>(), searchUser, searchUserInWorkgroups);
294     }
295     
296     private ReportQueryByCriteria getResponsibilitySubQuery(Collection<String> workgroupIds, String workflowId, Collection actionRequestCodes, Boolean searchUser, Boolean searchUserInWorkgroups) {
297         Criteria responsibilityCrit = null;
298         if ( (actionRequestCodes != null) && (!actionRequestCodes.isEmpty()) ) {
299         	responsibilityCrit = new Criteria();
300             responsibilityCrit.addIn("actionRequestedCd", actionRequestCodes);
301         }
302 
303         ReportQueryByCriteria query = null;
304         Criteria ruleResponsibilityNameCrit = null;
305         if (!org.apache.commons.lang.StringUtils.isEmpty(workflowId)) {
306             // workflow user id exists
307             if (searchUser != null && searchUser) {
308                 // searching user wishes to search for rules specific to user
309                 ruleResponsibilityNameCrit = new Criteria();
310                 ruleResponsibilityNameCrit.addLike("ruleResponsibilityName", workflowId);
311                 ruleResponsibilityNameCrit.addEqualTo("ruleResponsibilityType", KewApiConstants.RULE_RESPONSIBILITY_WORKFLOW_ID);
312             }
313             if ( (searchUserInWorkgroups != null && searchUserInWorkgroups) && (workgroupIds != null) && (!workgroupIds.isEmpty()) ) {
314                 // at least one workgroup id exists and user wishes to search on workgroups
315                 if (ruleResponsibilityNameCrit == null) {
316                     ruleResponsibilityNameCrit = new Criteria();
317                 }
318                 Criteria workgroupCrit = new Criteria();
319                 workgroupCrit.addIn("ruleResponsibilityName", workgroupIds);
320                 workgroupCrit.addEqualTo("ruleResponsibilityType", KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID);
321                 ruleResponsibilityNameCrit.addOrCriteria(workgroupCrit);
322             }
323         } else if ( (workgroupIds != null) && (workgroupIds.size() == 1) ) {
324             // no user and one workgroup id
325             ruleResponsibilityNameCrit = new Criteria();
326             ruleResponsibilityNameCrit.addLike("ruleResponsibilityName", workgroupIds.iterator().next());
327             ruleResponsibilityNameCrit.addEqualTo("ruleResponsibilityType", KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID);
328         } else if ( (workgroupIds != null) && (workgroupIds.size() > 1) ) {
329             // no user and more than one workgroup id
330             ruleResponsibilityNameCrit = new Criteria();
331             ruleResponsibilityNameCrit.addIn("ruleResponsibilityName", workgroupIds);
332             ruleResponsibilityNameCrit.addEqualTo("ruleResponsibilityType", KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID);
333         }
334         if (ruleResponsibilityNameCrit != null) {
335         	if (responsibilityCrit == null) {
336         		responsibilityCrit = new Criteria();
337         	}
338             responsibilityCrit.addAndCriteria(ruleResponsibilityNameCrit);
339         }
340         if (responsibilityCrit != null) {
341         	query = QueryFactory.newReportQuery(RuleResponsibilityBo.class, responsibilityCrit);
342         	query.setAttributes(new String[] { "ruleBaseValuesId" });
343         }
344         return query;
345     }
346 
347     private Criteria getSearchCriteria(String docTypeName, String ruleTemplateId, String ruleDescription, Boolean delegateRule, Boolean activeInd, Map extensionValues) {
348         Criteria crit = new Criteria();
349         crit.addEqualTo("currentInd", Boolean.TRUE);
350         crit.addEqualTo("templateRuleInd", Boolean.FALSE);
351         if (activeInd != null) {
352             crit.addEqualTo("active", activeInd);
353         }
354         if (docTypeName != null) {
355             crit.addLike("UPPER(docTypeName)", docTypeName.toUpperCase());
356         }
357         if (ruleDescription != null && !ruleDescription.trim().equals("")) {
358             crit.addLike("UPPER(description)", ruleDescription.toUpperCase());
359         }
360         if (ruleTemplateId != null) {
361             crit.addEqualTo("ruleTemplateId", ruleTemplateId);
362         }
363         if (delegateRule != null) {
364             crit.addEqualTo("delegateRule", delegateRule);
365         }
366         if (extensionValues != null && !extensionValues.isEmpty()) {
367             for (Iterator iter2 = extensionValues.entrySet().iterator(); iter2.hasNext();) {
368                 Map.Entry entry = (Map.Entry) iter2.next();
369                 if (!org.apache.commons.lang.StringUtils.isEmpty((String) entry.getValue())) {
370                     // Criteria extensionCrit = new Criteria();
371                     // extensionCrit.addEqualTo("extensionValues.key",
372                     // entry.getKey());
373                     // extensionCrit.addLike("extensionValues.value",
374                     // "%"+(String) entry.getValue()+"%");
375 
376                     Criteria extensionCrit2 = new Criteria();
377                     extensionCrit2.addEqualTo("extensionValues.key", entry.getKey());
378                     extensionCrit2.addLike("UPPER(extensionValues.value)", ("%" + (String) entry.getValue() + "%").toUpperCase());
379 
380                     // Criteria extensionCrit3 = new Criteria();
381                     // extensionCrit3.addEqualTo("extensionValues.key",
382                     // entry.getKey());
383                     // extensionCrit3.addLike("extensionValues.value",
384                     // ("%"+(String) entry.getValue()+"%").toLowerCase());
385 
386                     // extensionCrit.addOrCriteria(extensionCrit2);
387                     // extensionCrit.addOrCriteria(extensionCrit3);
388                     ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleExtensionBo.class, extensionCrit2);
389                     query.setAttributes(new String[] { "ruleBaseValuesId" });
390                     crit.addIn("ruleExtensions.ruleBaseValuesId", query);
391                 }
392             }
393         }
394         return crit;
395     }
396 
397     private Criteria getWorkgroupOrCriteria(Collection workgroupIds) {
398         Criteria responsibilityCrit = new Criteria();
399         for (Iterator iter = workgroupIds.iterator(); iter.hasNext();) {
400             String workgroupIdFromList = (String) iter.next();
401             Criteria orCriteria = new Criteria();
402             orCriteria.addLike("ruleResponsibilityName", workgroupIdFromList);
403             responsibilityCrit.addOrCriteria(orCriteria);
404         }
405         ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibilityBo.class, responsibilityCrit);
406         query.setAttributes(new String[] { "ruleBaseValuesId" });
407         Criteria crit = new Criteria();
408         crit.addIn("responsibilities.ruleBaseValuesId", query);
409         return crit;
410     }
411 
412 	private ReportQueryByCriteria getResponsibilitySubQuery(String ruleResponsibilityName) {
413 		Criteria responsibilityCrit = new Criteria();
414 		responsibilityCrit.addLike("ruleResponsibilityName", ruleResponsibilityName);
415 		ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibilityBo.class, responsibilityCrit);
416 		query.setAttributes(new String[] { "ruleBaseValuesId" });
417 		return query;
418 	}
419 
420 	private ReportQueryByCriteria getWorkgroupResponsibilitySubQuery(Set<Long> workgroupIds) {
421 	    	Set<String> workgroupIdStrings = new HashSet<String>();
422 	    	for (Long workgroupId : workgroupIds) {
423 	    	    workgroupIdStrings.add(workgroupId.toString());
424 	    	}
425 		Criteria responsibilityCrit = new Criteria();
426 		responsibilityCrit.addIn("ruleResponsibilityName", workgroupIds);
427 		responsibilityCrit.addEqualTo("ruleResponsibilityType", KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID);
428 		ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibilityBo.class, responsibilityCrit);
429 		query.setAttributes(new String[] { "ruleBaseValuesId" });
430 		return query;
431 	}
432 
433 	public List<RuleBaseValues> findByPreviousRuleId(String previousRuleId) {
434 		Criteria crit = new Criteria();
435 		crit.addEqualTo("previousRuleId", previousRuleId);
436 		return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleBaseValues.class, crit));
437 	}
438 
439 	public RuleBaseValues findDefaultRuleByRuleTemplateId(String ruleTemplateId) {
440 		Criteria crit = new Criteria();
441 		crit.addEqualTo("ruleTemplateId", ruleTemplateId);
442 		crit.addEqualTo("templateRuleInd", Boolean.TRUE);
443 		List rules = (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleBaseValues.class, crit));
444 		if (rules != null && !rules.isEmpty()) {
445 			return (RuleBaseValues) rules.get(0);
446 		}
447 		return null;
448 	}
449 
450 	public void retrieveAllReferences(RuleBaseValues rule) {
451 		// getPersistenceBroker().retrieveAllReferences(rule);
452 	}
453 
454 	public RuleBaseValues getParentRule(String 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.setString(1, oldRule.getId());
478 					ps.setString(2, newRule.getId());
479 					rs = ps.executeQuery();
480 					while (rs.next()) {
481 						oldDelegations.add(findRuleBaseValuesById(rs.getString(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 String 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(RuleResponsibilityBo.class, crit));
513 		if (responsibilities != null) {
514 			for (Iterator iter = responsibilities.iterator(); iter.hasNext();) {
515 				RuleResponsibilityBo responsibility = (RuleResponsibilityBo) iter.next();
516 				return responsibility.getResponsibilityId();
517 			}
518 		}
519 		return null;
520 	}
521 
522 }