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