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 | 0 | public class RuleTemplateServiceImpl implements RuleTemplateService { |
46 | |
|
47 | 0 | 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 | 0 | getRuleTemplateOptionDAO().delete(ruleTemplateOptionId); |
84 | 0 | } |
85 | |
|
86 | |
public RuleTemplate findByRuleTemplateName(String ruleTemplateName) { |
87 | 0 | return (getRuleTemplateDAO().findByRuleTemplateName(ruleTemplateName)); |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
public RuleTemplateAttribute findByRuleTemplateAttributeId(Long ruleTemplateAttributeId) { |
96 | 0 | return getRuleTemplateAttributeDAO().findByRuleTemplateAttributeId(ruleTemplateAttributeId); |
97 | |
} |
98 | |
|
99 | |
public List findAll() { |
100 | 0 | return ruleTemplateDAO.findAll(); |
101 | |
} |
102 | |
|
103 | |
public List findByRuleTemplate(RuleTemplate ruleTemplate) { |
104 | 0 | return ruleTemplateDAO.findByRuleTemplate(ruleTemplate); |
105 | |
} |
106 | |
|
107 | |
public void save(RuleTemplate ruleTemplate) { |
108 | 0 | LOG.debug("save RuleTemplateServiceImpl"); |
109 | 0 | validate(ruleTemplate); |
110 | 0 | fixAssociations(ruleTemplate); |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | 0 | getRuleTemplateDAO().save(ruleTemplate); |
133 | 0 | LOG.debug("end save RuleTemplateServiceImpl"); |
134 | 0 | } |
135 | |
|
136 | |
public void save(RuleTemplateAttribute ruleTemplateAttribute) { |
137 | 0 | ruleTemplateAttributeDAO.save(ruleTemplateAttribute); |
138 | 0 | } |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
public void saveRuleDefaults(RuleDelegation ruleDelegation, RuleBaseValues ruleBaseValues) { |
144 | 0 | KEWServiceLocator.getRuleService().saveRule(ruleBaseValues, false); |
145 | 0 | if (ruleDelegation != null) { |
146 | 0 | KEWServiceLocator.getRuleService().saveRule(ruleDelegation.getDelegationRuleBaseValues(), false); |
147 | 0 | KEWServiceLocator.getRuleDelegationService().save(ruleDelegation); |
148 | |
} |
149 | 0 | } |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
private void fixAssociations(RuleTemplate ruleTemplate) { |
156 | |
|
157 | 0 | if (ruleTemplate != null && ruleTemplate.getRuleTemplateId() != null) { |
158 | |
|
159 | 0 | for (RuleTemplateAttribute ruleTemplateAttribute: ruleTemplate.getRuleTemplateAttributes()) { |
160 | |
|
161 | 0 | if (ruleTemplateAttribute.getRuleTemplate() == null || ruleTemplateAttribute.getRuleTemplateId() == null) { |
162 | 0 | ruleTemplateAttribute.setRuleTemplate(ruleTemplate); |
163 | |
} |
164 | |
|
165 | 0 | if (ruleTemplateAttribute.getRuleAttribute() == null) { |
166 | 0 | RuleAttributeService ruleAttributeService = (RuleAttributeService) KEWServiceLocator.getService(KEWServiceLocator.RULE_ATTRIBUTE_SERVICE); |
167 | 0 | ruleTemplateAttribute.setRuleAttribute(ruleAttributeService.findByRuleAttributeId(ruleTemplateAttribute.getRuleAttributeId())); |
168 | 0 | } |
169 | |
} |
170 | |
|
171 | 0 | for (RuleTemplateOption option: ruleTemplate.getRuleTemplateOptions()) { |
172 | |
|
173 | 0 | if (option.getRuleTemplate() == null || option.getRuleTemplateId() == null) { |
174 | 0 | option.setRuleTemplate(ruleTemplate); |
175 | |
} |
176 | |
} |
177 | |
} |
178 | 0 | } |
179 | |
|
180 | |
private void validate(RuleTemplate ruleTemplate) { |
181 | 0 | LOG.debug("validating ruleTemplate"); |
182 | 0 | Collection errors = new ArrayList(); |
183 | 0 | if (ruleTemplate.getName() == null || ruleTemplate.getName().trim().equals("")) { |
184 | 0 | errors.add(new WorkflowServiceErrorImpl("Please enter a rule template name.", RULE_TEMPLATE_NAME_REQUIRED)); |
185 | 0 | LOG.error("Rule template name is missing"); |
186 | |
} else { |
187 | 0 | ruleTemplate.setName(ruleTemplate.getName().trim()); |
188 | 0 | if (ruleTemplate.getRuleTemplateId() == null) { |
189 | 0 | RuleTemplate nameInUse = findByRuleTemplateName(ruleTemplate.getName()); |
190 | 0 | if (nameInUse != null) { |
191 | 0 | errors.add(new WorkflowServiceErrorImpl("Rule template name already in use", "rule.template.name.duplicate")); |
192 | 0 | LOG.error("Rule template name already in use"); |
193 | |
} |
194 | |
} |
195 | |
} |
196 | 0 | if (ruleTemplate.getDescription() == null || ruleTemplate.getDescription().equals("")) { |
197 | 0 | errors.add(new WorkflowServiceErrorImpl("Please enter a rule template description.", RULE_TEMPLATE_DESCRIPTION_REQUIRED)); |
198 | 0 | LOG.error("Rule template description is missing"); |
199 | |
} |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | 0 | LOG.debug("end validating ruleTemplate"); |
207 | 0 | if (!errors.isEmpty()) { |
208 | 0 | throw new WorkflowServiceErrorException("RuleTemplate Validation Error", errors); |
209 | |
} |
210 | 0 | } |
211 | |
|
212 | |
public RuleTemplate findByRuleTemplateId(Long ruleTemplateId) { |
213 | 0 | LOG.debug("findByRuleTemplateId RuleTemplateServiceImpl"); |
214 | 0 | return getRuleTemplateDAO().findByRuleTemplateId(ruleTemplateId); |
215 | |
} |
216 | |
|
217 | |
public void delete(Long ruleTemplateId) { |
218 | 0 | LOG.debug("delete RuleTemplateServiceImpl"); |
219 | 0 | getRuleTemplateDAO().delete(ruleTemplateId); |
220 | 0 | LOG.debug("end delete RuleTemplateServiceImpl"); |
221 | 0 | } |
222 | |
|
223 | |
public RuleTemplateDAO getRuleTemplateDAO() { |
224 | 0 | return ruleTemplateDAO; |
225 | |
} |
226 | |
|
227 | |
public void setRuleTemplateDAO(RuleTemplateDAO ruleTemplateDAO) { |
228 | 0 | this.ruleTemplateDAO = ruleTemplateDAO; |
229 | 0 | } |
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
public RuleTemplateAttributeDAO getRuleTemplateAttributeDAO() { |
235 | 0 | return ruleTemplateAttributeDAO; |
236 | |
} |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
public void setRuleTemplateAttributeDAO(RuleTemplateAttributeDAO ruleTemplateAttributeDAO) { |
243 | 0 | this.ruleTemplateAttributeDAO = ruleTemplateAttributeDAO; |
244 | 0 | } |
245 | |
|
246 | |
public RuleDAO getRuleDAO() { |
247 | 0 | return ruleDAO; |
248 | |
} |
249 | |
|
250 | |
public void setRuleDAO(RuleDAO ruleDAO) { |
251 | 0 | this.ruleDAO = ruleDAO; |
252 | 0 | } |
253 | |
|
254 | |
public RuleDelegationDAO getRuleDelegationDAO() { |
255 | 0 | return ruleDelegationDAO; |
256 | |
} |
257 | |
|
258 | |
public void setRuleDelegationDAO(RuleDelegationDAO ruleDelegationDAO) { |
259 | 0 | this.ruleDelegationDAO = ruleDelegationDAO; |
260 | 0 | } |
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
public RuleTemplateOptionDAO getRuleTemplateOptionDAO() { |
266 | 0 | return ruleTemplateOptionDAO; |
267 | |
} |
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
public void setRuleTemplateOptionDAO(RuleTemplateOptionDAO ruleTemplateOptionDAO) { |
274 | 0 | this.ruleTemplateOptionDAO = ruleTemplateOptionDAO; |
275 | 0 | } |
276 | |
|
277 | |
public void loadXml(InputStream inputStream, String principalId) { |
278 | 0 | RuleTemplateXmlParser parser = new RuleTemplateXmlParser(); |
279 | |
try { |
280 | 0 | parser.parseRuleTemplates(inputStream); |
281 | 0 | } catch (Exception e) { |
282 | 0 | LOG.error("Error loading xml file", e); |
283 | 0 | WorkflowServiceErrorException wsee = new WorkflowServiceErrorException("Error loading xml file", new WorkflowServiceErrorImpl("Error loading xml file", XML_PARSE_ERROR)); |
284 | 0 | wsee.initCause(e); |
285 | 0 | throw wsee; |
286 | 0 | } |
287 | 0 | } |
288 | |
|
289 | |
public Element export(ExportDataSet dataSet) { |
290 | 0 | RuleTemplateXmlExporter exporter = new RuleTemplateXmlExporter(); |
291 | 0 | return exporter.export(dataSet); |
292 | |
} |
293 | |
|
294 | |
public Long getNextRuleTemplateId() { |
295 | 0 | return getRuleTemplateDAO().getNextRuleTemplateId(); |
296 | |
} |
297 | |
|
298 | |
} |