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-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  0
 public class RuleTemplateXmlExporter implements XmlExporter {
 42  
 
 43  0
     protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass());
 44  
     
 45  0
     private XmlRenderer renderer = new XmlRenderer(RULE_TEMPLATE_NAMESPACE);
 46  
     
 47  
         @Override
 48  
         public boolean supportPrettyPrint() {
 49  0
                 return true;
 50  
         }
 51  
 
 52  
     public Element export(ExportDataSet exportDataSet) {
 53  0
             KewExportDataSet dataSet = KewExportDataSet.fromExportDataSet(exportDataSet);
 54  0
         if (!dataSet.getRuleTemplates().isEmpty()) {
 55  0
             Element rootElement = renderer.renderElement(null, RULE_TEMPLATES);
 56  0
             rootElement.setAttribute(SCHEMA_LOCATION_ATTR, RULE_TEMPLATE_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
 57  0
             for (Iterator iterator = dataSet.getRuleTemplates().iterator(); iterator.hasNext();) {
 58  0
                 RuleTemplateBo template = (RuleTemplateBo)iterator.next();
 59  0
                 exportRuleTemplate(rootElement, template);
 60  0
             }
 61  0
             return rootElement;
 62  
         }
 63  0
         return null;
 64  
     }
 65  
     
 66  
     private void exportRuleTemplate(Element parent, RuleTemplateBo ruleTemplate) {
 67  0
         Element templateElement = renderer.renderElement(parent, RULE_TEMPLATE);
 68  0
         renderer.renderTextElement(templateElement, NAME, ruleTemplate.getName());
 69  0
         renderer.renderTextElement(templateElement, DESCRIPTION, ruleTemplate.getDescription());
 70  0
         if (ruleTemplate.getDelegationTemplate() != null) {
 71  0
             renderer.renderTextElement(templateElement, DELEGATION_TEMPLATE, ruleTemplate.getDelegationTemplate().getName());
 72  
         }
 73  0
         exportAttributes(templateElement, ruleTemplate.getActiveRuleTemplateAttributes());
 74  0
         exportDefaults(templateElement, ruleTemplate);
 75  0
     }
 76  
 
 77  
     private void exportAttributes(Element parent, List ruleTemplateAttributes) {
 78  0
         if (!ruleTemplateAttributes.isEmpty()) {
 79  0
             Element attributesElement = renderer.renderElement(parent, ATTRIBUTES);
 80  0
             for (Iterator iterator = ruleTemplateAttributes.iterator(); iterator.hasNext();) {
 81  0
                 RuleTemplateAttributeBo attribute = (RuleTemplateAttributeBo) iterator.next();
 82  0
                 Element attributeElement = renderer.renderElement(attributesElement, ATTRIBUTE);
 83  0
                 renderer.renderTextElement(attributeElement, NAME, attribute.getRuleAttribute().getName());
 84  0
                 renderer.renderBooleanElement(attributeElement, REQUIRED, attribute.getRequired(), false);
 85  0
             }
 86  
         }
 87  0
     }
 88  
     
 89  
     private void exportDefaults(Element parent, RuleTemplateBo ruleTemplate) {
 90  0
         RuleBaseValues defaultRuleValues = KEWServiceLocator.getRuleService().findDefaultRuleByRuleTemplateId(ruleTemplate.getId());
 91  0
         if (defaultRuleValues != null) {
 92  0
             RuleDelegationBo defaultDelegationValues = getDefaultDelegationValues(defaultRuleValues);
 93  0
             Element defaultsElement = renderer.renderElement(parent, RULE_DEFAULTS);
 94  0
             if (defaultDelegationValues != null) {
 95  0
                 renderer.renderTextElement(defaultsElement, DELEGATION_TYPE, defaultDelegationValues.getDelegationType().getCode());
 96  
             }
 97  0
             renderer.renderTextElement(defaultsElement, DESCRIPTION, defaultRuleValues.getDescription());
 98  0
             if (defaultRuleValues.getFromDateValue() != null) {
 99  0
                     renderer.renderDateElement(defaultsElement, FROM_DATE, defaultRuleValues.getFromDateValue());
 100  
             }
 101  0
             if (defaultRuleValues.getToDateValue() != null) {
 102  0
                     renderer.renderDateElement(defaultsElement, TO_DATE, defaultRuleValues.getToDateValue());
 103  
             }
 104  0
             renderer.renderBooleanElement(defaultsElement, FORCE_ACTION, defaultRuleValues.isForceAction(), false);
 105  0
             renderer.renderBooleanElement(defaultsElement, ACTIVE, defaultRuleValues.isActive(), true);
 106  0
             if (defaultDelegationValues == null) {
 107  0
                 RuleTemplateOptionBo defaultActionOption = ruleTemplate.getDefaultActionRequestValue();
 108  0
                 RuleTemplateOptionBo supportsComplete = ruleTemplate.getComplete();
 109  0
                 RuleTemplateOptionBo supportsApprove = ruleTemplate.getApprove();
 110  0
                 RuleTemplateOptionBo supportsAck = ruleTemplate.getAcknowledge();
 111  0
                 RuleTemplateOptionBo supportsFYI = ruleTemplate.getFyi();
 112  0
                 if (defaultActionOption != null) {
 113  0
                         String defaultActionValue = (defaultActionOption == null ? null : defaultActionOption.getValue());
 114  0
                         renderer.renderTextElement(defaultsElement, DEFAULT_ACTION_REQUESTED, defaultActionValue);
 115  
                 }
 116  0
                 if (supportsComplete != null) {
 117  0
                         String supportsCompleteValue = supportsComplete.getValue();
 118  0
                         renderer.renderTextElement(defaultsElement, SUPPORTS_COMPLETE, supportsCompleteValue);
 119  
                 }
 120  0
                 if (supportsApprove != null) {
 121  0
                         String supportsApproveValue = supportsApprove.getValue();
 122  0
                         renderer.renderTextElement(defaultsElement, SUPPORTS_APPROVE, supportsApproveValue);
 123  
                 }
 124  0
                 if (supportsAck != null) {
 125  0
                         String supportsAckValue = supportsAck.getValue();
 126  0
                         renderer.renderTextElement(defaultsElement, SUPPORTS_ACKNOWLEDGE, supportsAckValue);
 127  
                 }
 128  0
                 if (supportsFYI != null) {
 129  0
                         String supportsFYIValue = supportsFYI.getValue();
 130  0
                         renderer.renderTextElement(defaultsElement, SUPPORTS_FYI, supportsFYIValue);
 131  
                 }
 132  
             }
 133  
         }
 134  0
     }
 135  
     
 136  
     private RuleDelegationBo getDefaultDelegationValues(RuleBaseValues defaultRuleValues) {
 137  0
         List ruleDelegations = KEWServiceLocator.getRuleDelegationService().findByDelegateRuleId(defaultRuleValues.getId());
 138  0
         if (ruleDelegations.size() > 1) {
 139  0
             LOG.warn("The rule defaults has more than one associated delegation defaults.");
 140  
         }
 141  0
         return (ruleDelegations.isEmpty() ? null : (RuleDelegationBo)ruleDelegations.get(0));
 142  
     }
 143  
     
 144  
 }