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.datatools.oracle;
028:
029: import java.sql.Connection;
030:
031: import oracle.jdbc.driver.OracleConnection;
032: import oracle.sdoapi.adapter.SDOGeometry;
033: import oracle.sdoapi.adapter.SDOTemplateFactory;
034: import oracle.sdoapi.adapter.SDOTemplateFactoryImpl;
035: import oracle.sdoapi.geom.Geometry;
036: import oracle.sdoapi.geom.GeometryFactory;
037: import oracle.sdoapi.sref.SRManager;
038: import oracle.sql.STRUCT;
039:
040: import org.apache.ojb.broker.util.batch.BatchConnection;
041:
042: import fr.ign.cogit.geoxygene.spatial.geomroot.GM_Object;
043:
044: /**
045: * Conversion dans les 2 sens entre une SDO_GEOMETRY (format sql.STRUCT) et un GM_Object.
046: * Ceci est utilise par OJB.
047: *
048: * @author Thierry Badard & Arnaud Braun
049: * @version 1.1
050: *
051: */
052:
053: public class GeomGeOxygene2Oracle {
054:
055: // Initialise au demarrage de la Geodatabase Oracle comme un singleton
056: public static Connection CONNECTION;
057: public static SRManager SRM;
058: public static GeometryFactory GF;
059:
060: public static Object sqlToJava(Object object) {
061: try {
062: Geometry sdoGeom = SDOGeometry.STRUCTtoGeometry(
063: (STRUCT) object, GF, SRM);
064: GM_Object isoGeom = IsoAndSdo.sdoapi2iso(sdoGeom);
065: return isoGeom;
066: } catch (Exception e) {
067: e.printStackTrace();
068: return null;
069: }
070: }
071:
072: /** Methode utilisee si on n'utilise PAS les classes maison "OxygenePresistenceBroker" et "GeOxygeneStatementManager".
073: * Ceci est a definir dans OJB.properties.
074: * Inconvenient : on utilise une variable connection statique,
075: * donc impossible de se connecter a plusieurs bases Oracle simultanement.
076: * De plus il y a un bug avec OJB : impossible d'ecrire des geometries nulles.
077: */
078: public static Object javaToSql(Object object) {
079: try {
080: Geometry sdoGeom = IsoAndSdo.iso2sdoapi(GF,
081: (GM_Object) object);
082: SDOTemplateFactory sdoTF;
083: if (CONNECTION instanceof BatchConnection) {// ceci est pour OJB
084: OracleConnection oConn = (OracleConnection) ((BatchConnection) CONNECTION)
085: .getDelegate();
086: sdoTF = new SDOTemplateFactoryImpl(oConn);
087: } else
088: sdoTF = new SDOTemplateFactoryImpl(
089: (OracleConnection) CONNECTION);
090:
091: STRUCT str = SDOGeometry.geometryToSTRUCT(sdoGeom, sdoTF);
092: return str;
093: } catch (Exception e) {
094: e.printStackTrace();
095: return null;
096: }
097: }
098:
099: /** Methode utilisee si on utilise les classes maison "GeOxygenePresistenceBroker" et "GeOxygeneStatementManager".
100: * Ceci est a definir dans OJB.properties.
101: * Ceci corrige les deux defauts de la methode qui ne passe pas de connection en parametre.
102: */
103: public static Object javaToSql(Object object, Connection conn) {
104: try {
105: Geometry sdoGeom = IsoAndSdo.iso2sdoapi(GF,
106: (GM_Object) object);
107: SDOTemplateFactory sdoTF;
108: if (conn instanceof BatchConnection) {// ceci est pour OJB
109: OracleConnection oConn = (OracleConnection) ((BatchConnection) conn)
110: .getDelegate();
111: sdoTF = new SDOTemplateFactoryImpl(oConn);
112: } else
113: sdoTF = new SDOTemplateFactoryImpl(
114: (OracleConnection) conn);
115:
116: STRUCT str = SDOGeometry.geometryToSTRUCT(sdoGeom, sdoTF);
117: return str;
118: } catch (Exception e) {
119: e.printStackTrace();
120: return null;
121: }
122: }
123:
124: }
|