001    /**
002     * Copyright 2005-2013 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.ojb.broker.PersistenceBroker;
019    import org.apache.ojb.broker.query.Criteria;
020    import org.apache.ojb.broker.query.QueryByCriteria;
021    import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
022    import org.kuali.rice.core.api.util.RiceConstants;
023    import org.kuali.rice.core.framework.persistence.platform.DatabasePlatform;
024    import org.kuali.rice.kew.rule.bo.RuleTemplateBo;
025    import org.kuali.rice.kew.rule.dao.RuleTemplateDAO;
026    import org.springmodules.orm.ojb.PersistenceBrokerCallback;
027    import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
028    
029    import java.util.ArrayList;
030    import java.util.Iterator;
031    import java.util.List;
032    
033    
034    public class RuleTemplateDAOOjbImpl extends PersistenceBrokerDaoSupport implements RuleTemplateDAO {
035    
036    
037        public List<RuleTemplateBo> findAll() {
038            QueryByCriteria query = new QueryByCriteria(RuleTemplateBo.class);
039            query.addOrderByAscending("name");
040            return (List)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
041        }
042    
043        public RuleTemplateBo findByRuleTemplateName(String ruleTemplateName) {
044            Criteria crit = new Criteria();
045            crit.addEqualTo("name", ruleTemplateName);
046            QueryByCriteria query = new QueryByCriteria(RuleTemplateBo.class, crit);
047            query.addOrderByDescending("id");
048    
049            Iterator ruleTemplates = this.getPersistenceBrokerTemplate().getCollectionByQuery(query).iterator();
050            while(ruleTemplates.hasNext()) {
051                return (RuleTemplateBo) ruleTemplates.next();
052            }
053            return null;
054        }
055    
056        public List<RuleTemplateBo> findByRuleTemplate(RuleTemplateBo ruleTemplate) {
057            Criteria crit = new Criteria();
058            if (ruleTemplate.getName() != null) {
059              crit.addSql("UPPER(RULE_TMPL_NM) like '"+ ruleTemplate.getName().toUpperCase() +"'");
060            }
061            if (ruleTemplate.getDescription() != null) {
062              crit.addSql("UPPER(RULE_TMPL_DESC) like '"+ ruleTemplate.getDescription().toUpperCase()+"'");
063            }
064            return new ArrayList<RuleTemplateBo>(this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RuleTemplateBo.class, crit)));
065        }
066    
067        public void delete(String ruleTemplateId) {
068            this.getPersistenceBrokerTemplate().delete(findByRuleTemplateId(ruleTemplateId));
069        }
070    
071        public RuleTemplateBo findByRuleTemplateId(String ruleTemplateId) {
072            Criteria crit = new Criteria();
073            crit.addEqualTo("id", ruleTemplateId);
074            return (RuleTemplateBo) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(RuleTemplateBo.class, crit));
075        }
076    
077        public void save(RuleTemplateBo ruleTemplate) {
078            this.getPersistenceBrokerTemplate().store(ruleTemplate);
079        }
080    
081        public String getNextRuleTemplateId() {
082            return (String)this.getPersistenceBrokerTemplate().execute(new PersistenceBrokerCallback() {
083                public Object doInPersistenceBroker(PersistenceBroker broker) {
084                      return String.valueOf(getPlatform().getNextValSQL("KREW_RTE_TMPL_S", broker));
085                }
086            });
087        }
088    
089        protected DatabasePlatform getPlatform() {
090            return (DatabasePlatform)GlobalResourceLoader.getService(RiceConstants.DB_PLATFORM);
091        }
092    
093    
094    }