001: /*
002: * This file is part of the GeOxygene project source files.
003: *
004: * GeOxygene aims at providing an open framework which implements OGC/ISO specifications for
005: * the development and deployment of geographic (GIS) applications. It is a open source
006: * contribution of the COGIT laboratory at the Institut G�ographique National (the French
007: * National Mapping Agency).
008: *
009: * See: http://oxygene-project.sourceforge.net
010: *
011: * Copyright (C) 2005 Institut G�ographique National
012: *
013: * This library is free software; you can redistribute it and/or modify it under the terms
014: * of the GNU Lesser General Public License as published by the Free Software Foundation;
015: * either version 2.1 of the License, or any later version.
016: *
017: * This library is distributed in the hope that it will be useful, but WITHOUT ANY
018: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
019: * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser General Public License along with
022: * this library (see file LICENSE if present); if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: *
025: */
026:
027: package fr.ign.cogit.geoxygene.util.loader;
028:
029: import java.io.File;
030: import java.io.FileWriter;
031:
032: import fr.ign.cogit.geoxygene.datatools.Geodatabase;
033:
034: /**
035: * Usage interne.
036: * Generateur de fichier XML pour OJB.
037: *
038: *
039: * @author Thierry Badard & Arnaud Braun
040: * @version 1.1
041: *
042: */
043:
044: class OjbXMLGenerator {
045:
046: private FileWriter fw;
047: private String mappingFilePath; //GEOXYGENE_MAPPING/repository_geo.xml
048: private String extentMappingFilePath = null; //GEOXYGENE_MAPPING/repository_feature.xml
049: private String plateformePath;
050: private int i = 1; // identifiant du field dans le fichier de mapping
051: private String extentClassName;
052:
053: private String mappingString = ""; // chaine de caractere qu'on va ecrire dans le fichier de mapping
054: private String classeMereString = "";// chaine de caractere qu'on va ecrire dans le fichier de mapping de la classe mere
055:
056: private Geodatabase data;
057:
058: private String keyColumnName;
059: private final static String KEY_COLUMN_NAME_ORACLE = "COGITID";
060: private final static String KEY_COLUMN_NAME_POSTGIS = "COGITID";
061:
062: OjbXMLGenerator(Geodatabase Data, String path,
063: String mappingFileName, String ExtentClassName,
064: String extentMappingFileName) {
065: try {
066: data = Data;
067: plateformePath = path;
068: File thePath = new File(path);
069: File mappingFile = new File(thePath, mappingFileName);
070: mappingFilePath = mappingFile.getPath();
071: extentClassName = ExtentClassName;
072: if (extentMappingFileName != null) {
073: File extentMappingFile = new File(thePath,
074: extentMappingFileName);
075: extentMappingFilePath = extentMappingFile.getPath();
076: }
077: if (data.getDBMS() == Geodatabase.ORACLE)
078: keyColumnName = KEY_COLUMN_NAME_ORACLE;
079: else if (data.getDBMS() == Geodatabase.POSTGIS)
080: keyColumnName = KEY_COLUMN_NAME_POSTGIS;
081: } catch (Exception e) {
082: e.printStackTrace();
083: }
084: }
085:
086: void writeFileHeader() {
087: // fichier de mapping des classes filles
088: String line1 = "<!-- fichier de mapping OJB pour GeOxygene : classes geographiques -->\n";
089: String line2 = "<!-- fichier genere automatiquement par le chargeur de la plate-forme GeOxygene -->\n";
090: String line3 = "\n";
091:
092: // fichier de mapping classe mere
093: if (extentMappingFilePath != null) {
094: String line4 = "<class-descriptor class=\""
095: + extentClassName + "\" >\n";
096: classeMereString = line1 + line2 + line3 + line4;
097: }
098: }
099:
100: void writeFileBottom() {
101: // fichier de mapping de la classe mere
102: if (extentMappingFilePath != null) {
103: String line0 = "</class-descriptor>\n";
104: String line1 = "\n";
105: classeMereString += line0 + line1;
106: }
107: }
108:
109: void writeClassHeader(String className, String tableName) {
110: // fichier de mapping des classes filles
111: i = 1; // remise a 1 du compteur
112: String str1 = "<class-descriptor class=\"" + className
113: + "\" table=\"" + tableName + "\" >\n";
114: String str2 = " <field-descriptor name=\"id\" column=\""
115: + keyColumnName
116: + "\" jdbc-type=\"INTEGER\" primarykey=\"true\" autoincrement=\"true\"/>\n";
117: mappingString += str1 + str2;
118:
119: // fichier de mapping de la classe mere
120: if (extentMappingFilePath != null) {
121: str1 = " <extent-class class-ref=\"" + className
122: + "\" />\n";
123: classeMereString += str1;
124: }
125: }
126:
127: void writeClassBottom() {
128: // fichier de mapping des classes filles
129: String line0 = "</class-descriptor>\n";
130: String line1 = "\n";
131: mappingString += line0 + line1;
132: }
133:
134: void writeField(String javaName, String sqlName, String sqlType) {
135: try {
136: i++;
137: String str1 = " <field-descriptor name=\"" + javaName
138: + "\" column=\"" + sqlName + "\" jdbc-type=\""
139: + getJdbcType(sqlType) + "\" />\n";
140: mappingString += str1;
141: } catch (Exception e) {
142: System.out.println(e.getMessage());
143: }
144: }
145:
146: void writeInFile() {
147: try {
148: if (extentMappingFilePath != null) {
149: if (extentMappingFilePath.equals(mappingFilePath)) {
150: fw = new FileWriter(mappingFilePath);
151: fw.write(classeMereString + "\n" + mappingString);
152: fw.close();
153: } else {
154: fw = new FileWriter(extentMappingFilePath);
155: fw.write(classeMereString);
156: fw.close();
157: fw = new FileWriter(mappingFilePath);
158: fw.write(mappingString);
159: fw.close();
160: }
161: } else {
162: fw = new FileWriter(mappingFilePath);
163: fw.write(mappingString);
164: fw.close();
165: }
166: } catch (Exception e) {
167: e.printStackTrace();
168: }
169: }
170:
171: ///////////////////////////////////////////////////////////////////////////////////////////////////////////
172: ///////////////////////////////////////////////////////////////////////////////////////////////////////////
173: private String getJdbcType(String sqlType) {
174: try {
175: if (data.getDBMS() == Geodatabase.ORACLE)
176: return oracleType2JdbcType(sqlType);
177: else if (data.getDBMS() == Geodatabase.POSTGIS)
178: return postgisType2JdbcType(sqlType);
179: return null;
180: } catch (Exception e) {
181: e.printStackTrace();
182: return null;
183: }
184: }
185:
186: ///////////////////////////////////////////////////////////////////////////////////////////////////////////
187: ///////////////////////////////////////////////////////////////////////////////////////////////////////////
188: private String oracleType2JdbcType(String oracle) throws Exception {
189: if (oracle.compareToIgnoreCase("VARCHAR2") == 0)
190: return "VARCHAR";
191: else if (oracle.compareToIgnoreCase("VARCHAR") == 0)
192: return "VARCHAR";
193: else if (oracle.compareToIgnoreCase("CHAR") == 0)
194: return "VARCHAR";
195: else if (oracle.compareToIgnoreCase("NUMBER") == 0)
196: return "DOUBLE";
197: else if (oracle.compareToIgnoreCase("FLOAT") == 0)
198: return "DOUBLE";
199: else if (oracle.compareToIgnoreCase("INTEGER") == 0)
200: return "INTEGER";
201: else if (oracle.compareToIgnoreCase("BOOLEAN") == 0)
202: return "BIT";
203: else if (oracle.compareToIgnoreCase("SDO_GEOMETRY") == 0)
204: return "STRUCT\" conversion=\"fr.ign.cogit.geoxygene.datatools.ojb.GeomGeOxygene2Dbms";
205: else
206: throw new Exception("type non reconnu : " + oracle);
207: }
208:
209: ///////////////////////////////////////////////////////////////////////////////////////////////////////////
210: ///////////////////////////////////////////////////////////////////////////////////////////////////////////
211: private String postgisType2JdbcType(String postgis)
212: throws Exception {
213: if (postgis.compareToIgnoreCase("varchar") == 0)
214: return "VARCHAR";
215: else if (postgis.compareToIgnoreCase("bpchar") == 0)
216: return "VARCHAR";
217: else if (postgis.compareToIgnoreCase("float8") == 0)
218: return "DOUBLE";
219: else if (postgis.compareToIgnoreCase("float4") == 0)
220: return "FLOAT";
221: else if (postgis.compareToIgnoreCase("int4") == 0)
222: return "INTEGER";
223: else if (postgis.compareToIgnoreCase("int8") == 0)
224: return "BIGINT";
225: else if (postgis.compareToIgnoreCase("bool") == 0)
226: return "BIT";
227: else if (postgis.compareToIgnoreCase("geometry") == 0)
228: return "STRUCT\" conversion=\"fr.ign.cogit.geoxygene.datatools.ojb.GeomGeOxygene2Dbms";
229: else
230: throw new Exception("type non reconnu : " + postgis);
231: }
232:
233: }
|