| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 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 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 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 | |
|
| 55 | 0 | List<EDocLiteAssociation> assocList = dataSet.getEdocLites(); |
| 56 | |
|
| 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) { |
| 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) { |
| 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 | |
} |