Coverage Report - org.kuali.rice.kew.xml.export.RuleTemplateXmlExporter
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleTemplateXmlExporter
0%
0/68
0%
0/36
4.8
 
 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 java.util.Iterator;
 20  
 import java.util.List;
 21  
 
 22  
 import org.jdom.Element;
 23  
 import org.kuali.rice.kew.export.ExportDataSet;
 24  
 import org.kuali.rice.kew.rule.RuleBaseValues;
 25  
 import org.kuali.rice.kew.rule.RuleDelegation;
 26  
 import org.kuali.rice.kew.rule.RuleTemplateOption;
 27  
 import org.kuali.rice.kew.rule.bo.RuleTemplate;
 28  
 import org.kuali.rice.kew.rule.bo.RuleTemplateAttribute;
 29  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 30  
 import org.kuali.rice.kew.util.KEWConstants;
 31  
 import org.kuali.rice.kew.xml.XmlConstants;
 32  
 import org.kuali.rice.kew.xml.XmlRenderer;
 33  
 
 34  
 
 35  
 /**
 36  
  * Exports {@link RuleTemplate}s to XML.
 37  
  * 
 38  
  * @see RuleTemplate
 39  
  *
 40  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 41  
  */
 42  0
 public class RuleTemplateXmlExporter implements XmlExporter, XmlConstants {
 43  
 
 44  0
     protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass());
 45  
     
 46  0
     private XmlRenderer renderer = new XmlRenderer(RULE_TEMPLATE_NAMESPACE);
 47  
     
 48  
     public Element export(ExportDataSet dataSet) {
 49  0
         if (!dataSet.getRuleTemplates().isEmpty()) {
 50  0
             Element rootElement = renderer.renderElement(null, RULE_TEMPLATES);
 51  0
             rootElement.setAttribute(SCHEMA_LOCATION_ATTR, RULE_TEMPLATE_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
 52  0
             for (Iterator iterator = dataSet.getRuleTemplates().iterator(); iterator.hasNext();) {
 53  0
                 RuleTemplate template = (RuleTemplate)iterator.next();
 54  0
                 exportRuleTemplate(rootElement, template);
 55  0
             }
 56  0
             return rootElement;
 57  
         }
 58  0
         return null;
 59  
     }
 60  
     
 61  
     private void exportRuleTemplate(Element parent, RuleTemplate ruleTemplate) {
 62  0
         Element templateElement = renderer.renderElement(parent, RULE_TEMPLATE);
 63  0
         renderer.renderTextElement(templateElement, NAME, ruleTemplate.getName());
 64  0
         renderer.renderTextElement(templateElement, DESCRIPTION, ruleTemplate.getDescription());
 65  0
         if (ruleTemplate.getDelegationTemplate() != null) {
 66  0
             renderer.renderTextElement(templateElement, DELEGATION_TEMPLATE, ruleTemplate.getDelegationTemplate().getName());
 67  
         }
 68  0
         exportAttributes(templateElement, ruleTemplate.getActiveRuleTemplateAttributes());
 69  0
         exportDefaults(templateElement, ruleTemplate);
 70  0
     }
 71  
 
 72  
     private void exportAttributes(Element parent, List ruleTemplateAttributes) {
 73  0
         if (!ruleTemplateAttributes.isEmpty()) {
 74  0
             Element attributesElement = renderer.renderElement(parent, ATTRIBUTES);
 75  0
             for (Iterator iterator = ruleTemplateAttributes.iterator(); iterator.hasNext();) {
 76  0
                 RuleTemplateAttribute attribute = (RuleTemplateAttribute) iterator.next();
 77  0
                 Element attributeElement = renderer.renderElement(attributesElement, ATTRIBUTE);
 78  0
                 renderer.renderTextElement(attributeElement, NAME, attribute.getRuleAttribute().getName());
 79  0
                 renderer.renderBooleanElement(attributeElement, REQUIRED, attribute.getRequired(), false);
 80  0
             }
 81  
         }
 82  0
     }
 83  
     
 84  
     private void exportDefaults(Element parent, RuleTemplate ruleTemplate) {
 85  0
         RuleBaseValues defaultRuleValues = KEWServiceLocator.getRuleService().findDefaultRuleByRuleTemplateId(ruleTemplate.getRuleTemplateId());
 86  0
         if (defaultRuleValues != null) {
 87  0
             RuleDelegation defaultDelegationValues = getDefaultDelegationValues(defaultRuleValues);
 88  0
             Element defaultsElement = renderer.renderElement(parent, RULE_DEFAULTS);
 89  0
             if (defaultDelegationValues != null) {
 90  0
                 renderer.renderTextElement(defaultsElement, DELEGATION_TYPE, defaultDelegationValues.getDelegationType());
 91  
             }
 92  0
             renderer.renderTextElement(defaultsElement, DESCRIPTION, defaultRuleValues.getDescription());
 93  0
             if (defaultRuleValues.getFromDate() != null) {
 94  0
                     renderer.renderDateElement(defaultsElement, FROM_DATE, defaultRuleValues.getFromDate());
 95  
             }
 96  0
             if (defaultRuleValues.getToDate() != null) {
 97  0
                     renderer.renderDateElement(defaultsElement, TO_DATE, defaultRuleValues.getToDate());
 98  
             }
 99  0
             renderer.renderBooleanElement(defaultsElement, FORCE_ACTION, defaultRuleValues.getForceAction(), false);
 100  0
             renderer.renderBooleanElement(defaultsElement, ACTIVE, defaultRuleValues.getActiveInd(), true);
 101  0
             if (defaultDelegationValues == null) {
 102  0
                 RuleTemplateOption defaultActionOption = ruleTemplate.getDefaultActionRequestValue();
 103  0
                 RuleTemplateOption supportsComplete = ruleTemplate.getComplete();
 104  0
                 RuleTemplateOption supportsApprove = ruleTemplate.getApprove();
 105  0
                 RuleTemplateOption supportsAck = ruleTemplate.getAcknowledge();
 106  0
                 RuleTemplateOption supportsFYI = ruleTemplate.getFyi();
 107  0
                 if (defaultActionOption != null) {
 108  0
                         String defaultActionValue = (defaultActionOption == null ? null : defaultActionOption.getValue());
 109  0
                         renderer.renderTextElement(defaultsElement, DEFAULT_ACTION_REQUESTED, defaultActionValue);
 110  
                 }
 111  0
                 if (supportsComplete != null) {
 112  0
                         String supportsCompleteValue = supportsComplete.getValue();
 113  0
                         renderer.renderTextElement(defaultsElement, SUPPORTS_COMPLETE, supportsCompleteValue);
 114  
                 }
 115  0
                 if (supportsApprove != null) {
 116  0
                         String supportsApproveValue = supportsApprove.getValue();
 117  0
                         renderer.renderTextElement(defaultsElement, SUPPORTS_APPROVE, supportsApproveValue);
 118  
                 }
 119  0
                 if (supportsAck != null) {
 120  0
                         String supportsAckValue = supportsAck.getValue();
 121  0
                         renderer.renderTextElement(defaultsElement, SUPPORTS_ACKNOWLEDGE, supportsAckValue);
 122  
                 }
 123  0
                 if (supportsFYI != null) {
 124  0
                         String supportsFYIValue = supportsFYI.getValue();
 125  0
                         renderer.renderTextElement(defaultsElement, SUPPORTS_FYI, supportsFYIValue);
 126  
                 }
 127  
             }
 128  
         }
 129  0
     }
 130  
     
 131  
     private RuleDelegation getDefaultDelegationValues(RuleBaseValues defaultRuleValues) {
 132  0
         List ruleDelegations = KEWServiceLocator.getRuleDelegationService().findByDelegateRuleId(defaultRuleValues.getRuleBaseValuesId());
 133  0
         if (ruleDelegations.size() > 1) {
 134  0
             LOG.warn("The rule defaults has more than one associated delegation defaults.");
 135  
         }
 136  0
         return (ruleDelegations.isEmpty() ? null : (RuleDelegation)ruleDelegations.get(0));
 137  
     }
 138  
     
 139  
 }