Revision 29932 branches/v2_0_0_prep/extensions/org.gvsig.oracle/src/org/gvsig/fmap/dal/store/oracle/OracleHelper.java
OracleHelper.java | ||
---|---|---|
50 | 50 |
import org.gvsig.fmap.dal.DALLocator; |
51 | 51 |
import org.gvsig.fmap.dal.DataManager; |
52 | 52 |
import org.gvsig.fmap.dal.DataTypes; |
53 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
|
54 | 53 |
import org.gvsig.fmap.dal.exception.DataException; |
55 | 54 |
import org.gvsig.fmap.dal.exception.InitializeException; |
56 | 55 |
import org.gvsig.fmap.dal.exception.ReadException; |
... | ... | |
61 | 60 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
62 | 61 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
63 | 62 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
64 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
65 | 63 |
import org.gvsig.fmap.dal.feature.exception.UnsupportedDataTypeException; |
66 | 64 |
import org.gvsig.fmap.dal.feature.exception.UnsupportedGeometryException; |
67 | 65 |
import org.gvsig.fmap.dal.resource.spi.ResourceManagerProviderServices; |
... | ... | |
83 | 81 |
import org.slf4j.LoggerFactory; |
84 | 82 |
|
85 | 83 |
/** |
84 |
* Oracle helper |
|
85 |
* |
|
86 | 86 |
* @author vsanjaime |
87 | 87 |
* |
88 | 88 |
*/ |
89 | 89 |
public class OracleHelper extends JDBCHelper { |
90 | 90 |
|
91 |
private static Logger logger = LoggerFactory |
|
92 |
.getLogger(OracleHelper.class); |
|
91 |
private static Logger logger = LoggerFactory.getLogger(OracleHelper.class); |
|
93 | 92 |
|
94 | 93 |
private boolean tableHasSrid = true; |
95 | 94 |
private String oracleSRID; |
... | ... | |
102 | 101 |
* @throws InitializeException |
103 | 102 |
*/ |
104 | 103 |
public OracleHelper(JDBCHelperUser consumer, |
105 |
OracleConnectionParameters params) |
|
106 |
throws InitializeException { |
|
104 |
OracleConnectionParameters params) throws InitializeException { |
|
107 | 105 |
|
108 | 106 |
super(consumer, params); |
109 | 107 |
} |
... | ... | |
114 | 112 |
protected void initializeResource() throws InitializeException { |
115 | 113 |
ResourceManagerProviderServices manager = (ResourceManagerProviderServices) DALLocator |
116 | 114 |
.getResourceManager(); |
117 |
OracleResource resource = (OracleResource) manager |
|
118 |
.createResource(OracleResource.NAME, new Object[] { |
|
119 |
params.getUrl(), |
|
120 |
params.getHost(), |
|
121 |
params.getPort(), |
|
122 |
params.getDBName(), |
|
123 |
params.getUser(), |
|
124 |
params.getPassword(), |
|
115 |
OracleResource resource = (OracleResource) manager.createResource( |
|
116 |
OracleResource.NAME, new Object[] { params.getUrl(), |
|
117 |
params.getHost(), params.getPort(), params.getDBName(), |
|
118 |
params.getUser(), params.getPassword(), |
|
125 | 119 |
params.getJDBCDriverClassName(), |
126 |
((OracleConnectionParameters) params) |
|
127 |
.getUseSSL() });
|
|
120 |
((OracleConnectionParameters) params).getUseSSL(),
|
|
121 |
((OracleConnectionParameters) params).getOraDriverType() });
|
|
128 | 122 |
this.setResource(resource); |
129 | 123 |
} |
130 | 124 |
|
131 | 125 |
/** |
132 |
* Get default schema |
|
126 |
* Get default schema name |
|
127 |
* |
|
128 |
* @param conn |
|
129 |
* @return |
|
133 | 130 |
*/ |
134 | 131 |
protected String getDefaultSchema(Connection conn) throws JDBCException { |
135 | 132 |
if (defaultSchema == null) { |
136 |
String sql = "SELECT sys_context('USERENV', 'CURRENT_SCHEMA') FROM dual;";
|
|
133 |
String sql = "SELECT sys_context('USERENV', 'CURRENT_SCHEMA') FROM dual"; |
|
137 | 134 |
ResultSet rs = null; |
138 | 135 |
Statement st = null; |
139 | 136 |
String schema = null; |
... | ... | |
176 | 173 |
public Envelope getFullEnvelopeOfField(JDBCStoreParameters storeParams, |
177 | 174 |
String geometryAttrName, Envelope limit) throws DataException { |
178 | 175 |
|
176 |
// FIXME |
|
177 |
|
|
179 | 178 |
StringBuilder strb = new StringBuilder(); |
180 | 179 |
strb.append("Select asbinary(extent("); |
181 | 180 |
strb.append(escapeFieldName(geometryAttrName)); |
... | ... | |
261 | 260 |
|
262 | 261 |
} |
263 | 262 |
|
263 |
/** |
|
264 |
* |
|
265 |
*/ |
|
264 | 266 |
protected void initializeFromWKBOperation() throws BaseException { |
265 | 267 |
if (fromWKB == null) { |
266 | 268 |
fromWKB = (FromWKB) GeometryLocator.getGeometryManager() |
267 | 269 |
.getGeometryOperation(FromWKB.CODE, |
268 | 270 |
Geometry.TYPES.GEOMETRY, Geometry.SUBTYPES.GEOM2D); |
269 | 271 |
fromWKBContext = new FromWKBGeometryOperationContext(); |
270 |
|
|
271 | 272 |
} |
272 | 273 |
} |
273 | 274 |
|
... | ... | |
287 | 288 |
return geom; |
288 | 289 |
} |
289 | 290 |
|
290 |
// /** |
|
291 |
// * Fill <code>featureType</code> geometry attributes with SRS and |
|
292 |
// ShapeType |
|
293 |
// * information stored in the table GEOMETRY_COLUMNS |
|
294 |
// * |
|
295 |
// * @param conn |
|
296 |
// * @param rsMetadata |
|
297 |
// * @param featureType |
|
298 |
// * @throws ReadException |
|
299 |
// */ |
|
300 |
// protected void loadSRS_and_shapeType(Connection conn, |
|
301 |
// ResultSetMetaData rsMetadata, EditableFeatureType featureType, |
|
302 |
// String baseSchema, String baseTable) throws JDBCException { |
|
303 |
// |
|
304 |
// Statement st = null; |
|
305 |
// ResultSet rs = null; |
|
306 |
// try { |
|
307 |
// // Sacamos la lista de los attributos geometricos |
|
308 |
// EditableFeatureAttributeDescriptor attr; |
|
309 |
// List geoAttrs = new ArrayList(); |
|
310 |
// |
|
311 |
// Iterator iter = featureType.iterator(); |
|
312 |
// while (iter.hasNext()) { |
|
313 |
// attr = (EditableFeatureAttributeDescriptor) iter.next(); |
|
314 |
// if (attr.getDataType() == DataTypes.GEOMETRY) { |
|
315 |
// geoAttrs.add(attr); |
|
316 |
// } |
|
317 |
// } |
|
318 |
// if (geoAttrs.size() < 1) { |
|
319 |
// return; |
|
320 |
// } |
|
321 |
// |
|
322 |
// // preparamos un set con las lista de tablas de origen |
|
323 |
// // de los campos |
|
324 |
// class TableId { |
|
325 |
// public String schema = null; |
|
326 |
// public String table = null; |
|
327 |
// public String field = null; |
|
328 |
// |
|
329 |
// public void appendToSQL(StringBuilder strb) { |
|
330 |
// if (schema == null || schema.length() == 0) { |
|
331 |
// strb |
|
332 |
// .append("( F_TABLE_SCHEMA = current_schema() AND F_TABLE_NAME = '"); |
|
333 |
// } else { |
|
334 |
// strb.append("( F_TABLE_SCHEMA = '"); |
|
335 |
// strb.append(schema); |
|
336 |
// strb.append("' AND F_TABLE_NAME = '"); |
|
337 |
// } |
|
338 |
// strb.append(table); |
|
339 |
// strb.append("' AND F_GEOMETRY_COLUMN = '"); |
|
340 |
// strb.append(field); |
|
341 |
// strb.append("' )"); |
|
342 |
// } |
|
343 |
// |
|
344 |
// } |
|
345 |
// Comparator cmp = new Comparator() { |
|
346 |
// public int compare(Object arg0, Object arg1) { |
|
347 |
// TableId a0 = (TableId) arg0; |
|
348 |
// TableId a1 = (TableId) arg1; |
|
349 |
// |
|
350 |
// if (!a0.field.equals(a1.field)) { |
|
351 |
// return -1; |
|
352 |
// } |
|
353 |
// if (!a0.table.equals(a1.table)) { |
|
354 |
// return -1; |
|
355 |
// } |
|
356 |
// if (!a0.schema.equals(a1.schema)) { |
|
357 |
// return -1; |
|
358 |
// } |
|
359 |
// return 0; |
|
360 |
// } |
|
361 |
// }; |
|
362 |
// TreeSet set = new TreeSet(cmp); |
|
363 |
// TableId tableId; |
|
364 |
// iter = geoAttrs.iterator(); |
|
365 |
// int rsIndex; |
|
366 |
// while (iter.hasNext()) { |
|
367 |
// attr = (EditableFeatureAttributeDescriptor) iter.next(); |
|
368 |
// tableId = new TableId(); |
|
369 |
// rsIndex = attr.getIndex() + 1; |
|
370 |
// |
|
371 |
// if (baseSchema == null && baseTable == null) { |
|
372 |
// if (rsMetadata instanceof OracleResultSetMetaData) { |
|
373 |
// tableId.schema = ((OracleResultSetMetaData) rsMetadata) |
|
374 |
// .getSchemaName(rsIndex); |
|
375 |
// tableId.table = ((OracleResultSetMetaData) rsMetadata) |
|
376 |
// .getTableName(rsIndex); |
|
377 |
// tableId.field = ((OracleResultSetMetaData) rsMetadata) |
|
378 |
// .getColumnName(rsIndex); |
|
379 |
// |
|
380 |
// } else { |
|
381 |
// tableId.schema = rsMetadata.getSchemaName(rsIndex); |
|
382 |
// tableId.table = rsMetadata.getTableName(rsIndex); |
|
383 |
// tableId.field = rsMetadata.getColumnName(rsIndex); |
|
384 |
// } |
|
385 |
// } else { |
|
386 |
// tableId.schema = baseSchema; |
|
387 |
// tableId.table = baseTable; |
|
388 |
// tableId.field = rsMetadata.getColumnName(rsIndex); |
|
389 |
// } |
|
390 |
// if (tableId.table == null || tableId.table.length() == 0) { |
|
391 |
// // Si no tiene tabla origen (viene de algun calculo por ej.) |
|
392 |
// // lo saltamos ya que no estara en la tabla GEOMETRY_COLUMNS |
|
393 |
// continue; |
|
394 |
// } |
|
395 |
// set.add(tableId); |
|
396 |
// } |
|
397 |
// |
|
398 |
// if (set.size() == 0) { |
|
399 |
// return; |
|
400 |
// } |
|
401 |
// |
|
402 |
// // Preparamos una sql para que nos saque el resultado |
|
403 |
// StringBuilder strb = new StringBuilder(); |
|
404 |
// strb |
|
405 |
// .append("Select geometry_columns.*,auth_name || ':' || auth_srid as SRSID "); |
|
406 |
// strb.append("from geometry_columns left join spatial_ref_sys on "); |
|
407 |
// strb.append("geometry_columns.srid = spatial_ref_sys.srid WHERE "); |
|
408 |
// iter = set.iterator(); |
|
409 |
// for (int i = 0; i < set.size() - 1; i++) { |
|
410 |
// tableId = (TableId) iter.next(); |
|
411 |
// tableId.appendToSQL(strb); |
|
412 |
// strb.append(" OR "); |
|
413 |
// } |
|
414 |
// tableId = (TableId) iter.next(); |
|
415 |
// tableId.appendToSQL(strb); |
|
416 |
// String sql = strb.toString(); |
|
417 |
// |
|
418 |
// st = conn.createStatement(); |
|
419 |
// try { |
|
420 |
// rs = st.executeQuery(sql); |
|
421 |
// } catch (SQLException e) { |
|
422 |
// throw new JDBCExecuteSQLException(sql, e); |
|
423 |
// } |
|
424 |
// String srsID; |
|
425 |
// int oraSrid; |
|
426 |
// int geometryType; |
|
427 |
// int geometrySubtype; |
|
428 |
// String geomTypeStr; |
|
429 |
// int dimensions; |
|
430 |
// IProjection srs; |
|
431 |
// |
|
432 |
// while (rs.next()) { |
|
433 |
// srsID = rs.getString("SRSID"); |
|
434 |
// oraSrid = rs.getInt("SRID"); |
|
435 |
// geomTypeStr = rs.getString("TYPE").toUpperCase(); |
|
436 |
// geometryType = Geometry.TYPES.GEOMETRY; |
|
437 |
// if (geomTypeStr.startsWith("POINT")) { |
|
438 |
// geometryType = Geometry.TYPES.POINT; |
|
439 |
// } else if (geomTypeStr.startsWith("LINESTRING")) { |
|
440 |
// geometryType = Geometry.TYPES.CURVE; |
|
441 |
// } else if (geomTypeStr.startsWith("POLYGON")) { |
|
442 |
// geometryType = Geometry.TYPES.SURFACE; |
|
443 |
// } else if (geomTypeStr.startsWith("MULTIPOINT")) { |
|
444 |
// geometryType = Geometry.TYPES.MULTIPOINT; |
|
445 |
// } else if (geomTypeStr.startsWith("MULTILINESTRING")) { |
|
446 |
// geometryType = Geometry.TYPES.MULTICURVE; |
|
447 |
// } else if (geomTypeStr.startsWith("MULTIPOLYGON")) { |
|
448 |
// geometryType = Geometry.TYPES.MULTISURFACE; |
|
449 |
// } |
|
450 |
// dimensions = rs.getInt("coord_dimension"); |
|
451 |
// geometrySubtype = Geometry.SUBTYPES.GEOM2D; |
|
452 |
// if (dimensions > 2) { |
|
453 |
// if (dimensions == 3) { |
|
454 |
// if (geomTypeStr.endsWith("M")) { |
|
455 |
// geometrySubtype = Geometry.SUBTYPES.GEOM2DM; |
|
456 |
// } else { |
|
457 |
// geometrySubtype = Geometry.SUBTYPES.GEOM3D; |
|
458 |
// } |
|
459 |
// |
|
460 |
// } else { |
|
461 |
// geometrySubtype = Geometry.SUBTYPES.GEOM3DM; |
|
462 |
// } |
|
463 |
// } |
|
464 |
// |
|
465 |
// iter = geoAttrs.iterator(); |
|
466 |
// while (iter.hasNext()) { |
|
467 |
// attr = (EditableFeatureAttributeDescriptor) iter.next(); |
|
468 |
// rsIndex = attr.getIndex() + 1; |
|
469 |
// if (!rsMetadata.getColumnName(rsIndex).equals( |
|
470 |
// rs.getString("f_geometry_column"))) { |
|
471 |
// continue; |
|
472 |
// } |
|
473 |
// |
|
474 |
// if (baseSchema == null && baseTable == null) { |
|
475 |
// |
|
476 |
// if (rsMetadata instanceof PGResultSetMetaData) { |
|
477 |
// if (!((PGResultSetMetaData) rsMetadata) |
|
478 |
// .getBaseTableName(rsIndex).equals( |
|
479 |
// rs.getString("f_table_name"))) { |
|
480 |
// continue; |
|
481 |
// } |
|
482 |
// String curSchema = rs.getString("f_table_schema"); |
|
483 |
// String metaSchema = ((PGResultSetMetaData) rsMetadata) |
|
484 |
// .getBaseSchemaName(rsIndex); |
|
485 |
// if (!metaSchema.equals(curSchema)) { |
|
486 |
// if (metaSchema.length() == 0 |
|
487 |
// && metaSchema == getDefaultSchema(conn)) { |
|
488 |
// } else { |
|
489 |
// continue; |
|
490 |
// } |
|
491 |
// } |
|
492 |
// |
|
493 |
// } else { |
|
494 |
// |
|
495 |
// if (!rsMetadata.getTableName(rsIndex).equals( |
|
496 |
// rs.getString("f_table_name"))) { |
|
497 |
// continue; |
|
498 |
// } |
|
499 |
// String curSchema = rs.getString("f_table_schema"); |
|
500 |
// String metaSchema = rsMetadata |
|
501 |
// .getSchemaName(rsIndex); |
|
502 |
// if (!metaSchema.equals(curSchema)) { |
|
503 |
// if (metaSchema.length() == 0 |
|
504 |
// && metaSchema == getDefaultSchema(conn)) { |
|
505 |
// } else { |
|
506 |
// continue; |
|
507 |
// } |
|
508 |
// } |
|
509 |
// } |
|
510 |
// } |
|
511 |
// attr.setGeometryType(geometryType); |
|
512 |
// attr.setGeometrySubType(geometrySubtype); |
|
513 |
// if (srsID != null && srsID.length() > 0) { |
|
514 |
// attr.setSRS(CRSFactory.getCRS(srsID)); |
|
515 |
// } |
|
516 |
// iter.remove(); |
|
517 |
// } |
|
518 |
// iter = geoAttrs.iterator(); |
|
519 |
// while (iter.hasNext()) { |
|
520 |
// attr = (EditableFeatureAttributeDescriptor) iter.next(); |
|
521 |
// attr.setSRS(null); |
|
522 |
// attr.setGeometryType(Geometry.TYPES.GEOMETRY); |
|
523 |
// |
|
524 |
// } |
|
525 |
// } |
|
526 |
// |
|
527 |
// } catch (java.sql.SQLException e) { |
|
528 |
// throw new JDBCSQLException(e); |
|
529 |
// } finally { |
|
530 |
// try { |
|
531 |
// rs.close(); |
|
532 |
// } catch (Exception e) { |
|
533 |
// } |
|
534 |
// ; |
|
535 |
// try { |
|
536 |
// st.close(); |
|
537 |
// } catch (Exception e) { |
|
538 |
// } |
|
539 |
// ; |
|
540 |
// } |
|
541 |
// |
|
542 |
// } |
|
543 |
|
|
544 | 291 |
/** |
545 | 292 |
* get geometry column name "SDO_GEOMETRY" |
546 | 293 |
*/ |
... | ... | |
588 | 335 |
if (typeName != null) { |
589 | 336 |
return typeName; |
590 | 337 |
} |
591 |
|
|
592 | 338 |
throw new UnsupportedDataTypeException(attr.getDataTypeName(), attr |
593 | 339 |
.getDataType()); |
594 | 340 |
} |
... | ... | |
596 | 342 |
} |
597 | 343 |
|
598 | 344 |
/** |
599 |
* get geometry dimension
|
|
345 |
* Get geometry dimension
|
|
600 | 346 |
* |
601 | 347 |
* @param geometrySubType |
602 | 348 |
* @return |
... | ... | |
618 | 364 |
} |
619 | 365 |
} |
620 | 366 |
|
367 |
/** |
|
368 |
* Get Oracle geometry type |
|
369 |
* |
|
370 |
* @param geometryType |
|
371 |
* @param geometrySubType |
|
372 |
* @return |
|
373 |
*/ |
|
621 | 374 |
public String getOraGeomType(int geometryType, int geometrySubType) { |
622 | 375 |
String oraGeomType; |
623 | 376 |
switch (geometryType) { |
... | ... | |
673 | 426 |
throw new UnsupportedGeometryException(geometryType, |
674 | 427 |
geometrySubType); |
675 | 428 |
} |
676 |
|
|
677 | 429 |
return oraGeomType; |
678 | 430 |
} |
679 | 431 |
|
... | ... | |
715 | 467 |
} |
716 | 468 |
return -1; |
717 | 469 |
} |
718 |
|
|
470 |
|
|
719 | 471 |
/** |
720 | 472 |
* Get oracle srid from srs code(EPSG code) |
721 | 473 |
*/ |
... | ... | |
771 | 523 |
return -1; |
772 | 524 |
} |
773 | 525 |
|
774 |
// private int searchOraSRID(final IProjection srs) { |
|
775 |
// if (srs == null) { |
|
776 |
// return -1; |
|
777 |
// } |
|
778 |
// return searchOraSRID(srs.getAbrev()); |
|
779 |
// } |
|
780 |
// |
|
781 |
// private int searchOraSRID(final String srsID) { |
|
782 |
// if (srsID == null) { |
|
783 |
// return -1; |
|
784 |
// } |
|
785 |
// |
|
786 |
// ConnectionAction action = new ConnectionAction() { |
|
787 |
// |
|
788 |
// public Object action(Connection conn) throws DataException { |
|
789 |
// // select srid from spatial_ref_sys where auth_name = 'EPSG' and |
|
790 |
// // auth_srid = 23030 |
|
791 |
// String[] abrev = srsID.split(":"); |
|
792 |
// StringBuilder sqlb = new StringBuilder(); |
|
793 |
// sqlb.append("select SRID from MDSYS.CS_SRS where "); |
|
794 |
// if (abrev.length > 1) { |
|
795 |
// sqlb.append("auth_name = ? and "); |
|
796 |
// } |
|
797 |
// sqlb.append("auth_srid = ?"); |
|
798 |
// |
|
799 |
// String sql = sqlb.toString(); |
|
800 |
// PreparedStatement st; |
|
801 |
// try { |
|
802 |
// st = conn.prepareStatement(sql); |
|
803 |
// } catch (SQLException e) { |
|
804 |
// throw new JDBCPreparingSQLException(sql, e); |
|
805 |
// } |
|
806 |
// ResultSet rs = null; |
|
807 |
// try { |
|
808 |
// int i = 0; |
|
809 |
// if (abrev.length > 1) { |
|
810 |
// st.setString(i + 1, abrev[i]); |
|
811 |
// i++; |
|
812 |
// } |
|
813 |
// st.setInt(i + 1, Integer.parseInt(abrev[i])); |
|
814 |
// |
|
815 |
// try { |
|
816 |
// rs = st.executeQuery(); |
|
817 |
// } catch (SQLException e) { |
|
818 |
// throw new JDBCExecutePreparedSQLException(sql, abrev, e); |
|
819 |
// } |
|
820 |
// |
|
821 |
// if (!rs.next()) { |
|
822 |
// return null; |
|
823 |
// } |
|
824 |
// |
|
825 |
// return new Integer(rs.getInt(1)); |
|
826 |
// |
|
827 |
// } catch (SQLException e) { |
|
828 |
// throw new JDBCSQLException(e); |
|
829 |
// } finally { |
|
830 |
// try { |
|
831 |
// rs.close(); |
|
832 |
// } catch (Exception e) { |
|
833 |
// } |
|
834 |
// ; |
|
835 |
// try { |
|
836 |
// st.close(); |
|
837 |
// } catch (Exception e) { |
|
838 |
// } |
|
839 |
// ; |
|
840 |
// } |
|
841 |
// |
|
842 |
// } |
|
843 |
// |
|
844 |
// }; |
|
845 |
// |
|
846 |
// Integer oraSRSID = null; |
|
847 |
// try { |
|
848 |
// oraSRSID = (Integer) doConnectionAction(action); |
|
849 |
// return oraSRSID.intValue(); |
|
850 |
// } catch (Exception e) { |
|
851 |
// logger.error("Excetion searching pgSRS", e); |
|
852 |
// return -1; |
|
853 |
// } |
|
854 |
// } |
|
855 | 526 |
|
856 |
public List getSqlGeometyFieldAdd(FeatureAttributeDescriptor attr, |
|
857 |
String table, String schema) { |
|
858 |
// SELECT AddGeometryColumn({schema}, {table}, {field}, {srid}(int), |
|
859 |
// {geomType}(Str), {dimensions}(int)) |
|
860 | 527 |
|
861 |
// gemoType: |
|
862 |
/* |
|
863 |
* POINT, LINESTRING, POLYGON, MULTIPOINT, MULTILINESTRING, |
|
864 |
* MULTIPOLYGON, GEOMETRYCOLLECTION POINTM, LINESTRINGM, POLYGONM, |
|
865 |
* MULTIPOINTM, MULTILINESTRINGM, MULTIPOLYGONM, GEOMETRYCOLLECTIONM |
|
866 |
*/ |
|
528 |
|
|
867 | 529 |
|
868 |
List sqls = new ArrayList(); |
|
869 |
|
|
870 |
StringBuilder strb = new StringBuilder(); |
|
871 |
strb.append("SELECT AddGeometryColumn('"); |
|
872 |
if (schema != null && schema.length() > 0) { |
|
873 |
strb.append(schema); |
|
874 |
strb.append("', '"); |
|
875 |
} |
|
876 |
strb.append(table); |
|
877 |
strb.append("', '"); |
|
878 |
strb.append(attr.getName()); |
|
879 |
strb.append("', "); |
|
880 |
// strb.append("-1"); |
|
881 |
strb.append(getProviderSRID(attr.getSRS())); |
|
882 |
strb.append(", '"); |
|
883 |
strb.append(getOraGeomType(attr.getGeometryType(), attr |
|
884 |
.getGeometrySubType())); |
|
885 |
strb.append("', "); |
|
886 |
strb.append(getOraGeomDimensions(attr.getGeometrySubType())); |
|
887 |
strb.append(")"); |
|
888 |
|
|
889 |
sqls.add(strb.toString()); |
|
890 |
|
|
891 |
/* ALTER TABLE muni10000_peq_test DROP CONSTRAINT enforce_srid_the_geom; */ |
|
892 |
/* |
|
893 |
* strb = new StringBuilder(); strb.append("Alter table "); if (schema |
|
894 |
* != null && schema.length() > 0) { strb.append(schema); |
|
895 |
* strb.append("."); } strb.append("f_table_name = '"); |
|
896 |
* strb.append(table); strb.append("' AND f_geometry_column = '"); |
|
897 |
* strb.append(attr.getName()); strb.append("' AND srid = -1"); |
|
898 |
* |
|
899 |
* |
|
900 |
* sqls.add(strb.toString()); |
|
901 |
*/ |
|
902 |
return sqls; |
|
903 |
} |
|
904 |
|
|
905 | 530 |
public String getSqlFieldName(FeatureAttributeDescriptor attribute) { |
906 | 531 |
if (attribute.getDataType() == DataTypes.GEOMETRY) { |
907 | 532 |
return "asBinary(" + super.getSqlFieldName(attribute) + ")"; |
... | ... | |
923 | 548 |
return super.createAttributeFromJDBC(type, conn, rsMetadata, colIndex); |
924 | 549 |
} |
925 | 550 |
|
926 |
|
|
927 |
|
|
928 | 551 |
public boolean allowAutomaticValues() { |
929 | 552 |
return Boolean.TRUE; |
930 | 553 |
} |
... | ... | |
937 | 560 |
return true; |
938 | 561 |
} |
939 | 562 |
|
563 |
/** |
|
564 |
* get sql with fields description |
|
565 |
* @param attr |
|
566 |
* @return |
|
567 |
*/ |
|
940 | 568 |
public String getSqlFieldDescription(FeatureAttributeDescriptor attr) |
941 | 569 |
throws DataException { |
942 | 570 |
|
... | ... | |
952 | 580 |
return strb.toString(); |
953 | 581 |
} |
954 | 582 |
|
955 |
public static int nvarchar2Limited(int n) { |
|
583 |
|
|
584 |
|
|
585 |
/** |
|
586 |
* UTility method to get the SQL sentence needed to update the geographic metadata table |
|
587 |
* with a new bounding box and SRS |
|
588 |
* |
|
589 |
* @param tName table name |
|
590 |
* @param ora_srid new SRS |
|
591 |
* @param bbox new bounding box |
|
592 |
* @param dim geometries dimension |
|
593 |
* @param withsrid False if the SRS is set to NULL. True otherwise. |
|
594 |
* @return the SQL sentence to perform the update |
|
595 |
*/ |
|
596 |
public String getSqlUpdateMetadata(OracleStoreParameters params, String ora_srid, |
|
597 |
Rectangle2D bbox, int dim, boolean withsrid) { |
|
598 |
|
|
599 |
String[] dim_name = new String[dim]; |
|
600 |
double tolerance = 0.5; |
|
601 |
|
|
602 |
String _ora_srid = ora_srid; |
|
603 |
if (_ora_srid == null) _ora_srid = "NULL"; |
|
956 | 604 |
|
957 |
if (n <= OracleValues.VARCHAR2_STANDARD_SIZE) |
|
958 |
return OracleValues.VARCHAR2_STANDARD_SIZE; |
|
959 |
if (n <= OracleValues.VARCHAR2_LONG_SIZE) |
|
960 |
return n; |
|
605 |
if (_ora_srid.compareTo(OracleValues.GEODETIC_SRID) == 0) { |
|
606 |
dim_name[0] = "LONGITUDE"; |
|
607 |
dim_name[1] = "LATITUDE"; |
|
608 |
} |
|
609 |
else { |
|
610 |
dim_name[0] = "X"; |
|
611 |
dim_name[1] = "Y"; |
|
961 | 612 |
|
962 |
return OracleValues.VARCHAR2_LONG_SIZE;
|
|
963 |
}
|
|
613 |
if (dim > 2) {
|
|
614 |
dim_name[2] = "Z";
|
|
964 | 615 |
|
616 |
if (dim > 3) { |
|
617 |
dim_name[3] = "T"; |
|
618 |
} |
|
619 |
} |
|
620 |
} |
|
621 |
|
|
622 |
double minx = bbox.getMinX(); |
|
623 |
double miny = bbox.getMinY(); |
|
624 |
double maxx = bbox.getMaxX(); |
|
625 |
double maxy = bbox.getMaxY(); |
|
626 |
|
|
627 |
String resp = "INSERT INTO " + OracleValues.USER_ORACLE_GEOMETADATA_VIEW + " " + |
|
628 |
" ( TABLE_NAME, COLUMN_NAME, DIMINFO, SRID ) " + " VALUES (" |
|
629 |
+ "'" + params.getTable() + "', " |
|
630 |
+ "'" + OracleValues.DEFAULT_GEO_FIELD + "', " + |
|
631 |
"MDSYS.SDO_DIM_ARRAY( " + "MDSYS.SDO_DIM_ELEMENT ('" + dim_name[0] + "', " + |
|
632 |
minx + ", " + maxx + ", " + tolerance + " ), " + |
|
633 |
"MDSYS.SDO_DIM_ELEMENT ('" + dim_name[1] + "', " + miny + ", " + |
|
634 |
maxy + ", " + tolerance + " ))"; |
|
635 |
|
|
636 |
if (dim > 2) { |
|
637 |
resp = resp.substring(0, resp.length() - 1) + ","; |
|
638 |
resp = resp + "MDSYS.SDO_DIM_ELEMENT ('" + dim_name[2] + |
|
639 |
"', 0.0, 100.0, " + tolerance + " ))"; |
|
640 |
|
|
641 |
if (dim > 3) { |
|
642 |
resp = resp.substring(0, resp.length() - 1) + ","; |
|
643 |
resp = resp + "MDSYS.SDO_DIM_ELEMENT ('" + dim_name[3] + |
|
644 |
"', 0.0, 100.0, " + tolerance + " ))"; |
|
645 |
} |
|
646 |
} |
|
647 |
|
|
648 |
if (withsrid) { |
|
649 |
resp = resp + ", " + _ora_srid + " )"; |
|
650 |
} |
|
651 |
else { |
|
652 |
resp = resp + ", NULL )"; |
|
653 |
} |
|
654 |
|
|
655 |
return resp; |
|
656 |
} |
|
657 |
|
|
658 |
|
|
965 | 659 |
public void loadFeatureType(EditableFeatureType featureType, |
966 | 660 |
JDBCStoreParameters storeParams) throws DataException { |
967 | 661 |
if (storeParams.getSQL() != null |
... | ... | |
979 | 673 |
} |
980 | 674 |
} |
981 | 675 |
|
982 |
private void loadSdoMetadata(Connection conn, String tablename, |
|
983 |
String geoColName) { |
|
984 |
try { |
|
985 |
Statement _st = conn.createStatement(); |
|
986 |
String[] tokens = tablename.split("\\u002E", 2); |
|
987 |
String qry; |
|
988 |
if (tokens.length > 1) { |
|
989 |
qry = "select * from " |
|
990 |
+ OracleValues.ALL_ORACLE_GEOMETADATA_VIEW |
|
991 |
+ " where OWNER = '" + tokens[0] |
|
992 |
+ "' AND TABLE_NAME = '" + tokens[1] |
|
993 |
+ "' AND COLUMN_NAME = '" + geoColName + "'"; |
|
994 |
} else { |
|
995 |
qry = "select * from " |
|
996 |
+ OracleValues.ALL_ORACLE_GEOMETADATA_VIEW |
|
997 |
+ " where TABLE_NAME = " + "'" + tablename |
|
998 |
+ "' AND COLUMN_NAME = '" + geoColName + "'"; |
|
999 |
|
|
1000 |
} |
|
1001 |
|
|
1002 |
ResultSet _rs = _st.executeQuery(qry); |
|
1003 |
|
|
1004 |
if (_rs.next()) { |
|
1005 |
oracleSRID = getOracleSridFromCurrentRecord(_rs); |
|
1006 |
|
|
1007 |
boolean isGeogCS = OracleUtils.getIsGCS(oracleSRID, tableHasSrid); |
|
1008 |
|
|
1009 |
int epsgSRID = -1; |
|
1010 |
|
|
1011 |
try { |
|
1012 |
|
|
1013 |
epsgSRID = getProviderEPSG(oracleSRID); |
|
1014 |
} catch (Exception e) { |
|
1015 |
logger.error("Unknown oracle SRID: " + oracleSRID); |
|
1016 |
tableHasSrid = false; |
|
1017 |
} |
|
1018 |
Rectangle2D full_Extent = getFullExtentFromCurrentRecord(_rs); |
|
1019 |
|
|
1020 |
boolean hasRealiableExtent = realiableExtent(full_Extent, |
|
1021 |
isGeogCS); |
|
1022 |
|
|
1023 |
if (!hasRealiableExtent) { |
|
1024 |
full_Extent = getFastEstimatedExtent(tablename, geoColName, |
|
1025 |
conn, 20, 10, isGeogCS); |
|
1026 |
} |
|
1027 |
|
|
1028 |
_rs.close(); |
|
1029 |
_st.close(); |
|
1030 |
} else { |
|
1031 |
throw new SQLException("Empty resultset from this query: " |
|
1032 |
+ qry); |
|
1033 |
} |
|
1034 |
} catch (SQLException se) { |
|
1035 |
logger.error("While getting SDO metadata: " + se.getMessage()); |
|
1036 |
} |
|
1037 |
} |
|
1038 |
|
|
1039 |
private String getOracleSridFromCurrentRecord(ResultSet _rs) |
|
1040 |
throws SQLException { |
|
1041 |
Object obj = _rs.getObject("SRID"); |
|
1042 |
|
|
1043 |
if (obj == null) { |
|
1044 |
logger.warn("No SRID found for this table."); |
|
1045 |
tableHasSrid = false; |
|
1046 |
|
|
1047 |
return null; |
|
1048 |
} |
|
1049 |
|
|
1050 |
return obj.toString(); |
|
1051 |
} |
|
1052 |
|
|
676 |
|
|
1053 | 677 |
/** |
1054 |
* get full extend of one record |
|
678 |
* Fill <code>featureType</code> geometry attributes with SRS and ShapeType |
|
679 |
* information stored in the table GEOMETRY_COLUMNS |
|
1055 | 680 |
* |
1056 |
* @param _rs |
|
1057 |
* @return |
|
1058 |
* @throws SQLException |
|
681 |
* @param conn |
|
682 |
* @param rsMetadata |
|
683 |
* @param featureType |
|
684 |
* @throws ReadException |
|
1059 | 685 |
*/ |
1060 |
private Rectangle2D getFullExtentFromCurrentRecord(ResultSet _rs)
|
|
1061 |
throws SQLException {
|
|
1062 |
ARRAY dim_info_array = (ARRAY) _rs.getObject("DIMINFO");
|
|
686 |
protected void loadSRS_and_shapeType(Connection conn,
|
|
687 |
ResultSetMetaData rsMetadata, EditableFeatureType featureType,
|
|
688 |
String baseSchema, String baseTable) throws JDBCException {
|
|
1063 | 689 |
|
1064 |
if (dim_info_array == null) { |
|
1065 |
// no full extent found: |
|
1066 |
return null; |
|
1067 |
} else { |
|
1068 |
Datum[] da = dim_info_array.getOracleArray(); |
|
690 |
Statement st = null; |
|
691 |
ResultSet rs = null; |
|
692 |
try { |
|
693 |
// Sacamos la lista de los attributos geometricos |
|
694 |
EditableFeatureAttributeDescriptor attr; |
|
695 |
List geoAttrs = new ArrayList(); |
|
1069 | 696 |
|
1070 |
STRUCT sx = (STRUCT) da[0]; |
|
1071 |
STRUCT sy = (STRUCT) da[1]; |
|
697 |
Iterator iter = featureType.iterator(); |
|
698 |
while (iter.hasNext()) { |
|
699 |
attr = (EditableFeatureAttributeDescriptor) iter.next(); |
|
700 |
if (attr.getDataType() == DataTypes.GEOMETRY) { |
|
701 |
geoAttrs.add(attr); |
|
702 |
} |
|
703 |
} |
|
704 |
if (geoAttrs.size() < 1) { |
|
705 |
return; |
|
706 |
} |
|
1072 | 707 |
|
1073 |
try { |
|
1074 |
double minx = Double.parseDouble(sx.getAttributes()[1] |
|
1075 |
.toString()); |
|
1076 |
double maxx = Double.parseDouble(sx.getAttributes()[2] |
|
1077 |
.toString()); |
|
1078 |
double miny = Double.parseDouble(sy.getAttributes()[1] |
|
1079 |
.toString()); |
|
1080 |
double maxy = Double.parseDouble(sy.getAttributes()[2] |
|
1081 |
.toString()); |
|
708 |
// preparamos un set con las lista de tablas de origen |
|
709 |
// de los campos |
|
710 |
class TableId { |
|
711 |
public String schema = null; |
|
712 |
public String table = null; |
|
713 |
public String field = null; |
|
1082 | 714 |
|
1083 |
if (minx > maxx) { |
|
1084 |
double aux = minx; |
|
1085 |
minx = maxx; |
|
1086 |
maxx = aux; |
|
715 |
public void appendToSQL(StringBuilder strb) { |
|
716 |
if (schema == null || schema.length() == 0) { |
|
717 |
strb |
|
718 |
.append("( F_TABLE_SCHEMA = current_schema() AND F_TABLE_NAME = '"); |
|
719 |
} else { |
|
720 |
strb.append("( F_TABLE_SCHEMA = '"); |
|
721 |
strb.append(schema); |
|
722 |
strb.append("' AND F_TABLE_NAME = '"); |
|
723 |
} |
|
724 |
strb.append(table); |
|
725 |
strb.append("' AND F_GEOMETRY_COLUMN = '"); |
|
726 |
strb.append(field); |
|
727 |
strb.append("' )"); |
|
1087 | 728 |
} |
1088 | 729 |
|
1089 |
if (miny > maxy) { |
|
1090 |
double aux = miny; |
|
1091 |
miny = maxy; |
|
1092 |
maxy = aux; |
|
1093 |
} |
|
1094 |
|
|
1095 |
return new Rectangle2D.Double(minx, maxx, miny, maxy); |
|
1096 |
} catch (Exception ex) { |
|
1097 |
logger.error("While getting full extent from metadata table."); |
|
1098 |
return null; |
|
1099 | 730 |
} |
1100 |
} |
|
1101 |
} |
|
731 |
Comparator cmp = new Comparator() { |
|
732 |
public int compare(Object arg0, Object arg1) { |
|
733 |
TableId a0 = (TableId) arg0; |
|
734 |
TableId a1 = (TableId) arg1; |
|
1102 | 735 |
|
1103 |
private boolean realiableExtent(Rectangle2D ext, boolean isgeodetic) {
|
|
1104 |
// if (!isgeodetic) return true;
|
|
1105 |
if ((ext.getMinX() > -179.9) || (ext.getMinX() < -180.1))
|
|
1106 |
return true;
|
|
1107 |
if ((ext.getMinY() > -89.9) || (ext.getMinY() < -90.1))
|
|
1108 |
return true;
|
|
1109 |
if ((ext.getWidth() < 359.9) || (ext.getWidth() > 360.1))
|
|
1110 |
return true;
|
|
1111 |
if ((ext.getHeight() < 179.9) || (ext.getHeight() > 180.1))
|
|
1112 |
return true;
|
|
1113 |
return false;
|
|
1114 |
}
|
|
1115 |
|
|
1116 |
private Rectangle2D getFastEstimatedExtent(
|
|
1117 |
String tname,
|
|
1118 |
String gfield,
|
|
1119 |
Connection c,
|
|
1120 |
int sample_size,
|
|
1121 |
double enlargement,
|
|
1122 |
boolean is_geo) {
|
|
736 |
if (!a0.field.equals(a1.field)) {
|
|
737 |
return -1;
|
|
738 |
}
|
|
739 |
if (!a0.table.equals(a1.table)) {
|
|
740 |
return -1;
|
|
741 |
}
|
|
742 |
if (!a0.schema.equals(a1.schema)) {
|
|
743 |
return -1;
|
|
744 |
}
|
|
745 |
return 0;
|
|
746 |
}
|
|
747 |
};
|
|
748 |
TreeSet set = new TreeSet(cmp); |
|
749 |
TableId tableId;
|
|
750 |
iter = geoAttrs.iterator();
|
|
751 |
int rsIndex;
|
|
752 |
while (iter.hasNext()) {
|
|
753 |
attr = (EditableFeatureAttributeDescriptor) iter.next();
|
|
754 |
tableId = new TableId();
|
|
755 |
rsIndex = attr.getIndex() + 1;
|
|
1123 | 756 |
|
1124 |
Rectangle2D world = new Rectangle2D.Double(-180, -90, 360, 180); |
|
1125 |
Rectangle2D resp_aux = null; |
|
1126 |
String qry = "SELECT " + gfield + " FROM " + tname + " WHERE ROWNUM <= " + sample_size; |
|
1127 |
ResultSet _rs = null; |
|
757 |
if (baseSchema == null && baseTable == null) { |
|
758 |
if (rsMetadata instanceof OracleResultSetMetaData) { |
|
759 |
tableId.schema = ((ResultSetMetaData) rsMetadata) |
|
760 |
.getSchemaName(rsIndex); |
|
761 |
tableId.table = ((ResultSetMetaData) rsMetadata) |
|
762 |
.getTableName(rsIndex); |
|
763 |
tableId.field = ((ResultSetMetaData) rsMetadata) |
|
764 |
.getColumnName(rsIndex); |
|
1128 | 765 |
|
1129 |
try { |
|
1130 |
PreparedStatement _st = c.prepareStatement(qry); |
|
1131 |
_rs = _st.executeQuery(); |
|
1132 |
while (_rs.next()) { |
|
1133 |
STRUCT aux = (STRUCT) _rs.getObject(1); |
|
1134 |
Geometry ig = getGeometryUsing(aux, false); |
|
1135 |
|
|
1136 |
if (ig == null) continue; |
|
1137 |
|
|
1138 |
if (resp_aux == null) { |
|
1139 |
resp_aux = ig.getBounds(); |
|
1140 | 766 |
} else { |
1141 |
resp_aux.add(ig.getBounds()); |
|
767 |
tableId.schema = rsMetadata.getSchemaName(rsIndex); |
|
768 |
tableId.table = rsMetadata.getTableName(rsIndex); |
|
769 |
tableId.field = rsMetadata.getColumnName(rsIndex); |
|
1142 | 770 |
} |
1143 |
|
|
771 |
} else { |
|
772 |
tableId.schema = baseSchema; |
|
773 |
tableId.table = baseTable; |
|
774 |
tableId.field = rsMetadata.getColumnName(rsIndex); |
|
1144 | 775 |
} |
1145 |
} catch (Exception ex) { |
|
1146 |
logger.error("While getting random sample: " + ex.getMessage()); |
|
776 |
if (tableId.table == null || tableId.table.length() == 0) { |
|
777 |
// Si no tiene tabla origen (viene de algun calculo por ej.) |
|
778 |
// lo saltamos ya que no estara en la tabla GEOMETRY_COLUMNS |
|
779 |
continue; |
|
780 |
} |
|
781 |
set.add(tableId); |
|
1147 | 782 |
} |
1148 | 783 |
|
1149 |
if (resp_aux == null) { |
|
1150 |
logger.warn("ERROR, ESTIMATED BB = WORLD"); |
|
1151 |
return world; |
|
784 |
if (set.size() == 0) { |
|
785 |
return; |
|
1152 | 786 |
} |
1153 |
|
|
1154 |
double w = resp_aux.getWidth(); |
|
1155 |
double h = resp_aux.getHeight(); |
|
1156 |
double x = resp_aux.getMinX(); |
|
1157 |
double y = resp_aux.getMinY(); |
|
1158 | 787 |
|
1159 |
// enlarge n times: |
|
1160 |
double newx = x - (0.5 * (enlargement - 1)) * w; |
|
1161 |
double newy = y - (0.5 * (enlargement - 1)) * h; |
|
1162 |
Rectangle2D resp_aux_large = new Rectangle2D.Double(newx, newy, |
|
1163 |
enlargement * w, |
|
1164 |
enlargement * h); |
|
1165 |
|
|
1166 |
if (is_geo) { |
|
1167 |
Rectangle2D.intersect(world, resp_aux_large, resp_aux); |
|
1168 |
logger.debug("FAST BB: " + resp_aux.toString()); |
|
1169 |
return resp_aux; |
|
1170 |
} else { |
|
1171 |
logger.debug("FAST BB: " + resp_aux_large.toString()); |
|
1172 |
return resp_aux_large; |
|
788 |
// Preparamos una sql para que nos saque el resultado |
|
789 |
StringBuilder strb = new StringBuilder(); |
|
790 |
strb |
|
791 |
.append("Select geometry_columns.*,auth_name || ':' || auth_srid as SRSID "); |
|
792 |
strb.append("from geometry_columns left join spatial_ref_sys on "); |
|
793 |
strb.append("geometry_columns.srid = spatial_ref_sys.srid WHERE "); |
|
794 |
iter = set.iterator(); |
|
795 |
for (int i = 0; i < set.size() - 1; i++) { |
|
796 |
tableId = (TableId) iter.next(); |
|
797 |
tableId.appendToSQL(strb); |
|
798 |
strb.append(" OR "); |
|
1173 | 799 |
} |
800 |
tableId = (TableId) iter.next(); |
|
801 |
tableId.appendToSQL(strb); |
|
802 |
String sql = strb.toString(); |
|
1174 | 803 |
|
1175 |
} |
|
1176 |
|
|
1177 |
/** |
|
1178 |
* Utility method to get a geometry from a struct. |
|
1179 |
* |
|
1180 |
* @param theStruct the struct to be converted |
|
1181 |
* @param use_gtools switch to use geotools classes or not |
|
1182 |
* @return the geometry |
|
1183 |
* @throws SQLException |
|
1184 |
*/ |
|
1185 |
public Geometry getGeometryUsing(STRUCT theStruct, boolean use_gtools) |
|
1186 |
throws SQLException { |
|
1187 |
Geometry _igeom = null; |
|
1188 |
|
|
1189 |
if (theStruct == null) { |
|
1190 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
|
1191 |
return geomManager.createNullGeometry(subType); |
|
1192 |
} |
|
1193 |
|
|
1194 |
if (use_gtools) { // geotools |
|
1195 |
// _igeom = getGeotoolsIGeometry(theStruct); |
|
1196 |
} |
|
1197 |
else { // jgeometry |
|
1198 |
_igeom = getFMapGeometry(theStruct, false); |
|
1199 |
} |
|
1200 |
|
|
1201 |
return _igeom; |
|
1202 |
} |
|
1203 |
|
|
1204 |
/** |
|
1205 |
* Fill <code>featureType</code> geometry attributes with SRS and ShapeType |
|
1206 |
* information stored in the table GEOMETRY_COLUMNS |
|
1207 |
* |
|
1208 |
* @param conn |
|
1209 |
* @param rsMetadata |
|
1210 |
* @param featureType |
|
1211 |
* @throws ReadException |
|
1212 |
*/ |
|
1213 |
protected void loadSRS_and_shapeType(Connection conn, |
|
1214 |
ResultSetMetaData rsMetadata, EditableFeatureType featureType, |
|
1215 |
String baseSchema, String baseTable) |
|
1216 |
throws JDBCException { |
|
1217 |
|
|
1218 |
Statement st = null; |
|
1219 |
ResultSet rs = null; |
|
804 |
st = conn.createStatement(); |
|
1220 | 805 |
try { |
1221 |
// Sacamos la lista de los attributos geometricos |
|
1222 |
EditableFeatureAttributeDescriptor attr; |
|
1223 |
List geoAttrs = new ArrayList(); |
|
806 |
rs = st.executeQuery(sql); |
|
807 |
} catch (SQLException e) { |
|
808 |
throw new JDBCExecuteSQLException(sql, e); |
|
809 |
} |
|
810 |
String srsID; |
|
811 |
int pgSrid; |
|
812 |
int geometryType; |
|
813 |
int geometrySubtype; |
|
814 |
String geomTypeStr; |
|
815 |
int dimensions; |
|
816 |
IProjection srs; |
|
1224 | 817 |
|
1225 |
Iterator iter = featureType.iterator(); |
|
1226 |
while (iter.hasNext()) { |
|
1227 |
attr = (EditableFeatureAttributeDescriptor) iter.next(); |
|
1228 |
if (attr.getDataType() == DataTypes.GEOMETRY) { |
|
1229 |
geoAttrs.add(attr); |
|
1230 |
} |
|
818 |
while (rs.next()) { |
|
819 |
srsID = rs.getString("SRSID"); |
|
820 |
pgSrid = rs.getInt("SRID"); |
|
821 |
geomTypeStr = rs.getString("TYPE").toUpperCase(); |
|
822 |
geometryType = Geometry.TYPES.GEOMETRY; |
|
823 |
if (geomTypeStr.startsWith("POINT")) { |
|
824 |
geometryType = Geometry.TYPES.POINT; |
|
825 |
} else if (geomTypeStr.startsWith("LINESTRING")) { |
|
826 |
geometryType = Geometry.TYPES.CURVE; |
|
827 |
} else if (geomTypeStr.startsWith("POLYGON")) { |
|
828 |
geometryType = Geometry.TYPES.SURFACE; |
|
829 |
} else if (geomTypeStr.startsWith("MULTIPOINT")) { |
|
830 |
geometryType = Geometry.TYPES.MULTIPOINT; |
|
831 |
} else if (geomTypeStr.startsWith("MULTILINESTRING")) { |
|
832 |
geometryType = Geometry.TYPES.MULTICURVE; |
|
833 |
} else if (geomTypeStr.startsWith("MULTIPOLYGON")) { |
|
834 |
geometryType = Geometry.TYPES.MULTISURFACE; |
|
1231 | 835 |
} |
1232 |
if (geoAttrs.size() < 1) { |
|
1233 |
return; |
|
1234 |
} |
|
1235 |
|
|
1236 |
|
|
1237 |
// preparamos un set con las lista de tablas de origen |
|
1238 |
// de los campos |
|
1239 |
class TableId { |
|
1240 |
public String schema=null; |
|
1241 |
public String table=null; |
|
1242 |
public String field = null; |
|
1243 |
|
|
1244 |
public void appendToSQL(StringBuilder strb) { |
|
1245 |
if (schema == null || schema.length() == 0) { |
|
1246 |
strb |
|
1247 |
.append("( F_TABLE_SCHEMA = current_schema() AND F_TABLE_NAME = '"); |
|
836 |
dimensions = rs.getInt("coord_dimension"); |
|
837 |
geometrySubtype = Geometry.SUBTYPES.GEOM2D; |
|
838 |
if (dimensions > 2) { |
|
839 |
if (dimensions == 3) { |
|
840 |
if (geomTypeStr.endsWith("M")) { |
|
841 |
geometrySubtype = Geometry.SUBTYPES.GEOM2DM; |
|
1248 | 842 |
} else { |
1249 |
strb.append("( F_TABLE_SCHEMA = '"); |
|
1250 |
strb.append(schema); |
|
1251 |
strb.append("' AND F_TABLE_NAME = '"); |
|
843 |
geometrySubtype = Geometry.SUBTYPES.GEOM3D; |
|
1252 | 844 |
} |
1253 |
strb.append(table); |
|
1254 |
strb.append("' AND F_GEOMETRY_COLUMN = '"); |
|
1255 |
strb.append(field); |
|
1256 |
strb.append("' )"); |
|
1257 |
} |
|
1258 | 845 |
|
846 |
} else { |
|
847 |
geometrySubtype = Geometry.SUBTYPES.GEOM3DM; |
|
848 |
} |
|
1259 | 849 |
} |
1260 |
Comparator cmp = new Comparator(){ |
|
1261 |
public int compare(Object arg0, Object arg1) { |
|
1262 |
TableId a0 = (TableId) arg0; |
|
1263 |
TableId a1 = (TableId) arg1; |
|
1264 | 850 |
|
1265 |
if (!a0.field.equals(a1.field)) { |
|
1266 |
return -1; |
|
1267 |
} |
|
1268 |
if (!a0.table.equals(a1.table)) { |
|
1269 |
return -1; |
|
1270 |
} |
|
1271 |
if (!a0.schema.equals(a1.schema)) { |
|
1272 |
return -1; |
|
1273 |
} |
|
1274 |
return 0; |
|
1275 |
} |
|
1276 |
}; |
|
1277 |
TreeSet set = new TreeSet(cmp); |
|
1278 |
TableId tableId; |
|
1279 | 851 |
iter = geoAttrs.iterator(); |
1280 |
int rsIndex; |
|
1281 | 852 |
while (iter.hasNext()) { |
1282 | 853 |
attr = (EditableFeatureAttributeDescriptor) iter.next(); |
1283 |
tableId = new TableId(); |
|
1284 | 854 |
rsIndex = attr.getIndex() + 1; |
1285 |
|
|
1286 |
if (baseSchema == null && baseTable == null) { |
|
1287 |
if (rsMetadata instanceof OracleResultSetMetaData) { |
|
1288 |
tableId.schema = ((ResultSetMetaData) rsMetadata) |
|
1289 |
.getSchemaName(rsIndex); |
|
1290 |
tableId.table = ((ResultSetMetaData) rsMetadata) |
|
1291 |
.getTableName(rsIndex); |
|
1292 |
tableId.field = ((ResultSetMetaData) rsMetadata) |
|
1293 |
.getColumnName(rsIndex); |
|
1294 |
|
|
1295 |
} else { |
|
1296 |
tableId.schema = rsMetadata.getSchemaName(rsIndex); |
|
1297 |
tableId.table = rsMetadata.getTableName(rsIndex); |
|
1298 |
tableId.field = rsMetadata.getColumnName(rsIndex); |
|
1299 |
} |
|
1300 |
} else { |
|
1301 |
tableId.schema = baseSchema; |
|
1302 |
tableId.table = baseTable; |
|
1303 |
tableId.field = rsMetadata.getColumnName(rsIndex); |
|
1304 |
} |
|
1305 |
if (tableId.table == null || tableId.table.length() == 0) { |
|
1306 |
// Si no tiene tabla origen (viene de algun calculo por ej.) |
|
1307 |
// lo saltamos ya que no estara en la tabla GEOMETRY_COLUMNS |
|
855 |
if (!rsMetadata.getColumnName(rsIndex).equals( |
|
856 |
rs.getString("f_geometry_column"))) { |
|
1308 | 857 |
continue; |
1309 | 858 |
} |
1310 |
set.add(tableId); |
|
1311 |
} |
|
1312 | 859 |
|
1313 |
if (set.size() == 0) { |
|
1314 |
return; |
|
1315 |
} |
|
860 |
if (baseSchema == null && baseTable == null) { |
|
1316 | 861 |
|
1317 |
// Preparamos una sql para que nos saque el resultado |
|
1318 |
StringBuilder strb = new StringBuilder(); |
|
1319 |
strb.append("Select geometry_columns.*,auth_name || ':' || auth_srid as SRSID "); |
|
1320 |
strb.append("from geometry_columns left join spatial_ref_sys on "); |
|
1321 |
strb.append("geometry_columns.srid = spatial_ref_sys.srid WHERE "); |
|
1322 |
iter = set.iterator(); |
|
1323 |
for (int i=0;i<set.size()-1;i++) { |
|
1324 |
tableId = (TableId) iter.next(); |
|
1325 |
tableId.appendToSQL(strb); |
|
1326 |
strb.append(" OR "); |
|
1327 |
} |
|
1328 |
tableId = (TableId) iter.next(); |
|
1329 |
tableId.appendToSQL(strb); |
|
1330 |
String sql = strb.toString(); |
|
1331 |
|
|
1332 |
|
|
1333 |
st = conn.createStatement(); |
|
1334 |
try { |
|
1335 |
rs = st.executeQuery(sql); |
|
1336 |
} catch (SQLException e) { |
|
1337 |
throw new JDBCExecuteSQLException(sql, e); |
|
1338 |
} |
|
1339 |
String srsID; |
|
1340 |
int pgSrid; |
|
1341 |
int geometryType; |
|
1342 |
int geometrySubtype; |
|
1343 |
String geomTypeStr; |
|
1344 |
int dimensions; |
|
1345 |
IProjection srs; |
|
1346 |
|
|
1347 |
while (rs.next()){ |
|
1348 |
srsID = rs.getString("SRSID"); |
|
1349 |
pgSrid = rs.getInt("SRID"); |
|
1350 |
geomTypeStr = rs.getString("TYPE").toUpperCase(); |
|
1351 |
geometryType = Geometry.TYPES.GEOMETRY; |
|
1352 |
if (geomTypeStr.startsWith("POINT")) { |
|
1353 |
geometryType = Geometry.TYPES.POINT; |
|
1354 |
} else if (geomTypeStr.startsWith("LINESTRING")) { |
|
1355 |
geometryType = Geometry.TYPES.CURVE; |
|
1356 |
} else if (geomTypeStr.startsWith("POLYGON")) { |
|
1357 |
geometryType = Geometry.TYPES.SURFACE; |
|
1358 |
} else if (geomTypeStr.startsWith("MULTIPOINT")) { |
|
1359 |
geometryType = Geometry.TYPES.MULTIPOINT; |
|
1360 |
} else if (geomTypeStr.startsWith("MULTILINESTRING")) { |
|
1361 |
geometryType = Geometry.TYPES.MULTICURVE; |
|
1362 |
} else if (geomTypeStr.startsWith("MULTIPOLYGON")) { |
|
1363 |
geometryType = Geometry.TYPES.MULTISURFACE; |
|
1364 |
} |
|
1365 |
dimensions = rs.getInt("coord_dimension"); |
|
1366 |
geometrySubtype = Geometry.SUBTYPES.GEOM2D; |
|
1367 |
if (dimensions > 2) { |
|
1368 |
if (dimensions == 3) { |
|
1369 |
if (geomTypeStr.endsWith("M")) { |
|
1370 |
geometrySubtype = Geometry.SUBTYPES.GEOM2DM; |
|
1371 |
} else { |
|
1372 |
geometrySubtype = Geometry.SUBTYPES.GEOM3D; |
|
862 |
if (rsMetadata instanceof PGResultSetMetaData) { |
|
863 |
if (!((PGResultSetMetaData) rsMetadata) |
|
864 |
.getBaseTableName(rsIndex).equals( |
|
865 |
rs.getString("f_table_name"))) { |
|
866 |
continue; |
|
1373 | 867 |
} |
1374 |
|
|
1375 |
} else { |
|
1376 |
geometrySubtype = Geometry.SUBTYPES.GEOM3DM; |
|
1377 |
} |
|
1378 |
} |
|
1379 |
|
|
1380 |
|
|
1381 |
iter = geoAttrs.iterator(); |
|
1382 |
while (iter.hasNext()) { |
|
1383 |
attr = (EditableFeatureAttributeDescriptor) iter.next(); |
|
1384 |
rsIndex = attr.getIndex() + 1; |
|
1385 |
if (!rsMetadata.getColumnName(rsIndex).equals( |
|
1386 |
rs.getString("f_geometry_column"))) { |
|
1387 |
continue; |
|
1388 |
} |
|
1389 |
|
|
1390 |
if (baseSchema == null && baseTable == null) { |
|
1391 |
|
|
1392 |
if (rsMetadata instanceof PGResultSetMetaData) { |
|
1393 |
if (!((PGResultSetMetaData) rsMetadata) |
|
1394 |
.getBaseTableName(rsIndex).equals( |
|
1395 |
rs.getString("f_table_name"))) { |
|
868 |
String curSchema = rs.getString("f_table_schema"); |
|
869 |
String metaSchema = ((PGResultSetMetaData) rsMetadata) |
|
870 |
.getBaseSchemaName(rsIndex); |
|
871 |
if (!metaSchema.equals(curSchema)) { |
|
872 |
if (metaSchema.length() == 0 |
|
873 |
&& metaSchema == getDefaultSchema(conn)) { |
|
874 |
} else { |
|
1396 | 875 |
continue; |
1397 | 876 |
} |
1398 |
String curSchema = rs.getString("f_table_schema"); |
|
1399 |
String metaSchema = ((PGResultSetMetaData) rsMetadata) |
|
1400 |
.getBaseSchemaName(rsIndex); |
|
1401 |
if (!metaSchema.equals(curSchema)) { |
|
1402 |
if (metaSchema.length() == 0 |
|
1403 |
&& metaSchema == getDefaultSchema(conn)) { |
|
1404 |
} else { |
|
1405 |
continue; |
|
1406 |
} |
|
1407 |
} |
|
877 |
} |
|
1408 | 878 |
|
1409 |
} else {
|
|
879 |
} else { |
|
1410 | 880 |
|
1411 |
if (!rsMetadata.getTableName(rsIndex).equals( |
|
1412 |
rs.getString("f_table_name"))) { |
|
881 |
if (!rsMetadata.getTableName(rsIndex).equals( |
|
882 |
rs.getString("f_table_name"))) { |
|
883 |
continue; |
|
884 |
} |
|
885 |
String curSchema = rs.getString("f_table_schema"); |
|
886 |
String metaSchema = rsMetadata |
|
887 |
.getSchemaName(rsIndex); |
|
888 |
if (!metaSchema.equals(curSchema)) { |
|
889 |
if (metaSchema.length() == 0 |
|
890 |
&& metaSchema == getDefaultSchema(conn)) { |
|
891 |
} else { |
|
1413 | 892 |
continue; |
1414 | 893 |
} |
1415 |
String curSchema = rs.getString("f_table_schema"); |
|
1416 |
String metaSchema = rsMetadata |
|
1417 |
.getSchemaName(rsIndex); |
|
1418 |
if (!metaSchema.equals(curSchema)) { |
|
1419 |
if (metaSchema.length() == 0 |
|
1420 |
&& metaSchema == getDefaultSchema(conn)) { |
|
1421 |
} else { |
|
1422 |
continue; |
|
1423 |
} |
|
1424 |
} |
|
1425 | 894 |
} |
1426 | 895 |
} |
1427 |
attr.setGeometryType(geometryType); |
|
1428 |
attr.setGeometrySubType(geometrySubtype); |
|
1429 |
if (srsID != null && srsID.length() > 0) { |
|
1430 |
attr.setSRS(CRSFactory.getCRS(srsID)); |
|
1431 |
} |
|
1432 |
iter.remove(); |
|
1433 | 896 |
} |
1434 |
iter = geoAttrs.iterator(); |
|
1435 |
while (iter.hasNext()) { |
|
1436 |
attr = (EditableFeatureAttributeDescriptor) iter.next(); |
|
1437 |
attr.setSRS(null); |
|
1438 |
attr.setGeometryType(Geometry.TYPES.GEOMETRY); |
|
1439 |
|
|
897 |
attr.setGeometryType(geometryType); |
|
898 |
attr.setGeometrySubType(geometrySubtype); |
|
899 |
if (srsID != null && srsID.length() > 0) { |
|
900 |
attr.setSRS(CRSFactory.getCRS(srsID)); |
|
1440 | 901 |
} |
902 |
iter.remove(); |
|
1441 | 903 |
} |
904 |
iter = geoAttrs.iterator(); |
|
905 |
while (iter.hasNext()) { |
|
906 |
attr = (EditableFeatureAttributeDescriptor) iter.next(); |
|
907 |
attr.setSRS(null); |
|
908 |
attr.setGeometryType(Geometry.TYPES.GEOMETRY); |
|
1442 | 909 |
|
1443 |
} catch (java.sql.SQLException e) { |
|
1444 |
throw new JDBCSQLException(e); |
|
1445 |
} finally { |
|
1446 |
try {rs.close();} catch (Exception e) { }; |
|
1447 |
try {st.close();} catch (Exception e) { }; |
|
910 |
} |
|
1448 | 911 |
} |
1449 | 912 |
|
913 |
} catch (java.sql.SQLException e) { |
|
914 |
throw new JDBCSQLException(e); |
|
915 |
} finally { |
|
916 |
try { |
|
917 |
rs.close(); |
|
918 |
} catch (Exception e) { |
|
919 |
} |
|
920 |
; |
|
921 |
try { |
|
922 |
st.close(); |
|
923 |
} catch (Exception e) { |
|
924 |
} |
|
925 |
; |
|
1450 | 926 |
} |
1451 | 927 |
|
928 |
} |
|
929 |
|
|
930 |
|
|
931 |
|
|
1452 | 932 |
} |
Also available in: Unified diff