1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.edl.impl.xml.export;
17
18 import org.apache.log4j.Logger;
19 import org.jdom.Element;
20 import org.jdom.Namespace;
21 import org.kuali.rice.core.api.impex.ExportDataSet;
22 import org.kuali.rice.coreservice.api.style.Style;
23 import org.kuali.rice.coreservice.api.style.StyleService;
24 import org.kuali.rice.core.api.util.xml.XmlHelper;
25 import org.kuali.rice.core.api.util.xml.XmlRenderer;
26 import org.kuali.rice.core.framework.impex.xml.XmlExporter;
27 import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
28 import org.kuali.rice.edl.impl.bo.EDocLiteAssociation;
29 import org.kuali.rice.edl.impl.bo.EDocLiteDefinition;
30 import org.kuali.rice.edl.impl.service.EDocLiteService;
31 import org.kuali.rice.edl.impl.service.EdlServiceLocator;
32
33 import java.io.StringReader;
34 import java.util.Iterator;
35 import java.util.List;
36
37 import static org.kuali.rice.core.api.impex.xml.XmlConstants.*;
38
39
40
41
42
43
44
45 public class EDocLiteXmlExporter implements XmlExporter {
46
47 private static final Logger LOG = Logger.getLogger(EDocLiteXmlExporter.class);
48
49 private XmlRenderer renderer = new XmlRenderer(EDL_NAMESPACE);
50
51 @Override
52 public boolean supportPrettyPrint() {
53 return false;
54 }
55
56 public Element export(ExportDataSet exportDataSet) {
57 EdlExportDataSet dataSet = EdlExportDataSet.fromExportDataSet(exportDataSet);
58 if (!dataSet.getEdocLites().isEmpty()) {
59 Element rootElement = renderer.renderElement(null, EDL_EDOCLITE);
60 rootElement.setAttribute(SCHEMA_LOCATION_ATTR, EDL_SCHEMA_LOCATION, SCHEMA_NAMESPACE);
61
62 List<EDocLiteAssociation> assocList = dataSet.getEdocLites();
63
64 for (EDocLiteAssociation e : assocList) {
65 exportEdlDefinitions(rootElement, e);
66 }
67 for (EDocLiteAssociation e : assocList) {
68 exportStyles(rootElement, e);
69 }
70 for (EDocLiteAssociation e : assocList) {
71 exportAssociations(rootElement, e);
72 }
73 return rootElement;
74 }
75 return null;
76 }
77
78 private void exportEdlDefinitions(Element parentEl, EDocLiteAssociation edl) {
79
80 try {
81 EDocLiteService edlService = EdlServiceLocator.getEDocLiteService();
82 if (edl.getDefinition() != null) {
83 EDocLiteDefinition def = edlService.getEDocLiteDefinition(edl.getDefinition());
84 if (def == null) {
85 LOG.error("Attempted to export definition " + edl.getDefinition() + " which was not found");
86 return;
87 }
88 Element defEl = XmlHelper.buildJDocument(new StringReader(def.getXmlContent())).getRootElement();
89 setNamespace(defEl, EDL_NAMESPACE);
90 parentEl.addContent(defEl.detach());
91 }
92 } catch (Exception e) {
93 throw new RuntimeException(e);
94 }
95 }
96
97 private void exportStyles(Element parentEl, EDocLiteAssociation edl) {
98
99 try {
100 StyleService styleService = CoreServiceApiServiceLocator.getStyleService();
101
102 if (edl.getStyle() != null) {
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 }