Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2005-2013 The Kuali Foundation Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.opensource.org/licenses/ecl2.php Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.kuali.rice.kew.xml.export;
 
 
 import java.util.List;
 import java.util.Set;
 
 import static org.kuali.rice.core.api.impex.xml.XmlConstants.*;

Exports rules to XML.

Author(s):
Kuali Rice Team (rice.collab@kuali.org)
See also:
org.kuali.rice.kew.rule.RuleBaseValues
 
 public class RuleXmlExporter implements XmlExporter {
 
     protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass());
 
     private XmlRenderer renderer;
     
     public RuleXmlExporter(Namespace namespace) {
     	this. = new XmlRenderer(namespace);
     }
     
 	public boolean supportPrettyPrint() {
 		return true;
 	}
 
     public Element export(ExportDataSet exportDataSet) {
     	KewExportDataSet dataSet = KewExportDataSet.fromExportDataSet(exportDataSet);
         if (!dataSet.getRules().isEmpty()) {
             Element rootElement = .renderElement(null);
             rootElement.setAttribute();
             for (Iterator iterator = dataSet.getRules().iterator(); iterator.hasNext();) {
             	RuleBaseValues rule = (RuleBaseValuesiterator.next();
             	exportRule(rootElementrule);
             	//turn below on if need export delegates in rule exportation
 //            	if(rule.getDelegateRule().booleanValue()){	
 //            		exportRuleDelegations(rootElement, rule);
 //            	}
             }
             return rootElement;
         }
         return null;
     }
 
     public void exportRule(Element parentRuleBaseValues rule) {
     	Element ruleElement = .renderElement(parent);
         if (rule.getName() != null) {
             .renderTextElement(ruleElementrule.getName());
         }
         .renderTextElement(ruleElementrule.getDocTypeName());
         if (rule.getRuleTemplateName() != null) {
             .renderTextElement(ruleElementrule.getRuleTemplateName());
         }
         .renderTextElement(ruleElementrule.getDescription());
        if(rule.getFromDateString() != null){
            .renderTextElement(ruleElementrule.getFromDateString());
        }
        if(rule.getToDateString() != null){
            .renderTextElement(ruleElementrule.getToDateString());
        }
        if (rule.getRuleExpressionDef() != null) {
            Element expressionElement = .renderTextElement(ruleElementrule.getRuleExpressionDef().getExpression());
            if (rule.getRuleExpressionDef().getType() != null) {
                expressionElement.setAttribute("type"rule.getRuleExpressionDef().getType());
            }
        }
        .renderBooleanElement(ruleElementrule.isForceAction(), false);
        
        if (CollectionUtils.isEmpty(rule.getRuleExtensions()) &&
        		/* field values is not empty */
        		!(rule.getFieldValues() == null || rule.getFieldValues().size() == 0)) {
        	// the rule is in the wrong state (as far as we are concerned).
        	// translate it
        	WebRuleUtils.translateResponsibilitiesForSave(rule);
        	WebRuleUtils.translateFieldValuesForSave(rule);
        	
        	// do our exports
    		exportRuleExtensions(ruleElementrule.getRuleExtensions());
        	
        	// translate it back
        	WebRuleUtils.populateRuleMaintenanceFields(rule);
        } else { 
        	exportRuleExtensions(ruleElementrule.getRuleExtensions());
        }
        
        // put responsibilities in a single collection 
        Set<RuleResponsibilityBoresponsibilities = new HashSet<RuleResponsibilityBo>();
        responsibilities.addAll(rule.getRuleResponsibilities());
        responsibilities.addAll(rule.getPersonResponsibilities());
        responsibilities.addAll(rule.getGroupResponsibilities());
        responsibilities.addAll(rule.getRoleResponsibilities());
        
        exportResponsibilities(ruleElementresponsibilities);
    }
    private void exportRuleExtensions(Element parentList ruleExtensions) {
        if (!ruleExtensions.isEmpty()) {
            Element extsElement = .renderElement(parent);
            for (Iterator iterator = ruleExtensions.iterator(); iterator.hasNext();) {
                RuleExtensionBo extension = (RuleExtensionBoiterator.next();
                Element extElement = .renderElement(extsElement);
                RuleTemplateAttributeBo attribute = extension.getRuleTemplateAttribute();
                .renderTextElement(extElementattribute.getRuleAttribute().getName());
                .renderTextElement(extElementattribute.getRuleTemplate().getName());
                exportRuleExtensionValues(extElementextension.getExtensionValues());
            }
        }
    }
    // sorts by rule extension value key in order to establish a deterministic order
    private void exportRuleExtensionValues(Element parentList<RuleExtensionValueextensionValues) {
        if (!extensionValues.isEmpty()) {
            List<RuleExtensionValuesorted = new ArrayList<RuleExtensionValue>(extensionValues);
            // establish deterministic ordering of keys
            Collections.sort(sortednew Comparator<RuleExtensionValue>() {
                @Override
                public int compare(RuleExtensionValue o1RuleExtensionValue o2) {
                    if (o1 == nullreturn -1;
                    if (o2 == nullreturn 1;
                    return ObjectUtils.compare(o1.getKey(), o2.getKey());
                }
            });
            Element extValuesElement = .renderElement(parent);
            for (Iterator iterator = sorted.iterator(); iterator.hasNext();) {
                RuleExtensionValue extensionValue = (RuleExtensionValueiterator.next();
                Element extValueElement = .renderElement(extValuesElement);
                .renderTextElement(extValueElementextensionValue.getKey());
                .renderTextElement(extValueElementextensionValue.getValue());
            }
        }
    }
    private void exportResponsibilities(Element parentCollection<? extends RuleResponsibilityBoresponsibilities) {
        if (responsibilities != null && !responsibilities.isEmpty()) {
            Element responsibilitiesElement = .renderElement(parent);
            for (RuleResponsibilityBo ruleResponsibility : responsibilities) {
                Element respElement = .renderElement(responsibilitiesElement);
                .renderTextElement(respElement"" + ruleResponsibility.getResponsibilityId());
                if (ruleResponsibility.isUsingPrincipal()) {
				    .renderTextElement(respElementruleResponsibility.getPrincipal().getPrincipalName());
else if (ruleResponsibility.isUsingGroup()) {
					Group group = ruleResponsibility.getGroup();
				    Element groupElement = .renderTextElement(respElementgroup.getName());
				    groupElement.setAttribute(group.getNamespaceCode());
else if (ruleResponsibility.isUsingRole()) {
				    .renderTextElement(respElementruleResponsibility.getRuleResponsibilityName());
				    .renderTextElement(respElementruleResponsibility.getApprovePolicy());
				}
                if (!StringUtils.isBlank(ruleResponsibility.getActionRequestedCd())) {
                	.renderTextElement(respElementruleResponsibility.getActionRequestedCd());
                }
                if (ruleResponsibility.getPriority() != null) {
                	.renderTextElement(respElementruleResponsibility.getPriority().toString());
                }
            }
        }
    }
    
    //below are for exporting rule delegations in rule exportation
    private void exportRuleDelegations(Element rootElementRuleBaseValues rule){
		List<RuleDelegationBoruleDelegationDefaults = KEWServiceLocator.getRuleDelegationService().findByDelegateRuleId(rule.getId());
		for(RuleDelegationBo dele : ruleDelegationDefaults){
			if (.isInfoEnabled()) {
				.info("*******delegates********\t"  +  dele.getRuleDelegationId()) ;
			}
			exportRuleDelegation(rootElementdele);	
		}
    }
    
    private void exportRuleDelegation(Element parentRuleDelegationBo ruleDelegation) {
    	Element ruleDelegationElement = .renderElement(parent);
    	exportRuleDelegationParentResponsibility(ruleDelegationElementruleDelegation);
    	.renderTextElement(ruleDelegationElementruleDelegation.getDelegationType().getCode());
    	exportRule(ruleDelegationElementruleDelegation.getDelegationRule());
    }
    
    private void exportRuleDelegationParentResponsibility(Element parentRuleDelegationBo delegation) {
        Element parentResponsibilityElement = .renderElement(parent);
        RuleResponsibilityBo ruleResponsibility = KEWServiceLocator.getRuleService().findRuleResponsibility(delegation.getResponsibilityId());
        .renderTextElement(parentResponsibilityElementruleResponsibility.getRuleBaseValues().getName());
        if (ruleResponsibility.isUsingPrincipal()) {
        	Principal principal = ruleResponsibility.getPrincipal();
        	.renderTextElement(parentResponsibilityElementprincipal.getPrincipalName());
        } else if (ruleResponsibility.isUsingGroup()) {
        	Group group = ruleResponsibility.getGroup();
        	Element groupElement = .renderElement(parentResponsibilityElement);
        	groupElement.setText(group.getName());
        	groupElement.setAttribute(group.getNamespaceCode());
        } else if (ruleResponsibility.isUsingRole()) {
        	.renderTextElement(parentResponsibilityElementruleResponsibility.getRuleResponsibilityName());
        } else {
        	throw new RiceRuntimeException("Encountered a rule responsibility when exporting with an invalid type of '" + ruleResponsibility.getRuleResponsibilityType());
        }
    }
New to GrepCode? Check out our FAQ X