maesenka
|
97
|
1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
maesenka
|
87
|
25
|
package org.hibernatespatial.pojo;
|
|
26
|
|
|
27
|
import org.dom4j.Document;
|
|
28
|
import org.dom4j.DocumentHelper;
|
|
29
|
import org.dom4j.Element;
|
|
30
|
import org.dom4j.io.OutputFormat;
|
|
31
|
import org.dom4j.io.XMLWriter;
|
|
32
|
|
maesenka
|
233
|
33
|
import java.io.IOException;
|
|
34
|
import java.io.Writer;
|
maesenka
|
236
|
35
|
import java.util.Collection;
|
maesenka
|
233
|
36
|
|
maesenka
|
87
|
37
|
|
|
38
|
|
|
39
|
|
|
40
|
|
|
41
|
|
|
42
|
public class MappingsGenerator {
|
|
43
|
|
|
44
|
private Document mappingDoc;
|
maesenka
|
97
|
45
|
|
maesenka
|
87
|
46
|
private String packageName;
|
maesenka
|
97
|
47
|
|
|
48
|
public MappingsGenerator(String packageName) {
|
maesenka
|
87
|
49
|
this.packageName = packageName;
|
|
50
|
}
|
maesenka
|
97
|
51
|
|
maesenka
|
87
|
52
|
public void write(Writer writer) throws IOException {
|
|
53
|
OutputFormat format = OutputFormat.createPrettyPrint();
|
|
54
|
XMLWriter xmlWriter = new XMLWriter(writer, format);
|
|
55
|
xmlWriter.write(this.mappingDoc);
|
|
56
|
xmlWriter.close();
|
|
57
|
}
|
|
58
|
|
|
59
|
public Document getMappingsDoc() {
|
|
60
|
return this.mappingDoc;
|
|
61
|
}
|
|
62
|
|
maesenka
|
236
|
63
|
public void load(Collection<ClassInfo> mappedClasses, String schema)
|
maesenka
|
235
|
64
|
throws MissingIdentifierException {
|
maesenka
|
87
|
65
|
|
|
66
|
this.mappingDoc = DocumentHelper.createDocument();
|
maesenka
|
97
|
67
|
this.mappingDoc.addDocType("hibernate-mapping",
|
|
68
|
"-//Hibernate/Hibernate Mapping DTD 3.0//EN",
|
maesenka
|
87
|
69
|
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd");
|
maesenka
|
233
|
70
|
Element root = this.mappingDoc.addElement("hibernate-mapping");
|
|
71
|
root.addAttribute("package", this.packageName);
|
maesenka
|
234
|
72
|
if (schema != null){
|
|
73
|
root.addAttribute("schema", schema);
|
|
74
|
}
|
maesenka
|
121
|
75
|
for (ClassInfo classInfo: mappedClasses) {
|
|
76
|
addTableElement(root, classInfo);
|
maesenka
|
87
|
77
|
}
|
|
78
|
}
|
|
79
|
|
maesenka
|
97
|
80
|
private void addTableElement(Element root, ClassInfo classInfo)
|
maesenka
|
235
|
81
|
throws MissingIdentifierException {
|
maesenka
|
87
|
82
|
Element tableEl = root.addElement("class");
|
maesenka
|
121
|
83
|
tableEl.addAttribute("name", classInfo.getClassName());
|
maesenka
|
97
|
84
|
tableEl.addAttribute("table", classInfo.getTableName());
|
maesenka
|
87
|
85
|
AttributeInfo idAttr = classInfo.getIdAttribute();
|
maesenka
|
97
|
86
|
addColElement(tableEl, idAttr);
|
|
87
|
for (AttributeInfo ai : classInfo.getAttributes()) {
|
|
88
|
if (!ai.isIdentifier()) {
|
|
89
|
addColElement(tableEl, ai);
|
maesenka
|
87
|
90
|
}
|
|
91
|
}
|
maesenka
|
97
|
92
|
|
maesenka
|
87
|
93
|
}
|
|
94
|
|
|
95
|
private void addColElement(Element tableEl, AttributeInfo ai) {
|
|
96
|
Element colEl = null;
|
maesenka
|
97
|
97
|
if (ai.isIdentifier()) {
|
|
98
|
colEl = tableEl.addElement("id");
|
maesenka
|
87
|
99
|
} else {
|
|
100
|
colEl = tableEl.addElement("property");
|
|
101
|
}
|
|
102
|
colEl.addAttribute("name", ai.getFieldName());
|
|
103
|
colEl.addAttribute("column", ai.getColumnName());
|
|
104
|
colEl.addAttribute("type", ai.getHibernateType());
|
|
105
|
return;
|
|
106
|
}
|
|
107
|
}
|