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