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