Quick Search:

View

Revision:

Diff

Diff from 236 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 236 2010-07-29 21:49:00Z 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;
maesenka
236
35 import java.util.Collection;
maesenka
233
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
236
63         public void load(Collection<ClassInfomappedClassesString 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 classInfomappedClasses) {
76                         addTableElement(rootclassInfo);
maesenka
87
77                 }
78         }
79
maesenka
97
80         private void addTableElement(Element rootClassInfo 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(tableElidAttr);
87                 for (AttributeInfo ai : classInfo.getAttributes()) {
88                         if (!ai.isIdentifier()) {
89                                 addColElement(tableElai);
maesenka
87
90                         }
91                 }
maesenka
97
92
maesenka
87
93         }
94
95         private void addColElement(Element tableElAttributeInfo 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 }
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:33 +0200