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