1
2
3
4
5
6
7
8
9
10
11 package com.sri.emo.controller;
12
13 import com.jcorporate.expresso.core.controller.ControllerException;
14 import com.jcorporate.expresso.core.controller.ExpressoRequest;
15 import com.jcorporate.expresso.core.db.DBException;
16 import com.sri.emo.dbobj.*;
17 import org.dom4j.Document;
18 import org.dom4j.DocumentFactory;
19 import org.dom4j.DocumentHelper;
20 import org.dom4j.Element;
21
22 import java.util.*;
23
24
25 /***
26 * Extraction of XML writing to a separate class.
27 *
28 * @author Michael Rimov
29 */
30 public class ModelXMLWriter {
31
32 /***
33 * The key of the node id used to separate Entities from their
34 * respective Nodes.
35 */
36 public static final String DATA_NODES = "Data_nodes";
37 public static final String DELIMITER = "_x_";
38
39 /***
40 * Default constructor.
41 */
42 public ModelXMLWriter() {
43 }
44
45 /***
46 * Renders the entity and nodes as XML.
47 *
48 * @param entity NodeType the entity to render
49 * @param request ControllerRequest the ControllerRequest object
50 * @param renderNodes true if you want the individual nodes rendered as well.
51 * @return Document the resulting document.
52 * @throws DBException upon database error,
53 * @throws ControllerException upon rendering error.
54 */
55 public Document renderEntityAsXml(NodeType entity,
56 ExpressoRequest request, boolean renderNodes)
57 throws DBException, ControllerException {
58 Document d = renderEntityAsXml(entity);
59 if (renderNodes) {
60 Node node = new Node();
61 node.setNodeType(entity.getEntityName());
62 List results = node.searchAndRetrieveList();
63 Node[] nodes = (Node[]) results.toArray(new Node[results.size()]);
64 Element[] elements = renderNodesAsXml(nodes, request);
65
66 Element root = d.getRootElement();
67 Element parent = DocumentFactory.getInstance().createElement(DATA_NODES);
68 root.add(parent);
69 for (int i = 0; i < elements.length; i++) {
70 parent.add(elements[i]);
71 }
72 }
73
74 return d;
75 }
76
77 /***
78 * Converts a particular node to XML.
79 *
80 * @param querynode Node
81 * @param request ControllerRequest
82 * @return String
83 * @throws ControllerException
84 */
85 public Document renderNodeAsXml(Node querynode, ExpressoRequest request) throws ControllerException {
86 try {
87 Element[] elements = renderNodesAsXml(new Node[]{querynode}, request);
88 Document doc = DocumentHelper.createDocument();
89 for (int i = 0; i < elements.length; i++) {
90 doc.add(elements[i]);
91 }
92
93 return doc;
94 } catch (Exception e) {
95 throw new ControllerException(e);
96 }
97 }
98
99 /***
100 * Render an entity as an XML document.
101 *
102 * @param entity NodeType the node type to render
103 * @return Document the document.
104 * @throws DBException upon database access error.
105 */
106 public Document renderEntityAsXml(NodeType entity) throws DBException {
107 Document doc = DocumentHelper.createDocument();
108
109 Element root = doc.addElement(PartAction.ENTITY);
110 root.addAttribute(NodeType.DISPLAY_TITLE, entity.getDisplayName())
111 .addAttribute(NodeType.NODE_TYPE_DESCRIP, entity.getEntityDescription())
112 .addAttribute(NodeType.NODE_TYPE_NAME, entity.getEntityName());
113
114 Part[] parts = PartsFactory.getParts(entity.getEntityName());
115
116 for (int j = 0; j < parts.length; j++) {
117 Part part = parts[j];
118 Element partElm = root.addElement(PartAction.PART)
119 .addAttribute(Part.PART_TYPE, part.getPartType())
120 .addAttribute(Part.PART_LABEL, part.getPartLabel())
121 .addAttribute(Part.PART_DESCRIP, part.getPartDescription())
122 .addAttribute(Part.PART_NUM, part.getPartNum())
123 .addAttribute(Part.CARDINALITY, part.getCardinality())
124 .addAttribute(Part.PART_IS_ATTRIB, part.isOwnedAttribute() ? "true" : "false");
125
126 if (part.isHaveCustomHandler()) {
127 partElm.addAttribute(Part.SPECIAL_HANDLER, part.getSpecialHandlerName());
128 }
129
130 if (!part.isOwnedAttribute()) {
131 partElm.addAttribute(PartAction.RELATED_ENTITY_TYPE, part.getPartType());
132 partElm.addAttribute(Part.NODE_PART_RELATION_TYPE, part.getNodeRelation());
133 }
134 }
135 return doc;
136 }
137
138
139 /***
140 * Similar to the above list, but renders an array of XML nodes in succession
141 * to the resulting document.
142 *
143 * @param allNodes Node[] an array of nodes.
144 * @param request ControllerRequest
145 * @return Document
146 * @throws ControllerException
147 */
148 public Element[] renderNodesAsXml(Node allNodes[],
149 ExpressoRequest request) throws ControllerException {
150 try {
151 Element[] elements = new Element[allNodes.length];
152 ArrayList alreadyIncluded = new ArrayList();
153 for (int i = 0; i < allNodes.length; i++) {
154 Node querynode = allNodes[i];
155 Element xml = querynode.getXML(request, alreadyIncluded);
156 addPicklists(xml, alreadyIncluded);
157 elements[i] = xml;
158 }
159 return elements;
160 } catch (Exception e) {
161 throw new ControllerException(e);
162 }
163
164 }
165
166 /***
167 * Loop through nodes and add all picklists (just once) section to xml
168 *
169 * @param xml the XML DOM Element fragment root
170 * @param alreadyIncluded ArrayList
171 * @throws DBException upon error.
172 */
173 protected void addPicklists(Element xml, ArrayList alreadyIncluded) throws DBException {
174 HashMap map = new HashMap();
175
176 for (Iterator iterator = alreadyIncluded.iterator(); iterator.hasNext();) {
177 Node node = (Node) iterator.next();
178 Part[] parts = node.getParts();
179
180 for (int i = 0; i < parts.length; i++) {
181 Part apart = parts[i];
182
183 if (apart.hasPicklist()) {
184
185 map.put(apart.getParentType() + apart.getPartType(), apart);
186 }
187 }
188 }
189
190 if (map.isEmpty()) {
191 return;
192 }
193
194 Element picks = xml.addElement(AddNodeAction.PICKLISTS_TAGNAME);
195 Collection values = map.values();
196 ArrayList list = new ArrayList(values);
197 Collections.sort(list);
198
199 for (Iterator iterator = list.iterator(); iterator.hasNext();) {
200 Part apart = (Part) iterator.next();
201
202 Element thisPicklist = picks.addElement(apart.getPicklistXMLName());
203 String[][] picklistArray = apart.getPicklistArrayAssumeSecure();
204
205 for (int i = 0; (picklistArray != null) && (i < picklistArray.length); i++) {
206 String[] onerow = picklistArray[i];
207 String id = onerow[0];
208 String display = onerow[1];
209 String order = onerow[2];
210 thisPicklist.addElement(AddNodeAction.ROW)
211 .addAttribute(PickList.LIST_ID, id)
212 .addAttribute(PickList.ORDER_NUM, order)
213 .addAttribute(PickList.DISPLAY_IN_PICKLIST, display);
214 }
215 }
216 }
217 }