View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.rule.service.impl;
17  
18  import org.jdom.Element;
19  import org.kuali.rice.core.api.impex.ExportDataSet;
20  import org.kuali.rice.core.api.reflect.ObjectDefinition;
21  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
22  import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
23  import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
24  import org.kuali.rice.kew.rule.bo.RuleAttribute;
25  import org.kuali.rice.kew.rule.dao.RuleAttributeDAO;
26  import org.kuali.rice.kew.rule.service.RuleAttributeService;
27  import org.kuali.rice.kew.xml.RuleAttributeXmlParser;
28  import org.kuali.rice.kew.xml.export.RuleAttributeXmlExporter;
29  
30  import javax.xml.namespace.QName;
31  import java.io.FileNotFoundException;
32  import java.io.InputStream;
33  import java.util.ArrayList;
34  import java.util.Collection;
35  import java.util.List;
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          getRuleAttributeDAO().save(ruleAttribute);
51      }
52  
53      public void delete(String ruleAttributeId) {
54          getRuleAttributeDAO().delete(ruleAttributeId);
55      }
56  
57      public List<RuleAttribute> findByRuleAttribute(RuleAttribute ruleAttribute) {
58          return getRuleAttributeDAO().findByRuleAttribute(ruleAttribute);
59      }
60  
61      public RuleAttribute findByRuleAttributeId(String ruleAttributeId) {
62          return getRuleAttributeDAO().findByRuleAttributeId(ruleAttributeId);
63      }
64  
65      public List<RuleAttribute> findAll() {
66          return getRuleAttributeDAO().getAllRuleAttributes();
67      }
68  
69      public RuleAttribute findByName(String name) {
70      	return getRuleAttributeDAO().findByName(name);
71      }
72  
73      public RuleAttributeDAO getRuleAttributeDAO() {
74          return ruleAttributeDAO;
75      }
76  
77      public void setRuleAttributeDAO(RuleAttributeDAO ruleAttributeDAO) {
78          this.ruleAttributeDAO = ruleAttributeDAO;
79      }
80  
81      private void validate(RuleAttribute ruleAttribute) {
82          LOG.debug("validating ruleAttribute");
83          Collection errors = new ArrayList();
84          if (ruleAttribute.getName() == null || ruleAttribute.getName().trim().equals("")) {
85              errors.add(new WorkflowServiceErrorImpl("Please enter a rule attribute name.", RULE_ATTRIBUTE_NAME_REQUIRED));
86              LOG.error("Rule attribute name is missing");
87          } else {
88          	ruleAttribute.setName(ruleAttribute.getName().trim());
89              if (ruleAttribute.getId() == null) {
90                  RuleAttribute nameInUse = findByName(ruleAttribute.getName());
91                  if (nameInUse != null) {
92                      errors.add(new WorkflowServiceErrorImpl("Rule attribute name already in use", "routetemplate.ruleattribute.name.duplicate"));
93                      LOG.error("Rule attribute name already in use");
94                  }
95              }
96          }
97          if (ruleAttribute.getResourceDescriptor() == null || ruleAttribute.getResourceDescriptor().trim().equals("")) {
98              errors.add(new WorkflowServiceErrorImpl("Please enter a rule attribute class name.", RULE_ATTRIBUTE_CLASS_REQUIRED));
99              LOG.error("Rule attribute class name is missing");
100         } else {
101         	ruleAttribute.setResourceDescriptor(ruleAttribute.getResourceDescriptor().trim());
102         }
103 
104         LOG.debug("end validating ruleAttribute");
105         if (!errors.isEmpty()) {
106             throw new WorkflowServiceErrorException("RuleAttribute Validation Error", errors);
107         }
108     }
109 
110     @Override
111     public Object loadRuleAttributeService(RuleAttribute attribute) {
112         return loadRuleAttributeService(attribute, null);
113     }
114 
115     @Override
116     public Object loadRuleAttributeService(RuleAttribute attribute, String defaultApplicationId) {
117         Object attributeService = null;
118         // first check if the class name is a valid and available java class
119         String attributeName = attribute.getResourceDescriptor();
120         ObjectDefinition attributeObjectDefinition = getAttributeObjectDefinition(attribute, defaultApplicationId);
121         attributeService = GlobalResourceLoader.getObject(attributeObjectDefinition);
122         if (attributeService == null) {
123             // if we can't find a class, try a service
124             attributeService = GlobalResourceLoader.getService(QName.valueOf(attributeName));
125         }
126         return attributeService;
127     }
128 
129     protected ObjectDefinition getAttributeObjectDefinition(RuleAttribute ruleAttribute, String defaultApplicationId) {
130         if (ruleAttribute.getApplicationId() == null && defaultApplicationId != null) {
131             return new ObjectDefinition(ruleAttribute.getResourceDescriptor(), defaultApplicationId);
132         } else {
133             return new ObjectDefinition(ruleAttribute.getResourceDescriptor(), ruleAttribute.getApplicationId());
134         }
135     }
136 
137     public void loadXml(InputStream inputStream, String principalId) {
138         RuleAttributeXmlParser parser = new RuleAttributeXmlParser();
139         try {
140             parser.parseRuleAttributes(inputStream);
141         } catch(FileNotFoundException e) {
142             throw new WorkflowServiceErrorException("XML file not found", new WorkflowServiceErrorImpl("Rule Attribute XML file not found", XML_FILE_NOT_FOUND) );
143     	} catch (Exception e) { //any other exception
144             LOG.error("Error loading xml file", e);
145             throw new WorkflowServiceErrorException("Error loading xml file", new WorkflowServiceErrorImpl("Error loading xml file", XML_PARSE_ERROR));
146         }
147     }
148 
149     public Element export(ExportDataSet dataSet) {
150         RuleAttributeXmlExporter exporter = new RuleAttributeXmlExporter();
151         return exporter.export(dataSet);
152     }
153     
154 	@Override
155 	public boolean supportPrettyPrint() {
156 		return true;
157 	}
158 
159 	public List<RuleAttribute> findByClassName(String className) {
160 		return this.ruleAttributeDAO.findByClassName(className);
161 	}
162 }