001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.routetemplate;
018:
019: import java.io.InputStream;
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.jdom.Element;
026:
027: import edu.iu.uis.eden.KEWServiceLocator;
028: import edu.iu.uis.eden.WorkflowServiceErrorException;
029: import edu.iu.uis.eden.WorkflowServiceErrorImpl;
030: import edu.iu.uis.eden.export.ExportDataSet;
031: import edu.iu.uis.eden.routetemplate.dao.RuleDAO;
032: import edu.iu.uis.eden.routetemplate.dao.RuleDelegationDAO;
033: import edu.iu.uis.eden.routetemplate.dao.RuleTemplateAttributeDAO;
034: import edu.iu.uis.eden.routetemplate.dao.RuleTemplateDAO;
035: import edu.iu.uis.eden.routetemplate.dao.RuleTemplateOptionDAO;
036: import edu.iu.uis.eden.user.WorkflowUser;
037: import edu.iu.uis.eden.xml.RuleTemplateXmlParser;
038: import edu.iu.uis.eden.xml.export.RuleTemplateXmlExporter;
039:
040: public class RuleTemplateServiceImpl implements RuleTemplateService {
041:
042: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
043: .getLogger(RuleTemplateServiceImpl.class);
044:
045: private static final String RULE_TEMPLATE_NAME_REQUIRED = "rule.template.name.required";
046:
047: private static final String RULE_TEMPLATE_DESCRIPTION_REQUIRED = "rule.template.description.required";
048:
049: private static final String XML_PARSE_ERROR = "general.error.parsexml";
050:
051: private RuleTemplateDAO ruleTemplateDAO;
052:
053: private RuleTemplateAttributeDAO ruleTemplateAttributeDAO;
054:
055: private RuleTemplateOptionDAO ruleTemplateOptionDAO;
056:
057: private RuleDAO ruleDAO;
058:
059: private RuleDelegationDAO ruleDelegationDAO;
060:
061: /*
062: * (non-Javadoc)
063: *
064: * @see edu.iu.uis.eden.routetemplate.RuleTemplateAttributeService#delete(java.lang.Long)
065: */
066: public void deleteRuleTemplateAttribute(
067: Long ruleTemplateAttributeId, List ruleTemplateAttributes) {
068:
069: RuleTemplateAttribute ruleTemplateAttributeRemove = findByRuleTemplateAttributeId(ruleTemplateAttributeId);
070:
071: for (int i = ruleTemplateAttributeRemove.getDisplayOrder()
072: .intValue() + 1; i <= ruleTemplateAttributes.size(); i++) {
073: RuleTemplateAttribute ruleTemplateAttributeUpdate = (RuleTemplateAttribute) ruleTemplateAttributes
074: .get(i - 1);
075: ruleTemplateAttributeUpdate.setDisplayOrder(new Integer(
076: i - 1));
077: getRuleTemplateAttributeDAO().save(
078: ruleTemplateAttributeUpdate);
079: }
080: getRuleTemplateAttributeDAO().delete(ruleTemplateAttributeId);
081: }
082:
083: public void deleteRuleTemplateOption(Long ruleTemplateOptionId) {
084: getRuleTemplateOptionDAO().delete(ruleTemplateOptionId);
085: }
086:
087: public RuleTemplate findByRuleTemplateName(String ruleTemplateName) {
088: return (getRuleTemplateDAO()
089: .findByRuleTemplateName(ruleTemplateName));
090: }
091:
092: /*
093: * (non-Javadoc)
094: *
095: * @see edu.iu.uis.eden.routetemplate.RuleTemplateAttributeService#findByRuleTemplateAttributeId(java.lang.Long)
096: */
097: public RuleTemplateAttribute findByRuleTemplateAttributeId(
098: Long ruleTemplateAttributeId) {
099: return getRuleTemplateAttributeDAO()
100: .findByRuleTemplateAttributeId(ruleTemplateAttributeId);
101: }
102:
103: public List findAll() {
104: return ruleTemplateDAO.findAll();
105: }
106:
107: public List findByRuleTemplate(RuleTemplate ruleTemplate) {
108: return ruleTemplateDAO.findByRuleTemplate(ruleTemplate);
109: }
110:
111: public void save(RuleTemplate ruleTemplate) {
112: LOG.debug("save RuleTemplateServiceImpl");
113: validate(ruleTemplate);
114: fixAssociations(ruleTemplate);
115: if (ruleTemplate.getRuleTemplateId() != null) {
116: RuleTemplate previousRuleTemplate = findByRuleTemplateId(ruleTemplate
117: .getRuleTemplateId());
118: if (previousRuleTemplate != null) {
119: for (Iterator iter = previousRuleTemplate
120: .getRuleTemplateAttributes().iterator(); iter
121: .hasNext();) {
122: RuleTemplateAttribute previousAttribute = (RuleTemplateAttribute) iter
123: .next();
124: boolean found = false;
125:
126: for (Iterator iter2 = ruleTemplate
127: .getRuleTemplateAttributes().iterator(); iter2
128: .hasNext();) {
129: RuleTemplateAttribute attribute = (RuleTemplateAttribute) iter2
130: .next();
131: if (previousAttribute.getRuleAttribute()
132: .getName().equals(
133: attribute.getRuleAttribute()
134: .getName())) {
135: found = true;
136: break;
137: }
138: }
139: if (!found) {
140: getRuleTemplateAttributeDAO().delete(
141: previousAttribute
142: .getRuleTemplateAttributeId());
143: }
144: }
145: }
146: }
147:
148: getRuleTemplateDAO().save(ruleTemplate);
149: LOG.debug("end save RuleTemplateServiceImpl");
150: }
151:
152: public void save(RuleTemplateAttribute ruleTemplateAttribute) {
153: ruleTemplateAttributeDAO.save(ruleTemplateAttribute);
154: }
155:
156: public void save(RuleBaseValues ruleBaseValues) {
157: ruleDAO.save(ruleBaseValues);
158: }
159:
160: public void save(RuleDelegation ruleDelegation,
161: RuleBaseValues ruleBaseValues) {
162: save(ruleBaseValues);
163: if (ruleDelegation != null) {
164: ruleDelegationDAO.save(ruleDelegation);
165: }
166: }
167:
168: private void fixAssociations(RuleTemplate ruleTemplate) {
169: if (ruleTemplate != null
170: && ruleTemplate.getRuleTemplateId() != null) {
171: for (Iterator iter = ruleTemplate
172: .getRuleTemplateAttributes().iterator(); iter
173: .hasNext();) {
174: RuleTemplateAttribute ruleTemplateAttribute = (RuleTemplateAttribute) iter
175: .next();
176: if (ruleTemplateAttribute.getRuleTemplate() == null
177: || ruleTemplateAttribute.getRuleTemplateId() == null) {
178: ruleTemplateAttribute.setRuleTemplate(ruleTemplate);
179: }
180: if (ruleTemplateAttribute.getRuleAttribute() == null) {
181: RuleAttributeService ruleAttributeService = (RuleAttributeService) KEWServiceLocator
182: .getService(KEWServiceLocator.RULE_ATTRIBUTE_SERVICE);
183: ruleTemplateAttribute
184: .setRuleAttribute(ruleAttributeService
185: .findByRuleAttributeId(ruleTemplateAttribute
186: .getRuleAttributeId()));
187: }
188: }
189: for (Iterator iter = ruleTemplate.getRuleTemplateOptions()
190: .iterator(); iter.hasNext();) {
191: RuleTemplateOption option = (RuleTemplateOption) iter
192: .next();
193: if (option.getRuleTemplate() == null
194: || option.getRuleTemplateId() == null) {
195: option.setRuleTemplate(ruleTemplate);
196: }
197: }
198: }
199: }
200:
201: private void validate(RuleTemplate ruleTemplate) {
202: LOG.debug("validating ruleTemplate");
203: Collection errors = new ArrayList();
204: if (ruleTemplate.getName() == null
205: || ruleTemplate.getName().trim().equals("")) {
206: errors.add(new WorkflowServiceErrorImpl(
207: "Please enter a rule template name.",
208: RULE_TEMPLATE_NAME_REQUIRED));
209: LOG.error("Rule template name is missing");
210: } else {
211: ruleTemplate.setName(ruleTemplate.getName().trim());
212: if (ruleTemplate.getRuleTemplateId() == null) {
213: RuleTemplate nameInUse = findByRuleTemplateName(ruleTemplate
214: .getName());
215: if (nameInUse != null) {
216: errors.add(new WorkflowServiceErrorImpl(
217: "Rule template name already in use",
218: "rule.template.name.duplicate"));
219: LOG.error("Rule template name already in use");
220: }
221: }
222: }
223: if (ruleTemplate.getDescription() == null
224: || ruleTemplate.getDescription().equals("")) {
225: errors.add(new WorkflowServiceErrorImpl(
226: "Please enter a rule template description.",
227: RULE_TEMPLATE_DESCRIPTION_REQUIRED));
228: LOG.error("Rule template description is missing");
229: }
230: // if (ruleTemplate.getRuleTemplateAttributes() == null ||
231: // ruleTemplate.getRuleTemplateAttributes().isEmpty()) {
232: // errors.add(new WorkflowServiceErrorImpl("Please select at least one a
233: // rule template attribute.", RULE_TEMPLATE_ATTRIBUTE_REQUIRED));
234: // }
235:
236: LOG.debug("end validating ruleTemplate");
237: if (!errors.isEmpty()) {
238: throw new WorkflowServiceErrorException(
239: "RuleTemplate Validation Error", errors);
240: }
241: }
242:
243: public RuleTemplate findByRuleTemplateId(Long ruleTemplateId) {
244: LOG.debug("findByRuleTemplateId RuleTemplateServiceImpl");
245: return getRuleTemplateDAO()
246: .findByRuleTemplateId(ruleTemplateId);
247: }
248:
249: public void delete(Long ruleTemplateId) {
250: LOG.debug("delete RuleTemplateServiceImpl");
251: getRuleTemplateDAO().delete(ruleTemplateId);
252: LOG.debug("end delete RuleTemplateServiceImpl");
253: }
254:
255: public RuleTemplateDAO getRuleTemplateDAO() {
256: return ruleTemplateDAO;
257: }
258:
259: public void setRuleTemplateDAO(RuleTemplateDAO ruleTemplateDAO) {
260: this .ruleTemplateDAO = ruleTemplateDAO;
261: }
262:
263: /**
264: * @return Returns the ruleTemplateAttributeDAO.
265: */
266: public RuleTemplateAttributeDAO getRuleTemplateAttributeDAO() {
267: return ruleTemplateAttributeDAO;
268: }
269:
270: /**
271: * @param ruleTemplateAttributeDAO
272: * The ruleTemplateAttributeDAO to set.
273: */
274: public void setRuleTemplateAttributeDAO(
275: RuleTemplateAttributeDAO ruleTemplateAttributeDAO) {
276: this .ruleTemplateAttributeDAO = ruleTemplateAttributeDAO;
277: }
278:
279: public RuleDAO getRuleDAO() {
280: return ruleDAO;
281: }
282:
283: public void setRuleDAO(RuleDAO ruleDAO) {
284: this .ruleDAO = ruleDAO;
285: }
286:
287: public RuleDelegationDAO getRuleDelegationDAO() {
288: return ruleDelegationDAO;
289: }
290:
291: public void setRuleDelegationDAO(RuleDelegationDAO ruleDelegationDAO) {
292: this .ruleDelegationDAO = ruleDelegationDAO;
293: }
294:
295: /**
296: * @return Returns the ruleTemplateOptionDAO.
297: */
298: public RuleTemplateOptionDAO getRuleTemplateOptionDAO() {
299: return ruleTemplateOptionDAO;
300: }
301:
302: /**
303: * @param ruleTemplateOptionDAO
304: * The ruleTemplateOptionDAO to set.
305: */
306: public void setRuleTemplateOptionDAO(
307: RuleTemplateOptionDAO ruleTemplateOptionDAO) {
308: this .ruleTemplateOptionDAO = ruleTemplateOptionDAO;
309: }
310:
311: public void loadXml(InputStream inputStream, WorkflowUser user) {
312: RuleTemplateXmlParser parser = new RuleTemplateXmlParser();
313: try {
314: parser.parseRuleTemplates(inputStream);
315: } catch (Exception e) { //any other exception
316: LOG.error("Error loading xml file", e);
317: WorkflowServiceErrorException wsee = new WorkflowServiceErrorException(
318: "Error loading xml file",
319: new WorkflowServiceErrorImpl(
320: "Error loading xml file", XML_PARSE_ERROR));
321: wsee.initCause(e);
322: throw wsee;
323: }
324: }
325:
326: public Element export(ExportDataSet dataSet) {
327: RuleTemplateXmlExporter exporter = new RuleTemplateXmlExporter();
328: return exporter.export(dataSet);
329: }
330:
331: public Long getNextRuleTemplateId() {
332: return getRuleTemplateDAO().getNextRuleTemplateId();
333: }
334:
335: }
|