annotate src/main/java/org/mpi/openmind/repository/utils/OM4StreamWriter.java @ 75:e0be7c0030f5

cleanup and better comments.
author casties
date Thu, 23 Feb 2017 19:05:47 +0100
parents 7d8ebe8ac8a2
children a59984fd3c3f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
jurzua
parents:
diff changeset
1 package org.mpi.openmind.repository.utils;
jurzua
parents:
diff changeset
2
jurzua
parents:
diff changeset
3 import java.io.FileOutputStream;
jurzua
parents:
diff changeset
4 import java.io.IOException;
jurzua
parents:
diff changeset
5 import java.io.OutputStreamWriter;
jurzua
parents:
diff changeset
6 import java.text.DecimalFormat;
jurzua
parents:
diff changeset
7 import java.util.ArrayList;
jurzua
parents:
diff changeset
8 import java.util.List;
jurzua
parents:
diff changeset
9
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
10 import javax.xml.stream.XMLOutputFactory;
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
11 import javax.xml.stream.XMLStreamException;
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
12 import javax.xml.stream.XMLStreamWriter;
1
jurzua
parents:
diff changeset
13
jurzua
parents:
diff changeset
14 import org.apache.commons.lang.StringUtils;
jurzua
parents:
diff changeset
15 import org.apache.log4j.Logger;
jurzua
parents:
diff changeset
16 import org.mpi.openmind.repository.bo.Attribute;
jurzua
parents:
diff changeset
17 import org.mpi.openmind.repository.bo.Entity;
jurzua
parents:
diff changeset
18 import org.mpi.openmind.repository.bo.Node;
jurzua
parents:
diff changeset
19 import org.mpi.openmind.repository.bo.Relation;
jurzua
parents:
diff changeset
20 import org.mpi.openmind.repository.services.PersistenceService;
jurzua
parents:
diff changeset
21
31
7d8ebe8ac8a2 create reader and check script for XML dumps.
casties
parents: 29
diff changeset
22 /**
7d8ebe8ac8a2 create reader and check script for XML dumps.
casties
parents: 29
diff changeset
23 * Export all entities and relations and definitions to XML.
7d8ebe8ac8a2 create reader and check script for XML dumps.
casties
parents: 29
diff changeset
24 *
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
25 * Saves (content) entities and relations (i.e. assertions) and definitions
31
7d8ebe8ac8a2 create reader and check script for XML dumps.
casties
parents: 29
diff changeset
26 * (i.e. definition entities and relations) in separate files.
7d8ebe8ac8a2 create reader and check script for XML dumps.
casties
parents: 29
diff changeset
27 *
7d8ebe8ac8a2 create reader and check script for XML dumps.
casties
parents: 29
diff changeset
28 * @author jurzua, casties
7d8ebe8ac8a2 create reader and check script for XML dumps.
casties
parents: 29
diff changeset
29 *
7d8ebe8ac8a2 create reader and check script for XML dumps.
casties
parents: 29
diff changeset
30 */
1
jurzua
parents:
diff changeset
31 public class OM4StreamWriter {
jurzua
parents:
diff changeset
32
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
33 private static Logger logger = Logger.getLogger(OM4StreamWriter.class);
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
34
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
35 private static int itemsPerPage = 500;
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
36
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
37 /**
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
38 * Return the object's string representation or "null" if its null.
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
39 *
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
40 * @param s
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
41 * @return
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
42 */
31
7d8ebe8ac8a2 create reader and check script for XML dumps.
casties
parents: 29
diff changeset
43 private static String defaultString(Object s) {
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
44 if (s == null) {
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
45 return "null";
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
46 } else {
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
47 return s.toString();
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
48 }
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
49 }
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
50
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
51
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
52 /**
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
53 * Saves all content Entities with their Attributes and Relations in a XML file with the given fileName.
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
54 *
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
55 * @param fileName
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
56 * @param ps
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
57 */
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
58 public static void backupEntities(String fileName, PersistenceService ps) {
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
59 writeEntsAndRels(fileName, ps, Node.TYPE_ABOX);
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
60 }
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
61
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
62 /**
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
63 * Saves all definitions in a XML file with the given fileName.
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
64 *
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
65 * @param fileName
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
66 * @param ps
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
67 */
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
68 public static void backupDefinitions(String fileName, PersistenceService ps) {
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
69 writeEntsAndRels(fileName, ps, Node.TYPE_TBOX);
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
70 }
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
71
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
72 /**
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
73 * Writes all entities of the given type and their relations to the XML file at fileName.
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
74 *
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
75 * Type is either TYPE_TBOX or TYPE_ABOX.
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
76 *
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
77 * @param fileName
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
78 * @param ps
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
79 * @param type
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
80 */
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
81 private static void writeEntsAndRels(String fileName, PersistenceService ps, String type) {
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
82 OutputStreamWriter out;
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
83 try {
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
84 FileOutputStream fileOut = new FileOutputStream(fileName);
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
85 out = new OutputStreamWriter(fileOut, "UTF-8");
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
86 XMLOutputFactory factory = XMLOutputFactory.newInstance();
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
87 XMLStreamWriter writer = factory.createXMLStreamWriter(out);
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
88
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
89 int entitiesCount = 0;
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
90
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
91 writer.writeStartDocument("UTF-8", "1.0");
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
92
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
93 if (type.equals(Node.TYPE_ABOX)) {
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
94 writer.writeStartElement(XMLUtil.OPENMIND_DATA);
31
7d8ebe8ac8a2 create reader and check script for XML dumps.
casties
parents: 29
diff changeset
95 writer.writeAttribute("version", "4.3");
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
96 // get number of content Entities
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
97 entitiesCount = ps.getEntityCount(null).intValue();
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
98 } else {
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
99 writer.writeStartElement(XMLUtil.META_DATA);
31
7d8ebe8ac8a2 create reader and check script for XML dumps.
casties
parents: 29
diff changeset
100 writer.writeAttribute("version", "4.3");
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
101 // get number of definition Entities
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
102 entitiesCount = ps.getEntityCount(Node.TYPE_TBOX).intValue();
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
103 }
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
104
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
105 int numberOfPages = entitiesCount / itemsPerPage;
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
106 // debug: int numberOfPages = 1;
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
107 int counter = 0;
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
108 long start = System.currentTimeMillis();
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
109 DecimalFormat df = new DecimalFormat("#.##");
1
jurzua
parents:
diff changeset
110
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
111 // list of Relations (filled from Entities)
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
112 List<Relation> relList = new ArrayList<Relation>();
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
113
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
114 /*
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
115 * write entities
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
116 */
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
117 writer.writeStartElement((type.equals(Node.TYPE_TBOX)) ? XMLUtil.DEFINITIONS : XMLUtil.ENTITIES);
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
118 writer.writeAttribute("number", Integer.toString(entitiesCount));
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
119 // go through all pages
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
120 for (int currentPage = 0; currentPage <= numberOfPages; currentPage++) {
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
121 int startRecord = currentPage * itemsPerPage;
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
122 List<Entity> entities;
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
123
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
124 if (type.equals(Node.TYPE_ABOX)) {
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
125 // get page of content Entities
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
126 entities = ps.getEntityPage(null, startRecord, itemsPerPage);
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
127 } else {
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
128 // get page of definition Entities
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
129 entities = ps.getEntityPage(Node.TYPE_TBOX, startRecord, itemsPerPage);
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
130 }
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
131
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
132 for (Entity ent : entities) {
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
133 // write entity to XML
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
134 writeEntity(ent, writer, ps);
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
135 // add (source)relations to list
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
136 relList.addAll(ent.getSourceRelations());
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
137
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
138 counter++;
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
139 /* if ((counter % 50) == 0) {
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
140 logger.debug("*");
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
141 } */
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
142 }
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
143
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
144 long runtime = System.currentTimeMillis() - start;
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
145 double percent = ((double) counter / (double) entitiesCount) * 100.0;
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
146 logger.debug("(" + df.format(percent) + "%) \t[" + counter + "/" + entitiesCount + "]\t");
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
147 logger.debug("Speed[ents/s]: " + df.format((double) counter / ((double) runtime / 1000)));
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
148 writer.flush();
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
149 }
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
150 writer.writeEndElement();
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
151
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
152 /*
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
153 * write relations (from list)
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
154 */
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
155 writer.writeStartElement(XMLUtil.RELATIONS);
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
156 writer.writeAttribute("number", Integer.toString(relList.size()));
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
157 for (Relation rel : relList) {
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
158 writeRelation(rel, writer);
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
159 }
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
160 writer.writeEndElement();
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
161
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
162 // end file.
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
163 writer.writeEndElement();
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
164
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
165 writer.flush();
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
166 writer.close();
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
167
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
168 logger.info("END Stream Writer");
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
169 } catch (IOException e) {
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
170 logger.error(e);
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
171 } catch (XMLStreamException e) {
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
172 logger.error(e);
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
173 }
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
174 }
1
jurzua
parents:
diff changeset
175
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
176 /**
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
177 * Write OpenMind relation to XML.
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
178 *
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
179 * @param rel
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
180 * @param writer
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
181 * @throws XMLStreamException
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
182 */
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
183 private static void writeRelation(Relation rel, XMLStreamWriter writer) throws XMLStreamException {
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
184 writer.writeStartElement(XMLUtil.RELATION);
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
185
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
186 /*
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
187 * write XML-attributes
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
188 */
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
189 writer.writeAttribute(XMLUtil.OBJECT_CLASS, defaultString(rel.getObjectClass()));
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
190 writer.writeAttribute(XMLUtil.ID, defaultString(rel.getId()));
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
191 writer.writeAttribute(XMLUtil.ROW_ID, defaultString(rel.getRowId()));
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
192 if (StringUtils.isNotEmpty(rel.getContentType())) {
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
193 writer.writeAttribute(XMLUtil.CONTENT_TYPE, rel.getContentType());
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
194 }
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
195 writer.writeAttribute(XMLUtil.RELATION_SOURCE_ID, defaultString(rel.getSourceId()));
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
196 writer.writeAttribute(XMLUtil.RELATION_TARGET_ID, defaultString(rel.getTargetId()));
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
197 writer.writeAttribute(XMLUtil.VERSION, defaultString(rel.getVersion()));
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
198 writer.writeAttribute(XMLUtil.MODIFICATION_TIME, defaultString(rel.getModificationTime()));
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
199 if (rel.getUser() != null) {
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
200 writer.writeAttribute(XMLUtil.USER, rel.getUser());
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
201 }
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
202 if (rel.getIsPublic()) {
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
203 writer.writeAttribute(XMLUtil.PUBLIC, "true");
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
204 }
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
205
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
206 /*
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
207 * write OpenMind attributes of this relation as XML tags
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
208 */
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
209 if (rel.getAttributes().size() > 0) {
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
210 writer.writeStartElement(XMLUtil.ATTRIBUTES);
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
211 for (Attribute att : rel.getAttributes()) {
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
212 writeAttribute(att, writer);
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
213 }
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
214 writer.writeEndElement();
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
215 }
1
jurzua
parents:
diff changeset
216
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
217 /*
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
218 * write own value as content
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
219 */
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
220 if (StringUtils.isNotEmpty(rel.getOwnValue())) {
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
221 writer.writeCharacters(rel.getOwnValue());
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
222 }
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
223
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
224 writer.writeEndElement();
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
225 }
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
226
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
227 /**
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
228 * Write OpenMind entity to XML.
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
229 *
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
230 * @param entity
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
231 * @param writer
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
232 * @param ps
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
233 * @throws XMLStreamException
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
234 */
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
235 private static void writeEntity(Entity entity, XMLStreamWriter writer, PersistenceService ps)
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
236 throws XMLStreamException {
1
jurzua
parents:
diff changeset
237
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
238 writer.writeStartElement((entity.getType().equals(Node.TYPE_TBOX)) ? XMLUtil.DEFINITION : XMLUtil.ENTITY);
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
239
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
240 if (entity.isLightweight()) {
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
241 entity = ps.getEntityContent(entity);
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
242 }
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
243
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
244 /*
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
245 * write XML attributes
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
246 */
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
247 writer.writeAttribute(XMLUtil.OBJECT_CLASS, defaultString(entity.getObjectClass()));
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
248 writer.writeAttribute(XMLUtil.ID, defaultString(entity.getId()));
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
249 writer.writeAttribute(XMLUtil.ROW_ID, defaultString(entity.getRowId()));
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
250 if (StringUtils.isNotEmpty(entity.getContentType())) {
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
251 writer.writeAttribute(XMLUtil.CONTENT_TYPE, entity.getContentType());
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
252 }
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
253 writer.writeAttribute(XMLUtil.VERSION, defaultString(entity.getVersion()));
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
254 writer.writeAttribute(XMLUtil.MODIFICATION_TIME, defaultString(entity.getModificationTime()));
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
255 if (entity.getUser() != null) {
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
256 writer.writeAttribute(XMLUtil.USER, entity.getUser());
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
257 }
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
258 if (entity.getIsPublic()) {
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
259 writer.writeAttribute(XMLUtil.PUBLIC, "true");
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
260 }
1
jurzua
parents:
diff changeset
261
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
262 /*
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
263 * write OpenMind attributes of this entity as XML tags
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
264 */
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
265 if (entity.getAttributes().size() > 0) {
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
266 writer.writeStartElement(XMLUtil.ATTRIBUTES);
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
267 for (Attribute att : entity.getAttributes()) {
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
268 writeAttribute(att, writer);
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
269 }
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
270 writer.writeEndElement();
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
271 }
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
272
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
273 /*
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
274 * write own value
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
275 */
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
276 if (StringUtils.isNotEmpty(entity.getOwnValue())) {
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
277 writer.writeCharacters(entity.getOwnValue());
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
278 }
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
279
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
280 writer.writeEndElement();
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
281 }
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
282
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
283 private static void writeAttribute(Attribute att, XMLStreamWriter writer) throws XMLStreamException {
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
284 writer.writeStartElement(XMLUtil.ATTRIBUTE);
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
285
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
286 /*
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
287 * write XML attributes
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
288 */
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
289 writer.writeAttribute(XMLUtil.ATTRIBUTE_NAME, defaultString(att.getName()));
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
290 writer.writeAttribute(XMLUtil.ID, defaultString(att.getId()));
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
291 writer.writeAttribute(XMLUtil.ROW_ID, defaultString(att.getRowId()));
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
292 writer.writeAttribute(XMLUtil.CONTENT_TYPE, defaultString(att.getContentType()));
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
293 writer.writeAttribute(XMLUtil.VERSION, defaultString(att.getVersion()));
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
294 writer.writeAttribute(XMLUtil.MODIFICATION_TIME, defaultString(att.getModificationTime()));
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
295 if (att.getUser() != null) {
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
296 writer.writeAttribute(XMLUtil.USER, att.getUser());
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
297 }
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
298 if (att.getIsPublic()) {
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
299 writer.writeAttribute(XMLUtil.PUBLIC, "true");
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
300 }
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
301
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
302 /*
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
303 * write value as content
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
304 */
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
305 if (StringUtils.isNotEmpty(att.getValue())) {
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
306 writer.writeCharacters(att.getValue());
75
e0be7c0030f5 cleanup and better comments.
casties
parents: 31
diff changeset
307 }
1
jurzua
parents:
diff changeset
308
29
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
309 writer.writeEndElement();
5786aa6caeb3 new XML export and test script.
casties
parents: 1
diff changeset
310 }
1
jurzua
parents:
diff changeset
311 }