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