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