001    /**
002     * Copyright 2005-2013 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     */
016    package org.kuali.rice.edl.impl.xml.export;
017    
018    import org.apache.log4j.Logger;
019    import org.jdom.Element;
020    import org.jdom.Namespace;
021    import org.kuali.rice.core.api.impex.ExportDataSet;
022    import org.kuali.rice.coreservice.api.style.Style;
023    import org.kuali.rice.coreservice.api.style.StyleService;
024    import org.kuali.rice.core.api.util.xml.XmlHelper;
025    import org.kuali.rice.core.api.util.xml.XmlRenderer;
026    import org.kuali.rice.core.framework.impex.xml.XmlExporter;
027    import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
028    import org.kuali.rice.edl.impl.bo.EDocLiteAssociation;
029    import org.kuali.rice.edl.impl.bo.EDocLiteDefinition;
030    import org.kuali.rice.edl.impl.service.EDocLiteService;
031    import org.kuali.rice.edl.impl.service.EdlServiceLocator;
032    
033    import java.io.StringReader;
034    import java.util.Iterator;
035    import java.util.List;
036    
037    import static org.kuali.rice.core.api.impex.xml.XmlConstants.*;
038    /**
039     * Exports EDocLite definitions to XML.
040     *
041     * @see EDocLiteDefinition
042     *
043     * @author Kuali Rice Team (rice.collab@kuali.org)
044     */
045    public class EDocLiteXmlExporter implements XmlExporter {
046    
047            private static final Logger LOG = Logger.getLogger(EDocLiteXmlExporter.class);
048    
049            private XmlRenderer renderer = new XmlRenderer(EDL_NAMESPACE);
050    
051            @Override
052            public boolean supportPrettyPrint() {
053                    return false;
054            }
055            
056            public Element export(ExportDataSet exportDataSet) {
057                    EdlExportDataSet dataSet = EdlExportDataSet.fromExportDataSet(exportDataSet);
058                    if (!dataSet.getEdocLites().isEmpty()) {
059                            Element rootElement = renderer.renderElement(null, EDL_EDOCLITE);
060                            rootElement.setAttribute(SCHEMA_LOCATION_ATTR, EDL_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
061                            // create output in order of all edl followed by all stylesheets, followed by all associations, this is so multiple edoclite's can be ingested in a single xml file.
062                            List<EDocLiteAssociation> assocList = dataSet.getEdocLites();
063                            // loop thru same list 3 times.
064                            for (EDocLiteAssociation e : assocList) {
065                                    exportEdlDefinitions(rootElement, e);
066                            }
067                            for (EDocLiteAssociation e : assocList) {
068                                    exportStyles(rootElement, e);
069                            }
070                            for (EDocLiteAssociation e : assocList) {
071                                    exportAssociations(rootElement, e);
072                            }
073                            return rootElement;
074                    }
075                    return null;
076            }
077    
078            private void exportEdlDefinitions(Element parentEl, EDocLiteAssociation edl) {
079    
080                    try {
081                            EDocLiteService edlService = EdlServiceLocator.getEDocLiteService();
082                            if (edl.getDefinition() != null) {  //this probably shouldn't be supported on the entry side...
083                                    EDocLiteDefinition def = edlService.getEDocLiteDefinition(edl.getDefinition());
084                                    if (def == null) {
085                                            LOG.error("Attempted to export definition " + edl.getDefinition() + " which was not found");
086                                            return;
087                                    }
088                                    Element defEl = XmlHelper.buildJDocument(new StringReader(def.getXmlContent())).getRootElement();
089                                    setNamespace(defEl, EDL_NAMESPACE);
090                                    parentEl.addContent(defEl.detach());
091                            }
092                    } catch (Exception e) {
093                            throw new RuntimeException(e);
094                    }
095            }
096            
097            private void exportStyles(Element parentEl, EDocLiteAssociation edl) {
098    
099                    try {
100                            StyleService styleService = CoreServiceApiServiceLocator.getStyleService();
101    
102                            if (edl.getStyle() != null) {//this probably shouldn't be supported on the entry side...
103                                    Element styleWrapperEl = renderer.renderElement(parentEl, EDL_STYLE);
104                                    renderer.renderAttribute(styleWrapperEl, "name", edl.getStyle());
105                                    Style style = styleService.getStyle(edl.getStyle());
106                                    if (style == null) {
107                                            LOG.error("Attempted to export style " + edl.getStyle() + " which was not found");
108                                            return;
109                                    }
110                                    Element styleEl = XmlHelper.buildJDocument(new StringReader(style.getXmlContent())).getRootElement();
111                                    styleWrapperEl.addContent(styleEl.detach());
112                            }
113                    } catch (Exception e) {
114                            throw new RuntimeException(e);
115                    }
116            }
117            
118            private void exportAssociations(Element parentEl, EDocLiteAssociation edl) {
119                    try {
120                            Element associationEl = renderer.renderElement(parentEl, EDL_ASSOCIATION);
121                            renderer.renderTextElement(associationEl, EDL_DOC_TYPE, edl.getEdlName());
122                            if (edl.getDefinition() != null) {
123                                    renderer.renderTextElement(associationEl, EDL_DEFINITION, edl.getDefinition());
124                            }
125                            if (edl.getStyle() != null) {
126                                    renderer.renderTextElement(associationEl, EDL_STYLE, edl.getStyle());
127                            }
128    
129                            renderer.renderTextElement(associationEl, EDL_ACTIVE, edl.getActiveInd().toString());
130                    } catch (Exception e) {
131                            throw new RuntimeException(e);
132                    }
133            }
134    
135            private void setNamespace(Element element, Namespace namespace) {
136                    element.setNamespace(namespace);
137                    for (Iterator iter = element.getChildren().iterator(); iter.hasNext();) {
138                            setNamespace((Element)iter.next(), namespace);
139                    }
140            }
141    }