svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extOracleSpatial / src / org / gvsig / fmap / dal / store / oraclespatial / OracleSpatialHelper.java @ 29579
History | View | Annotate | Download (25.9 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2009 Prodevelop S.L. main development
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.fmap.dal.store.oraclespatial; |
29 |
|
30 |
import java.sql.Connection; |
31 |
import java.sql.ResultSet; |
32 |
import java.sql.ResultSetMetaData; |
33 |
import java.sql.SQLException; |
34 |
import java.sql.Statement; |
35 |
import java.util.ArrayList; |
36 |
import java.util.Arrays; |
37 |
import java.util.Comparator; |
38 |
import java.util.Iterator; |
39 |
import java.util.List; |
40 |
import java.util.TreeSet; |
41 |
|
42 |
import org.cresques.cts.IProjection; |
43 |
import org.gvsig.fmap.crs.CRSFactory; |
44 |
import org.gvsig.fmap.dal.DALLocator; |
45 |
import org.gvsig.fmap.dal.DataManager; |
46 |
import org.gvsig.fmap.dal.DataTypes; |
47 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
48 |
import org.gvsig.fmap.dal.exception.DataException; |
49 |
import org.gvsig.fmap.dal.exception.InitializeException; |
50 |
import org.gvsig.fmap.dal.exception.ReadException; |
51 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
52 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
53 |
import org.gvsig.fmap.dal.feature.Feature; |
54 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
55 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
56 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
57 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
58 |
import org.gvsig.fmap.dal.feature.FeatureType; |
59 |
import org.gvsig.fmap.dal.feature.exception.UnsupportedDataTypeException; |
60 |
import org.gvsig.fmap.dal.feature.exception.UnsupportedGeometryException; |
61 |
import org.gvsig.fmap.dal.resource.spi.ResourceManagerProviderServices; |
62 |
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper; |
63 |
import org.gvsig.fmap.dal.store.jdbc.JDBCHelperUser; |
64 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
65 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCException; |
66 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException; |
67 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
68 |
import org.gvsig.fmap.geom.Geometry; |
69 |
import org.gvsig.fmap.geom.GeometryLocator; |
70 |
import org.gvsig.fmap.geom.GeometryManager; |
71 |
import org.gvsig.fmap.geom.operation.fromwkb.FromWKB; |
72 |
import org.gvsig.fmap.geom.operation.fromwkb.FromWKBGeometryOperationContext; |
73 |
import org.gvsig.fmap.geom.primitive.Envelope; |
74 |
import org.gvsig.tools.exception.BaseException; |
75 |
import org.postgresql.PGResultSetMetaData; |
76 |
import org.slf4j.Logger; |
77 |
import org.slf4j.LoggerFactory; |
78 |
|
79 |
/**
|
80 |
* @author vsanjaime
|
81 |
*
|
82 |
*/
|
83 |
public class OracleSpatialHelper extends JDBCHelper { |
84 |
|
85 |
private static Logger logger = LoggerFactory |
86 |
.getLogger(OracleSpatialHelper.class); |
87 |
|
88 |
/**
|
89 |
* Constructor
|
90 |
*
|
91 |
* @param consumer
|
92 |
* @param params
|
93 |
* @throws InitializeException
|
94 |
*/
|
95 |
public OracleSpatialHelper(JDBCHelperUser consumer,
|
96 |
OracleSpatialConnectionParameters params) |
97 |
throws InitializeException {
|
98 |
|
99 |
super(consumer, params);
|
100 |
} |
101 |
|
102 |
/**
|
103 |
* Initialize resource
|
104 |
*/
|
105 |
protected void initializeResource() throws InitializeException { |
106 |
ResourceManagerProviderServices manager = (ResourceManagerProviderServices) DALLocator |
107 |
.getResourceManager(); |
108 |
OracleSpatialResource resource = (OracleSpatialResource) manager |
109 |
.createResource(OracleSpatialResource.NAME, new Object[] { |
110 |
params.getUrl(), |
111 |
params.getHost(), |
112 |
params.getPort(), |
113 |
params.getDBName(), |
114 |
params.getUser(), |
115 |
params.getPassword(), |
116 |
params.getJDBCDriverClassName(), |
117 |
((OracleSpatialConnectionParameters) params) |
118 |
.getUseSSL() }); |
119 |
this.setResource(resource);
|
120 |
} |
121 |
|
122 |
/**
|
123 |
* Get default schema
|
124 |
*/
|
125 |
protected String getDefaultSchema(Connection conn) throws JDBCException { |
126 |
if (defaultSchema == null) { |
127 |
String sql = "SELECT sys_context('USERENV', 'CURRENT_SCHEMA') FROM dual;"; |
128 |
ResultSet rs = null; |
129 |
Statement st = null; |
130 |
String schema = null; |
131 |
try {
|
132 |
st = conn.createStatement(); |
133 |
try {
|
134 |
rs = st.executeQuery(sql); |
135 |
} catch (java.sql.SQLException e) {
|
136 |
throw new JDBCExecuteSQLException(sql, e); |
137 |
} |
138 |
rs.next(); |
139 |
schema = rs.getString(1);
|
140 |
} catch (java.sql.SQLException e) {
|
141 |
throw new JDBCSQLException(e); |
142 |
} finally {
|
143 |
try {
|
144 |
rs.close(); |
145 |
} catch (Exception e) { |
146 |
logger.error("Exception clossing resulset", e);
|
147 |
} |
148 |
; |
149 |
try {
|
150 |
st.close(); |
151 |
} catch (Exception e) { |
152 |
logger.error("Exception clossing statement", e);
|
153 |
} |
154 |
; |
155 |
rs = null;
|
156 |
st = null;
|
157 |
} |
158 |
defaultSchema = schema; |
159 |
} |
160 |
|
161 |
return defaultSchema;
|
162 |
} |
163 |
|
164 |
/**
|
165 |
*
|
166 |
*/
|
167 |
public Envelope getFullEnvelopeOfField(JDBCStoreParameters storeParams,
|
168 |
String geometryAttrName, Envelope limit) throws DataException { |
169 |
|
170 |
StringBuilder strb = new StringBuilder(); |
171 |
strb.append("Select asbinary(extent(");
|
172 |
strb.append(escapeFieldName(geometryAttrName)); |
173 |
strb.append(")) from ");
|
174 |
|
175 |
if (storeParams.getSQL() != null |
176 |
&& storeParams.getSQL().trim().length() == 0) {
|
177 |
strb.append('(');
|
178 |
strb.append(storeParams.getSQL()); |
179 |
strb.append(") as __extentfield__ ");
|
180 |
} else {
|
181 |
strb.append(storeParams.tableID()); |
182 |
} |
183 |
|
184 |
if (limit != null) { |
185 |
strb.append(" where intersects(GeomFromText('");
|
186 |
strb.append(limit.toString()); |
187 |
strb.append("')), boundary(");
|
188 |
strb.append(escapeFieldName(geometryAttrName)); |
189 |
strb.append(")) ");
|
190 |
} |
191 |
|
192 |
String sql = strb.toString();
|
193 |
|
194 |
ResultSet rs = null; |
195 |
Statement st = null; |
196 |
String schema = null; |
197 |
Connection conn = null; |
198 |
|
199 |
GeometryManager geoMan = GeometryLocator.getGeometryManager(); |
200 |
|
201 |
Envelope fullEnvelope = null;
|
202 |
this.open();
|
203 |
this.begin();
|
204 |
try {
|
205 |
conn = getConnection(); |
206 |
st = conn.createStatement(); |
207 |
try {
|
208 |
rs = st.executeQuery(sql); |
209 |
} catch (java.sql.SQLException e) {
|
210 |
throw new JDBCExecuteSQLException(sql, e); |
211 |
} |
212 |
if (!rs.next()) {
|
213 |
return null; |
214 |
} |
215 |
|
216 |
byte[] data = rs.getBytes(1); |
217 |
if (data == null) { |
218 |
return null; |
219 |
} |
220 |
initializeFromWKBOperation(); |
221 |
fromWKBContext.setData(data); |
222 |
Geometry geom = (Geometry) fromWKB.invoke(null, fromWKBContext);
|
223 |
|
224 |
fullEnvelope = geom.getEnvelope(); |
225 |
|
226 |
return fullEnvelope;
|
227 |
} catch (java.sql.SQLException e) {
|
228 |
throw new JDBCSQLException(e); |
229 |
} catch (BaseException e) {
|
230 |
throw new ReadException(user.getName(), e); |
231 |
} finally {
|
232 |
try {
|
233 |
rs.close(); |
234 |
} catch (Exception e) { |
235 |
} |
236 |
; |
237 |
try {
|
238 |
st.close(); |
239 |
} catch (Exception e) { |
240 |
} |
241 |
; |
242 |
try {
|
243 |
conn.close(); |
244 |
} catch (Exception e) { |
245 |
} |
246 |
; |
247 |
rs = null;
|
248 |
st = null;
|
249 |
conn = null;
|
250 |
end(); |
251 |
} |
252 |
|
253 |
} |
254 |
|
255 |
protected void initializeFromWKBOperation() throws BaseException { |
256 |
if (fromWKB == null) { |
257 |
fromWKB = (FromWKB) GeometryLocator.getGeometryManager() |
258 |
.getGeometryOperation(FromWKB.CODE, |
259 |
Geometry.TYPES.GEOMETRY, Geometry.SUBTYPES.GEOM2D); |
260 |
fromWKBContext = new FromWKBGeometryOperationContext();
|
261 |
|
262 |
} |
263 |
} |
264 |
|
265 |
public Geometry getGeometry(byte[] buffer) throws BaseException { |
266 |
if (buffer == null) { |
267 |
return null; |
268 |
} |
269 |
initializeFromWKBOperation(); |
270 |
Geometry geom; |
271 |
try {
|
272 |
fromWKBContext.setData(buffer); |
273 |
|
274 |
geom = (Geometry) fromWKB.invoke(null, fromWKBContext);
|
275 |
} finally {
|
276 |
fromWKBContext.setData(null);
|
277 |
} |
278 |
return geom;
|
279 |
} |
280 |
|
281 |
/**
|
282 |
* Fill <code>featureType</code> geometry attributes with SRS and ShapeType
|
283 |
* information stored in the table GEOMETRY_COLUMNS
|
284 |
*
|
285 |
* @param conn
|
286 |
* @param rsMetadata
|
287 |
* @param featureType
|
288 |
* @throws ReadException
|
289 |
*/
|
290 |
protected void loadSRS_and_shapeType(Connection conn, |
291 |
ResultSetMetaData rsMetadata, EditableFeatureType featureType,
|
292 |
String baseSchema, String baseTable) throws JDBCException { |
293 |
|
294 |
Statement st = null; |
295 |
ResultSet rs = null; |
296 |
try {
|
297 |
// Sacamos la lista de los attributos geometricos
|
298 |
EditableFeatureAttributeDescriptor attr; |
299 |
List geoAttrs = new ArrayList(); |
300 |
|
301 |
Iterator iter = featureType.iterator();
|
302 |
while (iter.hasNext()) {
|
303 |
attr = (EditableFeatureAttributeDescriptor) iter.next(); |
304 |
if (attr.getDataType() == DataTypes.GEOMETRY) {
|
305 |
geoAttrs.add(attr); |
306 |
} |
307 |
} |
308 |
if (geoAttrs.size() < 1) { |
309 |
return;
|
310 |
} |
311 |
|
312 |
// preparamos un set con las lista de tablas de origen
|
313 |
// de los campos
|
314 |
class TableId { |
315 |
public String schema = null; |
316 |
public String table = null; |
317 |
public String field = null; |
318 |
|
319 |
public void appendToSQL(StringBuilder strb) { |
320 |
if (schema == null || schema.length() == 0) { |
321 |
strb |
322 |
.append("( F_TABLE_SCHEMA = current_schema() AND F_TABLE_NAME = '");
|
323 |
} else {
|
324 |
strb.append("( F_TABLE_SCHEMA = '");
|
325 |
strb.append(schema); |
326 |
strb.append("' AND F_TABLE_NAME = '");
|
327 |
} |
328 |
strb.append(table); |
329 |
strb.append("' AND F_GEOMETRY_COLUMN = '");
|
330 |
strb.append(field); |
331 |
strb.append("' )");
|
332 |
} |
333 |
|
334 |
} |
335 |
Comparator cmp = new Comparator() { |
336 |
public int compare(Object arg0, Object arg1) { |
337 |
TableId a0 = (TableId) arg0; |
338 |
TableId a1 = (TableId) arg1; |
339 |
|
340 |
if (!a0.field.equals(a1.field)) {
|
341 |
return -1; |
342 |
} |
343 |
if (!a0.table.equals(a1.table)) {
|
344 |
return -1; |
345 |
} |
346 |
if (!a0.schema.equals(a1.schema)) {
|
347 |
return -1; |
348 |
} |
349 |
return 0; |
350 |
} |
351 |
}; |
352 |
TreeSet set = new TreeSet(cmp); |
353 |
TableId tableId; |
354 |
iter = geoAttrs.iterator(); |
355 |
int rsIndex;
|
356 |
while (iter.hasNext()) {
|
357 |
attr = (EditableFeatureAttributeDescriptor) iter.next(); |
358 |
tableId = new TableId();
|
359 |
rsIndex = attr.getIndex() + 1;
|
360 |
|
361 |
if (baseSchema == null && baseTable == null) { |
362 |
if (rsMetadata instanceof PGResultSetMetaData) { |
363 |
tableId.schema = ((PGResultSetMetaData) rsMetadata) |
364 |
.getBaseSchemaName(rsIndex); |
365 |
tableId.table = ((PGResultSetMetaData) rsMetadata) |
366 |
.getBaseTableName(rsIndex); |
367 |
tableId.field = ((PGResultSetMetaData) rsMetadata) |
368 |
.getBaseColumnName(rsIndex); |
369 |
|
370 |
} else {
|
371 |
tableId.schema = rsMetadata.getSchemaName(rsIndex); |
372 |
tableId.table = rsMetadata.getTableName(rsIndex); |
373 |
tableId.field = rsMetadata.getColumnName(rsIndex); |
374 |
} |
375 |
} else {
|
376 |
tableId.schema = baseSchema; |
377 |
tableId.table = baseTable; |
378 |
tableId.field = rsMetadata.getColumnName(rsIndex); |
379 |
} |
380 |
if (tableId.table == null || tableId.table.length() == 0) { |
381 |
// Si no tiene tabla origen (viene de algun calculo por ej.)
|
382 |
// lo saltamos ya que no estara en la tabla GEOMETRY_COLUMNS
|
383 |
continue;
|
384 |
} |
385 |
set.add(tableId); |
386 |
} |
387 |
|
388 |
if (set.size() == 0) { |
389 |
return;
|
390 |
} |
391 |
|
392 |
// Preparamos una sql para que nos saque el resultado
|
393 |
StringBuilder strb = new StringBuilder(); |
394 |
strb |
395 |
.append("Select geometry_columns.*,auth_name || ':' || auth_srid as SRSID ");
|
396 |
strb.append("from geometry_columns left join spatial_ref_sys on ");
|
397 |
strb.append("geometry_columns.srid = spatial_ref_sys.srid WHERE ");
|
398 |
iter = set.iterator(); |
399 |
for (int i = 0; i < set.size() - 1; i++) { |
400 |
tableId = (TableId) iter.next(); |
401 |
tableId.appendToSQL(strb); |
402 |
strb.append(" OR ");
|
403 |
} |
404 |
tableId = (TableId) iter.next(); |
405 |
tableId.appendToSQL(strb); |
406 |
String sql = strb.toString();
|
407 |
|
408 |
st = conn.createStatement(); |
409 |
try {
|
410 |
rs = st.executeQuery(sql); |
411 |
} catch (SQLException e) { |
412 |
throw new JDBCExecuteSQLException(sql, e); |
413 |
} |
414 |
String srsID;
|
415 |
int pgSrid;
|
416 |
int geometryType;
|
417 |
int geometrySubtype;
|
418 |
String geomTypeStr;
|
419 |
int dimensions;
|
420 |
IProjection srs; |
421 |
|
422 |
while (rs.next()) {
|
423 |
srsID = rs.getString("SRSID");
|
424 |
pgSrid = rs.getInt("SRID");
|
425 |
geomTypeStr = rs.getString("TYPE").toUpperCase();
|
426 |
geometryType = Geometry.TYPES.GEOMETRY; |
427 |
if (geomTypeStr.startsWith("POINT")) { |
428 |
geometryType = Geometry.TYPES.POINT; |
429 |
} else if (geomTypeStr.startsWith("LINESTRING")) { |
430 |
geometryType = Geometry.TYPES.CURVE; |
431 |
} else if (geomTypeStr.startsWith("POLYGON")) { |
432 |
geometryType = Geometry.TYPES.SURFACE; |
433 |
} else if (geomTypeStr.startsWith("MULTIPOINT")) { |
434 |
geometryType = Geometry.TYPES.MULTIPOINT; |
435 |
} else if (geomTypeStr.startsWith("MULTILINESTRING")) { |
436 |
geometryType = Geometry.TYPES.MULTICURVE; |
437 |
} else if (geomTypeStr.startsWith("MULTIPOLYGON")) { |
438 |
geometryType = Geometry.TYPES.MULTISURFACE; |
439 |
} |
440 |
dimensions = rs.getInt("coord_dimension");
|
441 |
geometrySubtype = Geometry.SUBTYPES.GEOM2D; |
442 |
if (dimensions > 2) { |
443 |
if (dimensions == 3) { |
444 |
if (geomTypeStr.endsWith("M")) { |
445 |
geometrySubtype = Geometry.SUBTYPES.GEOM2DM; |
446 |
} else {
|
447 |
geometrySubtype = Geometry.SUBTYPES.GEOM3D; |
448 |
} |
449 |
|
450 |
} else {
|
451 |
geometrySubtype = Geometry.SUBTYPES.GEOM3DM; |
452 |
} |
453 |
} |
454 |
|
455 |
iter = geoAttrs.iterator(); |
456 |
while (iter.hasNext()) {
|
457 |
attr = (EditableFeatureAttributeDescriptor) iter.next(); |
458 |
rsIndex = attr.getIndex() + 1;
|
459 |
if (!rsMetadata.getColumnName(rsIndex).equals(
|
460 |
rs.getString("f_geometry_column"))) {
|
461 |
continue;
|
462 |
} |
463 |
|
464 |
if (baseSchema == null && baseTable == null) { |
465 |
|
466 |
if (rsMetadata instanceof PGResultSetMetaData) { |
467 |
if (!((PGResultSetMetaData) rsMetadata)
|
468 |
.getBaseTableName(rsIndex).equals( |
469 |
rs.getString("f_table_name"))) {
|
470 |
continue;
|
471 |
} |
472 |
String curSchema = rs.getString("f_table_schema"); |
473 |
String metaSchema = ((PGResultSetMetaData) rsMetadata)
|
474 |
.getBaseSchemaName(rsIndex); |
475 |
if (!metaSchema.equals(curSchema)) {
|
476 |
if (metaSchema.length() == 0 |
477 |
&& metaSchema == getDefaultSchema(conn)) { |
478 |
} else {
|
479 |
continue;
|
480 |
} |
481 |
} |
482 |
|
483 |
} else {
|
484 |
|
485 |
if (!rsMetadata.getTableName(rsIndex).equals(
|
486 |
rs.getString("f_table_name"))) {
|
487 |
continue;
|
488 |
} |
489 |
String curSchema = rs.getString("f_table_schema"); |
490 |
String metaSchema = rsMetadata
|
491 |
.getSchemaName(rsIndex); |
492 |
if (!metaSchema.equals(curSchema)) {
|
493 |
if (metaSchema.length() == 0 |
494 |
&& metaSchema == getDefaultSchema(conn)) { |
495 |
} else {
|
496 |
continue;
|
497 |
} |
498 |
} |
499 |
} |
500 |
} |
501 |
attr.setGeometryType(geometryType); |
502 |
attr.setGeometrySubType(geometrySubtype); |
503 |
if (srsID != null && srsID.length() > 0) { |
504 |
attr.setSRS(CRSFactory.getCRS(srsID)); |
505 |
} |
506 |
iter.remove(); |
507 |
} |
508 |
iter = geoAttrs.iterator(); |
509 |
while (iter.hasNext()) {
|
510 |
attr = (EditableFeatureAttributeDescriptor) iter.next(); |
511 |
attr.setSRS(null);
|
512 |
attr.setGeometryType(Geometry.TYPES.GEOMETRY); |
513 |
|
514 |
} |
515 |
} |
516 |
|
517 |
} catch (java.sql.SQLException e) {
|
518 |
throw new JDBCSQLException(e); |
519 |
} finally {
|
520 |
try {
|
521 |
rs.close(); |
522 |
} catch (Exception e) { |
523 |
} |
524 |
; |
525 |
try {
|
526 |
st.close(); |
527 |
} catch (Exception e) { |
528 |
} |
529 |
; |
530 |
} |
531 |
|
532 |
} |
533 |
|
534 |
/**
|
535 |
* get geometry column name "SDO_GEOMETRY"
|
536 |
*/
|
537 |
public String getSqlColumnTypeDescription(FeatureAttributeDescriptor attr) { |
538 |
|
539 |
switch (attr.getDataType()) {
|
540 |
|
541 |
case DataTypes.GEOMETRY:
|
542 |
return "SDO_GEOMETRY"; |
543 |
|
544 |
case DataTypes.STRING:
|
545 |
return "VARCHAR2(" + attr.getSize() + ")"; |
546 |
|
547 |
case DataTypes.BOOLEAN:
|
548 |
return "NUMBER(1, 0)"; |
549 |
|
550 |
case DataTypes.BYTE:
|
551 |
return "NUMBER"; |
552 |
|
553 |
case DataTypes.DATE:
|
554 |
return "DATE"; |
555 |
|
556 |
case DataTypes.TIMESTAMP:
|
557 |
return "TIMESTAMP"; |
558 |
|
559 |
case DataTypes.TIME:
|
560 |
return "TIMESTAMP"; |
561 |
|
562 |
case DataTypes.BYTEARRAY:
|
563 |
|
564 |
case DataTypes.DOUBLE:
|
565 |
return "FLOAT"; |
566 |
|
567 |
case DataTypes.FLOAT:
|
568 |
return "FLOAT"; |
569 |
|
570 |
case DataTypes.INT:
|
571 |
return "NUMBER(12, 0)"; |
572 |
|
573 |
case DataTypes.LONG:
|
574 |
return "NUMBER(38, 0)"; |
575 |
|
576 |
default:
|
577 |
String typeName = (String) attr.getAdditionalInfo("SQLTypeName"); |
578 |
if (typeName != null) { |
579 |
return typeName;
|
580 |
} |
581 |
|
582 |
throw new UnsupportedDataTypeException(attr.getDataTypeName(), attr |
583 |
.getDataType()); |
584 |
} |
585 |
|
586 |
} |
587 |
|
588 |
/**
|
589 |
* get geometry dimension
|
590 |
*
|
591 |
* @param geometrySubType
|
592 |
* @return
|
593 |
*/
|
594 |
public int getOraGeomDimensions(int geometrySubType) { |
595 |
switch (geometrySubType) {
|
596 |
case Geometry.SUBTYPES.GEOM2D:
|
597 |
return 2; |
598 |
case Geometry.SUBTYPES.GEOM2DM:
|
599 |
case Geometry.SUBTYPES.GEOM3D:
|
600 |
return 3; |
601 |
|
602 |
case Geometry.SUBTYPES.GEOM3DM:
|
603 |
return 4; |
604 |
default:
|
605 |
throw new UnsupportedDataTypeException( |
606 |
DataTypes.TYPE_NAMES[DataTypes.GEOMETRY], |
607 |
DataTypes.GEOMETRY); |
608 |
} |
609 |
} |
610 |
|
611 |
public String getOraGeomType(int geometryType, int geometrySubType) { |
612 |
String oraGeomType;
|
613 |
switch (geometryType) {
|
614 |
case Geometry.TYPES.GEOMETRY:
|
615 |
oraGeomType = OracleSpatialValues.OraGeometry_GTYPE_GEOMETRY; |
616 |
break;
|
617 |
case Geometry.TYPES.POINT:
|
618 |
oraGeomType = OracleSpatialValues.OraGeometry_GTYPE_POINT; |
619 |
break;
|
620 |
case Geometry.TYPES.CURVE:
|
621 |
oraGeomType = OracleSpatialValues.OraGeometry_GTYPE_CURVE; |
622 |
break;
|
623 |
case Geometry.TYPES.TEXT:
|
624 |
oraGeomType = OracleSpatialValues.OraGeometry_GTYPE_GEOMETRY; |
625 |
break;
|
626 |
case Geometry.TYPES.SOLID:
|
627 |
oraGeomType = OracleSpatialValues.OraGeometry_GTYPE_GEOMETRY; |
628 |
break;
|
629 |
case Geometry.TYPES.AGGREGATE:
|
630 |
oraGeomType = OracleSpatialValues.OraGeometry_GTYPE_GEOMETRY; |
631 |
break;
|
632 |
case Geometry.TYPES.SURFACE:
|
633 |
oraGeomType = OracleSpatialValues.OraGeometry_GTYPE_POLYGON; |
634 |
break;
|
635 |
case Geometry.TYPES.MULTIPOINT:
|
636 |
oraGeomType = OracleSpatialValues.OraGeometry_GTYPE_MULTIPOINT; |
637 |
break;
|
638 |
case Geometry.TYPES.MULTICURVE:
|
639 |
oraGeomType = OracleSpatialValues.OraGeometry_GTYPE_MULTICURVE; |
640 |
break;
|
641 |
case Geometry.TYPES.MULTISURFACE:
|
642 |
oraGeomType = OracleSpatialValues.OraGeometry_GTYPE_MULTIPOLYGON; |
643 |
break;
|
644 |
case Geometry.TYPES.MULTISOLID:
|
645 |
oraGeomType = OracleSpatialValues.OraGeometry_GTYPE_GEOMETRY; |
646 |
break;
|
647 |
case Geometry.TYPES.CIRCLE:
|
648 |
oraGeomType = OracleSpatialValues.OraGeometry_GTYPE_GEOMETRY; |
649 |
break;
|
650 |
case Geometry.TYPES.ARC:
|
651 |
oraGeomType = OracleSpatialValues.OraGeometry_GTYPE_GEOMETRY; |
652 |
break;
|
653 |
case Geometry.TYPES.ELLIPSE:
|
654 |
oraGeomType = OracleSpatialValues.OraGeometry_GTYPE_GEOMETRY; |
655 |
break;
|
656 |
case Geometry.TYPES.SPLINE:
|
657 |
oraGeomType = OracleSpatialValues.OraGeometry_GTYPE_GEOMETRY; |
658 |
break;
|
659 |
case Geometry.TYPES.ELLIPTICARC:
|
660 |
oraGeomType = OracleSpatialValues.OraGeometry_GTYPE_GEOMETRY; |
661 |
break;
|
662 |
default:
|
663 |
throw new UnsupportedGeometryException(geometryType, |
664 |
geometrySubType); |
665 |
} |
666 |
|
667 |
return oraGeomType;
|
668 |
} |
669 |
|
670 |
/**
|
671 |
* Get oracle srid from srs code(EPSG code)
|
672 |
*/
|
673 |
public int getProviderSRID(String srs) { |
674 |
|
675 |
DataManager manager = DALLocator.getDataManager(); |
676 |
Integer isrs = null; |
677 |
if (srs != null) { |
678 |
FeatureStore oraSrsStore = (FeatureStore) OracleSpatialLibrary |
679 |
.getSRSDataStore(); |
680 |
|
681 |
FeatureSet set = null;
|
682 |
try {
|
683 |
FeatureQuery query = oraSrsStore.createFeatureQuery(); |
684 |
query.setFilter(manager.createExpresion("EPSG = " + srs));
|
685 |
set = (FeatureSet) oraSrsStore.getDataSet(query); |
686 |
if (set.getSize() > 0) { |
687 |
Iterator<Feature> it = set.iterator();
|
688 |
while (it.hasNext()) {
|
689 |
Feature feat = it.next(); |
690 |
Double ora = feat.getDouble("ORACLE"); |
691 |
int iora = ora.intValue();
|
692 |
double prefe = feat.getDouble("PRF_ORACLE"); |
693 |
if (prefe == 1) { |
694 |
isrs = new Integer(iora); |
695 |
break;
|
696 |
} |
697 |
} |
698 |
} |
699 |
} catch (DataException e) {
|
700 |
e.printStackTrace(); |
701 |
} |
702 |
if (isrs != null) { |
703 |
return isrs;
|
704 |
} |
705 |
} |
706 |
return -1; |
707 |
} |
708 |
|
709 |
/**
|
710 |
* Get oracle srid from gvSIG projection
|
711 |
* @param proj
|
712 |
*/
|
713 |
public int getProviderSRID(IProjection proj) { |
714 |
if (proj != null) { |
715 |
String epsg = proj.getAbrev().trim();
|
716 |
int ocu = epsg.indexOf(":"); |
717 |
if (ocu != -1) { |
718 |
epsg = epsg.substring(ocu + 1);
|
719 |
} |
720 |
Integer oraSRID = getProviderSRID(epsg);
|
721 |
if (oraSRID != null) { |
722 |
return oraSRID.intValue();
|
723 |
} |
724 |
} |
725 |
return -1; |
726 |
} |
727 |
|
728 |
// private int searchOraSRID(final IProjection srs) {
|
729 |
// if (srs == null) {
|
730 |
// return -1;
|
731 |
// }
|
732 |
// return searchOraSRID(srs.getAbrev());
|
733 |
// }
|
734 |
//
|
735 |
// private int searchOraSRID(final String srsID) {
|
736 |
// if (srsID == null) {
|
737 |
// return -1;
|
738 |
// }
|
739 |
//
|
740 |
// ConnectionAction action = new ConnectionAction() {
|
741 |
//
|
742 |
// public Object action(Connection conn) throws DataException {
|
743 |
// // select srid from spatial_ref_sys where auth_name = 'EPSG' and
|
744 |
// // auth_srid = 23030
|
745 |
// String[] abrev = srsID.split(":");
|
746 |
// StringBuilder sqlb = new StringBuilder();
|
747 |
// sqlb.append("select SRID from MDSYS.CS_SRS where ");
|
748 |
// if (abrev.length > 1) {
|
749 |
// sqlb.append("auth_name = ? and ");
|
750 |
// }
|
751 |
// sqlb.append("auth_srid = ?");
|
752 |
//
|
753 |
// String sql = sqlb.toString();
|
754 |
// PreparedStatement st;
|
755 |
// try {
|
756 |
// st = conn.prepareStatement(sql);
|
757 |
// } catch (SQLException e) {
|
758 |
// throw new JDBCPreparingSQLException(sql, e);
|
759 |
// }
|
760 |
// ResultSet rs = null;
|
761 |
// try {
|
762 |
// int i = 0;
|
763 |
// if (abrev.length > 1) {
|
764 |
// st.setString(i + 1, abrev[i]);
|
765 |
// i++;
|
766 |
// }
|
767 |
// st.setInt(i + 1, Integer.parseInt(abrev[i]));
|
768 |
//
|
769 |
// try {
|
770 |
// rs = st.executeQuery();
|
771 |
// } catch (SQLException e) {
|
772 |
// throw new JDBCExecutePreparedSQLException(sql, abrev, e);
|
773 |
// }
|
774 |
//
|
775 |
// if (!rs.next()) {
|
776 |
// return null;
|
777 |
// }
|
778 |
//
|
779 |
// return new Integer(rs.getInt(1));
|
780 |
//
|
781 |
// } catch (SQLException e) {
|
782 |
// throw new JDBCSQLException(e);
|
783 |
// } finally {
|
784 |
// try {
|
785 |
// rs.close();
|
786 |
// } catch (Exception e) {
|
787 |
// }
|
788 |
// ;
|
789 |
// try {
|
790 |
// st.close();
|
791 |
// } catch (Exception e) {
|
792 |
// }
|
793 |
// ;
|
794 |
// }
|
795 |
//
|
796 |
// }
|
797 |
//
|
798 |
// };
|
799 |
//
|
800 |
// Integer oraSRSID = null;
|
801 |
// try {
|
802 |
// oraSRSID = (Integer) doConnectionAction(action);
|
803 |
// return oraSRSID.intValue();
|
804 |
// } catch (Exception e) {
|
805 |
// logger.error("Excetion searching pgSRS", e);
|
806 |
// return -1;
|
807 |
// }
|
808 |
// }
|
809 |
|
810 |
public List getSqlGeometyFieldAdd(FeatureAttributeDescriptor attr, |
811 |
String table, String schema) { |
812 |
// SELECT AddGeometryColumn({schema}, {table}, {field}, {srid}(int),
|
813 |
// {geomType}(Str), {dimensions}(int))
|
814 |
|
815 |
// gemoType:
|
816 |
/*
|
817 |
* POINT, LINESTRING, POLYGON, MULTIPOINT, MULTILINESTRING,
|
818 |
* MULTIPOLYGON, GEOMETRYCOLLECTION POINTM, LINESTRINGM, POLYGONM,
|
819 |
* MULTIPOINTM, MULTILINESTRINGM, MULTIPOLYGONM, GEOMETRYCOLLECTIONM
|
820 |
*/
|
821 |
|
822 |
List sqls = new ArrayList(); |
823 |
|
824 |
StringBuilder strb = new StringBuilder(); |
825 |
strb.append("SELECT AddGeometryColumn('");
|
826 |
if (schema != null && schema.length() > 0) { |
827 |
strb.append(schema); |
828 |
strb.append("', '");
|
829 |
} |
830 |
strb.append(table); |
831 |
strb.append("', '");
|
832 |
strb.append(attr.getName()); |
833 |
strb.append("', ");
|
834 |
// strb.append("-1");
|
835 |
strb.append(getProviderSRID(attr.getSRS())); |
836 |
strb.append(", '");
|
837 |
strb.append(getOraGeomType(attr.getGeometryType(), attr |
838 |
.getGeometrySubType())); |
839 |
strb.append("', ");
|
840 |
strb.append(getOraGeomDimensions(attr.getGeometrySubType())); |
841 |
strb.append(")");
|
842 |
|
843 |
sqls.add(strb.toString()); |
844 |
|
845 |
/* ALTER TABLE muni10000_peq_test DROP CONSTRAINT enforce_srid_the_geom; */
|
846 |
/*
|
847 |
* strb = new StringBuilder(); strb.append("Alter table "); if (schema
|
848 |
* != null && schema.length() > 0) { strb.append(schema);
|
849 |
* strb.append("."); } strb.append("f_table_name = '");
|
850 |
* strb.append(table); strb.append("' AND f_geometry_column = '");
|
851 |
* strb.append(attr.getName()); strb.append("' AND srid = -1");
|
852 |
*
|
853 |
*
|
854 |
* sqls.add(strb.toString());
|
855 |
*/
|
856 |
return sqls;
|
857 |
} |
858 |
|
859 |
public String getSqlFieldName(FeatureAttributeDescriptor attribute) { |
860 |
if (attribute.getDataType() == DataTypes.GEOMETRY) {
|
861 |
return "asBinary(" + super.getSqlFieldName(attribute) + ")"; |
862 |
} |
863 |
return super.getSqlFieldName(attribute); |
864 |
} |
865 |
|
866 |
protected EditableFeatureAttributeDescriptor createAttributeFromJDBC(
|
867 |
EditableFeatureType type, Connection conn,
|
868 |
ResultSetMetaData rsMetadata, int colIndex) throws SQLException { |
869 |
if (rsMetadata.getColumnType(colIndex) == java.sql.Types.OTHER) {
|
870 |
if (rsMetadata.getColumnTypeName(colIndex).equalsIgnoreCase(
|
871 |
"geometry")) {
|
872 |
return type.add(rsMetadata.getColumnName(colIndex),
|
873 |
DataTypes.GEOMETRY); |
874 |
|
875 |
} |
876 |
} |
877 |
|
878 |
return super.createAttributeFromJDBC(type, conn, rsMetadata, colIndex); |
879 |
} |
880 |
|
881 |
public List getAdditionalSqlToCreate(NewDataStoreParameters ndsp, |
882 |
FeatureType fType) { |
883 |
FeatureAttributeDescriptor attr; |
884 |
Iterator iter = fType.iterator();
|
885 |
List result = new ArrayList(); |
886 |
OracleSpatialNewStoreParameters oraNdsp = (OracleSpatialNewStoreParameters) ndsp; |
887 |
while (iter.hasNext()) {
|
888 |
attr = (FeatureAttributeDescriptor) iter.next(); |
889 |
// if (attr.getDataType() == DataTypes.GEOMETRY) {
|
890 |
// result.addAll(getSqlGeometyFieldAdd(attr, oraNdsp.getTable(),
|
891 |
// oraNdsp.getSchema()));
|
892 |
// }
|
893 |
} |
894 |
|
895 |
return result;
|
896 |
} |
897 |
|
898 |
public boolean allowAutomaticValues() { |
899 |
return Boolean.TRUE; |
900 |
} |
901 |
|
902 |
public boolean supportOffset() { |
903 |
return true; |
904 |
} |
905 |
|
906 |
public boolean supportsUnion() { |
907 |
return true; |
908 |
} |
909 |
|
910 |
public String getSqlFieldDescription(FeatureAttributeDescriptor attr) |
911 |
throws DataException {
|
912 |
|
913 |
StringBuilder strb = new StringBuilder(); |
914 |
// name
|
915 |
strb.append(attr.getName()); |
916 |
strb.append(" ");
|
917 |
|
918 |
// Type
|
919 |
strb.append(this.getSqlColumnTypeDescription(attr));
|
920 |
strb.append(" ");
|
921 |
|
922 |
boolean allowNull = attr.allowNull()
|
923 |
&& !(attr.isPrimaryKey() || attr.isAutomatic()); |
924 |
// Default
|
925 |
String typename = attr.getDataTypeName();
|
926 |
if (typename.compareToIgnoreCase("GEOMETRY") != 0) { |
927 |
if (attr.getDefaultValue() == null) { |
928 |
if (allowNull) {
|
929 |
strb.append("DEFAULT NULL ");
|
930 |
} |
931 |
} else {
|
932 |
String value = getDefaltFieldValueString(attr);
|
933 |
strb.append("DEFAULT '");
|
934 |
strb.append(value); |
935 |
strb.append("' ");
|
936 |
} |
937 |
|
938 |
// Null
|
939 |
if (allowNull) {
|
940 |
strb.append("NULL ");
|
941 |
} else {
|
942 |
strb.append("NOT NULL ");
|
943 |
} |
944 |
|
945 |
// Primery key
|
946 |
if (attr.isPrimaryKey()) {
|
947 |
strb.append("PRIMARY KEY ");
|
948 |
} |
949 |
} |
950 |
return strb.toString();
|
951 |
} |
952 |
|
953 |
public static int nvarchar2Limited(int n) { |
954 |
|
955 |
if (n <= OracleSpatialValues.VARCHAR2_STANDARD_SIZE)
|
956 |
return OracleSpatialValues.VARCHAR2_STANDARD_SIZE;
|
957 |
if (n <= OracleSpatialValues.VARCHAR2_LONG_SIZE)
|
958 |
return n;
|
959 |
|
960 |
return OracleSpatialValues.VARCHAR2_LONG_SIZE;
|
961 |
} |
962 |
|
963 |
public void loadFeatureType(EditableFeatureType featureType, |
964 |
JDBCStoreParameters storeParams) throws DataException {
|
965 |
if (storeParams.getSQL() != null |
966 |
&& storeParams.getSQL().trim().length() == 0) {
|
967 |
loadFeatureType(featureType, storeParams, storeParams.getSQL(), |
968 |
null, null); |
969 |
} else {
|
970 |
String sql = "select * from all_tab_columns where table_name ='"+storeParams.tableID()+"'"; |
971 |
|
972 |
|
973 |
loadFeatureType(featureType, storeParams, sql, storeParams |
974 |
.getSchema(), storeParams.getTable()); |
975 |
} |
976 |
} |
977 |
|
978 |
|
979 |
|
980 |
} |