Coverage Report - org.kuali.rice.kew.xml.export.RuleDelegationXmlExporter
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleDelegationXmlExporter
0%
0/35
0%
0/10
2.75
 
 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.exception.RiceRuntimeException;
 20  
 import org.kuali.rice.core.api.impex.ExportDataSet;
 21  
 import org.kuali.rice.core.api.util.xml.XmlRenderer;
 22  
 import org.kuali.rice.core.framework.impex.xml.XmlExporter;
 23  
 import org.kuali.rice.kew.export.KewExportDataSet;
 24  
 import org.kuali.rice.kew.rule.RuleBaseValues;
 25  
 import org.kuali.rice.kew.rule.RuleDelegationBo;
 26  
 import org.kuali.rice.kew.rule.RuleResponsibilityBo;
 27  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 28  
 import org.kuali.rice.kim.api.group.Group;
 29  
 import org.kuali.rice.kim.api.identity.principal.Principal;
 30  
 
 31  
 import java.util.Iterator;
 32  
 
 33  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.*;
 34  
 
 35  
 /**
 36  
  * Exports rules to XML.
 37  
  *
 38  
  * @see RuleBaseValues
 39  
  *
 40  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 41  
  */
 42  0
 public class RuleDelegationXmlExporter implements XmlExporter {
 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_NAMESPACE);
 47  0
     private RuleXmlExporter ruleExporter = new RuleXmlExporter(RULE_NAMESPACE);
 48  
 
 49  
         @Override
 50  
         public boolean supportPrettyPrint() {
 51  0
                 return true;
 52  
         }
 53  
 
 54  
     public Element export(ExportDataSet exportDataSet) {
 55  0
             KewExportDataSet dataSet = KewExportDataSet.fromExportDataSet(exportDataSet);
 56  0
         if (!dataSet.getRuleDelegations().isEmpty()) {
 57  0
             Element rootElement = renderer.renderElement(null, RULE_DELEGATIONS);
 58  0
             rootElement.setAttribute(SCHEMA_LOCATION_ATTR, RULE_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
 59  0
             for (Iterator iterator = dataSet.getRuleDelegations().iterator(); iterator.hasNext();) {
 60  0
                 RuleDelegationBo ruleDelegation = (RuleDelegationBo) iterator.next();
 61  0
                 exportRuleDelegation(rootElement, ruleDelegation);
 62  0
             }
 63  0
             return rootElement;
 64  
         }
 65  0
         return null;
 66  
     }
 67  
 
 68  
     private void exportRuleDelegation(Element parent, RuleDelegationBo ruleDelegation) {
 69  0
             Element ruleDelegationElement = renderer.renderElement(parent, RULE_DELEGATION);
 70  0
             exportParentResponsibility(ruleDelegationElement, ruleDelegation);
 71  0
             renderer.renderTextElement(ruleDelegationElement, DELEGATION_TYPE, ruleDelegation.getDelegationType().getCode());
 72  0
             ruleExporter.exportRule(ruleDelegationElement, ruleDelegation.getDelegationRule());
 73  0
     }
 74  
     
 75  
     private void exportParentResponsibility(Element parent, RuleDelegationBo delegation) {
 76  0
         Element parentResponsibilityElement = renderer.renderElement(parent, PARENT_RESPONSIBILITY);
 77  0
         RuleResponsibilityBo ruleResponsibility = KEWServiceLocator.getRuleService().findRuleResponsibility(delegation.getResponsibilityId());
 78  0
         renderer.renderTextElement(parentResponsibilityElement, PARENT_RULE_NAME, ruleResponsibility.getRuleBaseValues().getName());
 79  0
         if (ruleResponsibility.isUsingPrincipal()) {
 80  0
                 Principal principal = ruleResponsibility.getPrincipal();
 81  0
                 renderer.renderTextElement(parentResponsibilityElement, PRINCIPAL_NAME, principal.getPrincipalName());
 82  0
         } else if (ruleResponsibility.isUsingGroup()) {
 83  0
                 Group group = ruleResponsibility.getGroup();
 84  0
                 Element groupElement = renderer.renderElement(parentResponsibilityElement, GROUP_NAME);
 85  0
                 groupElement.setText(group.getName());
 86  0
                 groupElement.setAttribute(NAMESPACE, group.getNamespaceCode());
 87  0
         } else if (ruleResponsibility.isUsingRole()) {
 88  0
                 renderer.renderTextElement(parentResponsibilityElement, ROLE, ruleResponsibility.getRuleResponsibilityName());
 89  
         } else {
 90  0
                 throw new RiceRuntimeException("Encountered a rule responsibility when exporting with an invalid type of '" + ruleResponsibility.getRuleResponsibilityType());
 91  
         }
 92  0
     }
 93  
 
 94  
 }