Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / VectorialDBAdapter.java @ 1773

History | View | Annotate | Download (5.54 KB)

1 1100 fjp
/* 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 214 fernando
package com.iver.cit.gvsig.fmap.layers;
42
43 1691 fjp
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 org.postgis.Geometry;
50
import org.postgis.PGbox3d;
51
import org.postgis.Point;
52
53
import com.hardcode.driverManager.DriverLoadException;
54 1034 vcaballero
import com.hardcode.gdbms.engine.data.DataSource;
55 1691 fjp
import com.hardcode.gdbms.engine.data.DataSourceFactory;
56
import com.hardcode.gdbms.engine.data.NoSuchTableException;
57
import com.hardcode.gdbms.engine.data.ReadDriver;
58 651 fernando
import com.iver.cit.gvsig.fmap.DriverException;
59 1691 fjp
import com.iver.cit.gvsig.fmap.core.FShape;
60 305 fjp
import com.iver.cit.gvsig.fmap.core.IGeometry;
61 214 fernando
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
62
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
63 1693 fjp
import com.iver.cit.gvsig.fmap.drivers.jdbc.GeometryIterator;
64
import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.WKTParser;
65 1691 fjp
import com.vividsolutions.jts.io.ParseException;
66 214 fernando
67 1034 vcaballero
68
69 214 fernando
/**
70 1034 vcaballero
 * Adapta un driver de base de datos vectorial a la interfaz vectorial,
71
 * manteniendo adem?s el estado necesario por una capa vectorial de base de
72
 * datos (par?metros de la conexi?n)
73 214 fernando
 */
74
public class VectorialDBAdapter extends VectorialAdapter {
75 1691 fjp
    private int numReg=-1;
76
    private DataSource ds = null;
77 1034 vcaballero
        /**
78
         * incrementa el contador de las veces que se ha abierto el fichero.
79
         * Solamente cuando el contador est? a cero pide al driver que conecte con
80
         * la base de datos
81
         */
82
        public void start() {
83
        }
84 214 fernando
85 1034 vcaballero
        /**
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
        }
91 214 fernando
92 1034 vcaballero
        /**
93 1691 fjp
         * @return devuelve la Conexi?n a la base de datos, para que
94
         * el usuario pueda hacer la consulta que quiera, si lo desea.
95
         * Por ejemplo, esto puede ser ?til para abrir un cuadro de dialogo
96
         * avanazado y lanzar peticiones del tipo "Devuelveme un buffer
97
         * a las autopistas", y con el resultset que te venga, escribir
98
         * un shape, o cosas as?.
99 1034 vcaballero
         */
100 1691 fjp
        public Connection getConnection()
101
        {
102
            return ((VectorialDatabaseDriver)driver).getConnection();
103 1034 vcaballero
        }
104 1693 fjp
        public GeometryIterator getGeometryIterator(String sql) throws DriverException
105
        {
106
            return ((VectorialDatabaseDriver)driver).getGeometryIterator(sql);
107
        }
108 1691 fjp
        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 214 fernando
        /**
123
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
124
         */
125 305 fjp
        public IGeometry getShape(int index) throws DriverIOException {
126 1691 fjp
            IGeometry geom = null;
127
            geom = ((VectorialDatabaseDriver)driver).getShape(index);
128
            return geom;
129 214 fernando
        }
130
131
        /**
132 1691 fjp
         * @throws DriverException
133 214 fernando
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeCount()
134
         */
135
        public int getShapeCount() throws DriverIOException {
136 1691 fjp
                    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 214 fernando
        }
154
155
        /**
156
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
157
         */
158 1691 fjp
        public Rectangle2D getFullExtent() {
159
            return ((VectorialDatabaseDriver)driver).getFullExtent();
160 214 fernando
        }
161
162 369 fernando
163
        /**
164
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
165
         */
166
        public int getShapeType() throws DriverIOException {
167 1691 fjp
                return FShape.MULTI;
168 369 fernando
        }
169 408 fernando
170
        /**
171
         * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
172
         */
173 1691 fjp
        public DataSource getRecordset(String name) throws DriverLoadException {
174
            if (driver instanceof ReadDriver)
175
            {
176 1773 fernando
                        LayerFactory.getDataSourceFactory().addDataSource((ReadDriver)driver, name);
177 1691 fjp
                        try {
178 1773 fernando
                ds = LayerFactory.getDataSourceFactory().createRandomDataSource(name);
179 1691 fjp
            } catch (NoSuchTableException e) {
180
                throw new DriverLoadException(e);
181
            }
182
            }
183
                return ds;
184 408 fernando
        }
185 546 fernando
186 214 fernando
}