001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kew.xml.export;
017    
018    import org.jdom.Element;
019    import org.kuali.rice.core.api.impex.ExportDataSet;
020    import org.kuali.rice.core.api.util.xml.XmlRenderer;
021    import org.kuali.rice.core.framework.impex.xml.XmlExporter;
022    import org.kuali.rice.kew.export.KewExportDataSet;
023    import org.kuali.rice.kew.rule.RuleBaseValues;
024    import org.kuali.rice.kew.rule.RuleDelegationBo;
025    import org.kuali.rice.kew.rule.RuleTemplateOptionBo;
026    import org.kuali.rice.kew.rule.bo.RuleTemplateBo;
027    import org.kuali.rice.kew.rule.bo.RuleTemplateAttributeBo;
028    import org.kuali.rice.kew.service.KEWServiceLocator;
029    
030    import java.util.Iterator;
031    import java.util.List;
032    
033    import static org.kuali.rice.core.api.impex.xml.XmlConstants.*;
034    /**
035     * Exports {@link org.kuali.rice.kew.rule.bo.RuleTemplateBo}s to XML.
036     * 
037     * @see org.kuali.rice.kew.rule.bo.RuleTemplateBo
038     *
039     * @author Kuali Rice Team (rice.collab@kuali.org)
040     */
041    public class RuleTemplateXmlExporter implements XmlExporter {
042    
043        protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass());
044        
045        private XmlRenderer renderer = new XmlRenderer(RULE_TEMPLATE_NAMESPACE);
046        
047            @Override
048            public boolean supportPrettyPrint() {
049                    return true;
050            }
051    
052        public Element export(ExportDataSet exportDataSet) {
053            KewExportDataSet dataSet = KewExportDataSet.fromExportDataSet(exportDataSet);
054            if (!dataSet.getRuleTemplates().isEmpty()) {
055                Element rootElement = renderer.renderElement(null, RULE_TEMPLATES);
056                rootElement.setAttribute(SCHEMA_LOCATION_ATTR, RULE_TEMPLATE_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
057                for (Iterator iterator = dataSet.getRuleTemplates().iterator(); iterator.hasNext();) {
058                    RuleTemplateBo template = (RuleTemplateBo)iterator.next();
059                    exportRuleTemplate(rootElement, template);
060                }
061                return rootElement;
062            }
063            return null;
064        }
065        
066        private void exportRuleTemplate(Element parent, RuleTemplateBo ruleTemplate) {
067            Element templateElement = renderer.renderElement(parent, RULE_TEMPLATE);
068            renderer.renderTextElement(templateElement, NAME, ruleTemplate.getName());
069            renderer.renderTextElement(templateElement, DESCRIPTION, ruleTemplate.getDescription());
070            if (ruleTemplate.getDelegationTemplate() != null) {
071                renderer.renderTextElement(templateElement, DELEGATION_TEMPLATE, ruleTemplate.getDelegationTemplate().getName());
072            }
073            exportAttributes(templateElement, ruleTemplate.getActiveRuleTemplateAttributes());
074            exportDefaults(templateElement, ruleTemplate);
075        }
076    
077        private void exportAttributes(Element parent, List ruleTemplateAttributes) {
078            if (!ruleTemplateAttributes.isEmpty()) {
079                Element attributesElement = renderer.renderElement(parent, ATTRIBUTES);
080                for (Iterator iterator = ruleTemplateAttributes.iterator(); iterator.hasNext();) {
081                    RuleTemplateAttributeBo attribute = (RuleTemplateAttributeBo) iterator.next();
082                    Element attributeElement = renderer.renderElement(attributesElement, ATTRIBUTE);
083                    renderer.renderTextElement(attributeElement, NAME, attribute.getRuleAttribute().getName());
084                    renderer.renderBooleanElement(attributeElement, REQUIRED, attribute.getRequired(), false);
085                }
086            }
087        }
088        
089        private void exportDefaults(Element parent, RuleTemplateBo ruleTemplate) {
090            RuleBaseValues defaultRuleValues = KEWServiceLocator.getRuleService().findDefaultRuleByRuleTemplateId(ruleTemplate.getId());
091            if (defaultRuleValues != null) {
092                RuleDelegationBo defaultDelegationValues = getDefaultDelegationValues(defaultRuleValues);
093                Element defaultsElement = renderer.renderElement(parent, RULE_DEFAULTS);
094                if (defaultDelegationValues != null) {
095                    renderer.renderTextElement(defaultsElement, DELEGATION_TYPE, defaultDelegationValues.getDelegationType().getCode());
096                }
097                renderer.renderTextElement(defaultsElement, DESCRIPTION, defaultRuleValues.getDescription());
098                if (defaultRuleValues.getFromDateValue() != null) {
099                    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    }