Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / DefaultDBDriver.java @ 3305

History | View | Annotate | Download (20 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap.drivers;
42

    
43
import java.awt.geom.Rectangle2D;
44
import java.io.IOException;
45
import java.sql.Connection;
46
import java.sql.DatabaseMetaData;
47
import java.sql.Driver;
48
import java.sql.DriverManager;
49
import java.sql.ResultSet;
50
import java.sql.ResultSetMetaData;
51
import java.sql.SQLException;
52
import java.sql.Statement;
53
import java.sql.Types;
54
import java.util.Hashtable;
55
import java.util.StringTokenizer;
56

    
57
import org.apache.log4j.Logger;
58

    
59
import com.hardcode.gdbms.engine.data.DataSourceFactory;
60
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
61
import com.hardcode.gdbms.engine.values.IntValue;
62
import com.hardcode.gdbms.engine.values.Value;
63
import com.hardcode.gdbms.engine.values.ValueFactory;
64
import com.iver.cit.gvsig.fmap.DriverException;
65
import com.iver.cit.gvsig.fmap.Messages;
66
import com.iver.cit.gvsig.fmap.core.FShape;
67
import com.iver.cit.gvsig.fmap.core.IFeature;
68
import com.iver.cit.gvsig.fmap.core.IGeometry;
69
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
70
import com.iver.cit.gvsig.fmap.layers.XMLException;
71
import com.iver.utiles.XMLEntity;
72
import com.iver.utiles.swing.JPasswordDlg;
73

    
74

    
75

    
76
/**
77
 * Clase abstracta qu
78
 */
79
public abstract class DefaultDBDriver implements VectorialJDBCDriver, ObjectDriver {    
80
    private static Logger logger = Logger.getLogger(SelectableDataSource.class.getName());
81
    private static Hashtable poolPassw = new Hashtable();
82
    
83
    protected Connection conn;
84
    // protected String tableName;
85
    // protected String whereClause;
86
    // protected String fields;
87
    // protected String sqlOrig;
88
    private DBLayerDefinition lyrDef = null;
89
    protected ResultSet rs;
90
    protected boolean bCursorActivo = false;
91
    protected Statement st;
92
    protected int numReg=-1;
93
    
94
    private Rectangle2D fullExtent = null;
95
    
96
    // protected String strFID_FieldName;
97
    // protected String idFID_FieldName;
98
       
99
    protected Hashtable hashRelate;
100
    
101
    
102
    protected ResultSetMetaData metaData = null;
103
    protected Rectangle2D workingArea;
104
    
105
    abstract public void setData(Connection conn, DBLayerDefinition lyrDef);
106

    
107
        /**
108
         * @return devuelve la Conexi?n a la base de datos, para que 
109
         * el usuario pueda hacer la consulta que quiera, si lo desea.
110
         * Por ejemplo, esto puede ser ?til para abrir un cuadro de dialogo
111
         * avanazado y lanzar peticiones del tipo "Devuelveme un buffer
112
         * a las autopistas", y con el resultset que te venga, escribir
113
         * un shape, o cosas as?.
114
         */
115
        public Connection getConnection()
116
        {
117
            return conn;
118
        }
119
        public String[] getFields()
120
        {
121
        /* StringTokenizer tokenizer = new StringTokenizer(fields, ",");
122
        String[] arrayFields = new String[tokenizer.countTokens()];
123
        int i=0;
124
        while (tokenizer.hasMoreTokens())
125
        {
126
            arrayFields[i] = tokenizer.nextToken();
127
            i++;
128
        }
129
            return arrayFields; */
130
        return lyrDef.getFieldNames();
131
                    
132
        }
133
    /**
134
     * First, the geometry field. After, the rest of fields
135
     * @return
136
     */
137
    public String getTotalFields()
138
    {
139
        String strAux = getGeometryField(getLyrDef().getFieldGeometry());
140
        String[] fieldNames = getLyrDef().getFieldNames();
141
        for (int i=0; i< fieldNames.length; i++)
142
        {
143
            strAux = strAux + ", " + fieldNames[i];
144
        }
145
        return strAux;
146
    }
147
    
148
        public String getWhereClause()
149
        {
150
            return lyrDef.getWhereClause();
151
        }
152
        public String getTableName()
153
        {
154
            return lyrDef.getTableName();
155
        }
156
        
157

    
158
        /**
159
         * @throws DriverIOException
160
         * @throws DriverException
161
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeCount()
162
         */
163
        public int getShapeCount() throws IOException {
164
                    if (numReg == -1)
165
                    {
166
                        try
167
                    {
168
                            Statement s = conn.createStatement();                    
169
                            ResultSet r = s.executeQuery("SELECT COUNT(*) AS NUMREG FROM " + lyrDef.getTableName() + " " + getCompleteWhere());
170
                            r.next();
171
                            numReg = r.getInt(1);
172
                            System.err.println("numReg = " + numReg);
173
                    }
174
                        catch (SQLException e)
175
                        {
176
                            throw new IOException(e.getMessage());
177
                        }
178
                    }
179
                    
180
            return numReg;
181
        }
182

    
183
    /**
184
     * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
185
     */
186
    public Rectangle2D getFullExtent(){
187
        // Por defecto recorremos todas las geometrias.
188
        // Las bases de datos como PostGIS pueden y deben
189
        // sobreescribir este m?todo.
190
        if (fullExtent == null)
191
        {
192
            try
193
            {
194
                IFeatureIterator itGeom = getFeatureIterator("SELECT " +  
195
                        getGeometryField(getLyrDef().getFieldGeometry()) + " FROM " + 
196
                        getLyrDef().getTableName() +  " " + getCompleteWhere());
197
                IGeometry geom;
198
                int cont = 0;
199
                while (itGeom.hasNext())
200
                {
201
                    geom = itGeom.next().getGeometry();
202
                    if (cont==0)
203
                        fullExtent = geom.getBounds2D();
204
                    else
205
                        fullExtent.add(geom.getBounds2D());
206
                    cont++;
207
                }
208
            }
209
            catch (SQLException e)
210
            {
211
                System.err.println(e.getMessage());
212
            } catch (DriverException e) {
213
                // TODO Auto-generated catch block
214
                e.printStackTrace();
215
            }
216
            
217
        }
218
        return fullExtent;
219
    }
220

    
221

    
222
        /**
223
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
224
         */
225
        public int getShapeType() {
226
        IGeometry geom;
227
        int result = FShape.MULTI;
228
        try {
229
            geom = getShape(0);            
230
            if (geom != null)
231
                result = geom.getGeometryType();             
232
        } catch (IOException e) {
233
            e.printStackTrace();
234
        }
235
        return result;
236
        }
237
        
238
        public int getFieldType(int idField) throws com.hardcode.gdbms.engine.data.driver.DriverException
239
        {
240
            String str = "";
241
            try {
242
                int i = idField + 2; // idField viene basado en 1, y
243
                                        // adem?s nos saltamos el campo de geometry
244
                str = metaData.getColumnClassName(i);
245
            if (metaData.getColumnType(i) == Types.VARCHAR)
246
                return Types.VARCHAR;
247
                    if (metaData.getColumnType(i) == Types.FLOAT)
248
                        return Types.FLOAT;
249
                    if (metaData.getColumnType(i) == Types.DOUBLE)
250
                        return Types.DOUBLE;
251
                    if (metaData.getColumnType(i) == Types.INTEGER)
252
                        return Types.INTEGER;
253
                    if (metaData.getColumnType(i) == Types.BIGINT)
254
                        return Types.BIGINT;
255
                    if (metaData.getColumnType(i) == Types.BIT)
256
                        return Types.BIT;
257
                    if (metaData.getColumnType(i) == Types.DATE)
258
                        return Types.DATE;
259
            if (metaData.getColumnType(i) == Types.DECIMAL)
260
                return Types.DOUBLE;
261
            if (metaData.getColumnType(i) == Types.NUMERIC)
262
                return Types.DOUBLE;
263
            
264
            } catch (SQLException e) {
265
                    throw new com.hardcode.gdbms.engine.data.driver.DriverException(e);
266
            }
267
        throw new com.hardcode.gdbms.engine.data.driver.DriverException("Tipo no soportado: " + str);
268
        }
269
    /**
270
     * Obtiene el valor que se encuentra en la fila y columna indicada
271
     * Esta es la implementaci?n por defecto. Si lo del absolute
272
     * no va bien, como es el caso del PostGis, el driver lo
273
     * tiene que reimplementar
274
     *
275
     * @param rowIndex fila
276
     * @param fieldId columna
277
     *
278
     * @return subclase de Value con el valor del origen de datos
279
     *
280
     * @throws DriverException Si se produce un error accediendo al DataSource
281
     */
282
    public Value getFieldValue(long rowIndex, int idField)
283
        throws com.hardcode.gdbms.engine.data.driver.DriverException
284
        {
285
                int i = (int) (rowIndex + 1);
286
                int fieldId = idField+2;
287
                try {
288
                    rs.absolute(i);
289
                if (metaData.getColumnType(fieldId) == Types.VARCHAR)
290
                {
291
                    String strAux = rs.getString(fieldId);
292
                    if (strAux == null) strAux = "";
293
                    return ValueFactory.createValue(strAux);
294
                }
295
                        if (metaData.getColumnType(fieldId) == Types.FLOAT)
296
                            return ValueFactory.createValue(rs.getFloat(fieldId));
297
                        if (metaData.getColumnType(fieldId) == Types.DOUBLE)
298
                            return ValueFactory.createValue(rs.getDouble(fieldId));
299
                        if (metaData.getColumnType(fieldId) == Types.INTEGER)
300
                            return ValueFactory.createValue(rs.getInt(fieldId));
301
                        if (metaData.getColumnType(fieldId) == Types.BIGINT)
302
                            return ValueFactory.createValue(rs.getLong(fieldId));
303
                        if (metaData.getColumnType(fieldId) == Types.BIT)
304
                            return ValueFactory.createValue(rs.getBoolean(fieldId));
305
                        if (metaData.getColumnType(fieldId) == Types.DATE)
306
                            return ValueFactory.createValue(rs.getDate(fieldId));
307
                } catch (SQLException e) {
308
                throw new com.hardcode.gdbms.engine.data.driver.DriverException("Tipo no soportado: columna " + fieldId );
309
                }
310
                return null;
311
                
312
                
313
        }
314

    
315
    /**
316
     * Obtiene el n?mero de campos del DataSource
317
     *
318
     * @return
319
     *
320
     * @throws DriverException Si se produce alg?n error accediendo al
321
     *         DataSource
322
     */
323
    public int getFieldCount() throws com.hardcode.gdbms.engine.data.driver.DriverException
324
    {
325
        try {
326
            // Suponemos que el primer campo es el de las geometries, y no lo
327
            // contamos
328
            return rs.getMetaData().getColumnCount()-1;
329
        } catch (SQLException e) {
330
            throw new com.hardcode.gdbms.engine.data.driver.DriverException(e);
331
        }
332
        
333
    }
334

    
335
    /**
336
     * Devuelve el nombre del campo fieldId-?simo
337
     *
338
     * @param fieldId ?ndice del campo cuyo nombre se quiere obtener
339
     *
340
     * @return
341
     * @throws com.hardcode.gdbms.engine.data.driver.DriverException
342
     *
343
     * @throws DriverException Si se produce alg?n error accediendo al
344
     *         DataSource
345
     */
346
    public String getFieldName(int fieldId) throws com.hardcode.gdbms.engine.data.driver.DriverException
347
    {
348
        try {
349
            return rs.getMetaData().getColumnName(fieldId+2);
350
        } catch (SQLException e) {
351
            throw new com.hardcode.gdbms.engine.data.driver.DriverException(e);
352
        }
353
    }
354

    
355
    /**
356
     * Obtiene el n?mero de registros del DataSource
357
     *
358
     * @return
359
     *
360
     * @throws DriverException Si se produce alg?n error accediendo al
361
     *         DataSource
362
     */
363
    public long getRowCount()
364
    {
365
        try {
366
            return getShapeCount();
367
        } catch (IOException e) {
368
            // TODO Auto-generated catch block
369
            e.printStackTrace();
370
        }
371
        return -1;
372
    }
373

    
374
    public void close()
375
    {
376
    }
377
    
378
    /**
379
     * Recorre el recordset creando una tabla Hash que usaremos para 
380
     * relacionar el n?mero de un registro con su identificador ?nico.
381
     * Debe ser llamado en el setData justo despu?s de crear el recorset
382
     * principal
383
     * @throws SQLException 
384
     */
385
    protected void doRelateID_FID() throws SQLException
386
    {
387
        hashRelate = new Hashtable();
388
        
389
        
390
        String strSQL = "SELECT " + getLyrDef().getFieldID() + " FROM " + getLyrDef().getTableName()
391
        + " " + getCompleteWhere() + " ORDER BY " + getLyrDef().getFieldID();
392
        Statement s = getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
393
        ResultSet r = s.executeQuery(strSQL);
394
        int id=0;
395
        int gid;            
396
        int index = 0;
397
        while (r.next())
398
        {
399
            String aux = r.getString(1);
400
            Value val = ValueFactory.createValue(aux);
401
            hashRelate.put(val, new Integer(index));
402
            // System.out.println("ASOCIANDO CLAVE " + aux + " CON VALOR " + index);
403
            index++;
404
        }
405
        numReg = index;
406
        r.close();
407
        // rs.beforeFirst();
408
          
409
    }
410
    
411
    /* (non-Javadoc)
412
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getRowIndexByFID(java.lang.Object)
413
     */
414
    public int getRowIndexByFID(IFeature FID)
415
    {
416
        int resul;
417
        // Object obj = FID.getAttribute(lyrDef.getIdFieldID());
418
        String theId = FID.getID();
419
        Value aux = ValueFactory.createValue(theId);
420
        // System.err.println("Mirando si existe " + obj.toString());
421
        Integer rowIndex = (Integer) hashRelate.get(aux);
422
        resul = rowIndex.intValue();
423
        // System.err.println("Row asociada a " + obj.toString() + ":" + resul);
424
        return resul;
425
    }
426
    
427
    /* (non-Javadoc)
428
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#setXMLEntity(com.iver.utiles.XMLEntity)
429
     */
430
    public void setXMLEntity(XMLEntity xml) throws XMLException
431
    {
432
        String className = xml.getStringProperty("className");
433
        String dbUrl = xml.getStringProperty("dbURL");
434
        String catalogName = xml.getStringProperty("catalog");
435
        String userName =xml.getStringProperty("username");
436
        String driverClass =xml.getStringProperty("driverclass");     
437
        String tableName = xml.getStringProperty("tablename");
438
        String[] fields = xml.getStringArrayProperty("fields");
439
        String FIDfield = xml.getStringProperty("FID");
440
        String geometryField = xml.getStringProperty("THE_GEOM");        
441
        String whereClause = xml.getStringProperty("whereclause");
442
        String strSRID = xml.getStringProperty("SRID");
443
        if (xml.contains("minXworkArea"))
444
        {
445
            double x = xml.getDoubleProperty("minXworkArea");
446
            double y = xml.getDoubleProperty("minYworkArea");
447
            double H = xml.getDoubleProperty("HworkArea");
448
            double W = xml.getDoubleProperty("WworkArea");
449
            workingArea = new Rectangle2D.Double(x,y,W,H);
450
        }
451

    
452
        try {            
453
            Class.forName(driverClass);
454
            
455
            String keyPool = dbUrl + "_" + userName;
456
            Connection newConn = null;
457
            String clave = null;
458
            if (!poolPassw.containsKey(keyPool))
459
            {
460
                JPasswordDlg dlg = new JPasswordDlg();
461
                String strMessage = Messages.getString("conectar_jdbc");
462
                String strPassword = Messages.getString("password");
463
                dlg.setMessage(strMessage + " " + dbUrl + ". " + strPassword + "?");
464
                dlg.show();
465
                clave = dlg.getPassword();
466
                if (clave == null)
467
                    return;
468
                poolPassw.put(keyPool, clave);                    
469
            }
470
            else
471
            {
472
                clave = (String) poolPassw.get(keyPool);
473
            }
474
            newConn = DriverManager.getConnection(dbUrl, userName, clave);
475
            newConn.setAutoCommit(false);
476
            
477
            DBLayerDefinition lyrDef = new DBLayerDefinition();
478
            lyrDef.setCatalogName(catalogName);
479
            lyrDef.setTableName(tableName);
480
            lyrDef.setFieldNames(fields);
481
            lyrDef.setFieldID(FIDfield);
482
            lyrDef.setFieldGeometry(geometryField);
483
            lyrDef.setWhereClause(whereClause);
484
            // lyrDef.setClassToInstantiate(driverClass);
485
            if (workingArea != null)
486
                lyrDef.setWorkingArea(workingArea);
487
            
488
            lyrDef.setSRID_EPSG(strSRID);
489
            
490
            
491
            setData(newConn, lyrDef);
492
        } catch (ClassNotFoundException e) {
493
            logger.debug(e);
494
            throw new XMLException(e); 
495
        } catch (SQLException e) {
496
            logger.debug(e);
497
            throw new XMLException(e);
498
        }
499
            
500
    }
501
    /* (non-Javadoc)
502
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getXMLEntity()
503
     */
504
    public XMLEntity getXMLEntity()
505
    {       
506
        XMLEntity xml = new XMLEntity();
507
        xml.putProperty("className",this.getClass().getName());
508
        try {
509
            DatabaseMetaData metadata = getConnection().getMetaData();
510
            xml.putProperty("dbURL", metadata.getURL());
511
            xml.putProperty("catalog", getLyrDef().getCatalogName());
512
            // TODO: NO DEBEMOS GUARDAR EL NOMBRE DE USUARIO Y CONTRASE?A
513
            // AQUI. Hay que utilizar un pool de conexiones
514
            // y pedir al usuario que conecte a la base de datos
515
            // en la primera capa. En el resto, usar la conexi?n
516
            // creada con anterioridad.
517
            String userName = metadata.getUserName();
518
            int aux = userName.indexOf("@");
519
            if (aux != -1)
520
                userName = userName.substring(0,aux);
521
            xml.putProperty("username", userName);
522
            
523
            Driver drv = DriverManager.getDriver(metadata.getURL());
524
            // System.out.println(drv.getClass().getName());
525
            xml.putProperty("driverclass", drv.getClass().getName());
526
            
527
            xml.putProperty("tablename", getTableName());
528
            xml.putProperty("fields", lyrDef.getFieldNames());
529
            xml.putProperty("FID", lyrDef.getFieldID());
530
            xml.putProperty("THE_GEOM", lyrDef.getFieldGeometry());            
531
            xml.putProperty("whereclause", getWhereClause());
532
            xml.putProperty("SRID", lyrDef.getSRID_EPSG());
533
            if (getWorkingArea() != null)
534
            {
535
                xml.putProperty("minXworkArea", getWorkingArea().getMinX());
536
                xml.putProperty("minYworkArea", getWorkingArea().getMinY());
537
                xml.putProperty("HworkArea", getWorkingArea().getHeight());
538
                xml.putProperty("WworkArea", getWorkingArea().getWidth());
539
            }
540
            
541
        } catch (SQLException e) {
542
            // TODO Auto-generated catch block
543
            e.printStackTrace();
544
        }
545

    
546

    
547
        return xml;
548
       
549
    }
550

    
551
    /**
552
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver#setWorkingArea(java.awt.geom.Rectangle2D)
553
     */
554
    public void setWorkingArea(Rectangle2D rect) {
555
        this.workingArea = rect;
556
    }
557

    
558
    /**
559
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver#getWorkingArea()
560
     */
561
    public Rectangle2D getWorkingArea() {
562
        return workingArea;
563
    }
564
    
565
    /* (non-Javadoc)
566
     * @see com.hardcode.gdbms.engine.data.driver.GDBMSDriver#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
567
     */
568
    public void setDataSourceFactory(DataSourceFactory arg0) {
569
        // TODO Auto-generated method stub
570
        
571
    }
572

    
573
    /**
574
     * @return Returns the lyrDef.
575
     */
576
    public DBLayerDefinition getLyrDef() {
577
        return lyrDef;
578
    }
579

    
580
    /**
581
     * @param lyrDef The lyrDef to set.
582
     */
583
    public void setLyrDef(DBLayerDefinition lyrDef) {
584
        this.lyrDef = lyrDef;
585
    }
586
    
587
    abstract public String getSqlTotal();
588
    
589
    /**
590
     * @return Returns the completeWhere. WITHOUT order by clause!!
591
     */
592
    abstract public String getCompleteWhere();
593

    
594
    
595
}