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.EdenConstants;
025: import edu.iu.uis.eden.KEWServiceLocator;
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.RuleTemplate;
030: import edu.iu.uis.eden.routetemplate.RuleTemplateAttribute;
031: import edu.iu.uis.eden.routetemplate.RuleTemplateOption;
032: import edu.iu.uis.eden.xml.XmlConstants;
033:
034: /**
035: * Exports {@link RuleTemplate}s to XML.
036: *
037: * @see RuleTemplate
038: *
039: * @author ewestfal
040: */
041: public class RuleTemplateXmlExporter implements XmlExporter,
042: XmlConstants {
043:
044: protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
045: .getLogger(getClass());
046:
047: private ExportRenderer renderer = new ExportRenderer(
048: RULE_TEMPLATE_NAMESPACE);
049:
050: public Element export(ExportDataSet dataSet) {
051: if (!dataSet.getRuleTemplates().isEmpty()) {
052: Element rootElement = renderer.renderElement(null,
053: RULE_TEMPLATES);
054: rootElement.setAttribute(SCHEMA_LOCATION_ATTR,
055: RULE_TEMPLATE_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
056: for (Iterator iterator = dataSet.getRuleTemplates()
057: .iterator(); iterator.hasNext();) {
058: RuleTemplate template = (RuleTemplate) iterator.next();
059: exportRuleTemplate(rootElement, template);
060: }
061: return rootElement;
062: }
063: return null;
064: }
065:
066: private void exportRuleTemplate(Element parent,
067: RuleTemplate ruleTemplate) {
068: Element templateElement = renderer.renderElement(parent,
069: RULE_TEMPLATE);
070: renderer.renderTextElement(templateElement, NAME, ruleTemplate
071: .getName());
072: renderer.renderTextElement(templateElement, DESCRIPTION,
073: ruleTemplate.getDescription());
074: if (ruleTemplate.getDelegationTemplate() != null) {
075: renderer.renderTextElement(templateElement,
076: DELEGATION_TEMPLATE, ruleTemplate
077: .getDelegationTemplate().getName());
078: }
079: exportAttributes(templateElement, ruleTemplate
080: .getRuleTemplateAttributes());
081: exportDefaults(templateElement, ruleTemplate);
082: }
083:
084: private void exportAttributes(Element parent,
085: List ruleTemplateAttributes) {
086: if (!ruleTemplateAttributes.isEmpty()) {
087: Element attributesElement = renderer.renderElement(parent,
088: ATTRIBUTES);
089: for (Iterator iterator = ruleTemplateAttributes.iterator(); iterator
090: .hasNext();) {
091: RuleTemplateAttribute attribute = (RuleTemplateAttribute) iterator
092: .next();
093: Element attributeElement = renderer.renderElement(
094: attributesElement, ATTRIBUTE);
095: renderer.renderTextElement(attributeElement, NAME,
096: attribute.getRuleAttribute().getName());
097: renderer.renderBooleanElement(attributeElement,
098: REQUIRED, attribute.getRequired(), false);
099: }
100: }
101: }
102:
103: private void exportDefaults(Element parent,
104: RuleTemplate ruleTemplate) {
105: RuleBaseValues defaultRuleValues = KEWServiceLocator
106: .getRuleService().findDefaultRuleByRuleTemplateId(
107: ruleTemplate.getRuleTemplateId());
108: if (defaultRuleValues != null) {
109: RuleDelegation defaultDelegationValues = getDefaultDelegationValues(defaultRuleValues);
110: Element defaultsElement = renderer.renderElement(parent,
111: RULE_DEFAULTS);
112: if (defaultDelegationValues != null) {
113: renderer.renderTextElement(defaultsElement,
114: DELEGATION_TYPE, defaultDelegationValues
115: .getDelegationType());
116: }
117: RuleTemplateOption instructionsOption = ruleTemplate
118: .getInstructions();
119: String instructionsValue = (instructionsOption == null ? ""
120: : instructionsOption.getValue());
121: renderer.renderTextElement(defaultsElement,
122: RULE_INSTRUCTIONS, instructionsValue);
123: renderer.renderTextElement(defaultsElement, DESCRIPTION,
124: defaultRuleValues.getDescription());
125: renderer.renderDateElement(defaultsElement, FROM_DATE,
126: defaultRuleValues.getFromDate());
127: renderer.renderDateElement(defaultsElement, TO_DATE,
128: defaultRuleValues.getToDate());
129: renderer.renderBooleanElement(defaultsElement,
130: IGNORE_PREVIOUS, defaultRuleValues
131: .getIgnorePrevious(), false);
132: renderer.renderBooleanElement(defaultsElement, ACTIVE,
133: defaultRuleValues.getActiveInd(), true);
134: if (defaultDelegationValues == null) {
135: RuleTemplateOption defaultActionOption = ruleTemplate
136: .getDefaultActionRequestValue();
137: RuleTemplateOption supportsComplete = ruleTemplate
138: .getComplete();
139: RuleTemplateOption supportsApprove = ruleTemplate
140: .getApprove();
141: RuleTemplateOption supportsAck = ruleTemplate
142: .getAcknowledge();
143: RuleTemplateOption supportsFYI = ruleTemplate.getFyi();
144: String defaultActionValue = (defaultActionOption == null ? EdenConstants.ACTION_REQUEST_APPROVE_REQ
145: : defaultActionOption.getValue());
146: String supportsCompleteValue = (supportsComplete == null ? Boolean.TRUE
147: .toString()
148: : supportsComplete.getValue());
149: String supportsApproveValue = (supportsApprove == null ? Boolean.TRUE
150: .toString()
151: : supportsApprove.getValue());
152: String supportsAckValue = (supportsAck == null ? Boolean.TRUE
153: .toString()
154: : supportsAck.getValue());
155: String supportsFYIValue = (supportsFYI == null ? Boolean.TRUE
156: .toString()
157: : supportsFYI.getValue());
158: renderer.renderTextElement(defaultsElement,
159: DEFAULT_ACTION_REQUESTED, defaultActionValue);
160: renderer.renderTextElement(defaultsElement,
161: SUPPORTS_COMPLETE, supportsCompleteValue);
162: renderer.renderTextElement(defaultsElement,
163: SUPPORTS_APPROVE, supportsApproveValue);
164: renderer.renderTextElement(defaultsElement,
165: SUPPORTS_ACKNOWLEDGE, supportsAckValue);
166: renderer.renderTextElement(defaultsElement,
167: SUPPORTS_FYI, supportsFYIValue);
168: }
169: }
170: }
171:
172: private RuleDelegation getDefaultDelegationValues(
173: RuleBaseValues defaultRuleValues) {
174: List ruleDelegations = KEWServiceLocator
175: .getRuleDelegationService().findByDelegateRuleId(
176: defaultRuleValues.getRuleBaseValuesId());
177: if (ruleDelegations.size() > 1) {
178: LOG
179: .warn("The rule defaults has more than one associated delegation defaults.");
180: }
181: return (ruleDelegations.isEmpty() ? null
182: : (RuleDelegation) ruleDelegations.get(0));
183: }
184:
185: }
|