View Javadoc

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