svn-gvsig-desktop / tags / v1_0_2_Build_916 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / VectorialDBAdapter.java @ 12327
History | View | Annotate | Download (7.85 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 | 2183 | fernando | import java.io.IOException; |
45 | 1691 | fjp | import java.sql.Connection; |
46 | import java.sql.ResultSet; |
||
47 | import java.sql.SQLException; |
||
48 | import java.sql.Statement; |
||
49 | |||
50 | import com.hardcode.driverManager.DriverLoadException; |
||
51 | 1034 | vcaballero | import com.hardcode.gdbms.engine.data.DataSource; |
52 | 1691 | fjp | import com.hardcode.gdbms.engine.data.DataSourceFactory; |
53 | import com.hardcode.gdbms.engine.data.NoSuchTableException; |
||
54 | 1828 | fernando | import com.hardcode.gdbms.engine.data.driver.ObjectDriver; |
55 | 3248 | fjp | import com.hardcode.gdbms.engine.values.Value; |
56 | 651 | fernando | import com.iver.cit.gvsig.fmap.DriverException; |
57 | 3248 | fjp | import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
58 | 1691 | fjp | import com.iver.cit.gvsig.fmap.core.FShape; |
59 | 3248 | fjp | import com.iver.cit.gvsig.fmap.core.IFeature; |
60 | 305 | fjp | import com.iver.cit.gvsig.fmap.core.IGeometry; |
61 | 3268 | fjp | import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
62 | 214 | fernando | import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
63 | 2183 | fernando | import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
64 | 214 | fernando | import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver; |
65 | |||
66 | 1034 | vcaballero | |
67 | |||
68 | 214 | fernando | /**
|
69 | 1034 | vcaballero | * Adapta un driver de base de datos vectorial a la interfaz vectorial,
|
70 | * manteniendo adem?s el estado necesario por una capa vectorial de base de
|
||
71 | * datos (par?metros de la conexi?n)
|
||
72 | 214 | fernando | */
|
73 | 4171 | fjp | public class VectorialDBAdapter extends VectorialAdapter implements ISpatialDB { |
74 | 1691 | fjp | private int numReg=-1; |
75 | 3959 | caballero | private SelectableDataSource ds = null; |
76 | 1034 | vcaballero | /**
|
77 | * incrementa el contador de las veces que se ha abierto el fichero.
|
||
78 | * Solamente cuando el contador est? a cero pide al driver que conecte con
|
||
79 | * la base de datos
|
||
80 | */
|
||
81 | public void start() { |
||
82 | 2183 | fernando | try {
|
83 | 11006 | jmvivo | //System.out.println("====" +this.hashCode()+" Abrir tabla: " + ((VectorialDatabaseDriver)driver).getTableName());
|
84 | 2183 | fernando | ((VectorialDatabaseDriver)driver).open(); |
85 | } catch (DriverException e) {
|
||
86 | // TODO Auto-generated catch block
|
||
87 | e.printStackTrace(); |
||
88 | } |
||
89 | 1034 | vcaballero | } |
90 | 214 | fernando | |
91 | 1034 | vcaballero | /**
|
92 | * decrementa el contador de n?mero de aperturas y cuando llega a cero pide
|
||
93 | * al driver que cierre la conexion con el servidor de base de datos
|
||
94 | */
|
||
95 | public void stop() { |
||
96 | 11006 | jmvivo | //System.out.println("====" +this.hashCode()+" Cerrar tabla: " + ((VectorialDatabaseDriver)driver).getTableName());
|
97 | 2183 | fernando | ((VectorialDatabaseDriver)driver).close(); |
98 | 1034 | vcaballero | } |
99 | 214 | fernando | |
100 | 4171 | fjp | /* (non-Javadoc)
|
101 | * @see com.iver.cit.gvsig.fmap.layers.ISpatialDB#getFeatureIterator(java.awt.geom.Rectangle2D, java.lang.String)
|
||
102 | 1034 | vcaballero | */
|
103 | 2183 | fernando | /* public Connection getConnection()
|
104 | 1691 | fjp | {
|
105 | return ((VectorialDatabaseDriver)driver).getConnection();
|
||
106 | 2183 | fernando | }*/
|
107 | /* public IFeatureIterator getFeatureIterator(String sql) throws DriverException
|
||
108 | 1693 | fjp | {
|
109 | 2183 | fernando | return ((VectorialDatabaseDriver)driver).getFeatureIterator(sql);
|
110 | }*/
|
||
111 | public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG) throws DriverException |
||
112 | { |
||
113 | return ((VectorialDatabaseDriver)driver).getFeatureIterator(r, strEPSG);
|
||
114 | 1693 | fjp | } |
115 | 3959 | caballero | |
116 | 4171 | fjp | /* (non-Javadoc)
|
117 | * @see com.iver.cit.gvsig.fmap.layers.ISpatialDB#getFeatureIterator(java.awt.geom.Rectangle2D, java.lang.String, java.lang.String[])
|
||
118 | */
|
||
119 | 3268 | fjp | public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG, String[] alphaNumericFieldsNeeded) throws DriverException |
120 | { |
||
121 | return ((VectorialDatabaseDriver)driver).getFeatureIterator(r, strEPSG, alphaNumericFieldsNeeded);
|
||
122 | } |
||
123 | 3959 | caballero | |
124 | 4171 | fjp | /* (non-Javadoc)
|
125 | * @see com.iver.cit.gvsig.fmap.layers.ISpatialDB#getFields()
|
||
126 | */
|
||
127 | 2183 | fernando | public String[] getFields() |
128 | 1691 | fjp | { |
129 | 3959 | caballero | return ((VectorialDatabaseDriver)driver).getFields();
|
130 | 1691 | fjp | } |
131 | 4171 | fjp | /* (non-Javadoc)
|
132 | * @see com.iver.cit.gvsig.fmap.layers.ISpatialDB#getWhereClause()
|
||
133 | */
|
||
134 | 1691 | fjp | public String getWhereClause() |
135 | { |
||
136 | return ((VectorialDatabaseDriver)driver).getWhereClause();
|
||
137 | } |
||
138 | 4171 | fjp | /* (non-Javadoc)
|
139 | * @see com.iver.cit.gvsig.fmap.layers.ISpatialDB#getTableName()
|
||
140 | */
|
||
141 | 1691 | fjp | public String getTableName() |
142 | { |
||
143 | return ((VectorialDatabaseDriver)driver).getTableName();
|
||
144 | } |
||
145 | 3959 | caballero | |
146 | |||
147 | 214 | fernando | /**
|
148 | * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
|
||
149 | */
|
||
150 | 305 | fjp | public IGeometry getShape(int index) throws DriverIOException { |
151 | 1691 | fjp | IGeometry geom = null;
|
152 | 3271 | fjp | try {
|
153 | geom = ((VectorialDatabaseDriver)driver).getShape(index); |
||
154 | } catch (IOException e) { |
||
155 | throw new DriverIOException(e); |
||
156 | } |
||
157 | 1691 | fjp | return geom;
|
158 | 214 | fernando | } |
159 | |||
160 | /**
|
||
161 | 369 | fernando | * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
|
162 | */
|
||
163 | public int getShapeType() throws DriverIOException { |
||
164 | 2183 | fernando | return ((VectorialDatabaseDriver)driver).getShapeType();
|
165 | 369 | fernando | } |
166 | 408 | fernando | |
167 | /**
|
||
168 | * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
|
||
169 | */
|
||
170 | 3959 | caballero | public SelectableDataSource getRecordset() throws DriverLoadException { |
171 | 1828 | fernando | if (driver instanceof ObjectDriver) |
172 | 1691 | fjp | { |
173 | 3207 | fjp | if (ds == null) |
174 | { |
||
175 | String name = LayerFactory.getDataSourceFactory().addDataSource((ObjectDriver)driver);
|
||
176 | try {
|
||
177 | 11008 | jmvivo | // CHEMA: AUTOMATIC DATA SOURCE
|
178 | //ds = new SelectableDataSource(LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.MANUAL_OPENING));
|
||
179 | ds = new SelectableDataSource(LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.AUTOMATIC_OPENING));
|
||
180 | 3207 | fjp | } catch (NoSuchTableException e) {
|
181 | throw new RuntimeException(e); |
||
182 | } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
||
183 | throw new RuntimeException(e); |
||
184 | } |
||
185 | 3959 | caballero | } |
186 | 1691 | fjp | } |
187 | return ds;
|
||
188 | 408 | fernando | } |
189 | 546 | fernando | |
190 | 3268 | fjp | /* (non-Javadoc)
|
191 | * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getFeature(int)
|
||
192 | */
|
||
193 | public IFeature getFeature(int numReg) throws DriverException { |
||
194 | IGeometry geom; |
||
195 | IFeature feat = null;
|
||
196 | try {
|
||
197 | geom = getShape(numReg); |
||
198 | DataSource rs = getRecordset();
|
||
199 | int idFieldID = getLyrDef().getIdFieldID();
|
||
200 | Value[] regAtt = new Value[rs.getFieldCount()]; |
||
201 | String theID = null; |
||
202 | for (int fieldId=0; fieldId < rs.getFieldCount(); fieldId++ ) |
||
203 | 3959 | caballero | { |
204 | 3268 | fjp | regAtt[fieldId] = rs.getFieldValue(numReg, fieldId); |
205 | if (fieldId == idFieldID)
|
||
206 | theID = regAtt[fieldId].toString(); |
||
207 | } |
||
208 | 3959 | caballero | |
209 | 3268 | fjp | feat = new DefaultFeature(geom, regAtt, theID);
|
210 | } catch (DriverIOException e) {
|
||
211 | throw new DriverException(e); |
||
212 | } catch (DriverLoadException e) {
|
||
213 | throw new DriverException(e); |
||
214 | } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
||
215 | throw new DriverException(e); |
||
216 | } |
||
217 | return feat;
|
||
218 | } |
||
219 | |||
220 | 4171 | fjp | /* (non-Javadoc)
|
221 | * @see com.iver.cit.gvsig.fmap.layers.ISpatialDB#getLyrDef()
|
||
222 | */
|
||
223 | 3268 | fjp | public DBLayerDefinition getLyrDef()
|
224 | { |
||
225 | return ((VectorialDatabaseDriver)driver).getLyrDef();
|
||
226 | } |
||
227 | 4181 | fjp | |
228 | public int getRowIndexByFID(IFeature feat) { |
||
229 | return ((VectorialDatabaseDriver) driver).getRowIndexByFID(feat);
|
||
230 | } |
||
231 | 214 | fernando | } |