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