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