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