001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.xml.export;
018:
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import org.jdom.Element;
023:
024: import edu.iu.uis.eden.exception.EdenUserNotFoundException;
025: import edu.iu.uis.eden.exception.WorkflowRuntimeException;
026: import edu.iu.uis.eden.export.ExportDataSet;
027: import edu.iu.uis.eden.routetemplate.RuleBaseValues;
028: import edu.iu.uis.eden.routetemplate.RuleDelegation;
029: import edu.iu.uis.eden.routetemplate.RuleExtension;
030: import edu.iu.uis.eden.routetemplate.RuleExtensionValue;
031: import edu.iu.uis.eden.routetemplate.RuleResponsibility;
032: import edu.iu.uis.eden.routetemplate.RuleTemplateAttribute;
033: import edu.iu.uis.eden.xml.XmlConstants;
034:
035: /**
036: * Exports rules to XML.
037: *
038: * @see RuleBaseValues
039: *
040: * @author ewestfal
041: */
042: public class RuleXmlExporter implements XmlExporter, XmlConstants {
043:
044: protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
045: .getLogger(getClass());
046:
047: private ExportRenderer renderer = new ExportRenderer(RULE_NAMESPACE);
048:
049: public Element export(ExportDataSet dataSet) {
050: if (!dataSet.getRules().isEmpty()) {
051: Element rootElement = renderer.renderElement(null, RULES);
052: rootElement.setAttribute(SCHEMA_LOCATION_ATTR,
053: RULE_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
054: for (Iterator iterator = dataSet.getRules().iterator(); iterator
055: .hasNext();) {
056: RuleBaseValues rule = (RuleBaseValues) iterator.next();
057: if (!rule.getDelegateRule().booleanValue()) {
058: exportRule(rootElement, rule, null);
059: } else {
060: LOG
061: .info("Not exporting a top-level delegate rule that was in the result set: "
062: + rule.getRuleBaseValuesId());
063: }
064: }
065: return rootElement;
066: }
067: return null;
068: }
069:
070: private void exportRule(Element parent, RuleBaseValues rule,
071: RuleDelegation delegation) {
072: Element ruleElement = renderer.renderElement(parent, RULE);
073: renderer.renderTextElement(ruleElement, DOCUMENT_TYPE, rule
074: .getDocTypeName());
075: renderer.renderTextElement(ruleElement, RULE_TEMPLATE, rule
076: .getRuleTemplateName());
077: renderer.renderTextElement(ruleElement, DESCRIPTION, rule
078: .getDescription());
079: renderer.renderBooleanElement(ruleElement, IGNORE_PREVIOUS,
080: rule.getIgnorePrevious(), false);
081: exportRuleExtensions(ruleElement, rule.getRuleExtensions());
082: exportResponsibilities(ruleElement, rule.getResponsibilities(),
083: delegation);
084: if (delegation != null) {
085: renderer.renderTextElement(ruleElement, DELEGATION_TYPE,
086: delegation.getDelegationType());
087: }
088: }
089:
090: private void exportRuleExtensions(Element parent,
091: List ruleExtensions) {
092: if (!ruleExtensions.isEmpty()) {
093: Element extsElement = renderer.renderElement(parent,
094: RULE_EXTENSIONS);
095: for (Iterator iterator = ruleExtensions.iterator(); iterator
096: .hasNext();) {
097: RuleExtension extension = (RuleExtension) iterator
098: .next();
099: Element extElement = renderer.renderElement(
100: extsElement, RULE_EXTENSION);
101: RuleTemplateAttribute attribute = extension
102: .getRuleTemplateAttribute();
103: renderer.renderTextElement(extElement, ATTRIBUTE,
104: attribute.getRuleAttribute().getName());
105: renderer.renderTextElement(extElement, RULE_TEMPLATE,
106: attribute.getRuleTemplate().getName());
107: exportRuleExtensionValues(extElement, extension
108: .getExtensionValues());
109: }
110: }
111: }
112:
113: private void exportRuleExtensionValues(Element parent,
114: List extensionValues) {
115: if (!extensionValues.isEmpty()) {
116: Element extValuesElement = renderer.renderElement(parent,
117: RULE_EXTENSION_VALUES);
118: for (Iterator iterator = extensionValues.iterator(); iterator
119: .hasNext();) {
120: RuleExtensionValue extensionValue = (RuleExtensionValue) iterator
121: .next();
122: Element extValueElement = renderer.renderElement(
123: extValuesElement, RULE_EXTENSION_VALUE);
124: renderer.renderTextElement(extValueElement, KEY,
125: extensionValue.getKey());
126: renderer.renderTextElement(extValueElement, VALUE,
127: extensionValue.getValue());
128: }
129: }
130: }
131:
132: private void exportResponsibilities(Element parent,
133: List responsibilities, RuleDelegation delegation) {
134: if (!responsibilities.isEmpty()) {
135: Element responsibilitiesElement = renderer.renderElement(
136: parent, RESPONSIBILITIES);
137: for (Iterator iterator = responsibilities.iterator(); iterator
138: .hasNext();) {
139: RuleResponsibility ruleResponsibility = (RuleResponsibility) iterator
140: .next();
141: Element respElement = renderer.renderElement(
142: responsibilitiesElement, RESPONSIBILITY);
143: try {
144: if (ruleResponsibility.isUsingWorkflowUser()) {
145: renderer.renderTextElement(respElement, USER,
146: ruleResponsibility.getWorkflowUser()
147: .getAuthenticationUserId()
148: .getId());
149: } else if (ruleResponsibility.isUsingWorkgroup()) {
150: renderer.renderTextElement(respElement,
151: WORKGROUP, ruleResponsibility
152: .getWorkgroup()
153: .getGroupNameId().getNameId());
154: } else if (ruleResponsibility.isUsingRole()) {
155: renderer.renderTextElement(respElement, ROLE,
156: ruleResponsibility
157: .getRuleResponsibilityName());
158: renderer.renderTextElement(respElement,
159: APPROVE_POLICY, ruleResponsibility
160: .getApprovePolicy());
161: }
162: } catch (EdenUserNotFoundException e) {
163: throw new WorkflowRuntimeException(
164: "Could not locate user when attempting to export responsibility.");
165: }
166: if (delegation == null) {
167: renderer.renderTextElement(respElement,
168: ACTION_REQUESTED, ruleResponsibility
169: .getActionRequestedCd());
170: renderer
171: .renderTextElement(respElement, PRIORITY,
172: ruleResponsibility.getPriority()
173: .toString());
174: exportDelegations(respElement, ruleResponsibility
175: .getDelegationRules());
176: }
177: }
178: }
179: }
180:
181: private void exportDelegations(Element parent, List ruleDelegations) {
182: if (!ruleDelegations.isEmpty()) {
183: Element delegationsElement = renderer.renderElement(parent,
184: DELEGATIONS);
185: for (Iterator iterator = ruleDelegations.iterator(); iterator
186: .hasNext();) {
187: RuleDelegation delegation = (RuleDelegation) iterator
188: .next();
189: exportRule(delegationsElement, delegation
190: .getDelegationRuleBaseValues(), delegation);
191: }
192: }
193: }
194:
195: }
|