1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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 | 0 | public class RuleAttributeServiceImpl implements RuleAttributeService { |
38 | 0 | 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 | 0 | validate(ruleAttribute); |
50 | 0 | getRuleAttributeDAO().save(ruleAttribute); |
51 | 0 | } |
52 | |
|
53 | |
public void delete(String ruleAttributeId) { |
54 | 0 | getRuleAttributeDAO().delete(ruleAttributeId); |
55 | 0 | } |
56 | |
|
57 | |
public List<RuleAttribute> findByRuleAttribute(RuleAttribute ruleAttribute) { |
58 | 0 | return getRuleAttributeDAO().findByRuleAttribute(ruleAttribute); |
59 | |
} |
60 | |
|
61 | |
public RuleAttribute findByRuleAttributeId(String ruleAttributeId) { |
62 | 0 | return getRuleAttributeDAO().findByRuleAttributeId(ruleAttributeId); |
63 | |
} |
64 | |
|
65 | |
public List<RuleAttribute> findAll() { |
66 | 0 | return getRuleAttributeDAO().getAllRuleAttributes(); |
67 | |
} |
68 | |
|
69 | |
public RuleAttribute findByName(String name) { |
70 | 0 | return getRuleAttributeDAO().findByName(name); |
71 | |
} |
72 | |
|
73 | |
public RuleAttributeDAO getRuleAttributeDAO() { |
74 | 0 | return ruleAttributeDAO; |
75 | |
} |
76 | |
|
77 | |
public void setRuleAttributeDAO(RuleAttributeDAO ruleAttributeDAO) { |
78 | 0 | this.ruleAttributeDAO = ruleAttributeDAO; |
79 | 0 | } |
80 | |
|
81 | |
private void validate(RuleAttribute ruleAttribute) { |
82 | 0 | LOG.debug("validating ruleAttribute"); |
83 | 0 | Collection errors = new ArrayList(); |
84 | 0 | if (ruleAttribute.getName() == null || ruleAttribute.getName().trim().equals("")) { |
85 | 0 | errors.add(new WorkflowServiceErrorImpl("Please enter a rule attribute name.", RULE_ATTRIBUTE_NAME_REQUIRED)); |
86 | 0 | LOG.error("Rule attribute name is missing"); |
87 | |
} else { |
88 | 0 | ruleAttribute.setName(ruleAttribute.getName().trim()); |
89 | 0 | if (ruleAttribute.getId() == null) { |
90 | 0 | RuleAttribute nameInUse = findByName(ruleAttribute.getName()); |
91 | 0 | if (nameInUse != null) { |
92 | 0 | errors.add(new WorkflowServiceErrorImpl("Rule attribute name already in use", "routetemplate.ruleattribute.name.duplicate")); |
93 | 0 | LOG.error("Rule attribute name already in use"); |
94 | |
} |
95 | |
} |
96 | |
} |
97 | 0 | if (ruleAttribute.getResourceDescriptor() == null || ruleAttribute.getResourceDescriptor().trim().equals("")) { |
98 | 0 | errors.add(new WorkflowServiceErrorImpl("Please enter a rule attribute class name.", RULE_ATTRIBUTE_CLASS_REQUIRED)); |
99 | 0 | LOG.error("Rule attribute class name is missing"); |
100 | |
} else { |
101 | 0 | ruleAttribute.setResourceDescriptor(ruleAttribute.getResourceDescriptor().trim()); |
102 | |
} |
103 | |
|
104 | 0 | LOG.debug("end validating ruleAttribute"); |
105 | 0 | if (!errors.isEmpty()) { |
106 | 0 | throw new WorkflowServiceErrorException("RuleAttribute Validation Error", errors); |
107 | |
} |
108 | 0 | } |
109 | |
|
110 | |
@Override |
111 | |
public Object loadRuleAttributeService(RuleAttribute attribute) { |
112 | 0 | return loadRuleAttributeService(attribute, null); |
113 | |
} |
114 | |
|
115 | |
@Override |
116 | |
public Object loadRuleAttributeService(RuleAttribute attribute, String defaultApplicationId) { |
117 | 0 | Object attributeService = null; |
118 | |
|
119 | 0 | String attributeName = attribute.getResourceDescriptor(); |
120 | 0 | ObjectDefinition attributeObjectDefinition = getAttributeObjectDefinition(attribute, defaultApplicationId); |
121 | 0 | attributeService = GlobalResourceLoader.getObject(attributeObjectDefinition); |
122 | 0 | if (attributeService == null) { |
123 | |
|
124 | 0 | attributeService = GlobalResourceLoader.getService(QName.valueOf(attributeName)); |
125 | |
} |
126 | 0 | return attributeService; |
127 | |
} |
128 | |
|
129 | |
protected ObjectDefinition getAttributeObjectDefinition(RuleAttribute ruleAttribute, String defaultApplicationId) { |
130 | 0 | if (ruleAttribute.getApplicationId() == null && defaultApplicationId != null) { |
131 | 0 | return new ObjectDefinition(ruleAttribute.getResourceDescriptor(), defaultApplicationId); |
132 | |
} else { |
133 | 0 | return new ObjectDefinition(ruleAttribute.getResourceDescriptor(), ruleAttribute.getApplicationId()); |
134 | |
} |
135 | |
} |
136 | |
|
137 | |
public void loadXml(InputStream inputStream, String principalId) { |
138 | 0 | RuleAttributeXmlParser parser = new RuleAttributeXmlParser(); |
139 | |
try { |
140 | 0 | parser.parseRuleAttributes(inputStream); |
141 | 0 | } catch(FileNotFoundException e) { |
142 | 0 | throw new WorkflowServiceErrorException("XML file not found", new WorkflowServiceErrorImpl("Rule Attribute XML file not found", XML_FILE_NOT_FOUND) ); |
143 | 0 | } catch (Exception e) { |
144 | 0 | LOG.error("Error loading xml file", e); |
145 | 0 | throw new WorkflowServiceErrorException("Error loading xml file", new WorkflowServiceErrorImpl("Error loading xml file", XML_PARSE_ERROR)); |
146 | 0 | } |
147 | 0 | } |
148 | |
|
149 | |
public Element export(ExportDataSet dataSet) { |
150 | 0 | RuleAttributeXmlExporter exporter = new RuleAttributeXmlExporter(); |
151 | 0 | return exporter.export(dataSet); |
152 | |
} |
153 | |
|
154 | |
@Override |
155 | |
public boolean supportPrettyPrint() { |
156 | 0 | return true; |
157 | |
} |
158 | |
|
159 | |
public RuleAttribute findByClassName(String className) { |
160 | 0 | return this.ruleAttributeDAO.findByClassName(className); |
161 | |
} |
162 | |
} |