root / branches / v10 / extensions / extSDE / src / com / iver / cit / gvsig / fmap / drivers / sde / ArcSdeDriver.java @ 11012
History | View | Annotate | Download (31.5 KB)
1 | 2277 | vcaballero | /*
|
---|---|---|---|
2 | * Created on 13-may-2005
|
||
3 | *
|
||
4 | * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
||
5 | 10942 | caballero | *
|
6 | 2277 | vcaballero | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
7 | 10942 | caballero | *
|
8 | 2277 | vcaballero | * This program is free software; you can redistribute it and/or
|
9 | * modify it under the terms of the GNU General Public License
|
||
10 | * as published by the Free Software Foundation; either version 2
|
||
11 | * of the License, or (at your option) any later version.
|
||
12 | 10942 | caballero | *
|
13 | 2277 | vcaballero | * This program is distributed in the hope that it will be useful,
|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
16 | * GNU General Public License for more details.
|
||
17 | 10942 | caballero | *
|
18 | 2277 | vcaballero | * You should have received a copy of the GNU General Public License
|
19 | * along with this program; if not, write to the Free Software
|
||
20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||
21 | 10942 | caballero | *
|
22 | 2277 | vcaballero | * For more information, contact:
|
23 | *
|
||
24 | * Generalitat Valenciana
|
||
25 | * Conselleria d'Infraestructures i Transport
|
||
26 | * Av. Blasco Ib??ez, 50
|
||
27 | * 46010 VALENCIA
|
||
28 | * SPAIN
|
||
29 | *
|
||
30 | * +34 963862235
|
||
31 | * gvsig@gva.es
|
||
32 | * www.gvsig.gva.es
|
||
33 | 10942 | caballero | *
|
34 | 2277 | vcaballero | * or
|
35 | 10942 | caballero | *
|
36 | 2277 | vcaballero | * IVER T.I. S.A
|
37 | * Salamanca 50
|
||
38 | * 46005 Valencia
|
||
39 | * Spain
|
||
40 | 10942 | caballero | *
|
41 | 2277 | vcaballero | * +34 963163400
|
42 | * dac@iver.es
|
||
43 | */
|
||
44 | package com.iver.cit.gvsig.fmap.drivers.sde; |
||
45 | |||
46 | import java.awt.geom.Rectangle2D; |
||
47 | import java.io.IOException; |
||
48 | 10942 | caballero | import java.sql.Connection; |
49 | 2277 | vcaballero | import java.sql.Types; |
50 | import java.util.Hashtable; |
||
51 | import java.util.Vector; |
||
52 | |||
53 | import com.esri.sde.sdk.client.SeColumnDefinition; |
||
54 | import com.esri.sde.sdk.client.SeConnection; |
||
55 | import com.esri.sde.sdk.client.SeException; |
||
56 | import com.esri.sde.sdk.client.SeExtent; |
||
57 | import com.esri.sde.sdk.client.SeFilter; |
||
58 | import com.esri.sde.sdk.client.SeLayer; |
||
59 | import com.esri.sde.sdk.client.SeObjectId; |
||
60 | import com.esri.sde.sdk.client.SeQuery; |
||
61 | import com.esri.sde.sdk.client.SeQueryInfo; |
||
62 | import com.esri.sde.sdk.client.SeRow; |
||
63 | import com.esri.sde.sdk.client.SeShape; |
||
64 | import com.esri.sde.sdk.client.SeShapeFilter; |
||
65 | import com.esri.sde.sdk.client.SeSqlConstruct; |
||
66 | import com.esri.sde.sdk.client.SeTable; |
||
67 | 10942 | caballero | import com.hardcode.gdbms.engine.data.DataSourceFactory; |
68 | 11012 | caballero | import com.hardcode.gdbms.engine.data.driver.ObjectDriver; |
69 | 2277 | vcaballero | import com.hardcode.gdbms.engine.data.edition.DataWare; |
70 | import com.hardcode.gdbms.engine.values.Value; |
||
71 | import com.hardcode.gdbms.engine.values.ValueFactory; |
||
72 | import com.iver.cit.gvsig.fmap.DriverException; |
||
73 | 10942 | caballero | import com.iver.cit.gvsig.fmap.DriverJdbcNotFoundExceptionType; |
74 | import com.iver.cit.gvsig.fmap.Messages; |
||
75 | 2277 | vcaballero | import com.iver.cit.gvsig.fmap.core.FShape; |
76 | 10942 | caballero | import com.iver.cit.gvsig.fmap.core.ICanReproject; |
77 | 2277 | vcaballero | import com.iver.cit.gvsig.fmap.core.IFeature; |
78 | import com.iver.cit.gvsig.fmap.core.IGeometry; |
||
79 | 10942 | caballero | import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
80 | 2277 | vcaballero | import com.iver.cit.gvsig.fmap.drivers.DriverAttributes; |
81 | import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
||
82 | 11012 | caballero | import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver; |
83 | import com.iver.cit.gvsig.fmap.drivers.sde.utils.SDELayerDefinition; |
||
84 | import com.iver.cit.gvsig.fmap.edition.EditionException; |
||
85 | 10942 | caballero | import com.iver.cit.gvsig.fmap.edition.IWriteable; |
86 | import com.iver.cit.gvsig.fmap.edition.IWriter; |
||
87 | import com.iver.cit.gvsig.fmap.layers.XMLException; |
||
88 | import com.iver.utiles.XMLEntity; |
||
89 | import com.iver.utiles.swing.JPasswordDlg; |
||
90 | 2277 | vcaballero | |
91 | |||
92 | 10942 | caballero | /**
|
93 | * Driver of ArcSDE.
|
||
94 | * @author Vicente Caballero Navarro
|
||
95 | * @uml.dependency supplier="com.iver.cit.gvsig.fmap.drivers.sde.ArcSdeFeatureIterator"
|
||
96 | * @uml.dependency supplier="com.iver.cit.gvsig.fmap.drivers.sde.SDELayerDefinition"
|
||
97 | */
|
||
98 | 11012 | caballero | public class ArcSdeDriver implements ICanReproject, IWriteable, VectorialSDEDriver, ObjectDriver{ |
99 | 10942 | caballero | protected static Hashtable poolPassw = new Hashtable(); |
100 | 2277 | vcaballero | private SeLayer layer;
|
101 | private SeQuery query;
|
||
102 | private SeQuery queryAux;
|
||
103 | private SeSqlConstruct sqlConstruct;
|
||
104 | private SeColumnDefinition[] colDefs; |
||
105 | private SeRow row = null; |
||
106 | private int numReg = -1; |
||
107 | 10942 | caballero | /**
|
108 | * @uml.property name="fullExtent"
|
||
109 | */
|
||
110 | 2277 | vcaballero | private Rectangle2D fullExtent = null; |
111 | private Hashtable hashRelate = null; |
||
112 | private int idSpatialColumn = -1; |
||
113 | 10942 | caballero | /**
|
114 | * @uml.property name="fields"
|
||
115 | */
|
||
116 | 2277 | vcaballero | private String[] fields; |
117 | private int[] fieldTypes; |
||
118 | private long lastRowServed = -1; |
||
119 | private SeConnection conn = null; |
||
120 | 10942 | caballero | private String strEPSG; |
121 | private String originalEPSG = null; |
||
122 | 2277 | vcaballero | /**
|
123 | 10942 | caballero | * @uml.property name="sqlTotal"
|
124 | */
|
||
125 | private String sqlTotal; |
||
126 | private long posActual = -1; |
||
127 | private SeRow cachedRow = null; |
||
128 | /**
|
||
129 | * @uml.property name="lyrDef"
|
||
130 | */
|
||
131 | protected SDELayerDefinition lyrDef = null; |
||
132 | /**
|
||
133 | * @uml.property name="workingArea"
|
||
134 | */
|
||
135 | protected Rectangle2D workingArea; |
||
136 | protected String driverClass; |
||
137 | protected String className; |
||
138 | protected String catalogName; |
||
139 | 11012 | caballero | private ArcSdeWriter writer;
|
140 | 10942 | caballero | |
141 | /**
|
||
142 | * Recorre el recordset creando una tabla Hash que usaremos para relacionar
|
||
143 | * el n?mero de un registro con su identificador ?nico. Debe ser llamado
|
||
144 | * en el setData justo despu?s de crear el recorset principal
|
||
145 | 2277 | vcaballero | */
|
146 | 10942 | caballero | protected void doRelateID_FID() { |
147 | 2277 | vcaballero | hashRelate = new Hashtable(); |
148 | 10942 | caballero | |
149 | 2277 | vcaballero | try {
|
150 | 10942 | caballero | if (row == null) { |
151 | 2277 | vcaballero | System.out.println(" No rows fetched"); |
152 | 10942 | caballero | |
153 | 2277 | vcaballero | return;
|
154 | } |
||
155 | 10942 | caballero | |
156 | 2277 | vcaballero | int index = 0; |
157 | 11012 | caballero | Value value=getFieldValue(index,getLyrDef().getIdFieldID()); |
158 | int fid=getLyrDef().getIdFieldID();
|
||
159 | while (value != null) { |
||
160 | //SeShape shpVal = row.getShape(idSpatialColumn);
|
||
161 | //SeObjectId objID = shpVal.getFeatureId();
|
||
162 | String theKey = value.toString();
|
||
163 | 2277 | vcaballero | hashRelate.put(theKey, new Integer(index)); |
164 | 11012 | caballero | value = getFieldValue(index,fid); |
165 | index++; |
||
166 | 10942 | caballero | } |
167 | |||
168 | 2277 | vcaballero | numReg = index; |
169 | 11012 | caballero | } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
170 | e.printStackTrace(); |
||
171 | } |
||
172 | 2277 | vcaballero | } |
173 | 10942 | caballero | /**
|
174 | * @return
|
||
175 | * @uml.property name="fullExtent"
|
||
176 | */
|
||
177 | 2277 | vcaballero | public Rectangle2D getFullExtent() { |
178 | 10942 | caballero | return fullExtent;
|
179 | } |
||
180 | 2277 | vcaballero | public IGeometry getShape(int index) { |
181 | SeRow row; |
||
182 | 10942 | caballero | |
183 | 2277 | vcaballero | try {
|
184 | row = obtainRow(index); |
||
185 | 10942 | caballero | |
186 | 2277 | vcaballero | SeShape spVal = row.getShape(idSpatialColumn); |
187 | IGeometry geom = ArcSdeFeatureIterator.getGeometry(spVal); |
||
188 | return geom;
|
||
189 | } catch (SeException e) {
|
||
190 | e.printStackTrace(); |
||
191 | } |
||
192 | 10942 | caballero | return null; |
193 | 2277 | vcaballero | } |
194 | |||
195 | public IFeatureIterator getFeatureIterator(String sql) |
||
196 | 10942 | caballero | throws DriverException {
|
197 | 2277 | vcaballero | return null; |
198 | } |
||
199 | |||
200 | public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG) |
||
201 | 10942 | caballero | throws DriverException {
|
202 | 2277 | vcaballero | /*
|
203 | * Generate a rectangular shape that will be used as a filter
|
||
204 | */
|
||
205 | try {
|
||
206 | SeShape shape = new SeShape(layer.getCoordRef());
|
||
207 | 10942 | caballero | |
208 | SeExtent extent = new SeExtent(r.getMinX(), r.getMinY(),
|
||
209 | r.getMaxX(), r.getMaxY()); |
||
210 | 2277 | vcaballero | shape.generateRectangle(extent); |
211 | 10942 | caballero | |
212 | 2277 | vcaballero | SeShape[] shapes = new SeShape[1]; |
213 | shapes[0] = shape;
|
||
214 | 10942 | caballero | |
215 | 2277 | vcaballero | /*
|
216 | * Retrieve all the shapes that are contained within the
|
||
217 | * rectangles envelope.
|
||
218 | */
|
||
219 | 10942 | caballero | SeShapeFilter[] filters = new SeShapeFilter[1]; |
220 | 2277 | vcaballero | SeShapeFilter filter = null;
|
221 | 10942 | caballero | |
222 | filter = new SeShapeFilter(layer.getName(),
|
||
223 | layer.getSpatialColumn(), shape, SeFilter.METHOD_ENVP); |
||
224 | 2277 | vcaballero | filters[0] = filter;
|
225 | 10942 | caballero | |
226 | 2277 | vcaballero | SeQuery spatialQuery = null;
|
227 | 10942 | caballero | SeSqlConstruct sqlCons = new SeSqlConstruct(layer.getName());
|
228 | |||
229 | 2277 | vcaballero | spatialQuery = new SeQuery(conn, fields, sqlCons);
|
230 | 10942 | caballero | |
231 | 2277 | vcaballero | /*
|
232 | * Set spatial constraints
|
||
233 | */
|
||
234 | 10942 | caballero | spatialQuery.setSpatialConstraints(SeQuery.SE_OPTIMIZE, false,
|
235 | filters); |
||
236 | 2277 | vcaballero | |
237 | spatialQuery.prepareQuery(); |
||
238 | 10942 | caballero | |
239 | 2277 | vcaballero | spatialQuery.execute(); |
240 | 10942 | caballero | |
241 | 2277 | vcaballero | return new ArcSdeFeatureIterator(spatialQuery); |
242 | } catch (SeException e) {
|
||
243 | e.printStackTrace(); |
||
244 | return null; |
||
245 | } |
||
246 | } |
||
247 | |||
248 | public void close() { |
||
249 | } |
||
250 | |||
251 | public void open() throws DriverException { |
||
252 | } |
||
253 | |||
254 | public int getRowIndexByFID(IFeature FID) { |
||
255 | int resul;
|
||
256 | String strID = FID.getID();
|
||
257 | Integer rowIndex = (Integer) hashRelate.get(strID); |
||
258 | 10942 | caballero | |
259 | if (rowIndex == null) { |
||
260 | System.err.println("Error: No se ha encontrado " + strID + |
||
261 | " dentro del Hash");
|
||
262 | 2277 | vcaballero | } |
263 | resul = rowIndex.intValue(); |
||
264 | return resul;
|
||
265 | } |
||
266 | |||
267 | public String getGeometryField(String fieldName) { |
||
268 | return layer.getSpatialColumn();
|
||
269 | } |
||
270 | |||
271 | public int getShapeCount() throws IOException { |
||
272 | return numReg;
|
||
273 | } |
||
274 | |||
275 | public DriverAttributes getDriverAttributes() {
|
||
276 | return null; |
||
277 | } |
||
278 | |||
279 | public String getName() { |
||
280 | 11012 | caballero | return "gvSIG SDE driver"; |
281 | 2277 | vcaballero | } |
282 | |||
283 | 10942 | caballero | /**
|
284 | * @return
|
||
285 | * @uml.property name="fields"
|
||
286 | */
|
||
287 | 2277 | vcaballero | public String[] getFields() { |
288 | 10942 | caballero | String[] attributes = new String[fields.length - 1]; |
289 | for (int i = 1; i < fields.length; i++) |
||
290 | attributes[i - 1] = fields[i];
|
||
291 | return attributes;
|
||
292 | } |
||
293 | 2277 | vcaballero | |
294 | public String getTableName() { |
||
295 | return layer.getName();
|
||
296 | } |
||
297 | |||
298 | 10942 | caballero | private SeRow obtainRow(long rowIndex) throws SeException { |
299 | if (rowIndex != posActual) {
|
||
300 | if (rowIndex == 0) { |
||
301 | if (query != null) { |
||
302 | if (query.inProgress()) {
|
||
303 | query.close(); |
||
304 | } |
||
305 | } |
||
306 | query = new SeQuery(conn, fields, sqlConstruct);
|
||
307 | query.prepareQuery(); |
||
308 | query.execute(); |
||
309 | |||
310 | lastRowServed = -1;
|
||
311 | 2277 | vcaballero | } |
312 | 10942 | caballero | |
313 | if (lastRowServed == (rowIndex - 1)) { |
||
314 | row = query.fetch(); |
||
315 | lastRowServed++; |
||
316 | } else {
|
||
317 | if (queryAux != null) { |
||
318 | queryAux.close(); |
||
319 | } |
||
320 | |||
321 | queryAux = new SeQuery(conn, fields, sqlConstruct);
|
||
322 | |||
323 | SeObjectId rowID = new SeObjectId(rowIndex + 1); |
||
324 | row = queryAux.fetchRow(layer.getName(), rowID, fields); |
||
325 | } |
||
326 | |||
327 | posActual = rowIndex; |
||
328 | cachedRow = row; |
||
329 | 2277 | vcaballero | } |
330 | 10942 | caballero | |
331 | return cachedRow;
|
||
332 | 2277 | vcaballero | } |
333 | 10942 | caballero | |
334 | public Value getFieldValue(long rowIndex, int fieldId) |
||
335 | throws com.hardcode.gdbms.engine.data.driver.DriverException {
|
||
336 | 2277 | vcaballero | SeRow row; |
337 | Value val = null;
|
||
338 | 10942 | caballero | |
339 | 2277 | vcaballero | try {
|
340 | 11012 | caballero | int idFieldArcSDE = fieldId; // SIEMPRE CONTANDO CON |
341 | 10942 | caballero | // QUE NOS HAN PASADO EL PRIMER CAMPO EL DE SHAPE
|
342 | 2277 | vcaballero | row = obtainRow(rowIndex); |
343 | 11012 | caballero | if (row==null) |
344 | return null; |
||
345 | 2277 | vcaballero | int dataType = colDefs[idFieldArcSDE].getType();
|
346 | 10942 | caballero | |
347 | switch (dataType) {
|
||
348 | case SeColumnDefinition.TYPE_INT16:
|
||
349 | val = ValueFactory.createValue(row.getShort(idFieldArcSDE) |
||
350 | .intValue()); |
||
351 | |||
352 | break;
|
||
353 | |||
354 | case SeColumnDefinition.TYPE_INT32:
|
||
355 | case SeColumnDefinition.TYPE_INT64:
|
||
356 | val = ValueFactory.createValue(row.getInteger(idFieldArcSDE) |
||
357 | .intValue()); |
||
358 | |||
359 | break;
|
||
360 | |||
361 | case SeColumnDefinition.TYPE_FLOAT32:
|
||
362 | val = ValueFactory.createValue(row.getFloat(idFieldArcSDE) |
||
363 | .floatValue()); |
||
364 | |||
365 | break;
|
||
366 | |||
367 | case SeColumnDefinition.TYPE_FLOAT64:
|
||
368 | val = ValueFactory.createValue(row.getDouble(idFieldArcSDE) |
||
369 | .doubleValue()); |
||
370 | |||
371 | break;
|
||
372 | |||
373 | case SeColumnDefinition.TYPE_DATE:
|
||
374 | val = ValueFactory.createValue(row.getTime(idFieldArcSDE) |
||
375 | .getTime()); |
||
376 | |||
377 | break;
|
||
378 | |||
379 | default:
|
||
380 | |||
381 | 2277 | vcaballero | String strAux = row.getString(idFieldArcSDE);
|
382 | 10942 | caballero | |
383 | if (strAux == null) { |
||
384 | strAux = "";
|
||
385 | } |
||
386 | |||
387 | val = ValueFactory.createValue(strAux); |
||
388 | |||
389 | break;
|
||
390 | 2277 | vcaballero | } |
391 | 10942 | caballero | |
392 | 2277 | vcaballero | return val;
|
393 | } catch (SeException e) {
|
||
394 | e.printStackTrace(); |
||
395 | } |
||
396 | 10942 | caballero | |
397 | 2277 | vcaballero | return ValueFactory.createNullValue();
|
398 | } |
||
399 | |||
400 | 10942 | caballero | public int getFieldCount() |
401 | throws com.hardcode.gdbms.engine.data.driver.DriverException {
|
||
402 | return fields.length - 1; |
||
403 | 2277 | vcaballero | } |
404 | |||
405 | 10942 | caballero | public String getFieldName(int fieldId) |
406 | throws com.hardcode.gdbms.engine.data.driver.DriverException {
|
||
407 | return fields[fieldId + 1]; |
||
408 | 2277 | vcaballero | } |
409 | |||
410 | 10942 | caballero | public long getRowCount() { |
411 | 2277 | vcaballero | return numReg;
|
412 | } |
||
413 | |||
414 | 10942 | caballero | public int getFieldType(int i) |
415 | throws com.hardcode.gdbms.engine.data.driver.DriverException {
|
||
416 | 2277 | vcaballero | return fieldTypes[i];
|
417 | } |
||
418 | |||
419 | /**
|
||
420 | * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
|
||
421 | */
|
||
422 | 10942 | caballero | public int[] getPrimaryKeys() |
423 | throws com.hardcode.gdbms.engine.data.driver.DriverException {
|
||
424 | 2277 | vcaballero | throw new UnsupportedOperationException(); |
425 | } |
||
426 | |||
427 | /**
|
||
428 | * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
|
||
429 | */
|
||
430 | 10942 | caballero | public void write(DataWare arg0) |
431 | throws com.hardcode.gdbms.engine.data.driver.DriverException {
|
||
432 | } |
||
433 | |||
434 | public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG, |
||
435 | String[] alphaNumericFieldsNeeded) throws DriverException { |
||
436 | /*
|
||
437 | * Generate a rectangular shape that will be used as a filter
|
||
438 | */
|
||
439 | try {
|
||
440 | SeShape shape = new SeShape(layer.getCoordRef());
|
||
441 | |||
442 | SeExtent extent = new SeExtent(r.getMinX(), r.getMinY(),
|
||
443 | r.getMaxX(), r.getMaxY()); |
||
444 | shape.generateRectangle(extent); |
||
445 | |||
446 | SeShape[] shapes = new SeShape[1]; |
||
447 | shapes[0] = shape;
|
||
448 | |||
449 | /*
|
||
450 | * Retrieve all the shapes that are contained within the
|
||
451 | * rectangles envelope.
|
||
452 | */
|
||
453 | SeShapeFilter[] filters = new SeShapeFilter[1]; |
||
454 | SeShapeFilter filter = null;
|
||
455 | |||
456 | filter = new SeShapeFilter(layer.getName(),
|
||
457 | layer.getSpatialColumn(), shape, SeFilter.METHOD_ENVP); |
||
458 | filters[0] = filter;
|
||
459 | |||
460 | SeQuery spatialQuery = null;
|
||
461 | SeSqlConstruct sqlCons = new SeSqlConstruct(layer.getName());
|
||
462 | |||
463 | spatialQuery = new SeQuery(conn, fields, sqlCons);
|
||
464 | |||
465 | /*
|
||
466 | * Set spatial constraints
|
||
467 | */
|
||
468 | spatialQuery.setSpatialConstraints(SeQuery.SE_OPTIMIZE, false,
|
||
469 | filters); |
||
470 | |||
471 | spatialQuery.prepareQuery(); |
||
472 | |||
473 | spatialQuery.execute(); |
||
474 | |||
475 | return new ArcSdeFeatureIterator(spatialQuery); |
||
476 | } catch (SeException e) {
|
||
477 | e.printStackTrace(); |
||
478 | |||
479 | return null; |
||
480 | } |
||
481 | } |
||
482 | |||
483 | public boolean isWritable() { |
||
484 | return true; |
||
485 | } |
||
486 | |||
487 | public void setData(Connection connection, DBLayerDefinition lyrDef) { |
||
488 | setLyrDef(lyrDef); |
||
489 | // Conexi?n:
|
||
490 | SDELayerDefinition sdeDef = (SDELayerDefinition) lyrDef; |
||
491 | String server = sdeDef.getHost();
|
||
492 | int instance = Integer.parseInt(sdeDef.getPort()); |
||
493 | String database = sdeDef.getSchema();
|
||
494 | String user = sdeDef.getUser();
|
||
495 | String password = sdeDef.getPassword();
|
||
496 | String[] cols = sdeDef.getFieldNames(); |
||
497 | String tableName = sdeDef.getTableName();
|
||
498 | |||
499 | try {
|
||
500 | conn = new SeConnection(server, instance, database, user, password);
|
||
501 | } catch (SeException e) {
|
||
502 | e.printStackTrace(); |
||
503 | |||
504 | return;
|
||
505 | } |
||
506 | |||
507 | // Fetching data
|
||
508 | String layerName = tableName;
|
||
509 | SeObjectId layerID = null;
|
||
510 | String strSpatialColumn = ""; |
||
511 | |||
512 | try {
|
||
513 | Vector theLayers = conn.getLayers();
|
||
514 | |||
515 | for (int i = 0; i < theLayers.size(); i++) { |
||
516 | SeLayer layer = (SeLayer) theLayers.elementAt(i); |
||
517 | |||
518 | if (layer.getName().compareToIgnoreCase(layerName) == 0) { |
||
519 | ; |
||
520 | } |
||
521 | |||
522 | layerID = layer.getID(); |
||
523 | strSpatialColumn = layer.getSpatialColumn(); |
||
524 | } |
||
525 | |||
526 | if (layerID == null) { |
||
527 | System.err.println("Capa no encontrada"); |
||
528 | |||
529 | return;
|
||
530 | } |
||
531 | |||
532 | // layerName = layer.getName();
|
||
533 | layer = new SeLayer(conn, layerName, strSpatialColumn);
|
||
534 | |||
535 | //TODO aqu? se puede saber la proyecci?n
|
||
536 | //layer.getCoordRef().getSrid();
|
||
537 | |||
538 | 11012 | caballero | // if ((cols != null) && (cols.length != 0)) {
|
539 | // if (!cols[0].equals(lyrDef.getFieldGeometry())) {
|
||
540 | // this.fields = new String[cols.length + 1];
|
||
541 | // this.fields[0] = lyrDef.getFieldGeometry();
|
||
542 | //
|
||
543 | // for (int i = 1; i < fields.length; i++) {
|
||
544 | // fields[i] = cols[i - 1];
|
||
545 | // }
|
||
546 | // } else {
|
||
547 | // fields = cols;
|
||
548 | // }
|
||
549 | // } else {
|
||
550 | // SeSqlConstruct sqlConstruct = new SeSqlConstruct(tableName);
|
||
551 | // sqlTotal = sqlConstruct.getWhere();
|
||
552 | //
|
||
553 | // SeTable table1 = new SeTable(conn, tableName);
|
||
554 | //
|
||
555 | // /*
|
||
556 | // * Get the table's column definition.
|
||
557 | // */
|
||
558 | // SeColumnDefinition[] tableDef = table1.describe();
|
||
559 | // this.fields = new String[tableDef.length];
|
||
560 | //
|
||
561 | // /*
|
||
562 | // * Store the names of all the table's columns in the
|
||
563 | // * String array cols. This array specifies the columns
|
||
564 | // * to be retrieved from the database.
|
||
565 | // */
|
||
566 | // int idField = 1;
|
||
567 | //
|
||
568 | // for (int i = 0; i < tableDef.length; i++) {
|
||
569 | // if (tableDef[i].getType() == SeColumnDefinition.TYPE_SHAPE) {
|
||
570 | // this.fields[0] = tableDef[i].getName();
|
||
571 | // } else {
|
||
572 | // this.fields[idField] = tableDef[i].getName();
|
||
573 | // idField++;
|
||
574 | // }
|
||
575 | // }
|
||
576 | //
|
||
577 | // lyrDef.setFieldNames(fields);
|
||
578 | // }
|
||
579 | fields=cols; |
||
580 | 10942 | caballero | sqlConstruct = new SeSqlConstruct(layerName);
|
581 | |||
582 | // Create a query stream between the client and server
|
||
583 | query = new SeQuery(conn, fields, sqlConstruct);
|
||
584 | query.prepareQuery(); |
||
585 | query.execute(); |
||
586 | row = query.fetch(); |
||
587 | |||
588 | if (row == null) { |
||
589 | System.out.println("La tabla " + getName() + |
||
590 | " no tiene registros");
|
||
591 | |||
592 | return;
|
||
593 | } |
||
594 | |||
595 | colDefs = row.getColumns(); |
||
596 | lyrDef.setShapeType(obtainShapeType(row.getShape(0).getType()));
|
||
597 | fieldTypes = new int[colDefs.length - 1]; |
||
598 | |||
599 | for (int colNum = 0; colNum < colDefs.length; colNum++) { |
||
600 | SeColumnDefinition colDef = colDefs[colNum]; |
||
601 | int dataType = colDef.getType();
|
||
602 | |||
603 | switch (dataType) {
|
||
604 | case SeColumnDefinition.TYPE_SHAPE:
|
||
605 | |||
606 | if (colNum != 0) { |
||
607 | System.err.println(
|
||
608 | "Por favor, el campo de shapes deber ser el primero");
|
||
609 | |||
610 | return;
|
||
611 | } |
||
612 | |||
613 | idSpatialColumn = colNum; |
||
614 | |||
615 | break;
|
||
616 | |||
617 | case SeColumnDefinition.TYPE_INT16:
|
||
618 | case SeColumnDefinition.TYPE_INT32:
|
||
619 | case SeColumnDefinition.TYPE_INT64:
|
||
620 | fieldTypes[colNum - 1] = Types.INTEGER; |
||
621 | |||
622 | break;
|
||
623 | |||
624 | case SeColumnDefinition.TYPE_FLOAT32:
|
||
625 | fieldTypes[colNum - 1] = Types.FLOAT; |
||
626 | |||
627 | break;
|
||
628 | |||
629 | case SeColumnDefinition.TYPE_FLOAT64:
|
||
630 | fieldTypes[colNum - 1] = Types.DOUBLE; |
||
631 | |||
632 | break;
|
||
633 | |||
634 | case SeColumnDefinition.TYPE_DATE:
|
||
635 | fieldTypes[colNum - 1] = Types.DATE; |
||
636 | |||
637 | break;
|
||
638 | |||
639 | default:
|
||
640 | fieldTypes[colNum - 1] = Types.VARCHAR; |
||
641 | |||
642 | break;
|
||
643 | } |
||
644 | } |
||
645 | 11012 | caballero | fields=cols; |
646 | 10942 | caballero | SeQuery extentQuery = new SeQuery(conn, fields, sqlConstruct);
|
647 | SeQueryInfo queryInfo = new SeQueryInfo();
|
||
648 | queryInfo.setConstruct(sqlConstruct); |
||
649 | |||
650 | SeExtent seExtent = extentQuery.calculateLayerExtent(queryInfo); |
||
651 | fullExtent = new Rectangle2D.Double(seExtent.getMinX(), |
||
652 | seExtent.getMinY(), |
||
653 | seExtent.getMaxX() - seExtent.getMinX(), |
||
654 | seExtent.getMaxY() - seExtent.getMinY()); |
||
655 | extentQuery.close(); |
||
656 | |||
657 | doRelateID_FID(); |
||
658 | } catch (SeException e) {
|
||
659 | e.printStackTrace(); |
||
660 | } |
||
661 | } |
||
662 | |||
663 | private int obtainShapeType(int type) { |
||
664 | int shapeType = -1; |
||
665 | |||
666 | switch (type) {
|
||
667 | case SeLayer.TYPE_POINT:
|
||
668 | shapeType = FShape.POINT; |
||
669 | |||
670 | break;
|
||
671 | |||
672 | case SeLayer.TYPE_LINE:
|
||
673 | case SeLayer.TYPE_SIMPLE_LINE:
|
||
674 | case SeLayer.TYPE_MULTI_LINE:
|
||
675 | case SeLayer.TYPE_MULTI_SIMPLE_LINE:
|
||
676 | shapeType = FShape.LINE; |
||
677 | |||
678 | break;
|
||
679 | |||
680 | case SeLayer.TYPE_POLYGON:
|
||
681 | case SeLayer.TYPE_MULTI_POLYGON:
|
||
682 | shapeType = FShape.POLYGON; |
||
683 | |||
684 | break;
|
||
685 | |||
686 | default:
|
||
687 | shapeType = FShape.MULTI; |
||
688 | } |
||
689 | |||
690 | return shapeType;
|
||
691 | } |
||
692 | |||
693 | public String getCompleteWhere() { |
||
694 | return null; |
||
695 | } |
||
696 | |||
697 | public String getConnectionStringBeginning() { |
||
698 | return ""; |
||
699 | } |
||
700 | |||
701 | public int getDefaultPort() { |
||
702 | return 5151; |
||
703 | } |
||
704 | |||
705 | public String getSourceProjection() { |
||
706 | return originalEPSG;
|
||
707 | } |
||
708 | |||
709 | public String getDestProjection() { |
||
710 | return strEPSG;
|
||
711 | } |
||
712 | |||
713 | public void setDestProjection(String toEPSG) { |
||
714 | this.strEPSG = toEPSG;
|
||
715 | } |
||
716 | |||
717 | public boolean canReproject(String toEPSGdestinyProjection) { |
||
718 | // TODO POR AHORA, REPROYECTA SIEMPRE gvSIG.
|
||
719 | return false; |
||
720 | } |
||
721 | |||
722 | public IWriter getWriter() {
|
||
723 | 11012 | caballero | if (writer == null) { |
724 | writer = new ArcSdeWriter();
|
||
725 | writer.setDriver(this);
|
||
726 | writer.setSeLayer(layer); |
||
727 | try {
|
||
728 | writer.initialize(getLyrDef()); |
||
729 | } |
||
730 | catch (EditionException e) {
|
||
731 | |||
732 | } |
||
733 | } |
||
734 | |||
735 | return writer;
|
||
736 | 10942 | caballero | } |
737 | |||
738 | /**
|
||
739 | * @return
|
||
740 | * @uml.property name="sqlTotal"
|
||
741 | */
|
||
742 | public String getSqlTotal() { |
||
743 | return sqlTotal;
|
||
744 | } |
||
745 | |||
746 | public XMLEntity getXMLEntity() {
|
||
747 | SDELayerDefinition sdeDef = (SDELayerDefinition) getLyrDef(); |
||
748 | XMLEntity xml = new XMLEntity();
|
||
749 | xml.putProperty("className", this.getClass().getName()); |
||
750 | xml.putProperty("driverclass", this.getClass().getName()); |
||
751 | xml.putProperty("catalog", sdeDef.getCatalogName());
|
||
752 | |||
753 | // TODO: NO DEBEMOS GUARDAR EL NOMBRE DE USUARIO Y CONTRASE?A
|
||
754 | // AQUI. Hay que utilizar un pool de conexiones
|
||
755 | // y pedir al usuario que conecte a la base de datos
|
||
756 | // en la primera capa. En el resto, usar la conexi?n
|
||
757 | // creada con anterioridad.
|
||
758 | String userName = sdeDef.getUser();
|
||
759 | if (userName != null) { |
||
760 | int aux = userName.indexOf("@"); |
||
761 | |||
762 | if (aux != -1) { |
||
763 | userName = userName.substring(0, aux);
|
||
764 | } |
||
765 | } |
||
766 | |||
767 | xml.putProperty("username", userName);
|
||
768 | |||
769 | xml.putProperty("tablename", getTableName());
|
||
770 | xml.putProperty("fields", fields);
|
||
771 | xml.putProperty("ID", sdeDef.getFieldID());
|
||
772 | xml.putProperty("THE_GEOM", sdeDef.getFieldGeometry());
|
||
773 | xml.putProperty("whereclause", getWhereClause());
|
||
774 | xml.putProperty("SRID", sdeDef.getSRID_EPSG());
|
||
775 | |||
776 | xml.putProperty("host", sdeDef.getHost());
|
||
777 | xml.putProperty("port", sdeDef.getPort());
|
||
778 | xml.putProperty("dbName", sdeDef.getSchema());
|
||
779 | xml.putProperty("connName", sdeDef.getConnectionName());
|
||
780 | |||
781 | if (getWorkingArea() != null) { |
||
782 | xml.putProperty("minXworkArea", getWorkingArea().getMinX());
|
||
783 | xml.putProperty("minYworkArea", getWorkingArea().getMinY());
|
||
784 | xml.putProperty("HworkArea", getWorkingArea().getHeight());
|
||
785 | xml.putProperty("WworkArea", getWorkingArea().getWidth());
|
||
786 | } |
||
787 | |||
788 | return xml;
|
||
789 | } |
||
790 | |||
791 | public void setXMLEntity(XMLEntity xml) throws XMLException { |
||
792 | className = xml.getStringProperty("className");
|
||
793 | catalogName = xml.getStringProperty("catalog");
|
||
794 | lyrDef = new SDELayerDefinition();
|
||
795 | |||
796 | if (xml.contains("username")) { |
||
797 | lyrDef.setUser(xml.getStringProperty("username"));
|
||
798 | } |
||
799 | |||
800 | driverClass = xml.getStringProperty("driverclass");
|
||
801 | lyrDef.setTableName(xml.getStringProperty("tablename"));
|
||
802 | fields = xml.getStringArrayProperty("fields");
|
||
803 | lyrDef.setFieldNames(fields); |
||
804 | lyrDef.setFieldID(xml.getStringProperty("ID"));
|
||
805 | lyrDef.setFieldGeometry(xml.getStringProperty("THE_GEOM"));
|
||
806 | lyrDef.setWhereClause(xml.getStringProperty("whereclause"));
|
||
807 | lyrDef.setSRID_EPSG(xml.getStringProperty("SRID"));
|
||
808 | |||
809 | if (xml.contains("host")) { |
||
810 | lyrDef.setHost(xml.getStringProperty("host"));
|
||
811 | lyrDef.setPort(xml.getStringProperty("port"));
|
||
812 | lyrDef.setSchema(xml.getStringProperty("dbName"));
|
||
813 | lyrDef.setConnectionName(xml.getStringProperty("connName"));
|
||
814 | } |
||
815 | |||
816 | if (xml.contains("minXworkArea")) { |
||
817 | double x = xml.getDoubleProperty("minXworkArea"); |
||
818 | double y = xml.getDoubleProperty("minYworkArea"); |
||
819 | double H = xml.getDoubleProperty("HworkArea"); |
||
820 | double W = xml.getDoubleProperty("WworkArea"); |
||
821 | workingArea = new Rectangle2D.Double(x, y, W, H); |
||
822 | } |
||
823 | |||
824 | lyrDef.setCatalogName(catalogName); |
||
825 | lyrDef.setFieldNames(fields); |
||
826 | |||
827 | if (workingArea != null) { |
||
828 | lyrDef.setWorkingArea(workingArea); |
||
829 | } |
||
830 | |||
831 | setLyrDef(lyrDef); |
||
832 | } |
||
833 | |||
834 | public int getFieldWidth(int fieldId) { |
||
835 | return colDefs[fieldId].getSize();
|
||
836 | } |
||
837 | |||
838 | public void setLyrDef(DBLayerDefinition lyrDef) { |
||
839 | this.lyrDef = (SDELayerDefinition) lyrDef;
|
||
840 | } |
||
841 | |||
842 | /**
|
||
843 | * @see com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver#setWorkingArea(java.awt.geom.Rectangle2D)
|
||
844 | * @uml.property name="workingArea"
|
||
845 | */
|
||
846 | public void setWorkingArea(Rectangle2D rect) { |
||
847 | this.workingArea = rect;
|
||
848 | } |
||
849 | |||
850 | /**
|
||
851 | * @see com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver#getWorkingArea()
|
||
852 | * @uml.property name="workingArea"
|
||
853 | */
|
||
854 | public Rectangle2D getWorkingArea() { |
||
855 | return workingArea;
|
||
856 | } |
||
857 | |||
858 | /**
|
||
859 | * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
|
||
860 | */
|
||
861 | public int getShapeType() { |
||
862 | return lyrDef.getShapeType();
|
||
863 | } |
||
864 | |||
865 | /* (non-Javadoc)
|
||
866 | * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getWhereClause()
|
||
867 | */
|
||
868 | public String getWhereClause() { |
||
869 | if (lyrDef.getWhereClause() == null) { |
||
870 | return ""; |
||
871 | } |
||
872 | |||
873 | return lyrDef.getWhereClause().toUpperCase();
|
||
874 | } |
||
875 | |||
876 | /**
|
||
877 | * @return
|
||
878 | * @uml.property name="lyrDef"
|
||
879 | */
|
||
880 | public DBLayerDefinition getLyrDef() {
|
||
881 | return lyrDef;
|
||
882 | } |
||
883 | |||
884 | /**
|
||
885 | * Empty method called when the layer is going to be removed from the view.
|
||
886 | * Subclasses can overwrite it if needed.
|
||
887 | */
|
||
888 | public void remove() { |
||
889 | } |
||
890 | |||
891 | /* (non-Javadoc)
|
||
892 | * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#reLoad()
|
||
893 | */
|
||
894 | public void reload() throws IOException { |
||
895 | 11012 | caballero | try {
|
896 | load(); |
||
897 | } catch (DriverException e) {
|
||
898 | e.printStackTrace(); |
||
899 | } |
||
900 | //setData(null, lyrDef);
|
||
901 | 10942 | caballero | } |
902 | |||
903 | public void setDataSourceFactory(DataSourceFactory dsf) { |
||
904 | 2277 | vcaballero | // TODO Auto-generated method stub
|
905 | } |
||
906 | |||
907 | 10942 | caballero | public void load() throws DriverException { |
908 | try {
|
||
909 | if (driverClass != null) { |
||
910 | Class.forName(driverClass);
|
||
911 | } |
||
912 | |||
913 | String _drvName = getName();
|
||
914 | |||
915 | String keyPool = _drvName.toLowerCase() + "_" + |
||
916 | lyrDef.getHost().toLowerCase() + "_" + lyrDef.getPort() + "_" + |
||
917 | lyrDef.getSchema().toLowerCase() + "_" +
|
||
918 | lyrDef.getUser().toLowerCase(); |
||
919 | |||
920 | String clave = null; |
||
921 | |||
922 | if (poolPassw.containsKey(keyPool)) {
|
||
923 | clave = (String) poolPassw.get(keyPool);
|
||
924 | |||
925 | lyrDef.setPassword(clave); |
||
926 | } else {
|
||
927 | JPasswordDlg dlg = new JPasswordDlg();
|
||
928 | String strMessage = Messages.getString("conectar_sde"); |
||
929 | String strPassword = Messages.getString("password"); |
||
930 | dlg.setMessage(strMessage + " [" + _drvName + ", " + |
||
931 | lyrDef.getHost() + ", " + lyrDef.getPort() + ", " + |
||
932 | lyrDef.getSchema() + ", " + lyrDef.getUser() + "]. " + |
||
933 | strPassword + "?");
|
||
934 | dlg.show(); |
||
935 | clave = dlg.getPassword(); |
||
936 | |||
937 | if (clave == null) { |
||
938 | return;
|
||
939 | } |
||
940 | |||
941 | poolPassw.put(keyPool, clave); |
||
942 | lyrDef.setPassword(clave); |
||
943 | |||
944 | } |
||
945 | |||
946 | DBLayerDefinition lyrDef = new DBLayerDefinition();
|
||
947 | |||
948 | if (getLyrDef() == null) { |
||
949 | lyrDef.setCatalogName(catalogName); |
||
950 | lyrDef.setFieldNames(fields); |
||
951 | if (workingArea != null) { |
||
952 | lyrDef.setWorkingArea(workingArea); |
||
953 | } |
||
954 | } else {
|
||
955 | lyrDef = getLyrDef(); |
||
956 | } |
||
957 | |||
958 | setData(null, lyrDef);
|
||
959 | } catch (ClassNotFoundException e) { |
||
960 | DriverJdbcNotFoundExceptionType type = new DriverJdbcNotFoundExceptionType();
|
||
961 | type.setDriverJdbcClassName(driverClass); |
||
962 | type.setLayerName(this.getLyrDef().getTableName());
|
||
963 | throw new DriverException("Driver SDE no encontrado", e, type); |
||
964 | } |
||
965 | } |
||
966 | |||
967 | public Object getConnection() { |
||
968 | return conn;
|
||
969 | } |
||
970 | 11012 | caballero | // public String[] getTableNames(Object conex, String dbName) throws SQLException {
|
971 | // Vector theLayers=null;
|
||
972 | // try {
|
||
973 | // theLayers = ((SeConnection)conex).getLayers();
|
||
974 | // } catch (SeException e) {
|
||
975 | // throw new SQLException(e.getMessage());
|
||
976 | // }
|
||
977 | // TreeMap ret = new TreeMap();
|
||
978 | // for (int i = 0; i < theLayers.size(); i++) {
|
||
979 | // SeLayer layer = (SeLayer) theLayers.elementAt(i);
|
||
980 | // ret.put(layer.getTableName(), layer.getTableName());
|
||
981 | // }
|
||
982 | // return (String[]) ret.keySet().toArray(new String[0]);
|
||
983 | // }
|
||
984 | // public String[] getAllFields(Object conex, String tableName) {
|
||
985 | // try {
|
||
986 | // SeTable table = new SeTable(((SeConnection)conex),tableName);
|
||
987 | // SeRegisteredColumn[] columns=table.getColumnList();
|
||
988 | // String[] fieldNames=new String[columns.length];
|
||
989 | // for (int i=0; i < columns.length; i++){
|
||
990 | // fieldNames[i]=columns[i].getName();
|
||
991 | // }
|
||
992 | // return fieldNames;
|
||
993 | // } catch (SeException e) {
|
||
994 | // e.printStackTrace();
|
||
995 | // }
|
||
996 | // return null;
|
||
997 | // }
|
||
998 | // public String[] getAllFieldTypeNames(Object conex, String tableName) {
|
||
999 | // try {
|
||
1000 | // SeTable table = new SeTable(((SeConnection)conex),tableName);
|
||
1001 | // SeRegisteredColumn[] columns=table.getColumnList();
|
||
1002 | // String[] fieldNames=new String[columns.length];
|
||
1003 | // for (int i=0; i < columns.length; i++){
|
||
1004 | // fieldNames[i]=String.valueOf(columns[i].getType());
|
||
1005 | // }
|
||
1006 | // return fieldNames;
|
||
1007 | // } catch (SeException e) {
|
||
1008 | // e.printStackTrace();
|
||
1009 | // }
|
||
1010 | // return null;
|
||
1011 | // }
|
||
1012 | // public String[] getIdFieldsCandidates(Object conn2, String tableName) {
|
||
1013 | // return new String[]{"OBJECTID"};//getAllFields(conn2,tableName);
|
||
1014 | // }
|
||
1015 | // public String[] getGeometryFieldsCandidates(Object conn2, String tableName) {
|
||
1016 | // return new String[]{"SHAPE"};//getAllFields(conn2,tableName);
|
||
1017 | // }
|
||
1018 | 10954 | caballero | public String getConnectionString( |
1019 | String host,
|
||
1020 | String port,
|
||
1021 | String dbname,
|
||
1022 | String user,
|
||
1023 | String pw) {
|
||
1024 | |||
1025 | String resp = getConnectionStringBeginning() + "//" + host.toLowerCase(); |
||
1026 | |||
1027 | if (dbname.trim().length() > 0) { |
||
1028 | resp += ":" + port;
|
||
1029 | } else {
|
||
1030 | resp += ":" + getDefaultPort();
|
||
1031 | } |
||
1032 | |||
1033 | resp += "/" + dbname.toLowerCase();
|
||
1034 | return resp;
|
||
1035 | } |
||
1036 | 2277 | vcaballero | } |