View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.xml.export;
17  
18  import org.jdom.Element;
19  import org.kuali.rice.core.api.impex.ExportDataSet;
20  import org.kuali.rice.core.api.util.xml.XmlRenderer;
21  import org.kuali.rice.core.framework.impex.xml.XmlExporter;
22  import org.kuali.rice.kew.export.KewExportDataSet;
23  import org.kuali.rice.kew.rule.RuleBaseValues;
24  import org.kuali.rice.kew.rule.RuleDelegationBo;
25  import org.kuali.rice.kew.rule.RuleTemplateOptionBo;
26  import org.kuali.rice.kew.rule.bo.RuleTemplateBo;
27  import org.kuali.rice.kew.rule.bo.RuleTemplateAttributeBo;
28  import org.kuali.rice.kew.service.KEWServiceLocator;
29  
30  import java.util.Iterator;
31  import java.util.List;
32  
33  import static org.kuali.rice.core.api.impex.xml.XmlConstants.*;
34  /**
35   * Exports {@link org.kuali.rice.kew.rule.bo.RuleTemplateBo}s to XML.
36   * 
37   * @see org.kuali.rice.kew.rule.bo.RuleTemplateBo
38   *
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   */
41  public class RuleTemplateXmlExporter implements XmlExporter {
42  
43      protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass());
44      
45      private XmlRenderer renderer = new XmlRenderer(RULE_TEMPLATE_NAMESPACE);
46      
47  	@Override
48  	public boolean supportPrettyPrint() {
49  		return true;
50  	}
51  
52      public Element export(ExportDataSet exportDataSet) {
53      	KewExportDataSet dataSet = KewExportDataSet.fromExportDataSet(exportDataSet);
54          if (!dataSet.getRuleTemplates().isEmpty()) {
55              Element rootElement = renderer.renderElement(null, RULE_TEMPLATES);
56              rootElement.setAttribute(SCHEMA_LOCATION_ATTR, RULE_TEMPLATE_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
57              for (Iterator iterator = dataSet.getRuleTemplates().iterator(); iterator.hasNext();) {
58                  RuleTemplateBo template = (RuleTemplateBo)iterator.next();
59                  exportRuleTemplate(rootElement, template);
60              }
61              return rootElement;
62          }
63          return null;
64      }
65      
66      private void exportRuleTemplate(Element parent, RuleTemplateBo ruleTemplate) {
67          Element templateElement = renderer.renderElement(parent, RULE_TEMPLATE);
68          renderer.renderTextElement(templateElement, NAME, ruleTemplate.getName());
69          renderer.renderTextElement(templateElement, DESCRIPTION, ruleTemplate.getDescription());
70          if (ruleTemplate.getDelegationTemplate() != null) {
71              renderer.renderTextElement(templateElement, DELEGATION_TEMPLATE, ruleTemplate.getDelegationTemplate().getName());
72          }
73          exportAttributes(templateElement, ruleTemplate.getActiveRuleTemplateAttributes());
74          exportDefaults(templateElement, ruleTemplate);
75      }
76  
77      private void exportAttributes(Element parent, List ruleTemplateAttributes) {
78          if (!ruleTemplateAttributes.isEmpty()) {
79              Element attributesElement = renderer.renderElement(parent, ATTRIBUTES);
80              for (Iterator iterator = ruleTemplateAttributes.iterator(); iterator.hasNext();) {
81                  RuleTemplateAttributeBo attribute = (RuleTemplateAttributeBo) iterator.next();
82                  Element attributeElement = renderer.renderElement(attributesElement, ATTRIBUTE);
83                  renderer.renderTextElement(attributeElement, NAME, attribute.getRuleAttribute().getName());
84                  renderer.renderBooleanElement(attributeElement, REQUIRED, attribute.getRequired(), false);
85              }
86          }
87      }
88      
89      private void exportDefaults(Element parent, RuleTemplateBo ruleTemplate) {
90          RuleBaseValues defaultRuleValues = KEWServiceLocator.getRuleService().findDefaultRuleByRuleTemplateId(ruleTemplate.getId());
91          if (defaultRuleValues != null) {
92              RuleDelegationBo defaultDelegationValues = getDefaultDelegationValues(defaultRuleValues);
93              Element defaultsElement = renderer.renderElement(parent, RULE_DEFAULTS);
94              if (defaultDelegationValues != null) {
95                  renderer.renderTextElement(defaultsElement, DELEGATION_TYPE, defaultDelegationValues.getDelegationType().getCode());
96              }
97              renderer.renderTextElement(defaultsElement, DESCRIPTION, defaultRuleValues.getDescription());
98              if (defaultRuleValues.getFromDateValue() != null) {
99              	renderer.renderDateElement(defaultsElement, FROM_DATE, defaultRuleValues.getFromDateValue());
100             }
101             if (defaultRuleValues.getToDateValue() != null) {
102             	renderer.renderDateElement(defaultsElement, TO_DATE, defaultRuleValues.getToDateValue());
103             }
104             renderer.renderBooleanElement(defaultsElement, FORCE_ACTION, defaultRuleValues.isForceAction(), false);
105             renderer.renderBooleanElement(defaultsElement, ACTIVE, defaultRuleValues.isActive(), true);
106             if (defaultDelegationValues == null) {
107                 RuleTemplateOptionBo defaultActionOption = ruleTemplate.getDefaultActionRequestValue();
108                 RuleTemplateOptionBo supportsComplete = ruleTemplate.getComplete();
109                 RuleTemplateOptionBo supportsApprove = ruleTemplate.getApprove();
110                 RuleTemplateOptionBo supportsAck = ruleTemplate.getAcknowledge();
111                 RuleTemplateOptionBo supportsFYI = ruleTemplate.getFyi();
112                 if (defaultActionOption != null) {
113                 	String defaultActionValue = (defaultActionOption == null ? null : defaultActionOption.getValue());
114                 	renderer.renderTextElement(defaultsElement, DEFAULT_ACTION_REQUESTED, defaultActionValue);
115                 }
116                 if (supportsComplete != null) {
117                 	String supportsCompleteValue = supportsComplete.getValue();
118                 	renderer.renderTextElement(defaultsElement, SUPPORTS_COMPLETE, supportsCompleteValue);
119                 }
120                 if (supportsApprove != null) {
121                 	String supportsApproveValue = supportsApprove.getValue();
122                 	renderer.renderTextElement(defaultsElement, SUPPORTS_APPROVE, supportsApproveValue);
123                 }
124                 if (supportsAck != null) {
125                 	String supportsAckValue = supportsAck.getValue();
126                 	renderer.renderTextElement(defaultsElement, SUPPORTS_ACKNOWLEDGE, supportsAckValue);
127                 }
128                 if (supportsFYI != null) {
129                 	String supportsFYIValue = supportsFYI.getValue();
130                 	renderer.renderTextElement(defaultsElement, SUPPORTS_FYI, supportsFYIValue);
131                 }
132             }
133         }
134     }
135     
136     private RuleDelegationBo getDefaultDelegationValues(RuleBaseValues defaultRuleValues) {
137         List ruleDelegations = KEWServiceLocator.getRuleDelegationService().findByDelegateRuleId(defaultRuleValues.getId());
138         if (ruleDelegations.size() > 1) {
139             LOG.warn("The rule defaults has more than one associated delegation defaults.");
140         }
141         return (ruleDelegations.isEmpty() ? null : (RuleDelegationBo)ruleDelegations.get(0));
142     }
143     
144 }