geodatabaseojb.java
来自「用于GIS(全球地理系统)的分析和处理的代码。」· Java 代码 · 共 492 行 · 第 1/2 页
JAVA
492 行
/*
* This file is part of the GeOxygene project source files.
*
* GeOxygene aims at providing an open framework which implements OGC/ISO specifications for
* the development and deployment of geographic (GIS) applications. It is a open source
* contribution of the COGIT laboratory at the Institut G閛graphique National (the French
* National Mapping Agency).
*
* See: http://oxygene-project.sourceforge.net
*
* Copyright (C) 2005 Institut G閛graphique National
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this library (see file LICENSE if present); if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
package fr.ign.cogit.geoxygene.datatools.ojb;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.metadata.ClassDescriptor;
import org.apache.ojb.broker.metadata.DescriptorRepository;
import org.apache.ojb.broker.metadata.FieldDescriptor;
import org.apache.ojb.odmg.HasBroker;
import org.apache.ojb.odmg.OJB;
import org.odmg.DList;
import org.odmg.Database;
import org.odmg.Implementation;
import org.odmg.OQLQuery;
import org.odmg.Transaction;
import fr.ign.cogit.geoxygene.datatools.Metadata;
import fr.ign.cogit.geoxygene.feature.FT_Feature;
import fr.ign.cogit.geoxygene.feature.FT_FeatureCollection;
/**
* Implementation d'une Geodatabase utilisant OJB comme mappeur.
* On utilise la partie ODMG de OJB.
* Ne pas utiliser directement :
* classe a specialiser en fonction du SGBD geographique utilise.
* Attention pour les entiers :
* pour Oracle, caster en BigDecimal,
* pour Postgis caster en Long ...
*
* @author Thierry Badard & Arnaud Braun
* @version 1.1
*/
public class GeodatabaseOjb {
/////////////////////////////////////////////////////////////////////////////////////////
///// attributs /////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
protected Connection _conn; // connection JDBC
protected Implementation _odmg; // implementation ODMG
protected Database _db; // interaction avec une base ODMG
protected Transaction _tx; // represente une transaction
protected List _metadataList; // liste des metadonnnees pour les classes persistantes.
/////////////////////////////////////////////////////////////////////////////////////////
///// constructeur //////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/** Constructeur.
* @param jcdAlias : l'alias de connection dans repository_database.xml */
GeodatabaseOjb(String jcdAlias) {
initODMG(jcdAlias);
initConnection();
initMetadata();
}
/** Constructeur avec la connection par defaut dans repository_database.xml */
protected GeodatabaseOjb () {
this (null);
}
/////////////////////////////////////////////////////////////////////////////////////
/// initialisation des attributs ////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
/** Initialise la base ODMG et une transaction */
protected void initODMG (String jcdAlias) {
try {
_odmg = OJB.getInstance();
_db = _odmg.newDatabase();
if (jcdAlias != null)
_db.open(jcdAlias, Database.OPEN_READ_WRITE) ;
else
_db.open(null, Database.OPEN_READ_WRITE) ;
_tx = _odmg.newTransaction();
} catch ( Exception except ) {
System.err.println(" ### PROBLEME A LA LECTURE DES FICHIERS DE MAPPING OJB ... ### ");
System.err.println(" ### PROGRAMME ARRETE ! ### ");
System.err.println("");
except.printStackTrace( );
System.exit(0);
}
}
/** Initialise la connection JDBC. */
protected void initConnection() {
try {
_tx.begin();
PersistenceBroker broker = ((HasBroker) _tx).getBroker();
_conn = broker.serviceConnectionManager().getConnection();
_tx.commit();
} catch(Exception e) {
e.printStackTrace();
}
}
/** Renseigne l'attribut _metadataList. */
protected void initMetadata() {
try {
_tx.begin();
PersistenceBroker broker = ((HasBroker) _tx).getBroker();
DescriptorRepository desc = broker.getDescriptorRepository();
Iterator enDesc = desc.getDescriptorTable().values().iterator();
_metadataList = new ArrayList();
while (enDesc.hasNext()) {
ClassDescriptor cd = (ClassDescriptor) enDesc.next();
String className = (cd.getClassNameOfObject());
if (! (className.equals("org.apache.ojb.broker.util.sequence.HighLowSequence")
|| className.equals("org.apache.ojb.odmg.collections.DListImpl_2")
|| className.equals("org.apache.ojb.odmg.collections.DListEntry_2")
|| className.equals("org.apache.ojb.odmg.collections.DListImpl")
|| className.equals("org.apache.ojb.odmg.collections.DListEntry") )) {
Metadata metadataElt = new Metadata();
metadataElt.setClassName(className);
metadataElt.setTableName(cd.getFullTableName());
FieldDescriptor[] fdPK = cd.getPkFields();
if (fdPK.length == 0) {
System.out.println("WARNING - classe sans identifiant : "+cd.getClassNameOfObject());
continue;
}
if (fdPK.length > 1) {
if (cd.getClassNameOfObject().compareToIgnoreCase("org.apache.ojb.broker.util.sequence.HighLowSequence") != 0)
System.out.println("WARNING - cle primaire composee : "+cd.getClassNameOfObject());
continue;
}
metadataElt.setIdColumnName(fdPK[0].getColumnName());
metadataElt.setIdFieldName(fdPK[0].getAttributeName());
_metadataList.add(metadataElt);
}
}
_tx.commit();
} catch (Exception e) {
System.err.println(" ### PROBLEME A LA LECTURE DES FICHIERS DE MAPPING OJB ... ### ");
System.err.println(" ### PROGRAMME ARRETE ! ### ");
System.err.println("");
e.printStackTrace( );
System.exit(0);
}
}
/////////////////////////////////////////////////////////////////////////////////////////
///// gestion des transactions //////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/** Ouvre une transaction. */
public void begin() {
try {
_tx.begin();
} catch (Exception e) {
e.printStackTrace();
}
}
/** Commit la transaction sans la fermer. */
public void checkpoint() {
try {
_tx.checkpoint();
} catch (Exception e) {
e.printStackTrace();
}
}
/** Commite et ferme la transaction. */
public void commit() {
try {
_tx.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
/** Annule et ferme la transaction. */
public void abort() {
try {
_tx.abort();
} catch (Exception e) {
e.printStackTrace();
}
}
/** Renvoie true si la transaction est active. */
public boolean isOpen() {
return _tx.isOpen();
}
/** Ferme la connection (libere les ressources). */
public void close() {
try {
_db.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/** Vide le cache de la transaction.
A appeler a l'interieur d'une transaction ouverte. */
public void clearCache() {
PersistenceBroker broker = ((HasBroker) _tx).getBroker();
broker.clearCache();
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?