Revision 17691 branches/v10/extensions/extOracleSpatial/src/es/prodevelop/cit/gvsig/fmap/drivers/jdbc/oracle/OracleSpatialDriver.java

View differences:

OracleSpatialDriver.java
165 165
 * - Using Oracle's JGeometry static methods
166 166
 * - Using Geotools utilities
167 167
 *
168
 *  (currently, the driver parses the structs directly)
168
 *  (currently, the driver parses the structs directly).
169 169
 *
170 170
 * @author jldominguez
171 171
 *
......
178 178
    // constants
179 179
    public static final int GEODETIC_FULLEXTENT_SAMPLE_SIZE = 50;
180 180
    public static final String GEODETIC_SRID = "8307";
181
    public static final String ASSUMED_ORACLE_SRID = "8307";
182

  
181
    // public static final String ASSUMED_ORACLE_SRID = "8307";
182
    
183 183
    // ------------------------------------------------
184 184
    public static final String NAME = "Oracle Spatial Database Driver";
185 185
    public static final int ID_COLUMN_INDEX = 1;
......
190 190
    // public static final String DEFAULT_GEO_FIELD = "MERGEDGEOMETRY";
191 191

  
192 192
    public static final String ORACLE_ID_FIELD = "ROWID";
193
    public static final String DEFAULT_ID_FIELD = "GID";
193
    public static final String DEFAULT_ID_FIELD_CASE_SENSITIVE = "GID";
194 194
    public static final String ORACLE_GEO_SCHEMA = "MDSYS";
195 195
    public static final String CONN_STR_BEGIN = "jdbc:oracle:thin:";
196
    
196 197
    public static final int VARCHAR2_STANDARD_SIZE = 80;
197 198
    public static final int VARCHAR2_LONG_SIZE = 256;
198 199
    public static final int MAX_ID_LENGTH = 30;
......
319 320
     */
320 321
    public void setData(IConnection _conn, DBLayerDefinition lyrDef) {
321 322
        conn = _conn;
322

  
323
        
323 324
        // This metadata is required to store layer in GVP
324 325
        // without problems:
325 326
        ConnectionWithParams _cwp =
......
328 329
		port = _cwp.getPort();
329 330
		dbName = _cwp.getDb();
330 331
		connName = _cwp.getName();
331

  
332
		
332 333
        // ------------------
333 334

  
334 335
		setUpperCase(lyrDef);
335 336
        lyrDef.setConnection(conn);
336 337

  
338
        String geo_can[];
339
		try {
340
			geo_can = getGeometryFieldsCandidates(conn, lyrDef.getTableName());
341
			removeStructFields(lyrDef, geo_can);
342
		} catch (DBException e) {
343
			logger.error("While removing STRUCT fields: " + e.getMessage());
344
		}
345
        
337 346
        setLyrDef(lyrDef);
338 347

  
339 348
        geoColName = lyrDef.getFieldGeometry();
340

  
349
        
341 350
        String tn = lyrDef.getTableName();
342

  
351
        
343 352
        if (tn.indexOf(".") == -1) {
344

  
353
        	
345 354
        	if (lyrDef.getSchema() == null) {
346 355
        		fullTableName = _cwp.getUser().toUpperCase() + "." + tn;
347 356
        	} else {
348 357
        		fullTableName = lyrDef.getSchema() + "." + tn;
349 358
        	}
350

  
351

  
359
        	
360
        	
352 361
        } else {
353 362
        	fullTableName = tn;
354 363
        }
355

  
364
        
356 365
        not_restricted_sql = "select " + getStandardSelectExpression() +
357 366
            " from " + getTableName() + " c ";
358 367

  
......
363 372
        oneRowMetadata();
364 373

  
365 374
        setDestProjection(lyrDef.getSRID_EPSG());
366

  
367
        IProjection viewProj = CRSFactory.getCRS("EPSG:" + destProj);
368
        IProjection tableProj = CRSFactory.getCRS("EPSG:" + epsgSRID);
369
        ICoordTrans reprojecter = viewProj.getCT(tableProj);
370

  
371 375
        workingAreaInViewsCS = lyrDef.getWorkingArea();
372
        if (workingAreaInViewsCS != null) {
376
        
377
        if ((workingAreaInViewsCS != null) && (epsgSRID != null)) {
378
            IProjection viewProj = CRSFactory.getCRS("EPSG:" + destProj);
379
            IProjection tableProj = CRSFactory.getCRS("EPSG:" + epsgSRID);
380
            ICoordTrans reprojecter = viewProj.getCT(tableProj);
373 381
        	workingAreaInTablesCS = reprojecter.convert(workingAreaInViewsCS);
382
            workingAreaInTablesCSStruct = shapeToStruct(workingAreaInTablesCS,
383
                    FShape.NULL, tableHasSrid, false, true);
374 384
        }
375
        workingAreaInTablesCSStruct = shapeToStruct(workingAreaInTablesCS,
376
                FShape.NULL, tableHasSrid, false, true);
377 385

  
378 386
        cancelIDLoad = false;
379 387
        idLoader = new IdLoaderThread(this);
380 388
        idLoader.start();
381 389
    }
382 390

  
391
	private void removeStructFields(DBLayerDefinition def, String[] arr) {
392
		
393
		FieldDescription[] flds = def.getFieldsDesc();
394
		ArrayList aux = new ArrayList();
395
		
396
		for (int i=0; i<flds.length; i++) {
397
			if (!isOneOfThese(flds[i].getFieldName(), arr)) {
398
				aux.add(flds[i]);
399
			}
400
		}
401
		
402
		FieldDescription[] flds_new =
403
			(FieldDescription[]) aux.toArray(new FieldDescription[0]);
404
		def.setFieldsDesc(flds_new);
405
	}
406

  
407
	private boolean isOneOfThese(String name, String[] arr) {
408

  
409
		for (int i=0; i<arr.length; i++) {
410
				if (arr[i].compareToIgnoreCase(name) == 0) return true; 
411
			}
412
		return false;
413
	}
414

  
383 415
	private void setUpperCase(DBLayerDefinition def) {
384 416
		
385 417
		String aux = def.getCatalogName();
......
388 420
		aux = def.getSchema();
389 421
		if (aux != null) def.setSchema(aux.toUpperCase());
390 422
		
423
		/*
391 424
		aux = def.getFieldID();
392 425
		if (aux != null) def.setFieldID(aux.toUpperCase());
393 426
		
......
399 432
			for (int i=0; i<flds.length; i++) flds[i] = flds[i].toUpperCase();
400 433
			def.setFieldNames(flds);
401 434
		}
435
		*/
402 436
	}
403 437

  
404 438
	/**
......
427 461
        }
428 462

  
429 463
        if (!hasRealiableExtent) {
430
        	full_Extent = getEstimatedGeodeticExtent(
431
        			getTableName(), geoColName, conn, 20, 1.5);
464
        	full_Extent = getEstimatedExtent(
465
        			getTableName(), geoColName, conn, 20, 1.5, isGeogCS);
432 466
        }
433 467

  
434 468
        if (cancelIDLoad) {
......
513 547
            logger.warn("No SRID found for this table.");
514 548
            tableHasSrid = false;
515 549

  
516
            return ASSUMED_ORACLE_SRID;
550
            return null;
517 551
        }
518 552

  
519 553
        return obj.toString();
......
571 605
            {
572 606
            	qry = "select * from " + ORACLE_GEOMETADATA_VIEW +
573 607
                " where OWNER = '" + tokens[0] + "' AND TABLE_NAME = '" +
574
                tokens[1] + "'";
608
                tokens[1] + "' AND COLUMN_NAME = '" + geoColName + "'";
575 609
            }
576 610
            else
577 611
            {
578 612
            	qry = "select * from " + ORACLE_GEOMETADATA_VIEW +
579
                " where TABLE_NAME = " + "'" + getTableName() + "'";
613
                " where TABLE_NAME = " + "'" + getTableName()
614
                + "' AND COLUMN_NAME = '"
615
                + geoColName + "'";
580 616

  
581 617
            }
582
//            String qry = "select * from " + ORACLE_GEOMETADATA_VIEW +
583
//                " where TABLE_NAME = " + "'" + getTableName() + "'";
618

  
584 619
            ResultSet _rs = _st.executeQuery(qry);
585 620

  
586 621
            if (_rs.next()) {
......
599 634
                hasRealiableExtent = realiableExtent(full_Extent, isGeogCS);
600 635

  
601 636
                if (!hasRealiableExtent) {
602
                	full_Extent = getFastEstimatedGeodeticExtent(
603
                			getTableName(), geoColName, conn, 20, 10);
637
                	full_Extent = getFastEstimatedExtent(
638
                    			getTableName(), geoColName, conn, 20, 10, isGeogCS);
604 639
                }
605 640

  
606 641
                _rs.close();
......
627 662
    public static boolean getIsGCS(String oracleSRID2, boolean thas) {
628 663

  
629 664
        if (!thas) return false;
665
        if (oracleSRID2 == null) return false;
666
        
630 667
        int ora_cs = 0;
631 668

  
632 669
        try {
......
659 696
            st = ((ConnectionJDBC)conn).getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
660 697
                    ResultSet.CONCUR_READ_ONLY);
661 698

  
662
            ResultSet _rs = st.executeQuery(_sql + " where (rownum = 1)");
699
            ResultSet _rs = st.executeQuery(_sql + " where c." + geoColName + " is not NULL AND rownum = 1");
663 700

  
664 701
            if (_rs.next()) {
665 702
                STRUCT sample_geo = (STRUCT) _rs.getObject(geoColName);
......
702 739
    private int getShapeTypeOfStruct(STRUCT sample) throws SQLException {
703 740

  
704 741
        int code = ((NUMBER) sample.getOracleAttributes()[0]).intValue();
705

  
742
        
706 743
        int type_part = code % 10;
707 744
        int dim_part = code / 1000;
708 745

  
......
825 862

  
826 863
        singleCachedFeatureRowNum = -1;
827 864

  
828
        STRUCT local_st = shapeToStruct(r, FShape.NULL, true, false, true);
865
        STRUCT local_st = shapeToStruct(r, FShape.NULL, tableHasSrid, false, true);
829 866

  
830 867
        Object[] rs_st = getViewResultSet(local_st, null, tableHasSrid);
831 868

  
......
1118 1155
            }
1119 1156

  
1120 1157
            if (__type == Types.NUMERIC) {
1121
                return Types.DOUBLE;
1158
            	int dec_pos = metaData.getScale(i);
1159
            	if (dec_pos == 0) {
1160
            		return Types.INTEGER;
1161
            	} else {
1162
            		return Types.DOUBLE;
1163
            	}
1122 1164
            }
1123 1165

  
1124 1166
            if (__type == Types.DATE) {
......
1140 1182

  
1141 1183
        return -1;
1142 1184
    }
1185
    
1186
    private Value objToValue(Object obj, int idFld) {
1187
    	
1188
        if (obj == null) {
1189
        	return ValueFactory.createNullValue();
1190
        } else {
1191
        	
1192
        	String objToString = obj.toString();
1143 1193

  
1144
    public Value[] getAttributes(ResultSet rs) {
1194
            if (obj instanceof String) {
1195
                objToString = (String) obj;
1196
                return ValueFactory.createValue(objToString);
1197
            } else {
1198
                if (obj instanceof ROWID) {
1199
                    objToString = ((ROWID) obj).stringValue();
1200
                    return ValueFactory.createValue(objToString);
1201
                } else {
1202
                    if (obj instanceof STRUCT) {
1203
                        objToString = "STRUCT";
1204
                        return ValueFactory.createValue(objToString);
1205
                    } else {
1206
                        if (obj instanceof TIMESTAMP) {
1207
                        	TIMESTAMP aux = (TIMESTAMP) obj;
1208
                            objToString = aux.stringValue();
1209
                            Timestamp ts = flexibleTimeStamp(objToString);
1210
                            return ValueFactory.createValue(ts);
1211

  
1212
                        } else {
1213

  
1214
                        	// last try
1215
                        	int _type = -1;
1216
							try {
1217
								_type = getFieldType(idFld);
1218
	                            if (_type == Types.DATE) {
1219
	                            	objToString = objToString.replace('-', '/');
1220
	                            }
1221
                            	return ValueFactory.createValueByType(objToString, _type);
1222
                            } catch (Exception ex) {
1223
                            	logger.debug("Failed to create Value: _type = "
1224
                            			+ _type + ", objToString = " + objToString);
1225
                            	return ValueFactory.createNullValue();
1226
                            }
1227
                            
1228
                        }
1229
                    }
1230
                }
1231
            }
1232
        }
1233
    }
1234

  
1235
    public Value[] getAttributes(ResultSet rs, boolean use_main_metadata) {
1145 1236
        Value[] res = null;
1146 1237

  
1147 1238
        int fcount = 0;
1148 1239

  
1149 1240
        try {
1150
            fcount = rs.getMetaData().getColumnCount();
1241
        	if (use_main_metadata) {
1242
        		fcount = metaData.getColumnCount();
1243
        	} else {
1244
        		fcount = rs.getMetaData().getColumnCount();
1245
        	}
1246
            
1151 1247
            res = new Value[fcount];
1152 1248

  
1153 1249
            for (int i = 0; i < fcount; i++) {
1154 1250
                Object obj = rs.getObject(i + 1);
1155
                String objToString = null;
1156

  
1157
                if (obj == null) {
1158

  
1159
                	res[i] = ValueFactory.createNullValue();
1160

  
1161
                } else {
1162

  
1163
                    if (obj instanceof String) {
1164
                        objToString = (String) obj;
1165
                        res[i] = ValueFactory.createValue(objToString);
1166
                    } else {
1167
                        if (obj instanceof ROWID) {
1168
                            objToString = ((ROWID) obj).stringValue();
1169
                            res[i] = ValueFactory.createValue(objToString);
1170
                        } else {
1171
                            if (obj instanceof STRUCT) {
1172
                                objToString = "STRUCT";
1173
                                res[i] = ValueFactory.createValue(objToString);
1174
                            } else {
1175
                                if (obj instanceof TIMESTAMP) {
1176
                                	TIMESTAMP aux = (TIMESTAMP) obj;
1177
                                    objToString = aux.stringValue();
1178
                                    Timestamp ts = flexibleTimeStamp(objToString);
1179
                                    res[i] = ValueFactory.createValue(ts);
1180

  
1181
                                } else {
1182

  
1183
                                	objToString = obj.toString();
1184
                                	int _type = getFieldType(i);
1185

  
1186
                                    if (obj instanceof DATE) {
1187
                                    	objToString = objToString.replace('-', '/');
1188
                                    }
1189
                                    res[i] = ValueFactory.createValueByType(
1190
                                    		objToString, _type);
1191
                                }
1192
                            }
1193
                        }
1194
                    }
1195
                }
1251
                res[i] = objToValue(obj, i);
1196 1252
            }
1197 1253

  
1198 1254
        } catch (Exception se) {
1199

  
1200 1255
        	logger.error("While getting resultset attribute values: " + se.getMessage());
1201 1256
        	res = new Value[fcount];
1202 1257
        	for (int i=0; i<fcount; i++) res[i] = ValueFactory.createNullValue();
......
1205 1260
        return res;
1206 1261
    }
1207 1262

  
1208
    public Value[] getAttributesUsingMainMetadata(ResultSet rs) {
1263
    public Value[] _getAttributesUsingMainMetadata(ResultSet rs) {
1209 1264
        Value[] res = null;
1210 1265

  
1266
        String objToString = null;
1211 1267
        try {
1212 1268
            int fcount = metaData.getColumnCount();
1213 1269
            res = new Value[fcount];
1214 1270

  
1215 1271
            for (int i = 0; i < fcount; i++) {
1216 1272
                Object obj = rs.getObject(i + 1);
1217
                String objToString = null;
1273
                
1218 1274
                int _type = -1;
1219 1275

  
1220 1276
                if (obj instanceof String) {
......
1233 1289
                        }
1234 1290
                        else {
1235 1291
                            objToString = (obj == null) ? "NULL" : obj.toString();
1236
                            _type = getFieldType(i);
1292
                            try {
1293
								_type = getFieldType(i);
1294
							} catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
1295
								logger.error("TYPE = -1 due to exception");
1296
							}
1237 1297
                        }
1238 1298
                    }
1239 1299
                }
......
1255 1315
                    res[i] = ValueFactory.createValueByType(objToString, _type);
1256 1316
                }
1257 1317
            }
1258
        }
1259
        catch (Exception se) {
1260
        	logger.error("Error while getting attributes: " + se.getMessage());
1318
        } catch (Exception se) {
1319
        	logger.error("While getting attributes: " + se.getMessage() + " objToString = " + objToString);
1261 1320
            return null;
1262 1321
        }
1263 1322

  
......
1329 1388

  
1330 1389
        try {
1331 1390
            ROWID ri = (ROWID) _r.getObject(1);
1332
            atts = getAttributesUsingMainMetadata(_r);
1391
            atts = getAttributes(_r, true);
1333 1392

  
1334 1393
            String gid = ri.stringValue();
1335 1394
            STRUCT _st = (oracle.sql.STRUCT) _r.getObject(oneBasedGeoColInd);
......
1433 1492
    */
1434 1493

  
1435 1494
    private IGeometry getFMapGeometryCollection(Datum[] the_data, int dim) {
1436
        // int __srid) {
1437 1495

  
1438 1496
    	NUMBER _srid = new NUMBER(0);
1439 1497
        NUMBER main_type = new NUMBER((dim * 1000) +
......
1538 1596
            IGeometry ig = null;
1539 1597

  
1540 1598
            if (isActuallyACollection(the_data)) {
1541
            	logger.debug("isActuallyACollection(the_data) = TRUE");
1542 1599
                jgtype = FShape.MULTI;
1543 1600
            }
1544 1601

  
......
2206 2263
    }
2207 2264

  
2208 2265
    private Object[] groupByElement(Datum[] all_elem) {
2266
    	
2267
    	if (isSimpleCollectionOfLines(all_elem)) {
2268
    		
2269
    		Object[] r = new Object[1];
2270
    		r[0] = removeThreeFirst(all_elem);
2271
    		return r;
2272
    	}
2273
    	
2209 2274
        ArrayList resp = new ArrayList();
2210 2275

  
2211 2276
        int size = all_elem.length / 3;
......
2214 2279

  
2215 2280
        Datum[] newaux;
2216 2281
        int i = 1;
2282
        boolean add_last_time = true;
2217 2283

  
2218 2284
        try {
2219 2285
            while (i < size) {
2220 2286
                newaux = getNthGroupOfThree(all_elem, i);
2221 2287

  
2222
                if (newaux[0] == aux[0]) {
2288
                if (newaux[0].intValue() == aux[0].intValue()) {
2223 2289
                    // aux[2] says how many components
2224 2290
                    for (int j = 0; j < ((NUMBER) aux[2]).intValue(); j++) {
2225 2291
                        aux = appendDatArrays(aux,
......
2228 2294

  
2229 2295
                    resp.add(aux);
2230 2296
                    i = i + ((NUMBER) aux[2]).intValue();
2231
                    aux = getNthGroupOfThree(all_elem, i);
2297
                    if (i < size) { // in some cases (line collection, 4)
2298
                    	aux = getNthGroupOfThree(all_elem, i);
2299
                    } else {
2300
                    	add_last_time = false;
2301
                    }
2232 2302
                }
2233 2303
                else {
2234 2304
                    if (((NUMBER) newaux[1]).intValue() == 2003) {
2235 2305
                        aux = appendDatArrays(aux, newaux);
2236
                    }
2237
                    else {
2306
                    } else {
2238 2307
                        resp.add(aux);
2239 2308
                        aux = getNthGroupOfThree(all_elem, i);
2240 2309
                    }
......
2247 2316
            logger.error("Unexpected error: " + se.getMessage());
2248 2317
        }
2249 2318

  
2250
        resp.add(aux);
2319
        if (add_last_time) {
2320
        	resp.add(aux);
2321
        }
2251 2322

  
2252 2323
        return resp.toArray();
2253 2324
    }
......
2815 2886
            if (name.compareToIgnoreCase(DEFAULT_GEO_FIELD) == 0) {
2816 2887
            }
2817 2888
            else {
2818
                name = getValidOracleID(name, i);
2889
                name = getValidOracleID(name, i, false);
2819 2890
                resp = resp + "\"" + name + "\" ";
2820 2891
                type = fieldTypeToSqlStringType(flds[i]);
2821 2892
                resp = resp + type + ", ";
......
2827 2898
        resp = resp + ", ";
2828 2899

  
2829 2900
        String pk = "CONSTRAINT " + getDerivedNAme(table_name, "PK") +
2830
            " PRIMARY KEY (\"" + OracleSpatialDriver.DEFAULT_ID_FIELD +
2901
            " PRIMARY KEY (\"" + OracleSpatialDriver.DEFAULT_ID_FIELD_CASE_SENSITIVE +
2831 2902
            "\") ENABLE";
2832 2903

  
2833 2904
        resp = resp + pk + " )";
......
2895 2966
    	
2896 2967
        String[] dim_name = new String[dim];
2897 2968
        double tolerance = 0.5;
2969
        
2970
        String _ora_srid = ora_srid;
2971
        if (_ora_srid == null) _ora_srid = "NULL";
2898 2972

  
2899
        if (ora_srid.compareTo(GEODETIC_SRID) == 0) {
2973
        if (_ora_srid.compareTo(GEODETIC_SRID) == 0) {
2900 2974
            dim_name[0] = "LONGITUDE";
2901 2975
            dim_name[1] = "LATITUDE";
2902 2976
        }
......
2941 3015
        }
2942 3016

  
2943 3017
        if (withsrid) {
2944
            resp = resp + ", " + ora_srid + " )";
3018
            resp = resp + ", " + _ora_srid + " )";
2945 3019
        }
2946 3020
        else {
2947 3021
            resp = resp + ", NULL )";
......
2960 3034
     * @return the SQL sentence to perform the insertion
2961 3035
     */
2962 3036
    public static String getRowInsertSql(IFeature feat,
2963
        DBLayerDefinition dbLayerDef, int rowInd, String _geoColName) {
3037
        DBLayerDefinition dbLayerDef, int rowInd,
3038
        String _geoColName,
3039
        String geo_val) {
3040
    	
2964 3041
        String name = "";
2965 3042
        int ftype = -1;
2966 3043
        String aux_orig = "";
......
2982 3059
            if (name.compareToIgnoreCase(_geoColName) == 0) {
2983 3060
            }
2984 3061
            else {
2985
                name = getValidOracleID(name, i);
3062
                name = getValidOracleID(name, i, false);
2986 3063
                resp = resp + "\"" + name + "\"" + " , ";
2987 3064
            }
2988 3065
        }
......
3002 3079
            if (name.compareToIgnoreCase(_geoColName) == 0) {
3003 3080
            }
3004 3081
            else {
3005
                if (name.compareToIgnoreCase(
3006
                            OracleSpatialDriver.DEFAULT_ID_FIELD) == 0) {
3082
                if (name.compareTo(OracleSpatialDriver.DEFAULT_ID_FIELD_CASE_SENSITIVE) == 0) {
3007 3083
                    resp = resp + rowInd + " , ";
3008 3084
                }
3009 3085
                else {
......
3035 3111
            }
3036 3112
        }
3037 3113

  
3038
        resp = resp + " ? )";
3114
        resp = resp + " " + geo_val + " )";
3039 3115
        /*
3040 3116
        String test = "SDO_UTIL.APPEND(SDO_GEOMETRY("
3041 3117
        		+ "2002, NULL, NULL,"
......
3193 3269
     * @param epsg the EPSG code
3194 3270
     * @return the Oracle code
3195 3271
     */
3196
    public static String epsgSridToOracleSrid(String epsg) throws Exception {
3272
    public static String epsgSridToOracleSrid(String _epsg) throws Exception {
3273
    	
3274
    	String epsg = removePrefix(_epsg);
3275
    	
3197 3276
        String resp = "8307";
3198 3277

  
3199 3278
        // --------------------------------------------
......
3250 3329
     * @return the EPSG code
3251 3330
     */
3252 3331
    public static String oracleSridToEpsgSrid(String ora) throws Exception {
3332
    	
3333
    	if (ora == null) return null;
3334
    	
3253 3335
        String resp = "4326";
3254 3336

  
3255 3337
        // --------------------------------------------
......
3315 3397
     * improve the renaming)
3316 3398
     * @return an acceptable oracle identifier.
3317 3399
     */
3318
    public static String getValidOracleID(String _str, int ind) {
3400
    public static String getValidOracleID(String _str, int ind, boolean force_uppercase) {
3319 3401
    	
3320
    	String str = _str.toUpperCase();
3402
    	String str = _str;
3403
    	if (force_uppercase) str = _str.toUpperCase();
3321 3404
    	
3322 3405
        if (str.length() <= MAX_ID_LENGTH) {
3323 3406
            return str;
......
3489 3572
        return mpo;
3490 3573
    }
3491 3574

  
3492
    public String getSourceProjection(IConnection conn,DBLayerDefinition dbld) {
3493
        // TODO Auto-generated method stub
3494
        if (tableHasSrid) {
3495
            return epsgSRID;
3575
    public String getSourceProjection(IConnection conn, DBLayerDefinition lyrDef) {
3576
    	
3577
    	String resp = null;
3578
        try {
3579
            Statement _st = ((ConnectionJDBC) conn).getConnection().createStatement();
3580
            String[] tokens = lyrDef.getName().split("\\u002E", 2);
3581
            String qry;
3582
            if (tokens.length > 1) {
3583
            	qry = "select * from " + ORACLE_GEOMETADATA_VIEW +
3584
                " where OWNER = '" + tokens[0] + "' AND TABLE_NAME = '" +
3585
                tokens[1] + "'";
3586
            } else {
3587
            	qry = "select * from " + ORACLE_GEOMETADATA_VIEW +
3588
                " where TABLE_NAME = " + "'" + lyrDef.getName() + "'";
3589
            }
3590
            ResultSet _rs = _st.executeQuery(qry);
3591

  
3592
            if (_rs.next()) {
3593
                String aux = getOracleSridFromCurrentRecord(_rs);
3594
                try {
3595
					resp = oracleSridToEpsgSrid(aux);
3596
				} catch (Exception e) {
3597
					logger.error("Unknown oracle SRID: " + aux);
3598
				}
3599
            } else {
3600
            	
3601
            }
3602
        } catch (Exception ex) {
3603
        	logger.error("While getting Source Projection: " + ex.getMessage());
3496 3604
        }
3497

  
3498
        return destProj;
3605
        
3606
        if (resp != null) {
3607
        	return resp;
3608
        } else {
3609
        	return getDestProjection();
3610
        }
3611
        
3499 3612
    }
3500 3613

  
3501 3614
    public String getDestProjection() {
......
3808 3921
        boolean isView, boolean _isGeoCS) {
3809 3922
        int _srid = -1;
3810 3923

  
3811
        if (o_srid.length() > 0) {
3924
        if ((o_srid != null) && (o_srid.length() > 0)) {
3812 3925
            _srid = Integer.parseInt(o_srid);
3813 3926
        }
3814 3927

  
......
3918 4031
    private void writeMetaTableToLog(Connection con, String tname) {
3919 4032

  
3920 4033
    	logger.debug("======================================================");
3921
    	logger.debug("=     " + ORACLE_GEOMETADATA_VIEW + "     =====================");
4034
    	logger.debug("=     " + ORACLE_GEOMETADATA_VIEW + "  (1 EVERY 10 TABLES) ========");
3922 4035
    	logger.debug("======================================================");
3923 4036

  
3924 4037
    	try {
3925 4038
            Statement _stmt = con.createStatement();
3926 4039
            String sql = "SELECT * FROM " + tname;
3927 4040
            ResultSet res = _stmt.executeQuery(sql);
4041
            
4042
            int count = 0;
3928 4043
            while (res.next()) {
3929
            	logger.debug(
3930
            			"OWNER: " + res.getString("OWNER")
3931
            			+ ", TABLE_NAME: " + res.getString("TABLE_NAME")
3932
            			+ ", COLUMN_NAME: " + res.getString("COLUMN_NAME")
3933
            			+ ", SRID: " + res.getString("SRID"));
3934
            	ARRAY _aux = (ARRAY) res.getObject("DIMINFO");
3935
            	String dinfo = OracleSpatialUtils.getDimInfoAsString(_aux);
3936
            	logger.debug("DIMINFO: " + dinfo);
3937
            	logger.debug("=========");
4044
            	
4045
            	if ((count % 10) == 0) {
4046
                	logger.debug(
4047
                			"OWNER: " + res.getString("OWNER")
4048
                			+ ", TABLE_NAME: " + res.getString("TABLE_NAME")
4049
                			+ ", COLUMN_NAME: " + res.getString("COLUMN_NAME")
4050
                			+ ", SRID: " + res.getString("SRID"));
4051
                	ARRAY _aux = (ARRAY) res.getObject("DIMINFO");
4052
                	String dinfo = OracleSpatialUtils.getDimInfoAsString(_aux);
4053
                	logger.debug("DIMINFO: " + dinfo);
4054
                	logger.debug("=========");
4055
            	}
4056
            	count++;
4057
            	
3938 4058
            }
3939 4059
    	} catch (Throwable th) {
3940 4060

  
3941 4061
    	}
3942

  
3943

  
3944

  
3945

  
3946

  
3947
		// TODO Auto-generated method stub
3948

  
3949 4062
	}
3950 4063

  
3951 4064
	/**
......
4152 4265
        String[] ret = new String[rsmd.getColumnCount()];
4153 4266

  
4154 4267
        for (int i = 0; i < ret.length; i++) {
4155
            ret[i] = rsmd.getColumnTypeName(i + 1);
4268
            if (rsmd.getColumnType(i + 1) == Types.NUMERIC) {
4269
            	int scale = rsmd.getScale(i+1);
4270
            	if (scale >= 0) {
4271
                	String prec_dec = " (" + rsmd.getPrecision(i+1) + ", " + scale + ")";  
4272
                	ret[i] = rsmd.getColumnTypeName(i + 1) + prec_dec;
4273
            	} else {
4274
            		ret[i] = rsmd.getColumnTypeName(i + 1);
4275
            	}
4276
            } else {
4277
                ret[i] = rsmd.getColumnTypeName(i + 1);
4278
            }
4156 4279
        }
4157 4280

  
4158 4281
        rs.close();
......
4193 4316
        // on(VectorialEditableDBAdapter.java:290)
4194 4317
        if (writer == null) {
4195 4318

  
4196
            writer = new OracleSpatialWriter(getRowCount());
4319
        	long count = 0;
4320
        	count = getRowCount();
4321
        	
4322
            writer = new OracleSpatialWriter(count);
4197 4323
            writer.setDriver(this);
4198 4324
            writer.setLyrShapeType(getShapeType());
4199 4325
            writer.setGeoCS(isGeogCS());
......
4363 4489
    	return false;
4364 4490
    }
4365 4491

  
4366
    private Rectangle2D getFastEstimatedGeodeticExtent(
4367
    		String tname, String gfield, IConnection c, int sample_size, double enlargement) {
4492
    private Rectangle2D getFastEstimatedExtent(
4493
    		String tname,
4494
    		String gfield,
4495
    		IConnection c,
4496
    		int sample_size,
4497
    		double enlargement,
4498
    		boolean is_geo) {
4368 4499

  
4369 4500
    	Rectangle2D world = new Rectangle2D.Double(-180, -90, 360, 180);
4370 4501
    	Rectangle2D resp_aux = null;
......
4389 4520
			}
4390 4521
		} catch (Exception ex) {
4391 4522
			logger.error("While getting random sample: " + ex.getMessage());
4523
		}
4524

  
4525
		if (resp_aux == null) {
4526
			logger.warn("ERROR, ESTIMATED BB = WORLD");
4392 4527
			return world;
4393 4528
		}
4394

  
4395
		if (resp_aux == null) return world;
4529
		
4396 4530
		double w = resp_aux.getWidth();
4397 4531
		double h = resp_aux.getHeight();
4398 4532
		double x = resp_aux.getMinX();
4399 4533
		double y = resp_aux.getMinY();
4400 4534

  
4401
		// enlarge 10 times:
4535
		// enlarge n times:
4402 4536
		double newx = x - (0.5 * (enlargement - 1)) * w;
4403
		double newy = y - (0.5 * (enlargement - 1)) * w;
4537
		double newy = y - (0.5 * (enlargement - 1)) * h;
4404 4538
		Rectangle2D resp_aux_large = new Rectangle2D.Double(newx, newy,
4405 4539
				enlargement * w,
4406 4540
				enlargement * h);
4407 4541

  
4542
		if (is_geo) {
4543
			Rectangle2D.intersect(world, resp_aux_large, resp_aux);
4544
			logger.debug("FAST BB: " + resp_aux.toString());
4545
			return resp_aux;
4546
		} else {
4547
			logger.debug("FAST BB: " + resp_aux_large.toString());
4548
			return resp_aux_large;
4549
		}
4408 4550

  
4409
		Rectangle2D.intersect(world, resp_aux_large, resp_aux);
4410

  
4411
		logger.debug("FAST BB:");
4412
		logger.debug(" min x:" + resp_aux.getMinX());
4413
		logger.debug(" min y:" + resp_aux.getMinY());
4414
		logger.debug("     w:" + resp_aux.getWidth());
4415
		logger.debug("     h:" + resp_aux.getHeight());
4416
		return resp_aux;
4417 4551
    }
4418 4552

  
4419
    private Rectangle2D getEstimatedGeodeticExtent(
4420
    		String tname, String gfield, IConnection c, int sample_size, double enlargement) {
4553
    private Rectangle2D getEstimatedExtent(
4554
    		String tname,
4555
    		String gfield,
4556
    		IConnection c,
4557
    		int sample_size,
4558
    		double enlargement,
4559
    		boolean is_geo) {
4421 4560

  
4422 4561
    	Rectangle2D world = new Rectangle2D.Double(-180, -90, 360, 180);
4423 4562

  
......
4459 4598
			}
4460 4599
		} catch (Exception ex) {
4461 4600
			logger.error("While getting random sample: " + ex.getMessage());
4601
		}
4602

  
4603
		if (resp_aux == null) {
4604
			logger.warn("ERROR, ESTIMATED BB = WORLD");
4462 4605
			return world;
4463 4606
		}
4464

  
4465
		if (resp_aux == null) return world;
4607
		
4466 4608
		double w = resp_aux.getWidth();
4467 4609
		double h = resp_aux.getHeight();
4468 4610
		double x = resp_aux.getMinX();
......
4470 4612

  
4471 4613
		// enlarge 10 times:
4472 4614
		double newx = x - (0.5 * (enlargement - 1)) * w;
4473
		double newy = y - (0.5 * (enlargement - 1)) * w;
4615
		double newy = y - (0.5 * (enlargement - 1)) * h;
4474 4616
		Rectangle2D resp_aux_large = new Rectangle2D.Double(newx, newy,
4475 4617
				enlargement * w,
4476 4618
				enlargement * h);
4477 4619

  
4620
		if (is_geo) {
4621
			Rectangle2D.intersect(world, resp_aux_large, resp_aux);
4622
			logger.debug("ESTIMATED BB: " + resp_aux.toString());
4623
			return resp_aux;
4624
		} else {
4625
			logger.debug("ESTIMATED BB: " + resp_aux_large.toString());
4626
			return resp_aux_large;
4627
		}
4478 4628

  
4479
		Rectangle2D.intersect(world, resp_aux_large, resp_aux);
4480
		return resp_aux;
4481 4629
    }
4482 4630

  
4483 4631
    public void setUserName(String u) {
......
4663 4811
    	return new Timestamp(year, month, day, hour, minute, second, a_nanos);
4664 4812
    }
4665 4813
    
4666
    public static int nvarchar2Limited(int n) {
4814
	public static int nvarchar2Limited(int n) {
4815
		
4816
		if (n <= VARCHAR2_STANDARD_SIZE) return VARCHAR2_STANDARD_SIZE;
4667 4817
		if (n <= VARCHAR2_LONG_SIZE) return n;
4818
		
4668 4819
		return VARCHAR2_LONG_SIZE;
4669
	}    
4820
	}
4821
	
4822
    private boolean isSimpleCollectionOfLines(Datum[] all_elem) {
4823
    	
4824
    	try {
4825
        	int size = all_elem.length;
4826
        	if (all_elem[1].intValue() != 4) return false;
4827
        	int size3 = size / 3;
4828
        	
4829
        	for (int i=1; i<size3; i++) {
4830
        		if (all_elem[3 * i + 1].intValue() != 2) return false; 
4831
        	}
4832
        	return true;
4833
        	
4834
    	} catch (SQLException ex) {
4835
    		logger.error("While is simple line collection: " + ex.getMessage());
4836
    	}
4837
    	
4838
    	return false;
4839
    }
4840
    
4841
    private Datum[] removeThreeFirst(Datum[] elem) {
4842
    	int sz = elem.length;
4843
    	Datum[] resp = new Datum[sz - 3];
4844
    	for (int i=3; i<sz; i++) resp[i - 3] = elem[i];
4845
    	return resp;
4846
    }
4847
	
4848
	public static String removePrefix(String str) {
4849
		
4850
		int colon_ind = str.indexOf(":");
4851
		if (colon_ind != -1) {
4852
			return str.substring(colon_ind + 1);
4853
		} else {
4854
			return str;
4855
		}
4856
	}
4857
	
4858
	public boolean canWriteGeometry(int gvSIGgeometryType) {
4859
		if (writer == null) {
4860
			return true;
4861
		} else {
4862
			return writer.canWriteGeometry(gvSIGgeometryType);
4863
		}
4864
		
4865
	}
4670 4866

  
4671 4867

  
4672 4868
}

Also available in: Unified diff