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.FileNotFoundException;
020: import java.io.InputStream;
021: import java.util.ArrayList;
022: import java.util.Collection;
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.RuleAttributeDAO;
032: import edu.iu.uis.eden.user.WorkflowUser;
033: import edu.iu.uis.eden.xml.RuleAttributeXmlParser;
034: import edu.iu.uis.eden.xml.export.RuleAttributeXmlExporter;
035:
036: public class RuleAttributeServiceImpl implements RuleAttributeService {
037: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
038: .getLogger(RuleAttributeServiceImpl.class);
039:
040: private static final String RULE_ATTRIBUTE_NAME_REQUIRED = "rule.attribute.name.required";
041: private static final String RULE_ATTRIBUTE_CLASS_REQUIRED = "rule.attribute.className.required";
042:
043: private static final String XML_FILE_NOT_FOUND = "general.error.filenotfound";
044: private static final String XML_PARSE_ERROR = "general.error.parsexml";
045:
046: private RuleAttributeDAO ruleAttributeDAO;
047:
048: public void save(RuleAttribute ruleAttribute) {
049: validate(ruleAttribute);
050: KEWServiceLocator.getDocumentTypeService()
051: .clearCacheForAttributeUpdate(ruleAttribute);
052: getRuleAttributeDAO().save(ruleAttribute);
053: }
054:
055: public void delete(Long ruleAttributeId) {
056: getRuleAttributeDAO().delete(ruleAttributeId);
057: }
058:
059: public List findByRuleAttribute(RuleAttribute ruleAttribute) {
060: return getRuleAttributeDAO().findByRuleAttribute(ruleAttribute);
061: }
062:
063: public RuleAttribute findByRuleAttributeId(Long ruleAttributeId) {
064: return getRuleAttributeDAO().findByRuleAttributeId(
065: ruleAttributeId);
066: }
067:
068: public List findAll() {
069: return getRuleAttributeDAO().getAllRuleAttributes();
070: }
071:
072: public RuleAttribute findByName(String name) {
073: return getRuleAttributeDAO().findByName(name);
074: }
075:
076: public RuleAttributeDAO getRuleAttributeDAO() {
077: return ruleAttributeDAO;
078: }
079:
080: public void setRuleAttributeDAO(RuleAttributeDAO ruleAttributeDAO) {
081: this .ruleAttributeDAO = ruleAttributeDAO;
082: }
083:
084: private void validate(RuleAttribute ruleAttribute) {
085: LOG.debug("validating ruleAttribute");
086: Collection errors = new ArrayList();
087: if (ruleAttribute.getName() == null
088: || ruleAttribute.getName().trim().equals("")) {
089: errors.add(new WorkflowServiceErrorImpl(
090: "Please enter a rule attribute name.",
091: RULE_ATTRIBUTE_NAME_REQUIRED));
092: LOG.error("Rule attribute name is missing");
093: } else {
094: ruleAttribute.setName(ruleAttribute.getName().trim());
095: if (ruleAttribute.getRuleAttributeId() == null) {
096: RuleAttribute nameInUse = findByName(ruleAttribute
097: .getName());
098: if (nameInUse != null) {
099: errors
100: .add(new WorkflowServiceErrorImpl(
101: "Rule attribute name already in use",
102: "routetemplate.ruleattribute.name.duplicate"));
103: LOG.error("Rule attribute name already in use");
104: }
105: }
106: }
107: if (ruleAttribute.getClassName() == null
108: || ruleAttribute.getClassName().trim().equals("")) {
109: errors.add(new WorkflowServiceErrorImpl(
110: "Please enter a rule attribute class name.",
111: RULE_ATTRIBUTE_CLASS_REQUIRED));
112: LOG.error("Rule attribute class name is missing");
113: } else {
114: ruleAttribute.setClassName(ruleAttribute.getClassName()
115: .trim());
116: }
117:
118: LOG.debug("end validating ruleAttribute");
119: if (!errors.isEmpty()) {
120: throw new WorkflowServiceErrorException(
121: "RuleAttribute Validation Error", errors);
122: }
123: }
124:
125: public void loadXml(InputStream inputStream, WorkflowUser user) {
126: RuleAttributeXmlParser parser = new RuleAttributeXmlParser();
127: try {
128: parser.parseRuleAttributes(inputStream);
129: } catch (FileNotFoundException e) {
130: throw new WorkflowServiceErrorException(
131: "XML file not found", new WorkflowServiceErrorImpl(
132: "Rule Attribute XML file not found",
133: XML_FILE_NOT_FOUND));
134: } catch (Exception e) { //any other exception
135: LOG.error("Error loading xml file", e);
136: throw new WorkflowServiceErrorException(
137: "Error loading xml file",
138: new WorkflowServiceErrorImpl(
139: "Error loading xml file", XML_PARSE_ERROR));
140: }
141: }
142:
143: public Element export(ExportDataSet dataSet) {
144: RuleAttributeXmlExporter exporter = new RuleAttributeXmlExporter();
145: return exporter.export(dataSet);
146: }
147:
148: public RuleAttribute findByClassName(String className) {
149: return this.ruleAttributeDAO.findByClassName(className);
150: }
151: }
|