svn-gvsig-desktop / tags / v1_0_2_Build_915 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / VectorialDBAdapter.java @ 12217
History | View | Annotate | Download (7.85 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.io.IOException; |
45 |
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 |
import com.hardcode.gdbms.engine.data.DataSource; |
52 |
import com.hardcode.gdbms.engine.data.DataSourceFactory; |
53 |
import com.hardcode.gdbms.engine.data.NoSuchTableException; |
54 |
import com.hardcode.gdbms.engine.data.driver.ObjectDriver; |
55 |
import com.hardcode.gdbms.engine.values.Value; |
56 |
import com.iver.cit.gvsig.fmap.DriverException; |
57 |
import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
58 |
import com.iver.cit.gvsig.fmap.core.FShape; |
59 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
60 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
61 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
62 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
63 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
64 |
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver; |
65 |
|
66 |
|
67 |
|
68 |
/**
|
69 |
* 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 |
*/
|
73 |
public class VectorialDBAdapter extends VectorialAdapter implements ISpatialDB { |
74 |
private int numReg=-1; |
75 |
private SelectableDataSource ds = null; |
76 |
/**
|
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 |
try {
|
83 |
//System.out.println("====" +this.hashCode()+" Abrir tabla: " + ((VectorialDatabaseDriver)driver).getTableName());
|
84 |
((VectorialDatabaseDriver)driver).open(); |
85 |
} catch (DriverException e) {
|
86 |
// TODO Auto-generated catch block
|
87 |
e.printStackTrace(); |
88 |
} |
89 |
} |
90 |
|
91 |
/**
|
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 |
//System.out.println("====" +this.hashCode()+" Cerrar tabla: " + ((VectorialDatabaseDriver)driver).getTableName());
|
97 |
((VectorialDatabaseDriver)driver).close(); |
98 |
} |
99 |
|
100 |
/* (non-Javadoc)
|
101 |
* @see com.iver.cit.gvsig.fmap.layers.ISpatialDB#getFeatureIterator(java.awt.geom.Rectangle2D, java.lang.String)
|
102 |
*/
|
103 |
/* public Connection getConnection()
|
104 |
{
|
105 |
return ((VectorialDatabaseDriver)driver).getConnection();
|
106 |
}*/
|
107 |
/* public IFeatureIterator getFeatureIterator(String sql) throws DriverException
|
108 |
{
|
109 |
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 |
} |
115 |
|
116 |
/* (non-Javadoc)
|
117 |
* @see com.iver.cit.gvsig.fmap.layers.ISpatialDB#getFeatureIterator(java.awt.geom.Rectangle2D, java.lang.String, java.lang.String[])
|
118 |
*/
|
119 |
public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG, String[] alphaNumericFieldsNeeded) throws DriverException |
120 |
{ |
121 |
return ((VectorialDatabaseDriver)driver).getFeatureIterator(r, strEPSG, alphaNumericFieldsNeeded);
|
122 |
} |
123 |
|
124 |
/* (non-Javadoc)
|
125 |
* @see com.iver.cit.gvsig.fmap.layers.ISpatialDB#getFields()
|
126 |
*/
|
127 |
public String[] getFields() |
128 |
{ |
129 |
return ((VectorialDatabaseDriver)driver).getFields();
|
130 |
} |
131 |
/* (non-Javadoc)
|
132 |
* @see com.iver.cit.gvsig.fmap.layers.ISpatialDB#getWhereClause()
|
133 |
*/
|
134 |
public String getWhereClause() |
135 |
{ |
136 |
return ((VectorialDatabaseDriver)driver).getWhereClause();
|
137 |
} |
138 |
/* (non-Javadoc)
|
139 |
* @see com.iver.cit.gvsig.fmap.layers.ISpatialDB#getTableName()
|
140 |
*/
|
141 |
public String getTableName() |
142 |
{ |
143 |
return ((VectorialDatabaseDriver)driver).getTableName();
|
144 |
} |
145 |
|
146 |
|
147 |
/**
|
148 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
|
149 |
*/
|
150 |
public IGeometry getShape(int index) throws DriverIOException { |
151 |
IGeometry geom = null;
|
152 |
try {
|
153 |
geom = ((VectorialDatabaseDriver)driver).getShape(index); |
154 |
} catch (IOException e) { |
155 |
throw new DriverIOException(e); |
156 |
} |
157 |
return geom;
|
158 |
} |
159 |
|
160 |
/**
|
161 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
|
162 |
*/
|
163 |
public int getShapeType() throws DriverIOException { |
164 |
return ((VectorialDatabaseDriver)driver).getShapeType();
|
165 |
} |
166 |
|
167 |
/**
|
168 |
* @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
|
169 |
*/
|
170 |
public SelectableDataSource getRecordset() throws DriverLoadException { |
171 |
if (driver instanceof ObjectDriver) |
172 |
{ |
173 |
if (ds == null) |
174 |
{ |
175 |
String name = LayerFactory.getDataSourceFactory().addDataSource((ObjectDriver)driver);
|
176 |
try {
|
177 |
// 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 |
} 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 |
} |
186 |
} |
187 |
return ds;
|
188 |
} |
189 |
|
190 |
/* (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 |
{ |
204 |
regAtt[fieldId] = rs.getFieldValue(numReg, fieldId); |
205 |
if (fieldId == idFieldID)
|
206 |
theID = regAtt[fieldId].toString(); |
207 |
} |
208 |
|
209 |
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 |
/* (non-Javadoc)
|
221 |
* @see com.iver.cit.gvsig.fmap.layers.ISpatialDB#getLyrDef()
|
222 |
*/
|
223 |
public DBLayerDefinition getLyrDef()
|
224 |
{ |
225 |
return ((VectorialDatabaseDriver)driver).getLyrDef();
|
226 |
} |
227 |
|
228 |
public int getRowIndexByFID(IFeature feat) { |
229 |
return ((VectorialDatabaseDriver) driver).getRowIndexByFID(feat);
|
230 |
} |
231 |
} |