View Javadoc

1   /*
2    * Copyright 2005-2008 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.rule.service.impl;
18  
19  import java.io.FileNotFoundException;
20  import java.io.InputStream;
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.List;
24  
25  import org.jdom.Element;
26  import org.kuali.rice.core.api.impex.ExportDataSet;
27  import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
28  import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
29  import org.kuali.rice.kew.rule.bo.RuleAttribute;
30  import org.kuali.rice.kew.rule.dao.RuleAttributeDAO;
31  import org.kuali.rice.kew.rule.service.RuleAttributeService;
32  import org.kuali.rice.kew.service.KEWServiceLocator;
33  import org.kuali.rice.kew.xml.RuleAttributeXmlParser;
34  import org.kuali.rice.kew.xml.export.RuleAttributeXmlExporter;
35  
36  
37  public class RuleAttributeServiceImpl implements RuleAttributeService {
38      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RuleAttributeServiceImpl.class);
39  
40      private static final String RULE_ATTRIBUTE_NAME_REQUIRED = "rule.attribute.name.required";
41      private static final String RULE_ATTRIBUTE_CLASS_REQUIRED = "rule.attribute.className.required";
42  
43      private static final String XML_FILE_NOT_FOUND = "general.error.filenotfound";
44      private static final String XML_PARSE_ERROR = "general.error.parsexml";
45  
46      private RuleAttributeDAO ruleAttributeDAO;
47  
48      public void save(RuleAttribute ruleAttribute) {
49          validate(ruleAttribute);
50          KEWServiceLocator.getDocumentTypeService().clearCacheForAttributeUpdate(ruleAttribute);
51          getRuleAttributeDAO().save(ruleAttribute);
52      }
53  
54      public void delete(String ruleAttributeId) {
55          getRuleAttributeDAO().delete(ruleAttributeId);
56      }
57  
58      public List<RuleAttribute> findByRuleAttribute(RuleAttribute ruleAttribute) {
59          return getRuleAttributeDAO().findByRuleAttribute(ruleAttribute);
60      }
61  
62      public RuleAttribute findByRuleAttributeId(String ruleAttributeId) {
63          return getRuleAttributeDAO().findByRuleAttributeId(ruleAttributeId);
64      }
65  
66      public List<RuleAttribute> findAll() {
67          return getRuleAttributeDAO().getAllRuleAttributes();
68      }
69  
70      public RuleAttribute findByName(String name) {
71      	return getRuleAttributeDAO().findByName(name);
72      }
73  
74      public RuleAttributeDAO getRuleAttributeDAO() {
75          return ruleAttributeDAO;
76      }
77  
78      public void setRuleAttributeDAO(RuleAttributeDAO ruleAttributeDAO) {
79          this.ruleAttributeDAO = ruleAttributeDAO;
80      }
81  
82      private void validate(RuleAttribute ruleAttribute) {
83          LOG.debug("validating ruleAttribute");
84          Collection errors = new ArrayList();
85          if (ruleAttribute.getName() == null || ruleAttribute.getName().trim().equals("")) {
86              errors.add(new WorkflowServiceErrorImpl("Please enter a rule attribute name.", RULE_ATTRIBUTE_NAME_REQUIRED));
87              LOG.error("Rule attribute name is missing");
88          } else {
89          	ruleAttribute.setName(ruleAttribute.getName().trim());
90              if (ruleAttribute.getRuleAttributeId() == null) {
91                  RuleAttribute nameInUse = findByName(ruleAttribute.getName());
92                  if (nameInUse != null) {
93                      errors.add(new WorkflowServiceErrorImpl("Rule attribute name already in use", "routetemplate.ruleattribute.name.duplicate"));
94                      LOG.error("Rule attribute name already in use");
95                  }
96              }
97          }
98          if (ruleAttribute.getClassName() == null || ruleAttribute.getClassName().trim().equals("")) {
99              errors.add(new WorkflowServiceErrorImpl("Please enter a rule attribute class name.", RULE_ATTRIBUTE_CLASS_REQUIRED));
100             LOG.error("Rule attribute class name is missing");
101         } else {
102         	ruleAttribute.setClassName(ruleAttribute.getClassName().trim());
103         }
104 
105         LOG.debug("end validating ruleAttribute");
106         if (!errors.isEmpty()) {
107             throw new WorkflowServiceErrorException("RuleAttribute Validation Error", errors);
108         }
109     }
110 
111     public void loadXml(InputStream inputStream, String principalId) {
112         RuleAttributeXmlParser parser = new RuleAttributeXmlParser();
113         try {
114             parser.parseRuleAttributes(inputStream);
115         } catch(FileNotFoundException e) {
116             throw new WorkflowServiceErrorException("XML file not found", new WorkflowServiceErrorImpl("Rule Attribute XML file not found", XML_FILE_NOT_FOUND) );
117     	} catch (Exception e) { //any other exception
118             LOG.error("Error loading xml file", e);
119             throw new WorkflowServiceErrorException("Error loading xml file", new WorkflowServiceErrorImpl("Error loading xml file", XML_PARSE_ERROR));
120         }
121     }
122 
123     public Element export(ExportDataSet dataSet) {
124         RuleAttributeXmlExporter exporter = new RuleAttributeXmlExporter();
125         return exporter.export(dataSet);
126     }
127     
128 	@Override
129 	public boolean supportPrettyPrint() {
130 		return true;
131 	}
132 
133 	public RuleAttribute findByClassName(String className) {
134 		return this.ruleAttributeDAO.findByClassName(className);
135 	}
136 }