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