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