Coverage Report - org.kuali.rice.kew.xml.export.EDocLiteXmlExporter
 
Classes in this File Line Coverage Branch Coverage Complexity
EDocLiteXmlExporter
0%
0/57
0%
0/22
5
 
 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 java.io.StringReader;
 20  
 import java.util.Iterator;
 21  
 import java.util.List;
 22  
 
 23  
 import org.apache.log4j.Logger;
 24  
 import org.jdom.Element;
 25  
 import org.jdom.Namespace;
 26  
 import org.kuali.rice.kew.edl.bo.EDocLiteAssociation;
 27  
 import org.kuali.rice.kew.edl.bo.EDocLiteDefinition;
 28  
 import org.kuali.rice.kew.edl.bo.EDocLiteStyle;
 29  
 import org.kuali.rice.kew.edl.service.EDocLiteService;
 30  
 import org.kuali.rice.kew.export.ExportDataSet;
 31  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 32  
 import org.kuali.rice.kew.util.XmlHelper;
 33  
 import org.kuali.rice.kew.xml.XmlConstants;
 34  
 import org.kuali.rice.kew.xml.XmlRenderer;
 35  
 
 36  
 
 37  
 /**
 38  
  * Exports EDocLite definitions to XML.
 39  
  *
 40  
  * @see EDocLiteDefinition
 41  
  *
 42  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 43  
  */
 44  0
 public class EDocLiteXmlExporter implements XmlExporter, XmlConstants {
 45  
 
 46  0
         private static final Logger LOG = Logger.getLogger(EDocLiteXmlExporter.class);
 47  
 
 48  0
         private XmlRenderer renderer = new XmlRenderer(EDL_NAMESPACE);
 49  
 
 50  
         public Element export(ExportDataSet dataSet) {
 51  0
                 if (!dataSet.getEdocLites().isEmpty()) {
 52  0
                         Element rootElement = renderer.renderElement(null, EDL_EDOCLITE);
 53  0
                         rootElement.setAttribute(SCHEMA_LOCATION_ATTR, EDL_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
 54  
                         // 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.
 55  0
                         List<EDocLiteAssociation> assocList = dataSet.getEdocLites();
 56  
                         // loop thru same list 3 times.
 57  0
                         for (EDocLiteAssociation e : assocList) {
 58  0
                                 exportEdlDefinitions(rootElement, e);
 59  
                         }
 60  0
                         for (EDocLiteAssociation e : assocList) {
 61  0
                                 exportStyles(rootElement, e);
 62  
                         }
 63  0
                         for (EDocLiteAssociation e : assocList) {
 64  0
                                 exportAssociations(rootElement, e);
 65  
                         }
 66  0
                         return rootElement;
 67  
                 }
 68  0
                 return null;
 69  
         }
 70  
 
 71  
         private void exportEdlDefinitions(Element parentEl, EDocLiteAssociation edl) {
 72  
 
 73  
                 try {
 74  0
                         EDocLiteService edlService = KEWServiceLocator.getEDocLiteService();
 75  0
                         if (edl.getDefinition() != null) {  //this probably shouldn't be supported on the entry side...
 76  0
                                 EDocLiteDefinition def = edlService.getEDocLiteDefinition(edl.getDefinition());
 77  0
                                 if (def == null) {
 78  0
                                         LOG.error("Attempted to export definition " + edl.getDefinition() + " which was not found");
 79  0
                                         return;
 80  
                                 }
 81  0
                                 Element defEl = XmlHelper.buildJDocument(new StringReader(def.getXmlContent())).getRootElement();
 82  0
                                 setNamespace(defEl, EDL_NAMESPACE);
 83  0
                                 parentEl.addContent(defEl.detach());
 84  
                         }
 85  0
                 } catch (Exception e) {
 86  0
                         throw new RuntimeException(e);
 87  0
                 }
 88  0
         }
 89  
         
 90  
         private void exportStyles(Element parentEl, EDocLiteAssociation edl) {
 91  
 
 92  
                 try {
 93  0
                         EDocLiteService edlService = KEWServiceLocator.getEDocLiteService();
 94  
 
 95  0
                         if (edl.getStyle() != null) {//this probably shouldn't be supported on the entry side...
 96  0
                                 Element styleWrapperEl = renderer.renderElement(parentEl, EDL_STYLE);
 97  0
                                 renderer.renderAttribute(styleWrapperEl, "name", edl.getStyle());
 98  0
                                 EDocLiteStyle style = edlService.getEDocLiteStyle(edl.getStyle());
 99  0
                                 if (style == null) {
 100  0
                                         LOG.error("Attempted to export style " + edl.getStyle() + " which was not found");
 101  0
                                         return;
 102  
                                 }
 103  0
                                 Element styleEl = XmlHelper.buildJDocument(new StringReader(style.getXmlContent())).getRootElement();
 104  0
                                 styleWrapperEl.addContent(styleEl.detach());
 105  
                         }
 106  0
                 } catch (Exception e) {
 107  0
                         throw new RuntimeException(e);
 108  0
                 }
 109  0
         }
 110  
         
 111  
         private void exportAssociations(Element parentEl, EDocLiteAssociation edl) {
 112  
                 try {
 113  0
                         Element associationEl = renderer.renderElement(parentEl, EDL_ASSOCIATION);
 114  0
                         renderer.renderTextElement(associationEl, EDL_DOC_TYPE, edl.getEdlName());
 115  0
                         if (edl.getDefinition() != null) {
 116  0
                                 renderer.renderTextElement(associationEl, EDL_DEFINITION, edl.getDefinition());
 117  
                         }
 118  0
                         if (edl.getStyle() != null) {
 119  0
                                 renderer.renderTextElement(associationEl, EDL_STYLE, edl.getStyle());
 120  
                         }
 121  
 
 122  0
                         renderer.renderTextElement(associationEl, EDL_ACTIVE, edl.getActiveInd().toString());
 123  0
                 } catch (Exception e) {
 124  0
                         throw new RuntimeException(e);
 125  0
                 }
 126  0
         }
 127  
 
 128  
         private void setNamespace(Element element, Namespace namespace) {
 129  0
                 element.setNamespace(namespace);
 130  0
                 for (Iterator iter = element.getChildren().iterator(); iter.hasNext();) {
 131  0
                         setNamespace((Element)iter.next(), namespace);
 132  
                 }
 133  0
         }
 134  
 }