annotate src/main/java/org/mpi/openmind/repository/utils/OM4StreamWriter.java @ 77:a59984fd3c3f

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