001    /*
002     * Copyright 2005-2008 The Kuali Foundation
003     * 
004     * 
005     * Licensed under the Educational Community License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     * 
009     * http://www.opensource.org/licenses/ecl2.php
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.kuali.rice.kew.rule.dao.impl;
018    
019    import java.util.List;
020    
021    import javax.persistence.EntityManager;
022    import javax.persistence.PersistenceContext;
023    
024    import org.kuali.rice.core.database.platform.DatabasePlatform;
025    import org.kuali.rice.core.jpa.criteria.Criteria;
026    import org.kuali.rice.core.jpa.criteria.QueryByCriteria;
027    import org.kuali.rice.core.resourceloader.GlobalResourceLoader;
028    import org.kuali.rice.core.util.OrmUtils;
029    import org.kuali.rice.core.util.RiceConstants;
030    import org.kuali.rice.kew.rule.bo.RuleTemplate;
031    import org.kuali.rice.kew.rule.dao.RuleTemplateDAO;
032    
033    
034    
035    public class RuleTemplateDAOJpaImpl implements RuleTemplateDAO {
036    
037            @PersistenceContext(unitName="kew-unit")
038            private EntityManager entityManager;
039            
040        public List findAll() {
041            return entityManager.createNamedQuery("findAllOrderedByName").getResultList();
042        }
043    
044        public RuleTemplate findByRuleTemplateName(String ruleTemplateName) {
045            
046            Criteria crit = new Criteria(RuleTemplate.class.getName());
047            crit.eq("name", ruleTemplateName);
048            crit.orderBy("ruleTemplateId", false);
049            
050            List ruleTemplates =  new QueryByCriteria(entityManager, crit).toQuery().getResultList();
051            
052            if(ruleTemplates==null||ruleTemplates.size()==0){
053                    return null;
054            }
055            return (RuleTemplate) ruleTemplates.get(0);
056        }
057    
058        public List findByRuleTemplate(RuleTemplate ruleTemplate) {
059            Criteria crit = new Criteria(RuleTemplate.class.getName());
060            if (ruleTemplate.getName() != null) {
061              crit.rawJpql("UPPER(RULE_TMPL_NM) like '"+ ruleTemplate.getName().toUpperCase() +"'");
062            }
063            if (ruleTemplate.getDescription() != null) {
064              crit.rawJpql("UPPER(RULE_TMPL_DESC) like '"+ ruleTemplate.getDescription().toUpperCase()+"'");
065            }
066            return new QueryByCriteria(entityManager, crit).toQuery().getResultList();
067        }
068    
069        public void delete(Long ruleTemplateId) {
070            entityManager.remove(findByRuleTemplateId(ruleTemplateId));
071        }
072    
073        public RuleTemplate findByRuleTemplateId(Long ruleTemplateId) {
074            return entityManager.find(RuleTemplate.class, ruleTemplateId);
075         }
076    
077        public void save(RuleTemplate ruleTemplate) {
078            if(ruleTemplate.getRuleTemplateId()==null){
079                    entityManager.persist(ruleTemplate);
080            }else{
081                    OrmUtils.merge(entityManager, ruleTemplate);
082            }
083        }
084    
085        public Long getNextRuleTemplateId() {
086           return getPlatform().getNextValSQL("KREW_RTE_TMPL_S", entityManager);
087        }
088    
089        protected DatabasePlatform getPlatform() {
090            return (DatabasePlatform)GlobalResourceLoader.getService(RiceConstants.DB_PLATFORM);
091        }
092    
093        public EntityManager getEntityManager() {
094            return this.entityManager;
095        }
096    
097        public void setEntityManager(EntityManager entityManager) {
098            this.entityManager = entityManager;
099        }
100    
101    
102    }