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.criteria.OrderByField;
20 import org.kuali.rice.core.api.criteria.OrderDirection;
21 import org.kuali.rice.core.api.criteria.Predicate;
22 import org.kuali.rice.core.api.criteria.QueryResults;
23 import org.kuali.rice.kew.rule.bo.RuleTemplateBo;
24 import org.kuali.rice.kew.rule.dao.RuleTemplateDAO;
25 import org.kuali.rice.krad.data.DataObjectService;
26 import org.kuali.rice.krad.data.PersistenceOption;
27 import org.kuali.rice.krad.data.platform.MaxValueIncrementerFactory;
28 import org.springframework.beans.factory.annotation.Required;
29 import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
30
31 import javax.persistence.EntityManager;
32 import javax.sql.DataSource;
33 import java.util.ArrayList;
34 import java.util.List;
35
36 import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
37 import static org.kuali.rice.core.api.criteria.PredicateFactory.likeIgnoreCase;
38
39
40 public class RuleTemplateDAOJpa implements RuleTemplateDAO {
41
42 private static final String SEQUENCE_NAME = "KREW_RTE_TMPL_S";
43
44 private EntityManager entityManager;
45 private DataObjectService dataObjectService;
46
47 private DataSource dataSource;
48
49 public List<RuleTemplateBo> findAll() {
50 org.kuali.rice.core.api.criteria.QueryByCriteria.Builder builder =
51 org.kuali.rice.core.api.criteria.QueryByCriteria.Builder.create();
52
53 return getDataObjectService().findMatching(RuleTemplateBo.class,builder.build()).getResults();
54 }
55
56 public RuleTemplateBo findByRuleTemplateName(String ruleTemplateName) {
57 if (StringUtils.isBlank(ruleTemplateName)) {
58 return null;
59 }
60
61 org.kuali.rice.core.api.criteria.QueryByCriteria.Builder builder =
62 org.kuali.rice.core.api.criteria.QueryByCriteria.Builder.create();
63 builder.setPredicates(equal("name",ruleTemplateName));
64 builder.setOrderByFields(OrderByField.Builder.create("name", OrderDirection.DESCENDING).build());
65
66 QueryResults<RuleTemplateBo> ruleTemplates = getDataObjectService().findMatching(RuleTemplateBo.class,builder.build());
67
68 if(ruleTemplates==null || ruleTemplates.getResults() == null || ruleTemplates.getResults().isEmpty()){
69 return null;
70 }
71 return (RuleTemplateBo) ruleTemplates.getResults().get(0);
72 }
73
74 public List<RuleTemplateBo> findByRuleTemplate(RuleTemplateBo ruleTemplate) {
75 org.kuali.rice.core.api.criteria.QueryByCriteria.Builder builder =
76 org.kuali.rice.core.api.criteria.QueryByCriteria.Builder.create();
77 List<Predicate> predicates = new ArrayList<Predicate>();
78
79 if (ruleTemplate.getName() != null) {
80 predicates.add(likeIgnoreCase("name",ruleTemplate.getName()));
81 }
82 if (ruleTemplate.getDescription() != null) {
83 predicates.add(likeIgnoreCase("description",ruleTemplate.getDescription()));
84 }
85 Predicate[] preds = predicates.toArray(new Predicate[predicates.size()]);
86 builder.setPredicates(preds);
87 QueryResults<RuleTemplateBo> results = getDataObjectService().
88 findMatching(RuleTemplateBo.class, builder.build());
89 return results.getResults();
90 }
91
92 public void delete(String ruleTemplateId) {
93 getDataObjectService().delete(findByRuleTemplateId(ruleTemplateId));
94 }
95
96 public RuleTemplateBo findByRuleTemplateId(String ruleTemplateId) {
97 return getDataObjectService().find(RuleTemplateBo.class, ruleTemplateId);
98 }
99
100 public RuleTemplateBo save(RuleTemplateBo ruleTemplate) {
101 return getDataObjectService().save(ruleTemplate, PersistenceOption.FLUSH);
102 }
103
104 public String getNextRuleTemplateId() {
105 DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(
106 getDataSource(), SEQUENCE_NAME);
107 return incrementer.nextStringValue();
108 }
109
110 public EntityManager getEntityManager() {
111 return this.entityManager;
112 }
113
114 public void setEntityManager(EntityManager entityManager) {
115 this.entityManager = entityManager;
116 }
117
118 public DataSource getDataSource() {
119 return dataSource;
120 }
121
122 @Required
123 public void setDataSource(DataSource dataSource) {
124 this.dataSource = dataSource;
125 }
126
127 public DataObjectService getDataObjectService() {
128 return dataObjectService;
129 }
130
131 @Required
132 public void setDataObjectService(DataObjectService dataObjectService) {
133 this.dataObjectService = dataObjectService;
134 }
135 }