Coverage Report - org.kuali.rice.kew.xml.export.RuleXmlExporter
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleXmlExporter
0%
0/108
0%
0/56
4.333
 
 1  
 /*
 2  
  * Copyright 2005-2007 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.Collection;
 20  
 import java.util.HashSet;
 21  
 import java.util.Iterator;
 22  
 import java.util.List;
 23  
 import java.util.Set;
 24  
 
 25  
 import org.apache.commons.collections.CollectionUtils;
 26  
 import org.apache.commons.lang.StringUtils;
 27  
 import org.jdom.Element;
 28  
 import org.jdom.Namespace;
 29  
 import org.kuali.rice.core.exception.RiceRuntimeException;
 30  
 import org.kuali.rice.kew.export.ExportDataSet;
 31  
 import org.kuali.rice.kew.rule.RuleBaseValues;
 32  
 import org.kuali.rice.kew.rule.RuleExtension;
 33  
 import org.kuali.rice.kew.rule.RuleExtensionValue;
 34  
 import org.kuali.rice.kew.rule.RuleResponsibility;
 35  
 import org.kuali.rice.kew.rule.bo.RuleTemplateAttribute;
 36  
 import org.kuali.rice.kew.rule.web.WebRuleUtils;
 37  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 38  
 import org.kuali.rice.kew.xml.XmlConstants;
 39  
 import org.kuali.rice.kew.xml.XmlRenderer;
 40  
 import org.kuali.rice.kim.bo.Group;
 41  
 import org.kuali.rice.kim.bo.entity.KimPrincipal;
 42  
 import org.kuali.rice.kew.rule.RuleDelegation;
 43  
 
 44  
 
 45  
 /**
 46  
  * Exports rules to XML.
 47  
  *
 48  
  * @see RuleBaseValues
 49  
  *
 50  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 51  
  */
 52  
 public class RuleXmlExporter implements XmlExporter, XmlConstants {
 53  
 
 54  0
     protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass());
 55  
 
 56  
     private XmlRenderer renderer;
 57  
     
 58  0
     public RuleXmlExporter(Namespace namespace) {
 59  0
             this.renderer = new XmlRenderer(namespace);
 60  0
     }
 61  
     
 62  
     public Element export(ExportDataSet dataSet) {
 63  0
         if (!dataSet.getRules().isEmpty()) {
 64  0
             Element rootElement = renderer.renderElement(null, RULES);
 65  0
             rootElement.setAttribute(SCHEMA_LOCATION_ATTR, RULE_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
 66  0
             for (Iterator iterator = dataSet.getRules().iterator(); iterator.hasNext();) {
 67  0
                     RuleBaseValues rule = (RuleBaseValues) iterator.next();
 68  0
                     exportRule(rootElement, rule);
 69  
                     //turn below on if need export delegates in rule exportation
 70  
 //                    if(rule.getDelegateRule().booleanValue()){        
 71  
 //                            exportRuleDelegations(rootElement, rule);
 72  
 //                    }
 73  0
             }
 74  0
             return rootElement;
 75  
         }
 76  0
         return null;
 77  
     }
 78  
 
 79  
     public void exportRule(Element parent, RuleBaseValues rule) {
 80  0
             Element ruleElement = renderer.renderElement(parent, RULE);
 81  0
         if (rule.getName() != null) {
 82  0
             renderer.renderTextElement(ruleElement, NAME, rule.getName());
 83  
         }
 84  0
         renderer.renderTextElement(ruleElement, DOCUMENT_TYPE, rule.getDocTypeName());
 85  0
         if (rule.getRuleTemplateName() != null) {
 86  0
             renderer.renderTextElement(ruleElement, RULE_TEMPLATE, rule.getRuleTemplateName());
 87  
         }
 88  0
         renderer.renderTextElement(ruleElement, DESCRIPTION, rule.getDescription());
 89  0
         if(rule.getFromDateString() != null){
 90  0
             renderer.renderTextElement(ruleElement, FROM_DATE, rule.getFromDateString());
 91  
         }
 92  0
         if(rule.getToDateString() != null){
 93  0
             renderer.renderTextElement(ruleElement, TO_DATE, rule.getToDateString());
 94  
         }
 95  0
         if (rule.getRuleExpressionDef() != null) {
 96  0
             Element expressionElement = renderer.renderTextElement(ruleElement, EXPRESSION, rule.getRuleExpressionDef().getExpression());
 97  0
             if (rule.getRuleExpressionDef().getType() != null) {
 98  0
                 expressionElement.setAttribute("type", rule.getRuleExpressionDef().getType());
 99  
             }
 100  
         }
 101  0
         renderer.renderBooleanElement(ruleElement, FORCE_ACTION, rule.getForceAction(), false);
 102  
         
 103  0
         if (CollectionUtils.isEmpty(rule.getRuleExtensions()) && 
 104  
                         /* field values is not empty */
 105  
                         !(rule.getFieldValues() == null || rule.getFieldValues().size() == 0)) {
 106  
                 // the rule is in the wrong state (as far as we are concerned).
 107  
                 // translate it
 108  0
                 WebRuleUtils.translateResponsibilitiesForSave(rule);
 109  0
                 WebRuleUtils.translateFieldValuesForSave(rule);
 110  
                 
 111  
                 // do our exports
 112  0
                     exportRuleExtensions(ruleElement, rule.getRuleExtensions());
 113  
                 
 114  
                 // translate it back
 115  0
                 WebRuleUtils.populateRuleMaintenanceFields(rule);
 116  
         } else { 
 117  0
                 exportRuleExtensions(ruleElement, rule.getRuleExtensions());
 118  
         }
 119  
         
 120  
         // put responsibilities in a single collection 
 121  0
         Set<RuleResponsibility> responsibilities = new HashSet<RuleResponsibility>();
 122  0
         responsibilities.addAll(rule.getResponsibilities());
 123  0
         responsibilities.addAll(rule.getPersonResponsibilities());
 124  0
         responsibilities.addAll(rule.getGroupResponsibilities());
 125  0
         responsibilities.addAll(rule.getRoleResponsibilities());
 126  
         
 127  0
         exportResponsibilities(ruleElement, responsibilities);
 128  0
     }
 129  
 
 130  
     private void exportRuleExtensions(Element parent, List ruleExtensions) {
 131  0
         if (!ruleExtensions.isEmpty()) {
 132  0
             Element extsElement = renderer.renderElement(parent, RULE_EXTENSIONS);
 133  0
             for (Iterator iterator = ruleExtensions.iterator(); iterator.hasNext();) {
 134  0
                 RuleExtension extension = (RuleExtension) iterator.next();
 135  0
                 Element extElement = renderer.renderElement(extsElement, RULE_EXTENSION);
 136  0
                 RuleTemplateAttribute attribute = extension.getRuleTemplateAttribute();
 137  0
                 renderer.renderTextElement(extElement, ATTRIBUTE, attribute.getRuleAttribute().getName());
 138  0
                 renderer.renderTextElement(extElement, RULE_TEMPLATE, attribute.getRuleTemplate().getName());
 139  0
                 exportRuleExtensionValues(extElement, extension.getExtensionValues());
 140  0
             }
 141  
         }
 142  0
     }
 143  
 
 144  
     private void exportRuleExtensionValues(Element parent, List extensionValues) {
 145  0
         if (!extensionValues.isEmpty()) {
 146  0
             Element extValuesElement = renderer.renderElement(parent, RULE_EXTENSION_VALUES);
 147  0
             for (Iterator iterator = extensionValues.iterator(); iterator.hasNext();) {
 148  0
                 RuleExtensionValue extensionValue = (RuleExtensionValue) iterator.next();
 149  0
                 Element extValueElement = renderer.renderElement(extValuesElement, RULE_EXTENSION_VALUE);
 150  0
                 renderer.renderTextElement(extValueElement, KEY, extensionValue.getKey());
 151  0
                 renderer.renderTextElement(extValueElement, VALUE, extensionValue.getValue());
 152  0
             }
 153  
         }
 154  0
     }
 155  
 
 156  
     private void exportResponsibilities(Element parent, Collection<? extends RuleResponsibility> responsibilities) {
 157  0
         if (responsibilities != null && !responsibilities.isEmpty()) {
 158  0
             Element responsibilitiesElement = renderer.renderElement(parent, RESPONSIBILITIES);
 159  0
             for (RuleResponsibility ruleResponsibility : responsibilities) {
 160  0
                 Element respElement = renderer.renderElement(responsibilitiesElement, RESPONSIBILITY);
 161  0
                 renderer.renderTextElement(respElement, RESPONSIBILITY_ID, "" + ruleResponsibility.getResponsibilityId());
 162  0
                 if (ruleResponsibility.isUsingWorkflowUser()) {
 163  0
                                     renderer.renderTextElement(respElement, PRINCIPAL_NAME, ruleResponsibility.getPrincipal().getPrincipalName());
 164  0
                                 } else if (ruleResponsibility.isUsingGroup()) {
 165  0
                                         Group group = ruleResponsibility.getGroup();
 166  0
                                     Element groupElement = renderer.renderTextElement(respElement, GROUP_NAME, group.getGroupName());
 167  0
                                     groupElement.setAttribute(NAMESPACE, group.getNamespaceCode());
 168  0
                                 } else if (ruleResponsibility.isUsingRole()) {
 169  0
                                     renderer.renderTextElement(respElement, ROLE, ruleResponsibility.getRuleResponsibilityName());
 170  0
                                     renderer.renderTextElement(respElement, APPROVE_POLICY, ruleResponsibility.getApprovePolicy());
 171  
                                 }
 172  0
                 if (!StringUtils.isBlank(ruleResponsibility.getActionRequestedCd())) {
 173  0
                         renderer.renderTextElement(respElement, ACTION_REQUESTED, ruleResponsibility.getActionRequestedCd());
 174  
                 }
 175  0
                 if (ruleResponsibility.getPriority() != null) {
 176  0
                         renderer.renderTextElement(respElement, PRIORITY, ruleResponsibility.getPriority().toString());
 177  
                 }
 178  0
             }
 179  
         }
 180  0
     }
 181  
     
 182  
     //below are for exporting rule delegations in rule exportation
 183  
     private void exportRuleDelegations(Element rootElement, RuleBaseValues rule){
 184  0
                 List<RuleDelegation> ruleDelegationDefaults = KEWServiceLocator.getRuleDelegationService().findByDelegateRuleId(rule.getRuleBaseValuesId());
 185  0
                 for(RuleDelegation dele : ruleDelegationDefaults){
 186  0
                         if (LOG.isInfoEnabled()) {
 187  0
                                 LOG.info("*******delegates********\t"  +  dele.getRuleDelegationId()) ;
 188  
                         }
 189  0
                         exportRuleDelegation(rootElement, dele);        
 190  
                 }
 191  0
     }
 192  
     
 193  
     private void exportRuleDelegation(Element parent, RuleDelegation ruleDelegation) {
 194  0
             Element ruleDelegationElement = renderer.renderElement(parent, RULE_DELEGATION);
 195  0
             exportRuleDelegationParentResponsibility(ruleDelegationElement, ruleDelegation);
 196  0
             renderer.renderTextElement(ruleDelegationElement, DELEGATION_TYPE, ruleDelegation.getDelegationType());
 197  0
             exportRule(ruleDelegationElement, ruleDelegation.getDelegationRuleBaseValues());
 198  0
     }
 199  
     
 200  
     private void exportRuleDelegationParentResponsibility(Element parent, RuleDelegation delegation) {
 201  0
         Element parentResponsibilityElement = renderer.renderElement(parent, PARENT_RESPONSIBILITY);
 202  0
         RuleResponsibility ruleResponsibility = KEWServiceLocator.getRuleService().findRuleResponsibility(delegation.getResponsibilityId());
 203  0
         renderer.renderTextElement(parentResponsibilityElement, PARENT_RULE_NAME, ruleResponsibility.getRuleBaseValues().getName());
 204  0
         if (ruleResponsibility.isUsingWorkflowUser()) {
 205  0
                 KimPrincipal principal = ruleResponsibility.getPrincipal();
 206  0
                 renderer.renderTextElement(parentResponsibilityElement, PRINCIPAL_NAME, principal.getPrincipalName());
 207  0
         } else if (ruleResponsibility.isUsingGroup()) {
 208  0
                 Group group = ruleResponsibility.getGroup();
 209  0
                 Element groupElement = renderer.renderElement(parentResponsibilityElement, GROUP_NAME);
 210  0
                 groupElement.setText(group.getGroupName());
 211  0
                 groupElement.setAttribute(NAMESPACE, group.getNamespaceCode());
 212  0
         } else if (ruleResponsibility.isUsingRole()) {
 213  0
                 renderer.renderTextElement(parentResponsibilityElement, ROLE, ruleResponsibility.getRuleResponsibilityName());
 214  
         } else {
 215  0
                 throw new RiceRuntimeException("Encountered a rule responsibility when exporting with an invalid type of '" + ruleResponsibility.getRuleResponsibilityType());
 216  
         }
 217  0
     }
 218  
 
 219  
 }