Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / VectorialDBAdapter.java @ 1836

History | View | Annotate | Download (5.49 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.layers;
42

    
43
import java.awt.geom.Rectangle2D;
44
import java.sql.Connection;
45
import java.sql.ResultSet;
46
import java.sql.SQLException;
47
import java.sql.Statement;
48

    
49
import com.hardcode.driverManager.DriverLoadException;
50
import com.hardcode.gdbms.engine.data.DataSource;
51
import com.hardcode.gdbms.engine.data.DataSourceFactory;
52
import com.hardcode.gdbms.engine.data.NoSuchTableException;
53
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
54
import com.iver.cit.gvsig.fmap.DriverException;
55
import com.iver.cit.gvsig.fmap.core.FShape;
56
import com.iver.cit.gvsig.fmap.core.IGeometry;
57
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
58
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
59
import com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator;
60

    
61

    
62

    
63
/**
64
 * Adapta un driver de base de datos vectorial a la interfaz vectorial,
65
 * manteniendo adem?s el estado necesario por una capa vectorial de base de
66
 * datos (par?metros de la conexi?n)
67
 */
68
public class VectorialDBAdapter extends VectorialAdapter {
69
    private int numReg=-1;
70
    private DataSource ds = null;
71
        /**
72
         * incrementa el contador de las veces que se ha abierto el fichero.
73
         * Solamente cuando el contador est? a cero pide al driver que conecte con
74
         * la base de datos
75
         */
76
        public void start() {
77
        }
78

    
79
        /**
80
         * decrementa el contador de n?mero de aperturas y cuando llega a cero pide
81
         * al driver que cierre la conexion con el servidor de base de datos
82
         */
83
        public void stop() {
84
        }
85

    
86
        /**
87
         * @return devuelve la Conexi?n a la base de datos, para que 
88
         * el usuario pueda hacer la consulta que quiera, si lo desea.
89
         * Por ejemplo, esto puede ser ?til para abrir un cuadro de dialogo
90
         * avanazado y lanzar peticiones del tipo "Devuelveme un buffer
91
         * a las autopistas", y con el resultset que te venga, escribir
92
         * un shape, o cosas as?.
93
         */
94
        public Connection getConnection()
95
        {
96
            return ((VectorialDatabaseDriver)driver).getConnection();
97
        }
98
        public GeometryIterator getGeometryIterator(String sql) throws DriverException
99
        {
100
            return ((VectorialDatabaseDriver)driver).getGeometryIterator(sql);
101
        }
102
        public String getFields()
103
        {
104
            return ((VectorialDatabaseDriver)driver).getFields();            
105
        }
106
        public String getWhereClause()
107
        {
108
            return ((VectorialDatabaseDriver)driver).getWhereClause();
109
        }
110
        public String getTableName()
111
        {
112
            return ((VectorialDatabaseDriver)driver).getTableName();
113
        }
114
        
115
        
116
        /**
117
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
118
         */
119
        public IGeometry getShape(int index) throws DriverIOException {
120
            IGeometry geom = null;
121
            geom = ((VectorialDatabaseDriver)driver).getShape(index);
122
            return geom;
123
        }
124

    
125
        /**
126
         * @throws DriverException
127
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeCount()
128
         */
129
        public int getShapeCount() throws DriverIOException {
130
                    if (numReg == -1)
131
                    {
132
                        try
133
                    {
134
                            Statement s = ((VectorialDatabaseDriver)driver).getConnection().createStatement();                    
135
                            ResultSet r = s.executeQuery("SELECT COUNT(*) AS NUMREG FROM " + getTableName());
136
                            r.next();
137
                            numReg = r.getInt(1);
138
                            System.err.println("numReg = " + numReg);
139
                    }
140
                        catch (SQLException e)
141
                        {
142
                            throw new DriverIOException(e);
143
                        }
144
                    }
145
                    
146
            return numReg;
147
        }
148

    
149
        /**
150
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
151
         */
152
        public Rectangle2D getFullExtent() {
153
            return ((VectorialDatabaseDriver)driver).getFullExtent();
154
        }
155

    
156

    
157
        /**
158
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
159
         */
160
        public int getShapeType() throws DriverIOException {
161
                return FShape.MULTI;
162
        }
163

    
164
        /**
165
         * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
166
         */
167
        public DataSource getRecordset() throws DriverLoadException {
168
            if (driver instanceof ObjectDriver)
169
            {
170
                        String name = LayerFactory.getDataSourceFactory().addDataSource((ObjectDriver)driver, null);
171
                        try {
172
                ds = LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.AUTOMATIC_MODE);
173
            } catch (NoSuchTableException e) {
174
                throw new RuntimeException(e);
175
                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
176
                                throw new RuntimeException(e);
177
                        }
178
            }
179
                return ds;
180
        }
181

    
182
}