1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.xml.export; |
18 | |
|
19 | |
import org.apache.commons.collections.CollectionUtils; |
20 | |
import org.apache.commons.lang.StringUtils; |
21 | |
import org.jdom.Element; |
22 | |
import org.jdom.Namespace; |
23 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
24 | |
import org.kuali.rice.core.api.impex.ExportDataSet; |
25 | |
import org.kuali.rice.core.api.util.xml.XmlRenderer; |
26 | |
import org.kuali.rice.core.framework.impex.xml.XmlExporter; |
27 | |
import org.kuali.rice.kew.export.KewExportDataSet; |
28 | |
import org.kuali.rice.kew.rule.RuleBaseValues; |
29 | |
import org.kuali.rice.kew.rule.RuleDelegation; |
30 | |
import org.kuali.rice.kew.rule.RuleExtension; |
31 | |
import org.kuali.rice.kew.rule.RuleExtensionValue; |
32 | |
import org.kuali.rice.kew.rule.RuleResponsibility; |
33 | |
import org.kuali.rice.kew.rule.bo.RuleTemplateAttribute; |
34 | |
import org.kuali.rice.kew.rule.web.WebRuleUtils; |
35 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
36 | |
import org.kuali.rice.kim.api.group.Group; |
37 | |
import org.kuali.rice.kim.api.identity.principal.Principal; |
38 | |
|
39 | |
import java.util.Collection; |
40 | |
import java.util.HashSet; |
41 | |
import java.util.Iterator; |
42 | |
import java.util.List; |
43 | |
import java.util.Set; |
44 | |
|
45 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.*; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public class RuleXmlExporter implements XmlExporter { |
55 | |
|
56 | 0 | protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass()); |
57 | |
|
58 | |
private XmlRenderer renderer; |
59 | |
|
60 | 0 | public RuleXmlExporter(Namespace namespace) { |
61 | 0 | this.renderer = new XmlRenderer(namespace); |
62 | 0 | } |
63 | |
|
64 | |
@Override |
65 | |
public boolean supportPrettyPrint() { |
66 | 0 | return true; |
67 | |
} |
68 | |
|
69 | |
public Element export(ExportDataSet exportDataSet) { |
70 | 0 | KewExportDataSet dataSet = KewExportDataSet.fromExportDataSet(exportDataSet); |
71 | 0 | if (!dataSet.getRules().isEmpty()) { |
72 | 0 | Element rootElement = renderer.renderElement(null, RULES); |
73 | 0 | rootElement.setAttribute(SCHEMA_LOCATION_ATTR, RULE_SCHEMA_LOCATION, SCHEMA_NAMESPACE); |
74 | 0 | for (Iterator iterator = dataSet.getRules().iterator(); iterator.hasNext();) { |
75 | 0 | RuleBaseValues rule = (RuleBaseValues) iterator.next(); |
76 | 0 | exportRule(rootElement, rule); |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | 0 | } |
82 | 0 | return rootElement; |
83 | |
} |
84 | 0 | return null; |
85 | |
} |
86 | |
|
87 | |
public void exportRule(Element parent, RuleBaseValues rule) { |
88 | 0 | Element ruleElement = renderer.renderElement(parent, RULE); |
89 | 0 | if (rule.getName() != null) { |
90 | 0 | renderer.renderTextElement(ruleElement, NAME, rule.getName()); |
91 | |
} |
92 | 0 | renderer.renderTextElement(ruleElement, DOCUMENT_TYPE, rule.getDocTypeName()); |
93 | 0 | if (rule.getRuleTemplateName() != null) { |
94 | 0 | renderer.renderTextElement(ruleElement, RULE_TEMPLATE, rule.getRuleTemplateName()); |
95 | |
} |
96 | 0 | renderer.renderTextElement(ruleElement, DESCRIPTION, rule.getDescription()); |
97 | 0 | if(rule.getFromDateString() != null){ |
98 | 0 | renderer.renderTextElement(ruleElement, FROM_DATE, rule.getFromDateString()); |
99 | |
} |
100 | 0 | if(rule.getToDateString() != null){ |
101 | 0 | renderer.renderTextElement(ruleElement, TO_DATE, rule.getToDateString()); |
102 | |
} |
103 | 0 | if (rule.getRuleExpressionDef() != null) { |
104 | 0 | Element expressionElement = renderer.renderTextElement(ruleElement, EXPRESSION, rule.getRuleExpressionDef().getExpression()); |
105 | 0 | if (rule.getRuleExpressionDef().getType() != null) { |
106 | 0 | expressionElement.setAttribute("type", rule.getRuleExpressionDef().getType()); |
107 | |
} |
108 | |
} |
109 | 0 | renderer.renderBooleanElement(ruleElement, FORCE_ACTION, rule.getForceAction(), false); |
110 | |
|
111 | 0 | if (CollectionUtils.isEmpty(rule.getRuleExtensions()) && |
112 | |
|
113 | |
!(rule.getFieldValues() == null || rule.getFieldValues().size() == 0)) { |
114 | |
|
115 | |
|
116 | 0 | WebRuleUtils.translateResponsibilitiesForSave(rule); |
117 | 0 | WebRuleUtils.translateFieldValuesForSave(rule); |
118 | |
|
119 | |
|
120 | 0 | exportRuleExtensions(ruleElement, rule.getRuleExtensions()); |
121 | |
|
122 | |
|
123 | 0 | WebRuleUtils.populateRuleMaintenanceFields(rule); |
124 | |
} else { |
125 | 0 | exportRuleExtensions(ruleElement, rule.getRuleExtensions()); |
126 | |
} |
127 | |
|
128 | |
|
129 | 0 | Set<RuleResponsibility> responsibilities = new HashSet<RuleResponsibility>(); |
130 | 0 | responsibilities.addAll(rule.getResponsibilities()); |
131 | 0 | responsibilities.addAll(rule.getPersonResponsibilities()); |
132 | 0 | responsibilities.addAll(rule.getGroupResponsibilities()); |
133 | 0 | responsibilities.addAll(rule.getRoleResponsibilities()); |
134 | |
|
135 | 0 | exportResponsibilities(ruleElement, responsibilities); |
136 | 0 | } |
137 | |
|
138 | |
private void exportRuleExtensions(Element parent, List ruleExtensions) { |
139 | 0 | if (!ruleExtensions.isEmpty()) { |
140 | 0 | Element extsElement = renderer.renderElement(parent, RULE_EXTENSIONS); |
141 | 0 | for (Iterator iterator = ruleExtensions.iterator(); iterator.hasNext();) { |
142 | 0 | RuleExtension extension = (RuleExtension) iterator.next(); |
143 | 0 | Element extElement = renderer.renderElement(extsElement, RULE_EXTENSION); |
144 | 0 | RuleTemplateAttribute attribute = extension.getRuleTemplateAttribute(); |
145 | 0 | renderer.renderTextElement(extElement, ATTRIBUTE, attribute.getRuleAttribute().getName()); |
146 | 0 | renderer.renderTextElement(extElement, RULE_TEMPLATE, attribute.getRuleTemplate().getName()); |
147 | 0 | exportRuleExtensionValues(extElement, extension.getExtensionValues()); |
148 | 0 | } |
149 | |
} |
150 | 0 | } |
151 | |
|
152 | |
private void exportRuleExtensionValues(Element parent, List extensionValues) { |
153 | 0 | if (!extensionValues.isEmpty()) { |
154 | 0 | Element extValuesElement = renderer.renderElement(parent, RULE_EXTENSION_VALUES); |
155 | 0 | for (Iterator iterator = extensionValues.iterator(); iterator.hasNext();) { |
156 | 0 | RuleExtensionValue extensionValue = (RuleExtensionValue) iterator.next(); |
157 | 0 | Element extValueElement = renderer.renderElement(extValuesElement, RULE_EXTENSION_VALUE); |
158 | 0 | renderer.renderTextElement(extValueElement, KEY, extensionValue.getKey()); |
159 | 0 | renderer.renderTextElement(extValueElement, VALUE, extensionValue.getValue()); |
160 | 0 | } |
161 | |
} |
162 | 0 | } |
163 | |
|
164 | |
private void exportResponsibilities(Element parent, Collection<? extends RuleResponsibility> responsibilities) { |
165 | 0 | if (responsibilities != null && !responsibilities.isEmpty()) { |
166 | 0 | Element responsibilitiesElement = renderer.renderElement(parent, RESPONSIBILITIES); |
167 | 0 | for (RuleResponsibility ruleResponsibility : responsibilities) { |
168 | 0 | Element respElement = renderer.renderElement(responsibilitiesElement, RESPONSIBILITY); |
169 | 0 | renderer.renderTextElement(respElement, RESPONSIBILITY_ID, "" + ruleResponsibility.getResponsibilityId()); |
170 | 0 | if (ruleResponsibility.isUsingWorkflowUser()) { |
171 | 0 | renderer.renderTextElement(respElement, PRINCIPAL_NAME, ruleResponsibility.getPrincipal().getPrincipalName()); |
172 | 0 | } else if (ruleResponsibility.isUsingGroup()) { |
173 | 0 | Group group = ruleResponsibility.getGroup(); |
174 | 0 | Element groupElement = renderer.renderTextElement(respElement, GROUP_NAME, group.getName()); |
175 | 0 | groupElement.setAttribute(NAMESPACE, group.getNamespaceCode()); |
176 | 0 | } else if (ruleResponsibility.isUsingRole()) { |
177 | 0 | renderer.renderTextElement(respElement, ROLE, ruleResponsibility.getRuleResponsibilityName()); |
178 | 0 | renderer.renderTextElement(respElement, APPROVE_POLICY, ruleResponsibility.getApprovePolicy()); |
179 | |
} |
180 | 0 | if (!StringUtils.isBlank(ruleResponsibility.getActionRequestedCd())) { |
181 | 0 | renderer.renderTextElement(respElement, ACTION_REQUESTED, ruleResponsibility.getActionRequestedCd()); |
182 | |
} |
183 | 0 | if (ruleResponsibility.getPriority() != null) { |
184 | 0 | renderer.renderTextElement(respElement, PRIORITY, ruleResponsibility.getPriority().toString()); |
185 | |
} |
186 | 0 | } |
187 | |
} |
188 | 0 | } |
189 | |
|
190 | |
|
191 | |
private void exportRuleDelegations(Element rootElement, RuleBaseValues rule){ |
192 | 0 | List<RuleDelegation> ruleDelegationDefaults = KEWServiceLocator.getRuleDelegationService().findByDelegateRuleId(rule.getRuleBaseValuesId()); |
193 | 0 | for(RuleDelegation dele : ruleDelegationDefaults){ |
194 | 0 | if (LOG.isInfoEnabled()) { |
195 | 0 | LOG.info("*******delegates********\t" + dele.getRuleDelegationId()) ; |
196 | |
} |
197 | 0 | exportRuleDelegation(rootElement, dele); |
198 | |
} |
199 | 0 | } |
200 | |
|
201 | |
private void exportRuleDelegation(Element parent, RuleDelegation ruleDelegation) { |
202 | 0 | Element ruleDelegationElement = renderer.renderElement(parent, RULE_DELEGATION); |
203 | 0 | exportRuleDelegationParentResponsibility(ruleDelegationElement, ruleDelegation); |
204 | 0 | renderer.renderTextElement(ruleDelegationElement, DELEGATION_TYPE, ruleDelegation.getDelegationType()); |
205 | 0 | exportRule(ruleDelegationElement, ruleDelegation.getDelegationRuleBaseValues()); |
206 | 0 | } |
207 | |
|
208 | |
private void exportRuleDelegationParentResponsibility(Element parent, RuleDelegation delegation) { |
209 | 0 | Element parentResponsibilityElement = renderer.renderElement(parent, PARENT_RESPONSIBILITY); |
210 | 0 | RuleResponsibility ruleResponsibility = KEWServiceLocator.getRuleService().findRuleResponsibility(delegation.getResponsibilityId()); |
211 | 0 | renderer.renderTextElement(parentResponsibilityElement, PARENT_RULE_NAME, ruleResponsibility.getRuleBaseValues().getName()); |
212 | 0 | if (ruleResponsibility.isUsingWorkflowUser()) { |
213 | 0 | Principal principal = ruleResponsibility.getPrincipal(); |
214 | 0 | renderer.renderTextElement(parentResponsibilityElement, PRINCIPAL_NAME, principal.getPrincipalName()); |
215 | 0 | } else if (ruleResponsibility.isUsingGroup()) { |
216 | 0 | Group group = ruleResponsibility.getGroup(); |
217 | 0 | Element groupElement = renderer.renderElement(parentResponsibilityElement, GROUP_NAME); |
218 | 0 | groupElement.setText(group.getName()); |
219 | 0 | groupElement.setAttribute(NAMESPACE, group.getNamespaceCode()); |
220 | 0 | } else if (ruleResponsibility.isUsingRole()) { |
221 | 0 | renderer.renderTextElement(parentResponsibilityElement, ROLE, ruleResponsibility.getRuleResponsibilityName()); |
222 | |
} else { |
223 | 0 | throw new RiceRuntimeException("Encountered a rule responsibility when exporting with an invalid type of '" + ruleResponsibility.getRuleResponsibilityType()); |
224 | |
} |
225 | 0 | } |
226 | |
|
227 | |
} |