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 import org.kuali.rice.krad.data.DataObjectService;
43 import org.springframework.beans.factory.annotation.Required;
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 RuleDAO ruleDAO;
58
59 private RuleDelegationDAO ruleDelegationDAO;
60
61 private DataObjectService dataObjectService;
62
63
64 public void deleteRuleTemplateOption(String ruleTemplateOptionId) {
65 RuleTemplateOptionBo ruleTemplateOptionBo = getDataObjectService().find(
66 RuleTemplateOptionBo.class,ruleTemplateOptionId);
67 getDataObjectService().delete(ruleTemplateOptionBo);
68 }
69
70 public RuleTemplateBo findByRuleTemplateName(String ruleTemplateName) {
71 return (getRuleTemplateDAO().findByRuleTemplateName(ruleTemplateName));
72 }
73
74
75
76
77
78
79 public RuleTemplateAttributeBo findByRuleTemplateAttributeId(String ruleTemplateAttributeId) {
80 return getDataObjectService().find(RuleTemplateAttributeBo.class,ruleTemplateAttributeId);
81 }
82
83 public List<RuleTemplateBo> findAll() {
84 return ruleTemplateDAO.findAll();
85 }
86
87 public List findByRuleTemplate(RuleTemplateBo ruleTemplate) {
88 return ruleTemplateDAO.findByRuleTemplate(ruleTemplate);
89 }
90
91 public RuleTemplateBo save(RuleTemplateBo ruleTemplate) {
92 LOG.debug("save RuleTemplateServiceImpl");
93 validate(ruleTemplate);
94 fixAssociations(ruleTemplate);
95
96
97 LOG.debug("end save RuleTemplateServiceImpl");
98 return getRuleTemplateDAO().save(ruleTemplate);
99 }
100
101 public void save(RuleTemplateAttributeBo ruleTemplateAttribute) {
102 getDataObjectService().save(ruleTemplateAttribute);
103 }
104
105
106
107
108 public void saveRuleDefaults(RuleDelegationBo ruleDelegation, RuleBaseValues ruleBaseValues) {
109 KEWServiceLocator.getRuleService().saveRule(ruleBaseValues, false);
110 if (ruleDelegation != null) {
111 KEWServiceLocator.getRuleService().saveRule(ruleDelegation.getDelegationRule(), false);
112 KEWServiceLocator.getRuleDelegationService().save(ruleDelegation);
113 }
114 }
115
116
117
118
119
120 private void fixAssociations(RuleTemplateBo ruleTemplate) {
121
122 if (ruleTemplate != null && ruleTemplate.getId() != null) {
123
124 for (RuleTemplateAttributeBo ruleTemplateAttribute: ruleTemplate.getRuleTemplateAttributes()) {
125
126 if (ruleTemplateAttribute.getRuleTemplate() == null || ruleTemplateAttribute.getRuleTemplateId() == null) {
127 ruleTemplateAttribute.setRuleTemplate(ruleTemplate);
128 }
129
130 if (ruleTemplateAttribute.getRuleAttribute() == null) {
131 RuleAttributeService ruleAttributeService = (RuleAttributeService) KEWServiceLocator.getService(KEWServiceLocator.RULE_ATTRIBUTE_SERVICE);
132 ruleTemplateAttribute.setRuleAttribute(ruleAttributeService.findByRuleAttributeId(ruleTemplateAttribute.getRuleAttributeId()));
133 }
134 }
135
136 for (RuleTemplateOptionBo option: ruleTemplate.getRuleTemplateOptions()) {
137
138 if (option.getRuleTemplate() == null || option.getRuleTemplateId() == null) {
139 option.setRuleTemplate(ruleTemplate);
140 }
141 }
142 }
143 }
144
145 private void validate(RuleTemplateBo ruleTemplate) {
146 LOG.debug("validating ruleTemplate");
147 Collection errors = new ArrayList();
148 if (ruleTemplate.getName() == null || ruleTemplate.getName().trim().equals("")) {
149 errors.add(new WorkflowServiceErrorImpl("Please enter a rule template name.", RULE_TEMPLATE_NAME_REQUIRED));
150 LOG.error("Rule template name is missing");
151 } else {
152 ruleTemplate.setName(ruleTemplate.getName().trim());
153 if (ruleTemplate.getId() == null) {
154 RuleTemplateBo nameInUse = findByRuleTemplateName(ruleTemplate.getName());
155 if (nameInUse != null) {
156 errors.add(new WorkflowServiceErrorImpl("Rule template name already in use", "rule.template.name.duplicate"));
157 LOG.error("Rule template name already in use");
158 }
159 }
160 }
161 if (ruleTemplate.getDescription() == null || ruleTemplate.getDescription().equals("")) {
162 errors.add(new WorkflowServiceErrorImpl("Please enter a rule template description.", RULE_TEMPLATE_DESCRIPTION_REQUIRED));
163 LOG.error("Rule template description is missing");
164 }
165
166
167
168
169
170
171 LOG.debug("end validating ruleTemplate");
172 if (!errors.isEmpty()) {
173 throw new WorkflowServiceErrorException("RuleTemplate Validation Error", errors);
174 }
175 }
176
177 public RuleTemplateBo findByRuleTemplateId(String ruleTemplateId) {
178 LOG.debug("findByRuleTemplateId RuleTemplateServiceImpl");
179 return getRuleTemplateDAO().findByRuleTemplateId(ruleTemplateId);
180 }
181
182 public void delete(String ruleTemplateId) {
183 LOG.debug("delete RuleTemplateServiceImpl");
184 getRuleTemplateDAO().delete(ruleTemplateId);
185 LOG.debug("end delete RuleTemplateServiceImpl");
186 }
187
188 public RuleTemplateDAO getRuleTemplateDAO() {
189 return ruleTemplateDAO;
190 }
191
192 public void setRuleTemplateDAO(RuleTemplateDAO ruleTemplateDAO) {
193 this.ruleTemplateDAO = ruleTemplateDAO;
194 }
195
196 public RuleDAO getRuleDAO() {
197 return ruleDAO;
198 }
199
200 public void setRuleDAO(RuleDAO ruleDAO) {
201 this.ruleDAO = ruleDAO;
202 }
203
204 public RuleDelegationDAO getRuleDelegationDAO() {
205 return ruleDelegationDAO;
206 }
207
208 public void setRuleDelegationDAO(RuleDelegationDAO ruleDelegationDAO) {
209 this.ruleDelegationDAO = ruleDelegationDAO;
210 }
211
212 public void loadXml(InputStream inputStream, String principalId) {
213 RuleTemplateXmlParser parser = new RuleTemplateXmlParser();
214 try {
215 parser.parseRuleTemplates(inputStream);
216 } catch (Exception e) {
217 LOG.error("Error loading xml file", e);
218 WorkflowServiceErrorException wsee = new WorkflowServiceErrorException("Error loading xml file", new WorkflowServiceErrorImpl("Error loading xml file", XML_PARSE_ERROR));
219 wsee.initCause(e);
220 throw wsee;
221 }
222 }
223
224 public Element export(ExportDataSet dataSet) {
225 RuleTemplateXmlExporter exporter = new RuleTemplateXmlExporter();
226 return exporter.export(dataSet);
227 }
228
229 @Override
230 public boolean supportPrettyPrint() {
231 return true;
232 }
233
234 public String getNextRuleTemplateId() {
235 return getRuleTemplateDAO().getNextRuleTemplateId();
236 }
237
238
239 public DataObjectService getDataObjectService() {
240 return dataObjectService;
241 }
242
243 @Required
244 public void setDataObjectService(DataObjectService dataObjectService) {
245 this.dataObjectService = dataObjectService;
246 }
247
248 }