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-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kew.xml.export;
 17  
 
 18  
 import org.apache.commons.collections.CollectionUtils;
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.jdom.Element;
 21  
 import org.jdom.Namespace;
 22  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 23  
 import org.kuali.rice.core.api.impex.ExportDataSet;
 24  
 import org.kuali.rice.core.api.util.xml.XmlRenderer;
 25  
 import org.kuali.rice.core.framework.impex.xml.XmlExporter;
 26  
 import org.kuali.rice.kew.export.KewExportDataSet;
 27  
 import org.kuali.rice.kew.rule.RuleBaseValues;
 28  
 import org.kuali.rice.kew.rule.RuleDelegationBo;
 29  
 import org.kuali.rice.kew.rule.RuleExtensionBo;
 30  
 import org.kuali.rice.kew.rule.RuleExtensionValue;
 31  
 import org.kuali.rice.kew.rule.RuleResponsibilityBo;
 32  
 import org.kuali.rice.kew.rule.bo.RuleTemplateAttributeBo;
 33  
 import org.kuali.rice.kew.rule.web.WebRuleUtils;
 34  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 35  
 import org.kuali.rice.kim.api.group.Group;
 36  
 import org.kuali.rice.kim.api.identity.principal.Principal;
 37  
 
 38  
 import java.util.Collection;
 39  
 import java.util.HashSet;
 40  
 import java.util.Iterator;
 41  
 import java.util.List;
 42  
 import java.util.Set;
 43  
 
 44  
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.*;
 45  
 
 46  
 /**
 47  
  * Exports rules to XML.
 48  
  *
 49  
  * @see RuleBaseValues
 50  
  *
 51  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 52  
  */
 53  
 public class RuleXmlExporter implements XmlExporter {
 54  
 
 55  0
     protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass());
 56  
 
 57  
     private XmlRenderer renderer;
 58  
     
 59  0
     public RuleXmlExporter(Namespace namespace) {
 60  0
             this.renderer = new XmlRenderer(namespace);
 61  0
     }
 62  
     
 63  
         @Override
 64  
         public boolean supportPrettyPrint() {
 65  0
                 return true;
 66  
         }
 67  
 
 68  
     public Element export(ExportDataSet exportDataSet) {
 69  0
             KewExportDataSet dataSet = KewExportDataSet.fromExportDataSet(exportDataSet);
 70  0
         if (!dataSet.getRules().isEmpty()) {
 71  0
             Element rootElement = renderer.renderElement(null, RULES);
 72  0
             rootElement.setAttribute(SCHEMA_LOCATION_ATTR, RULE_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
 73  0
             for (Iterator iterator = dataSet.getRules().iterator(); iterator.hasNext();) {
 74  0
                     RuleBaseValues rule = (RuleBaseValues) iterator.next();
 75  0
                     exportRule(rootElement, rule);
 76  
                     //turn below on if need export delegates in rule exportation
 77  
 //                    if(rule.getDelegateRule().booleanValue()){        
 78  
 //                            exportRuleDelegations(rootElement, rule);
 79  
 //                    }
 80  0
             }
 81  0
             return rootElement;
 82  
         }
 83  0
         return null;
 84  
     }
 85  
 
 86  
     public void exportRule(Element parent, RuleBaseValues rule) {
 87  0
             Element ruleElement = renderer.renderElement(parent, RULE);
 88  0
         if (rule.getName() != null) {
 89  0
             renderer.renderTextElement(ruleElement, NAME, rule.getName());
 90  
         }
 91  0
         renderer.renderTextElement(ruleElement, DOCUMENT_TYPE, rule.getDocTypeName());
 92  0
         if (rule.getRuleTemplateName() != null) {
 93  0
             renderer.renderTextElement(ruleElement, RULE_TEMPLATE, rule.getRuleTemplateName());
 94  
         }
 95  0
         renderer.renderTextElement(ruleElement, DESCRIPTION, rule.getDescription());
 96  0
         if(rule.getFromDateString() != null){
 97  0
             renderer.renderTextElement(ruleElement, FROM_DATE, rule.getFromDateString());
 98  
         }
 99  0
         if(rule.getToDateString() != null){
 100  0
             renderer.renderTextElement(ruleElement, TO_DATE, rule.getToDateString());
 101  
         }
 102  0
         if (rule.getRuleExpressionDef() != null) {
 103  0
             Element expressionElement = renderer.renderTextElement(ruleElement, EXPRESSION, rule.getRuleExpressionDef().getExpression());
 104  0
             if (rule.getRuleExpressionDef().getType() != null) {
 105  0
                 expressionElement.setAttribute("type", rule.getRuleExpressionDef().getType());
 106  
             }
 107  
         }
 108  0
         renderer.renderBooleanElement(ruleElement, FORCE_ACTION, rule.isForceAction(), false);
 109  
         
 110  0
         if (CollectionUtils.isEmpty(rule.getRuleExtensions()) &&
 111  
                         /* field values is not empty */
 112  
                         !(rule.getFieldValues() == null || rule.getFieldValues().size() == 0)) {
 113  
                 // the rule is in the wrong state (as far as we are concerned).
 114  
                 // translate it
 115  0
                 WebRuleUtils.translateResponsibilitiesForSave(rule);
 116  0
                 WebRuleUtils.translateFieldValuesForSave(rule);
 117  
                 
 118  
                 // do our exports
 119  0
                     exportRuleExtensions(ruleElement, rule.getRuleExtensions());
 120  
                 
 121  
                 // translate it back
 122  0
                 WebRuleUtils.populateRuleMaintenanceFields(rule);
 123  
         } else { 
 124  0
                 exportRuleExtensions(ruleElement, rule.getRuleExtensions());
 125  
         }
 126  
         
 127  
         // put responsibilities in a single collection 
 128  0
         Set<RuleResponsibilityBo> responsibilities = new HashSet<RuleResponsibilityBo>();
 129  0
         responsibilities.addAll(rule.getRuleResponsibilities());
 130  0
         responsibilities.addAll(rule.getPersonResponsibilities());
 131  0
         responsibilities.addAll(rule.getGroupResponsibilities());
 132  0
         responsibilities.addAll(rule.getRoleResponsibilities());
 133  
         
 134  0
         exportResponsibilities(ruleElement, responsibilities);
 135  0
     }
 136  
 
 137  
     private void exportRuleExtensions(Element parent, List ruleExtensions) {
 138  0
         if (!ruleExtensions.isEmpty()) {
 139  0
             Element extsElement = renderer.renderElement(parent, RULE_EXTENSIONS);
 140  0
             for (Iterator iterator = ruleExtensions.iterator(); iterator.hasNext();) {
 141  0
                 RuleExtensionBo extension = (RuleExtensionBo) iterator.next();
 142  0
                 Element extElement = renderer.renderElement(extsElement, RULE_EXTENSION);
 143  0
                 RuleTemplateAttributeBo attribute = extension.getRuleTemplateAttribute();
 144  0
                 renderer.renderTextElement(extElement, ATTRIBUTE, attribute.getRuleAttribute().getName());
 145  0
                 renderer.renderTextElement(extElement, RULE_TEMPLATE, attribute.getRuleTemplate().getName());
 146  0
                 exportRuleExtensionValues(extElement, extension.getExtensionValues());
 147  0
             }
 148  
         }
 149  0
     }
 150  
 
 151  
     private void exportRuleExtensionValues(Element parent, List extensionValues) {
 152  0
         if (!extensionValues.isEmpty()) {
 153  0
             Element extValuesElement = renderer.renderElement(parent, RULE_EXTENSION_VALUES);
 154  0
             for (Iterator iterator = extensionValues.iterator(); iterator.hasNext();) {
 155  0
                 RuleExtensionValue extensionValue = (RuleExtensionValue) iterator.next();
 156  0
                 Element extValueElement = renderer.renderElement(extValuesElement, RULE_EXTENSION_VALUE);
 157  0
                 renderer.renderTextElement(extValueElement, KEY, extensionValue.getKey());
 158  0
                 renderer.renderTextElement(extValueElement, VALUE, extensionValue.getValue());
 159  0
             }
 160  
         }
 161  0
     }
 162  
 
 163  
     private void exportResponsibilities(Element parent, Collection<? extends RuleResponsibilityBo> responsibilities) {
 164  0
         if (responsibilities != null && !responsibilities.isEmpty()) {
 165  0
             Element responsibilitiesElement = renderer.renderElement(parent, RESPONSIBILITIES);
 166  0
             for (RuleResponsibilityBo ruleResponsibility : responsibilities) {
 167  0
                 Element respElement = renderer.renderElement(responsibilitiesElement, RESPONSIBILITY);
 168  0
                 renderer.renderTextElement(respElement, RESPONSIBILITY_ID, "" + ruleResponsibility.getResponsibilityId());
 169  0
                 if (ruleResponsibility.isUsingPrincipal()) {
 170  0
                                     renderer.renderTextElement(respElement, PRINCIPAL_NAME, ruleResponsibility.getPrincipal().getPrincipalName());
 171  0
                                 } else if (ruleResponsibility.isUsingGroup()) {
 172  0
                                         Group group = ruleResponsibility.getGroup();
 173  0
                                     Element groupElement = renderer.renderTextElement(respElement, GROUP_NAME, group.getName());
 174  0
                                     groupElement.setAttribute(NAMESPACE, group.getNamespaceCode());
 175  0
                                 } else if (ruleResponsibility.isUsingRole()) {
 176  0
                                     renderer.renderTextElement(respElement, ROLE, ruleResponsibility.getRuleResponsibilityName());
 177  0
                                     renderer.renderTextElement(respElement, APPROVE_POLICY, ruleResponsibility.getApprovePolicy());
 178  
                                 }
 179  0
                 if (!StringUtils.isBlank(ruleResponsibility.getActionRequestedCd())) {
 180  0
                         renderer.renderTextElement(respElement, ACTION_REQUESTED, ruleResponsibility.getActionRequestedCd());
 181  
                 }
 182  0
                 if (ruleResponsibility.getPriority() != null) {
 183  0
                         renderer.renderTextElement(respElement, PRIORITY, ruleResponsibility.getPriority().toString());
 184  
                 }
 185  0
             }
 186  
         }
 187  0
     }
 188  
     
 189  
     //below are for exporting rule delegations in rule exportation
 190  
     private void exportRuleDelegations(Element rootElement, RuleBaseValues rule){
 191  0
                 List<RuleDelegationBo> ruleDelegationDefaults = KEWServiceLocator.getRuleDelegationService().findByDelegateRuleId(rule.getId());
 192  0
                 for(RuleDelegationBo dele : ruleDelegationDefaults){
 193  0
                         if (LOG.isInfoEnabled()) {
 194  0
                                 LOG.info("*******delegates********\t"  +  dele.getRuleDelegationId()) ;
 195  
                         }
 196  0
                         exportRuleDelegation(rootElement, dele);        
 197  
                 }
 198  0
     }
 199  
     
 200  
     private void exportRuleDelegation(Element parent, RuleDelegationBo ruleDelegation) {
 201  0
             Element ruleDelegationElement = renderer.renderElement(parent, RULE_DELEGATION);
 202  0
             exportRuleDelegationParentResponsibility(ruleDelegationElement, ruleDelegation);
 203  0
             renderer.renderTextElement(ruleDelegationElement, DELEGATION_TYPE, ruleDelegation.getDelegationType().getCode());
 204  0
             exportRule(ruleDelegationElement, ruleDelegation.getDelegationRule());
 205  0
     }
 206  
     
 207  
     private void exportRuleDelegationParentResponsibility(Element parent, RuleDelegationBo delegation) {
 208  0
         Element parentResponsibilityElement = renderer.renderElement(parent, PARENT_RESPONSIBILITY);
 209  0
         RuleResponsibilityBo ruleResponsibility = KEWServiceLocator.getRuleService().findRuleResponsibility(delegation.getResponsibilityId());
 210  0
         renderer.renderTextElement(parentResponsibilityElement, PARENT_RULE_NAME, ruleResponsibility.getRuleBaseValues().getName());
 211  0
         if (ruleResponsibility.isUsingPrincipal()) {
 212  0
                 Principal principal = ruleResponsibility.getPrincipal();
 213  0
                 renderer.renderTextElement(parentResponsibilityElement, PRINCIPAL_NAME, principal.getPrincipalName());
 214  0
         } else if (ruleResponsibility.isUsingGroup()) {
 215  0
                 Group group = ruleResponsibility.getGroup();
 216  0
                 Element groupElement = renderer.renderElement(parentResponsibilityElement, GROUP_NAME);
 217  0
                 groupElement.setText(group.getName());
 218  0
                 groupElement.setAttribute(NAMESPACE, group.getNamespaceCode());
 219  0
         } else if (ruleResponsibility.isUsingRole()) {
 220  0
                 renderer.renderTextElement(parentResponsibilityElement, ROLE, ruleResponsibility.getRuleResponsibilityName());
 221  
         } else {
 222  0
                 throw new RiceRuntimeException("Encountered a rule responsibility when exporting with an invalid type of '" + ruleResponsibility.getRuleResponsibilityType());
 223  
         }
 224  0
     }
 225  
 
 226  
 }