1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 }