001    /**
002     * Copyright 2005-2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kew.xml.export;
017    
018    import org.jdom.Element;
019    import org.kuali.rice.core.api.exception.RiceRuntimeException;
020    import org.kuali.rice.core.api.impex.ExportDataSet;
021    import org.kuali.rice.core.api.util.xml.XmlRenderer;
022    import org.kuali.rice.core.framework.impex.xml.XmlExporter;
023    import org.kuali.rice.kew.export.KewExportDataSet;
024    import org.kuali.rice.kew.rule.RuleBaseValues;
025    import org.kuali.rice.kew.rule.RuleDelegationBo;
026    import org.kuali.rice.kew.rule.RuleResponsibilityBo;
027    import org.kuali.rice.kew.service.KEWServiceLocator;
028    import org.kuali.rice.kim.api.group.Group;
029    import org.kuali.rice.kim.api.identity.principal.Principal;
030    
031    import java.util.Iterator;
032    
033    import static org.kuali.rice.core.api.impex.xml.XmlConstants.*;
034    
035    /**
036     * Exports rules to XML.
037     *
038     * @see RuleBaseValues
039     *
040     * @author Kuali Rice Team (rice.collab@kuali.org)
041     */
042    public class RuleDelegationXmlExporter implements XmlExporter {
043    
044        protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass());
045    
046        private XmlRenderer renderer = new XmlRenderer(RULE_NAMESPACE);
047        private RuleXmlExporter ruleExporter = new RuleXmlExporter(RULE_NAMESPACE);
048    
049            @Override
050            public boolean supportPrettyPrint() {
051                    return true;
052            }
053    
054        public Element export(ExportDataSet exportDataSet) {
055            KewExportDataSet dataSet = KewExportDataSet.fromExportDataSet(exportDataSet);
056            if (!dataSet.getRuleDelegations().isEmpty()) {
057                Element rootElement = renderer.renderElement(null, RULE_DELEGATIONS);
058                rootElement.setAttribute(SCHEMA_LOCATION_ATTR, RULE_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
059                for (Iterator iterator = dataSet.getRuleDelegations().iterator(); iterator.hasNext();) {
060                    RuleDelegationBo ruleDelegation = (RuleDelegationBo) iterator.next();
061                    exportRuleDelegation(rootElement, ruleDelegation);
062                }
063                return rootElement;
064            }
065            return null;
066        }
067    
068        private void exportRuleDelegation(Element parent, RuleDelegationBo ruleDelegation) {
069            Element ruleDelegationElement = renderer.renderElement(parent, RULE_DELEGATION);
070            exportParentResponsibility(ruleDelegationElement, ruleDelegation);
071            renderer.renderTextElement(ruleDelegationElement, DELEGATION_TYPE, ruleDelegation.getDelegationType().getCode());
072            ruleExporter.exportRule(ruleDelegationElement, ruleDelegation.getDelegationRule());
073        }
074        
075        private void exportParentResponsibility(Element parent, RuleDelegationBo delegation) {
076            Element parentResponsibilityElement = renderer.renderElement(parent, PARENT_RESPONSIBILITY);
077            RuleResponsibilityBo ruleResponsibility = KEWServiceLocator.getRuleService().findRuleResponsibility(delegation.getResponsibilityId());
078            renderer.renderTextElement(parentResponsibilityElement, PARENT_RULE_NAME, ruleResponsibility.getRuleBaseValues().getName());
079            if (ruleResponsibility.isUsingPrincipal()) {
080                    Principal principal = ruleResponsibility.getPrincipal();
081                    renderer.renderTextElement(parentResponsibilityElement, PRINCIPAL_NAME, principal.getPrincipalName());
082            } else if (ruleResponsibility.isUsingGroup()) {
083                    Group group = ruleResponsibility.getGroup();
084                    Element groupElement = renderer.renderElement(parentResponsibilityElement, GROUP_NAME);
085                    groupElement.setText(group.getName());
086                    groupElement.setAttribute(NAMESPACE, group.getNamespaceCode());
087            } else if (ruleResponsibility.isUsingRole()) {
088                    renderer.renderTextElement(parentResponsibilityElement, ROLE, ruleResponsibility.getRuleResponsibilityName());
089            } else {
090                    throw new RiceRuntimeException("Encountered a rule responsibility when exporting with an invalid type of '" + ruleResponsibility.getRuleResponsibilityType());
091            }
092        }
093    
094    }