Statistics
| Revision:

root / branches / FMap_SLD / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / VectorialDBAdapter.java @ 1854

History | View | Annotate | Download (5.92 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.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
            try {
78
                ((VectorialDatabaseDriver)driver).open();
79
            } catch (DriverException e) {
80
                // TODO Auto-generated catch block
81
                e.printStackTrace();
82
            }
83
        }
84

    
85
        /**
86
         * decrementa el contador de n?mero de aperturas y cuando llega a cero pide
87
         * al driver que cierre la conexion con el servidor de base de datos
88
         */
89
        public void stop() {
90
            ((VectorialDatabaseDriver)driver).close();
91
        }
92

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

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

    
161
        /**
162
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
163
         */
164
        public Rectangle2D getFullExtent() {
165
            return ((VectorialDatabaseDriver)driver).getFullExtent();
166
        }
167

    
168

    
169
        /**
170
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
171
         */
172
        public int getShapeType() throws DriverIOException {
173
                return FShape.MULTI;
174
        }
175

    
176
        /**
177
         * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
178
         */
179
        public DataSource getRecordset() throws DriverLoadException {
180
            if (driver instanceof ObjectDriver)
181
            {
182
                        String name = LayerFactory.getDataSourceFactory().addDataSource((ObjectDriver)driver, null);
183
                        try {
184
                ds = LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.AUTOMATIC_MODE);
185
            } catch (NoSuchTableException e) {
186
                throw new RuntimeException(e);
187
                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
188
                                throw new RuntimeException(e);
189
                        }
190
            }
191
                return ds;
192
        }
193

    
194
}