001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kew.rule.dao.impl;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.apache.ojb.broker.PersistenceBroker;
020    import org.apache.ojb.broker.query.Criteria;
021    import org.apache.ojb.broker.query.QueryByCriteria;
022    import org.apache.ojb.broker.query.QueryFactory;
023    import org.apache.ojb.broker.query.ReportQueryByCriteria;
024    import org.kuali.rice.core.api.exception.RiceRuntimeException;
025    import org.kuali.rice.kew.api.WorkflowRuntimeException;
026    import org.kuali.rice.kew.rule.RuleBaseValues;
027    import org.kuali.rice.kew.rule.RuleExtensionBo;
028    import org.kuali.rice.kew.rule.RuleResponsibilityBo;
029    import org.kuali.rice.kew.rule.dao.RuleDAO;
030    import org.kuali.rice.kew.api.KewApiConstants;
031    import org.kuali.rice.kim.api.identity.principal.Principal;
032    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
033    import org.kuali.rice.krad.service.KRADServiceLocator;
034    import org.springmodules.orm.ojb.PersistenceBrokerCallback;
035    import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
036    
037    import java.sql.PreparedStatement;
038    import java.sql.ResultSet;
039    import java.sql.Timestamp;
040    import java.util.ArrayList;
041    import java.util.Collection;
042    import java.util.Date;
043    import java.util.HashMap;
044    import java.util.HashSet;
045    import java.util.Iterator;
046    import java.util.List;
047    import java.util.Map;
048    import java.util.Set;
049    
050    
051    public class RuleDAOOjbImpl extends PersistenceBrokerDaoSupport implements RuleDAO {
052    
053            private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RuleDAOOjbImpl.class);
054    
055            private static final String OLD_DELEGATIONS_SQL =
056                    "select oldDel.dlgn_rule_id "+
057                    "from krew_rule_rsp_t oldRsp, krew_dlgn_rsp_t oldDel "+
058                    "where oldRsp.rule_id=? and "+
059                    "oldRsp.rule_rsp_id=oldDel.rule_rsp_id and "+
060                    "oldDel.dlgn_rule_base_val_id not in "+
061                    "(select newDel.dlgn_rule_base_val_id from krew_rule_rsp_t newRsp, krew_dlgn_rsp_t newDel "+
062                    "where newRsp.rule_id=? and "+
063                    "newRsp.rule_rsp_id=newDel.rule_rsp_id)";
064    
065            public void save(RuleBaseValues ruleBaseValues) {
066                    this.getPersistenceBrokerTemplate().store(ruleBaseValues);
067            }
068    
069            public List<RuleBaseValues> fetchAllCurrentRulesForTemplateDocCombination(String ruleTemplateId, List documentTypes) {
070                    Criteria crit = new Criteria();
071                    crit.addIn("docTypeName", documentTypes);
072                    crit.addEqualTo("ruleTemplateId", ruleTemplateId);
073                    crit.addEqualTo("currentInd", Boolean.TRUE);
074                    crit.addEqualTo("active", Boolean.TRUE);
075                    crit.addEqualTo("delegateRule", Boolean.FALSE);
076                    crit.addEqualTo("templateRuleInd", Boolean.FALSE);
077    
078                    crit.addAndCriteria(generateFromToDateCriteria(new Date()));
079                    return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleBaseValues.class, crit));
080            }
081    
082            public List<RuleBaseValues> fetchAllCurrentRulesForTemplateDocCombination(String ruleTemplateId, List documentTypes, Timestamp effectiveDate) {
083                    Criteria crit = new Criteria();
084                    crit.addIn("docTypeName", documentTypes);
085                    crit.addEqualTo("ruleTemplateId", ruleTemplateId);
086                    crit.addEqualTo("active", Boolean.TRUE);
087                    crit.addEqualTo("delegateRule", Boolean.FALSE);
088                    crit.addEqualTo("templateRuleInd", Boolean.FALSE);
089                    if (effectiveDate != null) {
090                            crit.addLessOrEqualThan("activationDate", effectiveDate);
091                            crit.addGreaterThan("deactivationDate", effectiveDate);
092                    }
093    
094                    crit.addAndCriteria(generateFromToDateCriteria(new Date()));
095                    return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleBaseValues.class, crit));
096            }
097    
098            public Criteria generateFromToDateCriteria(Date date) {
099                    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    }