Statistics
| Revision:

svn-gvsig-desktop / tags / Root_Fmap_GisPlanet / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / VectorialDBAdapter.java @ 1826

History | View | Annotate | Download (5.53 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.ReadDriver;
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.IFeatureIterator;
59
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
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
            ((VectorialDatabaseDriver)driver).close();
85
        }
86

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

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

    
155
        /**
156
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
157
         */
158
        public Rectangle2D getFullExtent() {
159
            return ((VectorialDatabaseDriver)driver).getFullExtent();
160
        }
161

    
162

    
163
        /**
164
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
165
         */
166
        public int getShapeType() throws DriverIOException {
167
                return FShape.MULTI;
168
        }
169

    
170
        /**
171
         * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
172
         */
173
        public DataSource getRecordset(String name) throws DriverLoadException {
174
            if (driver instanceof ReadDriver)
175
            {
176
                        DataSourceFactory.addDataSource((ReadDriver)driver, name);
177
                        try {
178
                ds = DataSourceFactory.createRandomDataSource(name);
179
            } catch (NoSuchTableException e) {
180
                throw new DriverLoadException(e);
181
            }
182
            }
183
                return ds;
184
        }
185

    
186
}