001/**
002 * Copyright 2005-2017 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.xml.export;
017
018import org.jdom.Document;
019import org.jdom.Element;
020import org.jdom.input.SAXBuilder;
021import org.kuali.rice.core.api.impex.ExportDataSet;
022import org.kuali.rice.core.api.util.xml.XmlHelper;
023import org.kuali.rice.core.api.util.xml.XmlRenderer;
024import org.kuali.rice.core.framework.impex.xml.XmlExporter;
025import org.kuali.rice.kew.api.WorkflowRuntimeException;
026import org.kuali.rice.kew.export.KewExportDataSet;
027import org.kuali.rice.kew.rule.bo.RuleAttribute;
028
029import java.io.StringReader;
030import java.util.Iterator;
031
032import static org.kuali.rice.core.api.impex.xml.XmlConstants.*;
033
034/**
035 * Exports {@link org.kuali.rice.kew.rule.bo.RuleAttribute}s to XML.
036 * 
037 * @see org.kuali.rice.kew.rule.bo.RuleAttribute
038 *
039 * @author Kuali Rice Team (rice.collab@kuali.org)
040 */
041public class RuleAttributeXmlExporter implements XmlExporter {
042
043    protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass());
044    
045    private XmlRenderer renderer = new XmlRenderer(RULE_ATTRIBUTE_NAMESPACE);
046    
047        @Override
048        public boolean supportPrettyPrint() {
049                return true;
050        }
051
052    public Element export(ExportDataSet exportDataSet) {
053        KewExportDataSet dataSet = KewExportDataSet.fromExportDataSet(exportDataSet);
054        if (!dataSet.getRuleAttributes().isEmpty()) {
055            Element rootElement = renderer.renderElement(null, RULE_ATTRIBUTES);
056            rootElement.setAttribute(SCHEMA_LOCATION_ATTR, RULE_ATTRIBUTE_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
057            for (Iterator iterator = dataSet.getRuleAttributes().iterator(); iterator.hasNext();) {
058                RuleAttribute template = (RuleAttribute)iterator.next();
059                exportRuleAttribute(rootElement, template);
060            }
061            return rootElement;
062        }
063        return null;
064    }
065    
066    private void exportRuleAttribute(Element parent, RuleAttribute ruleAttribute) {
067        Element attributeElement = renderer.renderElement(parent, RULE_ATTRIBUTE);
068        renderer.renderTextElement(attributeElement, NAME, ruleAttribute.getName());
069        renderer.renderTextElement(attributeElement, CLASS_NAME, ruleAttribute.getResourceDescriptor());
070        renderer.renderTextElement(attributeElement, LABEL, ruleAttribute.getLabel());
071        renderer.renderTextElement(attributeElement, DESCRIPTION, ruleAttribute.getDescription());
072        renderer.renderTextElement(attributeElement, TYPE, ruleAttribute.getType());
073        renderer.renderTextElement(attributeElement, APPLICATION_ID, ruleAttribute.getApplicationId());
074        if (!org.apache.commons.lang.StringUtils.isEmpty(ruleAttribute.getXmlConfigData())) {
075            try {
076                Document configDoc = new SAXBuilder().build(new StringReader(ruleAttribute.getXmlConfigData()));
077                XmlHelper.propagateNamespace(configDoc.getRootElement(), RULE_ATTRIBUTE_NAMESPACE);
078                attributeElement.addContent(configDoc.getRootElement().detach());
079            } catch (Exception e) {
080                LOG.error("Error parsing attribute XML configuration.", e);
081                throw new WorkflowRuntimeException("Error parsing attribute XML configuration.");
082            }
083        }
084    }
085    
086}