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