1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
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.exception.RiceRemoteServiceConnectionException; |
27 | |
import org.kuali.rice.core.api.impex.ExportDataSet; |
28 | |
import org.kuali.rice.core.api.reflect.ObjectDefinition; |
29 | |
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; |
30 | |
import org.kuali.rice.core.api.util.ClassLoaderUtils; |
31 | |
import org.kuali.rice.kew.docsearch.SearchableAttributeOld; |
32 | |
import org.kuali.rice.kew.docsearch.xml.GenericXMLSearchableAttribute; |
33 | |
import org.kuali.rice.kew.exception.WorkflowServiceErrorException; |
34 | |
import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl; |
35 | |
import org.kuali.rice.kew.rule.bo.RuleAttribute; |
36 | |
import org.kuali.rice.kew.rule.dao.RuleAttributeDAO; |
37 | |
import org.kuali.rice.kew.rule.service.RuleAttributeService; |
38 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
39 | |
import org.kuali.rice.kew.util.KEWConstants; |
40 | |
import org.kuali.rice.kew.xml.RuleAttributeXmlParser; |
41 | |
import org.kuali.rice.kew.xml.export.RuleAttributeXmlExporter; |
42 | |
|
43 | |
import javax.xml.namespace.QName; |
44 | |
|
45 | 0 | public class RuleAttributeServiceImpl implements RuleAttributeService { |
46 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RuleAttributeServiceImpl.class); |
47 | |
|
48 | |
private static final String RULE_ATTRIBUTE_NAME_REQUIRED = "rule.attribute.name.required"; |
49 | |
private static final String RULE_ATTRIBUTE_CLASS_REQUIRED = "rule.attribute.className.required"; |
50 | |
|
51 | |
private static final String XML_FILE_NOT_FOUND = "general.error.filenotfound"; |
52 | |
private static final String XML_PARSE_ERROR = "general.error.parsexml"; |
53 | |
|
54 | |
private RuleAttributeDAO ruleAttributeDAO; |
55 | |
|
56 | |
public void save(RuleAttribute ruleAttribute) { |
57 | 0 | validate(ruleAttribute); |
58 | 0 | KEWServiceLocator.getDocumentTypeService().clearCacheForAttributeUpdate(ruleAttribute); |
59 | 0 | getRuleAttributeDAO().save(ruleAttribute); |
60 | 0 | } |
61 | |
|
62 | |
public void delete(String ruleAttributeId) { |
63 | 0 | getRuleAttributeDAO().delete(ruleAttributeId); |
64 | 0 | } |
65 | |
|
66 | |
public List<RuleAttribute> findByRuleAttribute(RuleAttribute ruleAttribute) { |
67 | 0 | return getRuleAttributeDAO().findByRuleAttribute(ruleAttribute); |
68 | |
} |
69 | |
|
70 | |
public RuleAttribute findByRuleAttributeId(String ruleAttributeId) { |
71 | 0 | return getRuleAttributeDAO().findByRuleAttributeId(ruleAttributeId); |
72 | |
} |
73 | |
|
74 | |
public List<RuleAttribute> findAll() { |
75 | 0 | return getRuleAttributeDAO().getAllRuleAttributes(); |
76 | |
} |
77 | |
|
78 | |
public RuleAttribute findByName(String name) { |
79 | 0 | return getRuleAttributeDAO().findByName(name); |
80 | |
} |
81 | |
|
82 | |
public RuleAttributeDAO getRuleAttributeDAO() { |
83 | 0 | return ruleAttributeDAO; |
84 | |
} |
85 | |
|
86 | |
public void setRuleAttributeDAO(RuleAttributeDAO ruleAttributeDAO) { |
87 | 0 | this.ruleAttributeDAO = ruleAttributeDAO; |
88 | 0 | } |
89 | |
|
90 | |
private void validate(RuleAttribute ruleAttribute) { |
91 | 0 | LOG.debug("validating ruleAttribute"); |
92 | 0 | Collection errors = new ArrayList(); |
93 | 0 | if (ruleAttribute.getName() == null || ruleAttribute.getName().trim().equals("")) { |
94 | 0 | errors.add(new WorkflowServiceErrorImpl("Please enter a rule attribute name.", RULE_ATTRIBUTE_NAME_REQUIRED)); |
95 | 0 | LOG.error("Rule attribute name is missing"); |
96 | |
} else { |
97 | 0 | ruleAttribute.setName(ruleAttribute.getName().trim()); |
98 | 0 | if (ruleAttribute.getRuleAttributeId() == null) { |
99 | 0 | RuleAttribute nameInUse = findByName(ruleAttribute.getName()); |
100 | 0 | if (nameInUse != null) { |
101 | 0 | errors.add(new WorkflowServiceErrorImpl("Rule attribute name already in use", "routetemplate.ruleattribute.name.duplicate")); |
102 | 0 | LOG.error("Rule attribute name already in use"); |
103 | |
} |
104 | |
} |
105 | |
} |
106 | 0 | if (ruleAttribute.getClassName() == null || ruleAttribute.getClassName().trim().equals("")) { |
107 | 0 | errors.add(new WorkflowServiceErrorImpl("Please enter a rule attribute class name.", RULE_ATTRIBUTE_CLASS_REQUIRED)); |
108 | 0 | LOG.error("Rule attribute class name is missing"); |
109 | |
} else { |
110 | 0 | ruleAttribute.setClassName(ruleAttribute.getClassName().trim()); |
111 | |
} |
112 | |
|
113 | 0 | LOG.debug("end validating ruleAttribute"); |
114 | 0 | if (!errors.isEmpty()) { |
115 | 0 | throw new WorkflowServiceErrorException("RuleAttribute Validation Error", errors); |
116 | |
} |
117 | 0 | } |
118 | |
|
119 | |
@Override |
120 | |
public Object loadRuleAttributeService(RuleAttribute attribute) { |
121 | 0 | return loadRuleAttributeService(attribute, null); |
122 | |
} |
123 | |
|
124 | |
@Override |
125 | |
public Object loadRuleAttributeService(RuleAttribute attribute, String defaultApplicationId) { |
126 | 0 | Object attributeService = null; |
127 | |
|
128 | 0 | String attributeName = attribute.getClassName(); |
129 | 0 | ObjectDefinition attributeObjectDefinition = getAttributeObjectDefinition(attribute, defaultApplicationId); |
130 | 0 | attributeService = GlobalResourceLoader.getObject(attributeObjectDefinition); |
131 | 0 | if (attributeService == null) { |
132 | |
|
133 | 0 | attributeService = GlobalResourceLoader.getService(QName.valueOf(attributeName)); |
134 | |
} |
135 | 0 | return attributeService; |
136 | |
} |
137 | |
|
138 | |
protected ObjectDefinition getAttributeObjectDefinition(RuleAttribute ruleAttribute, String defaultApplicationId) { |
139 | 0 | if (ruleAttribute.getApplicationId() == null && defaultApplicationId != null) { |
140 | 0 | return new ObjectDefinition(ruleAttribute.getClassName(), defaultApplicationId); |
141 | |
} else { |
142 | 0 | return new ObjectDefinition(ruleAttribute.getClassName(), ruleAttribute.getApplicationId()); |
143 | |
} |
144 | |
} |
145 | |
|
146 | |
public void loadXml(InputStream inputStream, String principalId) { |
147 | 0 | RuleAttributeXmlParser parser = new RuleAttributeXmlParser(); |
148 | |
try { |
149 | 0 | parser.parseRuleAttributes(inputStream); |
150 | 0 | } catch(FileNotFoundException e) { |
151 | 0 | throw new WorkflowServiceErrorException("XML file not found", new WorkflowServiceErrorImpl("Rule Attribute XML file not found", XML_FILE_NOT_FOUND) ); |
152 | 0 | } catch (Exception e) { |
153 | 0 | LOG.error("Error loading xml file", e); |
154 | 0 | throw new WorkflowServiceErrorException("Error loading xml file", new WorkflowServiceErrorImpl("Error loading xml file", XML_PARSE_ERROR)); |
155 | 0 | } |
156 | 0 | } |
157 | |
|
158 | |
public Element export(ExportDataSet dataSet) { |
159 | 0 | RuleAttributeXmlExporter exporter = new RuleAttributeXmlExporter(); |
160 | 0 | return exporter.export(dataSet); |
161 | |
} |
162 | |
|
163 | |
@Override |
164 | |
public boolean supportPrettyPrint() { |
165 | 0 | return true; |
166 | |
} |
167 | |
|
168 | |
public RuleAttribute findByClassName(String className) { |
169 | 0 | return this.ruleAttributeDAO.findByClassName(className); |
170 | |
} |
171 | |
} |