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.jdom.Element; |
20 | |
import org.kuali.rice.core.api.impex.ExportDataSet; |
21 | |
import org.kuali.rice.core.api.util.xml.XmlRenderer; |
22 | |
import org.kuali.rice.core.framework.impex.xml.XmlExporter; |
23 | |
import org.kuali.rice.kew.export.KewExportDataSet; |
24 | |
import org.kuali.rice.kew.rule.RuleBaseValues; |
25 | |
import org.kuali.rice.kew.rule.RuleDelegation; |
26 | |
import org.kuali.rice.kew.rule.RuleTemplateOption; |
27 | |
import org.kuali.rice.kew.rule.bo.RuleTemplate; |
28 | |
import org.kuali.rice.kew.rule.bo.RuleTemplateAttribute; |
29 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
30 | |
|
31 | |
import java.util.Iterator; |
32 | |
import java.util.List; |
33 | |
|
34 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.*; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | public class RuleTemplateXmlExporter implements XmlExporter { |
43 | |
|
44 | 0 | protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass()); |
45 | |
|
46 | 0 | private XmlRenderer renderer = new XmlRenderer(RULE_TEMPLATE_NAMESPACE); |
47 | |
|
48 | |
@Override |
49 | |
public boolean supportPrettyPrint() { |
50 | 0 | return true; |
51 | |
} |
52 | |
|
53 | |
public Element export(ExportDataSet exportDataSet) { |
54 | 0 | KewExportDataSet dataSet = KewExportDataSet.fromExportDataSet(exportDataSet); |
55 | 0 | if (!dataSet.getRuleTemplates().isEmpty()) { |
56 | 0 | Element rootElement = renderer.renderElement(null, RULE_TEMPLATES); |
57 | 0 | rootElement.setAttribute(SCHEMA_LOCATION_ATTR, RULE_TEMPLATE_SCHEMA_LOCATION, SCHEMA_NAMESPACE); |
58 | 0 | for (Iterator iterator = dataSet.getRuleTemplates().iterator(); iterator.hasNext();) { |
59 | 0 | RuleTemplate template = (RuleTemplate)iterator.next(); |
60 | 0 | exportRuleTemplate(rootElement, template); |
61 | 0 | } |
62 | 0 | return rootElement; |
63 | |
} |
64 | 0 | return null; |
65 | |
} |
66 | |
|
67 | |
private void exportRuleTemplate(Element parent, RuleTemplate ruleTemplate) { |
68 | 0 | Element templateElement = renderer.renderElement(parent, RULE_TEMPLATE); |
69 | 0 | renderer.renderTextElement(templateElement, NAME, ruleTemplate.getName()); |
70 | 0 | renderer.renderTextElement(templateElement, DESCRIPTION, ruleTemplate.getDescription()); |
71 | 0 | if (ruleTemplate.getDelegationTemplate() != null) { |
72 | 0 | renderer.renderTextElement(templateElement, DELEGATION_TEMPLATE, ruleTemplate.getDelegationTemplate().getName()); |
73 | |
} |
74 | 0 | exportAttributes(templateElement, ruleTemplate.getActiveRuleTemplateAttributes()); |
75 | 0 | exportDefaults(templateElement, ruleTemplate); |
76 | 0 | } |
77 | |
|
78 | |
private void exportAttributes(Element parent, List ruleTemplateAttributes) { |
79 | 0 | if (!ruleTemplateAttributes.isEmpty()) { |
80 | 0 | Element attributesElement = renderer.renderElement(parent, ATTRIBUTES); |
81 | 0 | for (Iterator iterator = ruleTemplateAttributes.iterator(); iterator.hasNext();) { |
82 | 0 | RuleTemplateAttribute attribute = (RuleTemplateAttribute) iterator.next(); |
83 | 0 | Element attributeElement = renderer.renderElement(attributesElement, ATTRIBUTE); |
84 | 0 | renderer.renderTextElement(attributeElement, NAME, attribute.getRuleAttribute().getName()); |
85 | 0 | renderer.renderBooleanElement(attributeElement, REQUIRED, attribute.getRequired(), false); |
86 | 0 | } |
87 | |
} |
88 | 0 | } |
89 | |
|
90 | |
private void exportDefaults(Element parent, RuleTemplate ruleTemplate) { |
91 | 0 | RuleBaseValues defaultRuleValues = KEWServiceLocator.getRuleService().findDefaultRuleByRuleTemplateId(ruleTemplate.getRuleTemplateId()); |
92 | 0 | if (defaultRuleValues != null) { |
93 | 0 | RuleDelegation defaultDelegationValues = getDefaultDelegationValues(defaultRuleValues); |
94 | 0 | Element defaultsElement = renderer.renderElement(parent, RULE_DEFAULTS); |
95 | 0 | if (defaultDelegationValues != null) { |
96 | 0 | renderer.renderTextElement(defaultsElement, DELEGATION_TYPE, defaultDelegationValues.getDelegationType()); |
97 | |
} |
98 | 0 | renderer.renderTextElement(defaultsElement, DESCRIPTION, defaultRuleValues.getDescription()); |
99 | 0 | if (defaultRuleValues.getFromDate() != null) { |
100 | 0 | renderer.renderDateElement(defaultsElement, FROM_DATE, defaultRuleValues.getFromDate()); |
101 | |
} |
102 | 0 | if (defaultRuleValues.getToDate() != null) { |
103 | 0 | renderer.renderDateElement(defaultsElement, TO_DATE, defaultRuleValues.getToDate()); |
104 | |
} |
105 | 0 | renderer.renderBooleanElement(defaultsElement, FORCE_ACTION, defaultRuleValues.getForceAction(), false); |
106 | 0 | renderer.renderBooleanElement(defaultsElement, ACTIVE, defaultRuleValues.getActiveInd(), true); |
107 | 0 | if (defaultDelegationValues == null) { |
108 | 0 | RuleTemplateOption defaultActionOption = ruleTemplate.getDefaultActionRequestValue(); |
109 | 0 | RuleTemplateOption supportsComplete = ruleTemplate.getComplete(); |
110 | 0 | RuleTemplateOption supportsApprove = ruleTemplate.getApprove(); |
111 | 0 | RuleTemplateOption supportsAck = ruleTemplate.getAcknowledge(); |
112 | 0 | RuleTemplateOption supportsFYI = ruleTemplate.getFyi(); |
113 | 0 | if (defaultActionOption != null) { |
114 | 0 | String defaultActionValue = (defaultActionOption == null ? null : defaultActionOption.getValue()); |
115 | 0 | renderer.renderTextElement(defaultsElement, DEFAULT_ACTION_REQUESTED, defaultActionValue); |
116 | |
} |
117 | 0 | if (supportsComplete != null) { |
118 | 0 | String supportsCompleteValue = supportsComplete.getValue(); |
119 | 0 | renderer.renderTextElement(defaultsElement, SUPPORTS_COMPLETE, supportsCompleteValue); |
120 | |
} |
121 | 0 | if (supportsApprove != null) { |
122 | 0 | String supportsApproveValue = supportsApprove.getValue(); |
123 | 0 | renderer.renderTextElement(defaultsElement, SUPPORTS_APPROVE, supportsApproveValue); |
124 | |
} |
125 | 0 | if (supportsAck != null) { |
126 | 0 | String supportsAckValue = supportsAck.getValue(); |
127 | 0 | renderer.renderTextElement(defaultsElement, SUPPORTS_ACKNOWLEDGE, supportsAckValue); |
128 | |
} |
129 | 0 | if (supportsFYI != null) { |
130 | 0 | String supportsFYIValue = supportsFYI.getValue(); |
131 | 0 | renderer.renderTextElement(defaultsElement, SUPPORTS_FYI, supportsFYIValue); |
132 | |
} |
133 | |
} |
134 | |
} |
135 | 0 | } |
136 | |
|
137 | |
private RuleDelegation getDefaultDelegationValues(RuleBaseValues defaultRuleValues) { |
138 | 0 | List ruleDelegations = KEWServiceLocator.getRuleDelegationService().findByDelegateRuleId(defaultRuleValues.getRuleBaseValuesId()); |
139 | 0 | if (ruleDelegations.size() > 1) { |
140 | 0 | LOG.warn("The rule defaults has more than one associated delegation defaults."); |
141 | |
} |
142 | 0 | return (ruleDelegations.isEmpty() ? null : (RuleDelegation)ruleDelegations.get(0)); |
143 | |
} |
144 | |
|
145 | |
} |