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