1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.rice.kew.rule.service.impl;
18
19 import java.io.InputStream;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
23
24 import org.jdom.Element;
25 import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
26 import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
27 import org.kuali.rice.kew.export.ExportDataSet;
28 import org.kuali.rice.kew.rule.RuleBaseValues;
29 import org.kuali.rice.kew.rule.RuleDelegation;
30 import org.kuali.rice.kew.rule.RuleTemplateOption;
31 import org.kuali.rice.kew.rule.bo.RuleTemplate;
32 import org.kuali.rice.kew.rule.bo.RuleTemplateAttribute;
33 import org.kuali.rice.kew.rule.dao.RuleDAO;
34 import org.kuali.rice.kew.rule.dao.RuleDelegationDAO;
35 import org.kuali.rice.kew.rule.dao.RuleTemplateAttributeDAO;
36 import org.kuali.rice.kew.rule.dao.RuleTemplateDAO;
37 import org.kuali.rice.kew.rule.dao.RuleTemplateOptionDAO;
38 import org.kuali.rice.kew.rule.service.RuleAttributeService;
39 import org.kuali.rice.kew.rule.service.RuleTemplateService;
40 import org.kuali.rice.kew.service.KEWServiceLocator;
41 import org.kuali.rice.kew.xml.RuleTemplateXmlParser;
42 import org.kuali.rice.kew.xml.export.RuleTemplateXmlExporter;
43
44
45 public class RuleTemplateServiceImpl implements RuleTemplateService {
46
47 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RuleTemplateServiceImpl.class);
48
49 private static final String RULE_TEMPLATE_NAME_REQUIRED = "rule.template.name.required";
50
51 private static final String RULE_TEMPLATE_DESCRIPTION_REQUIRED = "rule.template.description.required";
52
53 private static final String XML_PARSE_ERROR = "general.error.parsexml";
54
55 private RuleTemplateDAO ruleTemplateDAO;
56
57 private RuleTemplateAttributeDAO ruleTemplateAttributeDAO;
58
59 private RuleTemplateOptionDAO ruleTemplateOptionDAO;
60
61 private RuleDAO ruleDAO;
62
63 private RuleDelegationDAO ruleDelegationDAO;
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 public void deleteRuleTemplateOption(Long ruleTemplateOptionId) {
83 getRuleTemplateOptionDAO().delete(ruleTemplateOptionId);
84 }
85
86 public RuleTemplate findByRuleTemplateName(String ruleTemplateName) {
87 return (getRuleTemplateDAO().findByRuleTemplateName(ruleTemplateName));
88 }
89
90
91
92
93
94
95 public RuleTemplateAttribute findByRuleTemplateAttributeId(Long ruleTemplateAttributeId) {
96 return getRuleTemplateAttributeDAO().findByRuleTemplateAttributeId(ruleTemplateAttributeId);
97 }
98
99 public List findAll() {
100 return ruleTemplateDAO.findAll();
101 }
102
103 public List findByRuleTemplate(RuleTemplate ruleTemplate) {
104 return ruleTemplateDAO.findByRuleTemplate(ruleTemplate);
105 }
106
107 public void save(RuleTemplate ruleTemplate) {
108 LOG.debug("save RuleTemplateServiceImpl");
109 validate(ruleTemplate);
110 fixAssociations(ruleTemplate);
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132 getRuleTemplateDAO().save(ruleTemplate);
133 LOG.debug("end save RuleTemplateServiceImpl");
134 }
135
136 public void save(RuleTemplateAttribute ruleTemplateAttribute) {
137 ruleTemplateAttributeDAO.save(ruleTemplateAttribute);
138 }
139
140
141
142
143 public void saveRuleDefaults(RuleDelegation ruleDelegation, RuleBaseValues ruleBaseValues) {
144 KEWServiceLocator.getRuleService().saveRule(ruleBaseValues, false);
145 if (ruleDelegation != null) {
146 KEWServiceLocator.getRuleService().saveRule(ruleDelegation.getDelegationRuleBaseValues(), false);
147 KEWServiceLocator.getRuleDelegationService().save(ruleDelegation);
148 }
149 }
150
151
152
153
154
155 private void fixAssociations(RuleTemplate ruleTemplate) {
156
157 if (ruleTemplate != null && ruleTemplate.getRuleTemplateId() != null) {
158
159 for (RuleTemplateAttribute ruleTemplateAttribute: ruleTemplate.getRuleTemplateAttributes()) {
160
161 if (ruleTemplateAttribute.getRuleTemplate() == null || ruleTemplateAttribute.getRuleTemplateId() == null) {
162 ruleTemplateAttribute.setRuleTemplate(ruleTemplate);
163 }
164
165 if (ruleTemplateAttribute.getRuleAttribute() == null) {
166 RuleAttributeService ruleAttributeService = (RuleAttributeService) KEWServiceLocator.getService(KEWServiceLocator.RULE_ATTRIBUTE_SERVICE);
167 ruleTemplateAttribute.setRuleAttribute(ruleAttributeService.findByRuleAttributeId(ruleTemplateAttribute.getRuleAttributeId()));
168 }
169 }
170
171 for (RuleTemplateOption option: ruleTemplate.getRuleTemplateOptions()) {
172
173 if (option.getRuleTemplate() == null || option.getRuleTemplateId() == null) {
174 option.setRuleTemplate(ruleTemplate);
175 }
176 }
177 }
178 }
179
180 private void validate(RuleTemplate ruleTemplate) {
181 LOG.debug("validating ruleTemplate");
182 Collection errors = new ArrayList();
183 if (ruleTemplate.getName() == null || ruleTemplate.getName().trim().equals("")) {
184 errors.add(new WorkflowServiceErrorImpl("Please enter a rule template name.", RULE_TEMPLATE_NAME_REQUIRED));
185 LOG.error("Rule template name is missing");
186 } else {
187 ruleTemplate.setName(ruleTemplate.getName().trim());
188 if (ruleTemplate.getRuleTemplateId() == null) {
189 RuleTemplate nameInUse = findByRuleTemplateName(ruleTemplate.getName());
190 if (nameInUse != null) {
191 errors.add(new WorkflowServiceErrorImpl("Rule template name already in use", "rule.template.name.duplicate"));
192 LOG.error("Rule template name already in use");
193 }
194 }
195 }
196 if (ruleTemplate.getDescription() == null || ruleTemplate.getDescription().equals("")) {
197 errors.add(new WorkflowServiceErrorImpl("Please enter a rule template description.", RULE_TEMPLATE_DESCRIPTION_REQUIRED));
198 LOG.error("Rule template description is missing");
199 }
200
201
202
203
204
205
206 LOG.debug("end validating ruleTemplate");
207 if (!errors.isEmpty()) {
208 throw new WorkflowServiceErrorException("RuleTemplate Validation Error", errors);
209 }
210 }
211
212 public RuleTemplate findByRuleTemplateId(Long ruleTemplateId) {
213 LOG.debug("findByRuleTemplateId RuleTemplateServiceImpl");
214 return getRuleTemplateDAO().findByRuleTemplateId(ruleTemplateId);
215 }
216
217 public void delete(Long ruleTemplateId) {
218 LOG.debug("delete RuleTemplateServiceImpl");
219 getRuleTemplateDAO().delete(ruleTemplateId);
220 LOG.debug("end delete RuleTemplateServiceImpl");
221 }
222
223 public RuleTemplateDAO getRuleTemplateDAO() {
224 return ruleTemplateDAO;
225 }
226
227 public void setRuleTemplateDAO(RuleTemplateDAO ruleTemplateDAO) {
228 this.ruleTemplateDAO = ruleTemplateDAO;
229 }
230
231
232
233
234 public RuleTemplateAttributeDAO getRuleTemplateAttributeDAO() {
235 return ruleTemplateAttributeDAO;
236 }
237
238
239
240
241
242 public void setRuleTemplateAttributeDAO(RuleTemplateAttributeDAO ruleTemplateAttributeDAO) {
243 this.ruleTemplateAttributeDAO = ruleTemplateAttributeDAO;
244 }
245
246 public RuleDAO getRuleDAO() {
247 return ruleDAO;
248 }
249
250 public void setRuleDAO(RuleDAO ruleDAO) {
251 this.ruleDAO = ruleDAO;
252 }
253
254 public RuleDelegationDAO getRuleDelegationDAO() {
255 return ruleDelegationDAO;
256 }
257
258 public void setRuleDelegationDAO(RuleDelegationDAO ruleDelegationDAO) {
259 this.ruleDelegationDAO = ruleDelegationDAO;
260 }
261
262
263
264
265 public RuleTemplateOptionDAO getRuleTemplateOptionDAO() {
266 return ruleTemplateOptionDAO;
267 }
268
269
270
271
272
273 public void setRuleTemplateOptionDAO(RuleTemplateOptionDAO ruleTemplateOptionDAO) {
274 this.ruleTemplateOptionDAO = ruleTemplateOptionDAO;
275 }
276
277 public void loadXml(InputStream inputStream, String principalId) {
278 RuleTemplateXmlParser parser = new RuleTemplateXmlParser();
279 try {
280 parser.parseRuleTemplates(inputStream);
281 } catch (Exception e) {
282 LOG.error("Error loading xml file", e);
283 WorkflowServiceErrorException wsee = new WorkflowServiceErrorException("Error loading xml file", new WorkflowServiceErrorImpl("Error loading xml file", XML_PARSE_ERROR));
284 wsee.initCause(e);
285 throw wsee;
286 }
287 }
288
289 public Element export(ExportDataSet dataSet) {
290 RuleTemplateXmlExporter exporter = new RuleTemplateXmlExporter();
291 return exporter.export(dataSet);
292 }
293
294 public Long getNextRuleTemplateId() {
295 return getRuleTemplateDAO().getNextRuleTemplateId();
296 }
297
298 }