Quick Search:

View

Revision:

Diff

Diff from 233 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/HibernateSpatial/trunk/hibernate-spatial/src/main/java/org/hibernatespatial/pojo/MappingsGenerator.java

Annotated File View

maesenka
97
1 /**
2  * $Id: MappingsGenerator.java 233 2010-07-27 20:45:14Z maesenka $
3  *
4  * This file is part of Hibernate Spatial, an extension to the 
5  * hibernate ORM solution for geographic data. 
6  *  
7  * Copyright © 2008 Geovise BVBA
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  * For more information, visit: http://www.hibernatespatial.org/
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;
35 import java.util.List;
36
maesenka
87
37 /**
38  * This class creates a Hibernate mapping file for a list of tables.
39  * 
40  * @author Karel Maesen, Geovise BVBA (http://www.geovise.com/)
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 writerthrows IOException {
53                 OutputFormat format = OutputFormat.createPrettyPrint();
54                 XMLWriter xmlWriter = new XMLWriter(writerformat);
55                 xmlWriter.write(this.mappingDoc);
56                 xmlWriter.close();
57         }
58
59         public Document getMappingsDoc() {
60                 return this.mappingDoc;
61         }
62
maesenka
233
63         public void load(List<ClassInfomappedClassesString schema)
maesenka
97
64                         throws PKeyNotFoundException {
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);
72         root.addAttribute("schema"schema);
maesenka
121
73                 for (ClassInfo classInfomappedClasses) {
74                         addTableElement(rootclassInfo);
maesenka
87
75                 }
76         }
77
maesenka
97
78         private void addTableElement(Element rootClassInfo classInfo)
79                         throws PKeyNotFoundException {
maesenka
87
80                 Element tableEl = root.addElement("class");
maesenka
121
81                 tableEl.addAttribute("name"classInfo.getClassName());
maesenka
97
82                 tableEl.addAttribute("table"classInfo.getTableName());
maesenka
87
83                 AttributeInfo idAttr = classInfo.getIdAttribute();
maesenka
97
84                 addColElement(tableElidAttr);
85                 for (AttributeInfo ai : classInfo.getAttributes()) {
86                         if (!ai.isIdentifier()) {
87                                 addColElement(tableElai);
maesenka
87
88                         }
89                 }
maesenka
97
90
maesenka
87
91         }
92
93         private void addColElement(Element tableElAttributeInfo ai) {
94                 Element colEl = null;
maesenka
97
95                 if (ai.isIdentifier()) {
96                         colEl = tableEl.addElement("id");
maesenka
87
97                 } else {
98                         colEl = tableEl.addElement("property");
99                 }
100                 colEl.addAttribute("name"ai.getFieldName());
101                 colEl.addAttribute("column"ai.getColumnName());
102                 colEl.addAttribute("type"ai.getHibernateType());
103                 return;
104         }
105 }
FishEye: Open Source License registered to Hibernate Spatial.
Your maintenance has expired. You can renew your license at http://www.atlassian.com/fisheye/renew
Atlassian FishEye, Subversion, CVS & Perforce analysis. (Version:1.5.2 Build:build-298 2008-05-26 ) - Administration - Page generated 2010-09-09 22:41 +0200