View Javadoc

1   /*
2    * Copyright 2005-2008 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.util.List;
20  
21  import javax.persistence.EntityManager;
22  import javax.persistence.PersistenceContext;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
26  import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
27  import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria;
28  import org.kuali.rice.core.framework.persistence.platform.DatabasePlatform;
29  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
30  import org.kuali.rice.core.util.RiceConstants;
31  import org.kuali.rice.kew.rule.bo.RuleTemplate;
32  import org.kuali.rice.kew.rule.dao.RuleTemplateDAO;
33  
34  
35  
36  public class RuleTemplateDAOJpaImpl implements RuleTemplateDAO {
37  
38  	@PersistenceContext(unitName="kew-unit")
39  	private EntityManager entityManager;
40  	
41      public List<RuleTemplate> findAll() {
42          return entityManager.createNamedQuery("findAllOrderedByName").getResultList();
43      }
44  
45      public RuleTemplate findByRuleTemplateName(String ruleTemplateName) {
46          if (StringUtils.isBlank(ruleTemplateName)) {
47          	return null;
48          }
49      	
50      	Criteria crit = new Criteria(RuleTemplate.class.getName());
51          crit.eq("name", ruleTemplateName);
52          crit.orderBy("ruleTemplateId", false);
53          
54          List ruleTemplates =  new QueryByCriteria(entityManager, crit).toQuery().getResultList();
55          
56          if(ruleTemplates==null||ruleTemplates.size()==0){
57          	return null;
58          }
59          return (RuleTemplate) ruleTemplates.get(0);
60      }
61  
62      public List<RuleTemplate> findByRuleTemplate(RuleTemplate ruleTemplate) {
63          Criteria crit = new Criteria(RuleTemplate.class.getName());
64          if (ruleTemplate.getName() != null) {
65            crit.rawJpql("UPPER(RULE_TMPL_NM) like '"+ ruleTemplate.getName().toUpperCase() +"'");
66          }
67          if (ruleTemplate.getDescription() != null) {
68            crit.rawJpql("UPPER(RULE_TMPL_DESC) like '"+ ruleTemplate.getDescription().toUpperCase()+"'");
69          }
70          return new QueryByCriteria(entityManager, crit).toQuery().getResultList();
71      }
72  
73      public void delete(String ruleTemplateId) {
74      	entityManager.remove(findByRuleTemplateId(ruleTemplateId));
75      }
76  
77      public RuleTemplate findByRuleTemplateId(String ruleTemplateId) {
78          return entityManager.find(RuleTemplate.class, ruleTemplateId);
79       }
80  
81      public void save(RuleTemplate ruleTemplate) {
82      	if(ruleTemplate.getRuleTemplateId()==null){
83      		entityManager.persist(ruleTemplate);
84      	}else{
85      		OrmUtils.merge(entityManager, ruleTemplate);
86      	}
87      }
88  
89      public String getNextRuleTemplateId() {
90         return String.valueOf(getPlatform().getNextValSQL("KREW_RTE_TMPL_S", entityManager));
91      }
92  
93      protected DatabasePlatform getPlatform() {
94      	return (DatabasePlatform)GlobalResourceLoader.getService(RiceConstants.DB_PLATFORM);
95      }
96  
97      public EntityManager getEntityManager() {
98          return this.entityManager;
99      }
100 
101     public void setEntityManager(EntityManager entityManager) {
102         this.entityManager = entityManager;
103     }
104 
105 
106 }