root / branches / v10 / extensions / extSDE / src / com / iver / cit / gvsig / fmap / drivers / sde / ArcSdeWriter.java @ 11012
History | View | Annotate | Download (13.8 KB)
1 |
package com.iver.cit.gvsig.fmap.drivers.sde; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
import java.sql.SQLException; |
5 |
import java.sql.Types; |
6 |
import java.util.Calendar; |
7 |
import java.util.Date; |
8 |
|
9 |
import com.esri.sde.sdk.client.SeColumnDefinition; |
10 |
import com.esri.sde.sdk.client.SeConnection; |
11 |
import com.esri.sde.sdk.client.SeCoordinateReference; |
12 |
import com.esri.sde.sdk.client.SeDelete; |
13 |
import com.esri.sde.sdk.client.SeException; |
14 |
import com.esri.sde.sdk.client.SeInsert; |
15 |
import com.esri.sde.sdk.client.SeLayer; |
16 |
import com.esri.sde.sdk.client.SeObjectId; |
17 |
import com.esri.sde.sdk.client.SeRow; |
18 |
import com.esri.sde.sdk.client.SeShape; |
19 |
import com.esri.sde.sdk.client.SeUpdate; |
20 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
21 |
import com.iver.cit.gvsig.fmap.core.FShape; |
22 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
23 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
24 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
25 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
26 |
import com.iver.cit.gvsig.fmap.drivers.sde.utils.SDELayerDefinition; |
27 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
28 |
import com.iver.cit.gvsig.fmap.edition.IFieldManager; |
29 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
30 |
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter; |
31 |
import com.iver.cit.gvsig.fmap.edition.writers.AbstractWriter; |
32 |
|
33 |
public class ArcSdeWriter extends AbstractWriter implements ISpatialWriter, IFieldManager { |
34 |
|
35 |
private int numRows; |
36 |
|
37 |
private SDELayerDefinition lyrDef;
|
38 |
|
39 |
private SeConnection conex;
|
40 |
|
41 |
//private Statement st;
|
42 |
|
43 |
private boolean bCreateTable; |
44 |
|
45 |
private ArcSdeDriver driver;
|
46 |
|
47 |
private SeLayer selayer;
|
48 |
|
49 |
//private ArcSde postGisSQL = new ArcSde();
|
50 |
// private double flatness;
|
51 |
|
52 |
//private JdbcFieldManager fieldManager;
|
53 |
|
54 |
/**
|
55 |
* Useful to create a layer from scratch Call setFile before using this
|
56 |
* function
|
57 |
*
|
58 |
* @param lyrDef
|
59 |
* @throws IOException
|
60 |
* @throws DriverException
|
61 |
*/
|
62 |
public void initialize(ITableDefinition lyrD) throws EditionException { |
63 |
super.initialize(lyrD);
|
64 |
this.lyrDef = (SDELayerDefinition) lyrD;
|
65 |
conex = (SeConnection)lyrDef.getConnection(); |
66 |
//
|
67 |
// try {
|
68 |
// //st = conex.createStatement();
|
69 |
//
|
70 |
// if (bCreateTable) {
|
71 |
// try {
|
72 |
// st.execute("DROP TABLE " + lyrDef.getTableName() + ";");
|
73 |
// } catch (SQLException e1) {
|
74 |
// // Si no existe la tabla, no hay que borrarla.
|
75 |
// }
|
76 |
//
|
77 |
// String sqlCreate = postGisSQL.getSqlCreateSpatialTable(lyrDef,
|
78 |
// lyrDef.getFieldsDesc(), true);
|
79 |
// System.out.println("sqlCreate =" + sqlCreate);
|
80 |
// st.execute(sqlCreate);
|
81 |
//
|
82 |
// String sqlAlter = postGisSQL.getSqlAlterTable(lyrDef);
|
83 |
// System.out.println("sqlAlter =" + sqlAlter);
|
84 |
// st.execute(sqlAlter);
|
85 |
// // CREATE TABLE PARKS ( PARK_ID int4, PARK_NAME varchar(128),
|
86 |
// // PARK_DATE date, PARK_TYPE varchar(2) );
|
87 |
// // SELECT AddGeometryColumn('parks_db', 'parks', 'park_geom',
|
88 |
// // 128,
|
89 |
// // 'MULTIPOLYGON', 2 );
|
90 |
//
|
91 |
// /*
|
92 |
// * BEGIN; INSERT INTO ROADS_GEOM (ID,GEOM,NAME ) VALUES
|
93 |
// * (1,GeometryFromText('LINESTRING(191232 243118,191108
|
94 |
// * 243242)',-1),'Jeff Rd'); INSERT INTO ROADS_GEOM (ID,GEOM,NAME )
|
95 |
// * VALUES (2,GeometryFromText('LINESTRING(189141 244158,189265
|
96 |
// * 244817)',-1),'Geordie Rd'); COMMIT;
|
97 |
// */
|
98 |
// conex.commit();
|
99 |
// }
|
100 |
// conex.setAutoCommit(false);
|
101 |
// fieldManager = new JdbcFieldManager(conex, lyrDef.getTableName());
|
102 |
//
|
103 |
// } catch (SQLException e) {
|
104 |
// e.printStackTrace();
|
105 |
// throw new EditionException(e);
|
106 |
// }
|
107 |
|
108 |
} |
109 |
|
110 |
public void preProcess() throws EditionException { |
111 |
numRows = 0;
|
112 |
// ATENTION: We will transform (in PostGIS class; doubleQuote())
|
113 |
// to UTF-8 strings. Then, we tell the PostgreSQL server
|
114 |
// that we will use UTF-8, and it can translate
|
115 |
// to its charset
|
116 |
// Note: we have to translate to UTF-8 because
|
117 |
// the server cannot manage UTF-16
|
118 |
|
119 |
//ResultSet rsAux;
|
120 |
try {
|
121 |
//conex.rollbackTransaction();
|
122 |
conex.startTransaction(); |
123 |
alterTable(); |
124 |
|
125 |
//rsAux = conex.getRelease().st.executeQuery("SHOW server_encoding;");
|
126 |
//rsAux.next();
|
127 |
//String serverEncoding = rsAux.getString(1);
|
128 |
//System.out.println("Server encoding = " + serverEncoding);
|
129 |
// st.execute("SET CLIENT_ENCODING TO 'UNICODE';");
|
130 |
// Intentamos convertir nuestras cadenas a ese encode.
|
131 |
// postGisSQL.setEncoding(serverEncoding);
|
132 |
} catch (SeException e) {
|
133 |
// TODO Auto-generated catch block
|
134 |
e.printStackTrace(); |
135 |
} |
136 |
|
137 |
} |
138 |
|
139 |
public void process(IRowEdited _row) throws EditionException { |
140 |
int status = _row.getStatus();
|
141 |
try {
|
142 |
switch (status) {
|
143 |
case IRowEdited.STATUS_ADDED:
|
144 |
addRow(_row); |
145 |
|
146 |
/*
|
147 |
// TODO when addRowInCreation() is implemented:
|
148 |
if (tableCreation) {
|
149 |
addRowInCreation(_row);
|
150 |
} else {
|
151 |
addRow(_row);
|
152 |
}
|
153 |
*/
|
154 |
break;
|
155 |
|
156 |
case IRowEdited.STATUS_DELETED:
|
157 |
|
158 |
deleteRow(_row); |
159 |
|
160 |
|
161 |
break;
|
162 |
|
163 |
case IRowEdited.STATUS_MODIFIED:
|
164 |
updateRow(_row); |
165 |
|
166 |
break;
|
167 |
|
168 |
case IRowEdited.STATUS_ORIGINAL:
|
169 |
originalRow(_row); |
170 |
|
171 |
break;
|
172 |
} |
173 |
} catch (SeException e) {
|
174 |
e.printStackTrace(); |
175 |
throw new EditionException(); |
176 |
} catch (IOException e) { |
177 |
e.printStackTrace(); |
178 |
throw new EditionException(); |
179 |
} |
180 |
} |
181 |
private void originalRow(IRowEdited _row) { |
182 |
// TODO Auto-generated method stub
|
183 |
|
184 |
} |
185 |
|
186 |
private void updateRow(IRowEdited irow) throws SeException, IOException { |
187 |
// the values associated with the given record.
|
188 |
SeUpdate update = new SeUpdate(conex);
|
189 |
String[] cols = lyrDef.getFieldNames(); |
190 |
// String featureId = feature.getID().substring(
|
191 |
// feature.getID().lastIndexOf('.') + 1,
|
192 |
// feature.getID().length());
|
193 |
update.toTable(selayer.getQualifiedName(), cols, |
194 |
"SHAPE" + " = " + irow.getID()); |
195 |
update.setWriteMode(true);
|
196 |
|
197 |
SeRow row = update.getRowToSet(); |
198 |
|
199 |
// Set values on rows here.....
|
200 |
for (int i = 0; i < cols.length; i++) { |
201 |
if (cols[i].equals("SHAPE")){ |
202 |
setRowValue(row,i,((IFeature)irow.getLinkedRow()).getGeometry()); |
203 |
}else{
|
204 |
setRowValue(row, i, irow.getAttribute(i)); |
205 |
} |
206 |
} |
207 |
|
208 |
update.execute(); |
209 |
update.close(); |
210 |
|
211 |
} |
212 |
|
213 |
private void deleteRow(IRowEdited _row) throws SeException { |
214 |
// if ((this.features == null)
|
215 |
// || (this.currentIndex >= this.features.size())) {
|
216 |
// throw new IOException("No current feature available.");
|
217 |
// }
|
218 |
|
219 |
// if (this.notInserted) {
|
220 |
// this.features.remove(this.currentIndex--);
|
221 |
// this.notInserted = false;
|
222 |
// } else {
|
223 |
// Feature feature = (Feature) this.features.get(this.currentIndex);
|
224 |
// PooledConnection connection = null;
|
225 |
|
226 |
// connection = getConnection();
|
227 |
|
228 |
SeDelete seDelete = new SeDelete(conex);
|
229 |
|
230 |
long featureId = Long.parseLong(_row.getID()); |
231 |
SeObjectId objectID = new SeObjectId(featureId);
|
232 |
seDelete.byId(selayer.getQualifiedName(), objectID); |
233 |
//this.dataStore.fireRemoved(feature);
|
234 |
// } catch (Exception e) {
|
235 |
// throw new SOException(e.getMessage());
|
236 |
// } finally {
|
237 |
conex.close(); |
238 |
// }
|
239 |
// }
|
240 |
} |
241 |
|
242 |
private void addRow(IRowEdited irow) throws EditionException { |
243 |
try {
|
244 |
//Feature feature = (Feature) this.features.get(this.currentIndex);
|
245 |
//FeatureType featureType = feature.getFeatureType();
|
246 |
//AttributeType[] attributeTypes = featureType.getAttributeTypes();
|
247 |
//connection = getConnection();
|
248 |
|
249 |
//if (this.notInserted) {
|
250 |
// We must insert the record into ArcSDE
|
251 |
SeInsert insert = new SeInsert(conex);
|
252 |
String[] cols = lyrDef.getFieldNames(); |
253 |
insert.intoTable(selayer.getQualifiedName(), cols); |
254 |
insert.setWriteMode(true);
|
255 |
|
256 |
SeRow row = insert.getRowToSet(); |
257 |
|
258 |
// Now set the values for the new row here...
|
259 |
for (int i = 0; i < cols.length; i++) { |
260 |
if (cols[i].equals("SHAPE")){ |
261 |
setRowValue(row,i,((IFeature)irow.getLinkedRow()).getGeometry()); |
262 |
}else{
|
263 |
setRowValue(row, i, irow.getAttribute(i)); |
264 |
} |
265 |
} |
266 |
|
267 |
// Now "commit" the changes.
|
268 |
insert.execute(); |
269 |
insert.close(); |
270 |
//this.dataStore.fireAdded(feature);
|
271 |
//}
|
272 |
|
273 |
// else {
|
274 |
// // The record is already inserted, so we will be updating
|
275 |
// // the values associated with the given record.
|
276 |
// SeUpdate update = new SeUpdate(connection);
|
277 |
// String[] cols = getColumns(attributeTypes, connection);
|
278 |
// String featureId = feature.getID().substring(
|
279 |
// feature.getID().lastIndexOf('.') + 1,
|
280 |
// feature.getID().length());
|
281 |
// update.toTable(this.layer.getQualifiedName(), cols,
|
282 |
// this.spatialColumnName + " = " + featureId);
|
283 |
// update.setWriteMode(true);
|
284 |
//
|
285 |
// SeRow row = update.getRowToSet();
|
286 |
//
|
287 |
// // Set values on rows here.....
|
288 |
// for (int i = 0; i < cols.length; i++) {
|
289 |
// Object value = feature
|
290 |
// .getAttribute(this.mutableAttributeIndexes[i]
|
291 |
// .intValue());
|
292 |
// setRowValue(row, i, value);
|
293 |
// }
|
294 |
//
|
295 |
// update.execute();
|
296 |
// update.close();
|
297 |
//
|
298 |
// }
|
299 |
} catch (Exception e) { |
300 |
|
301 |
// LOGGER.log(Level.WARNING, e.getMessage(), e);
|
302 |
// if (LOGGER.isLoggable(Level.FINE)) {
|
303 |
// e.printStackTrace();
|
304 |
// }
|
305 |
// throw new DataSourceException(e.getMessage(), e);
|
306 |
} finally {
|
307 |
try {
|
308 |
conex.close(); |
309 |
} catch (SeException e) {
|
310 |
// TODO Auto-generated catch block
|
311 |
e.printStackTrace(); |
312 |
} |
313 |
} |
314 |
} |
315 |
|
316 |
/**
|
317 |
* Used to set a value on an SeRow object. The values is converted to the
|
318 |
* appropriate type based on an inspection of the SeColumnDefintion object.
|
319 |
*
|
320 |
* @param row
|
321 |
* @param index
|
322 |
* @param value
|
323 |
*
|
324 |
* @throws SeException
|
325 |
* DOCUMENT ME!
|
326 |
* @throws IOException
|
327 |
* DOCUMENT ME!
|
328 |
*/
|
329 |
private void setRowValue(SeRow row, int index, Object value) |
330 |
throws SeException, IOException { |
331 |
SeColumnDefinition seColumnDefinition = null;
|
332 |
seColumnDefinition = row.getColumnDef(index); |
333 |
|
334 |
switch (seColumnDefinition.getType()) {
|
335 |
case SeColumnDefinition.TYPE_INTEGER: {
|
336 |
if (value != null) { |
337 |
row.setInteger(index, new Integer(value.toString())); |
338 |
} else {
|
339 |
row.setInteger(index, null);
|
340 |
} |
341 |
|
342 |
break;
|
343 |
} |
344 |
|
345 |
case SeColumnDefinition.TYPE_SMALLINT: {
|
346 |
if (value != null) { |
347 |
row.setShort(index, new Short(value.toString())); |
348 |
} else {
|
349 |
row.setShort(index, null);
|
350 |
} |
351 |
|
352 |
break;
|
353 |
} |
354 |
|
355 |
case SeColumnDefinition.TYPE_FLOAT: {
|
356 |
if (value != null) { |
357 |
row.setFloat(index, new Float(value.toString())); |
358 |
} else {
|
359 |
row.setFloat(index, null);
|
360 |
} |
361 |
|
362 |
break;
|
363 |
} |
364 |
|
365 |
case SeColumnDefinition.TYPE_DOUBLE: {
|
366 |
if (value != null) { |
367 |
row.setDouble(index, new Double(value.toString())); |
368 |
} else {
|
369 |
row.setDouble(index, null);
|
370 |
} |
371 |
|
372 |
break;
|
373 |
} |
374 |
|
375 |
case SeColumnDefinition.TYPE_STRING: {
|
376 |
if (value != null) { |
377 |
row.setString(index, value.toString()); |
378 |
} else {
|
379 |
row.setString(index, null);
|
380 |
} |
381 |
|
382 |
break;
|
383 |
} |
384 |
|
385 |
case SeColumnDefinition.TYPE_DATE: {
|
386 |
if (value != null) { |
387 |
Calendar calendar = Calendar.getInstance(); |
388 |
calendar.setTime((Date) value);
|
389 |
row.setTime(index, calendar); |
390 |
} else {
|
391 |
row.setTime(index, null);
|
392 |
} |
393 |
|
394 |
break;
|
395 |
} |
396 |
|
397 |
case SeColumnDefinition.TYPE_SHAPE: {
|
398 |
if (value != null) { |
399 |
try {
|
400 |
SeCoordinateReference coordRef = selayer.getCoordRef(); |
401 |
IGeometry geom = (IGeometry) value; |
402 |
SeShape shape = ArcSdeFeatureIterator.constructShape(geom, |
403 |
coordRef); |
404 |
row.setShape(index, shape); |
405 |
} catch (Exception e) { |
406 |
e.printStackTrace(); |
407 |
} |
408 |
} else {
|
409 |
row.setShape(index, null);
|
410 |
} |
411 |
|
412 |
break;
|
413 |
} |
414 |
} |
415 |
} |
416 |
|
417 |
public void postProcess() throws EditionException { |
418 |
try {
|
419 |
conex.commitTransaction(); |
420 |
} catch (SeException e) {
|
421 |
e.printStackTrace(); |
422 |
throw new EditionException(e); |
423 |
} |
424 |
} |
425 |
|
426 |
public String getName() { |
427 |
return "gvSIG SDE Writer"; |
428 |
} |
429 |
|
430 |
public boolean canWriteGeometry(int gvSIGgeometryType) { |
431 |
switch (gvSIGgeometryType) {
|
432 |
case FShape.POINT:
|
433 |
return true; |
434 |
case FShape.LINE:
|
435 |
return true; |
436 |
case FShape.POLYGON:
|
437 |
return true; |
438 |
case FShape.ARC:
|
439 |
return false; |
440 |
case FShape.ELLIPSE:
|
441 |
return false; |
442 |
case FShape.MULTIPOINT:
|
443 |
return true; |
444 |
case FShape.TEXT:
|
445 |
return false; |
446 |
} |
447 |
return false; |
448 |
} |
449 |
|
450 |
public boolean canWriteAttribute(int sqlType) { |
451 |
switch (sqlType) {
|
452 |
case Types.DOUBLE: |
453 |
case Types.FLOAT: |
454 |
case Types.INTEGER: |
455 |
case Types.BIGINT: |
456 |
return true; |
457 |
case Types.DATE: |
458 |
return true; |
459 |
case Types.BIT: |
460 |
case Types.BOOLEAN: |
461 |
return true; |
462 |
case Types.VARCHAR: |
463 |
case Types.CHAR: |
464 |
case Types.LONGVARCHAR: |
465 |
return true; |
466 |
|
467 |
} |
468 |
|
469 |
return false; |
470 |
} |
471 |
|
472 |
/**
|
473 |
* @return Returns the bCreateTable.
|
474 |
*/
|
475 |
public boolean isCreateTable() { |
476 |
return bCreateTable;
|
477 |
} |
478 |
|
479 |
/**
|
480 |
* @param createTable
|
481 |
* The bCreateTable to set.
|
482 |
*/
|
483 |
public void setCreateTable(boolean createTable) { |
484 |
bCreateTable = createTable; |
485 |
} |
486 |
|
487 |
public FieldDescription[] getOriginalFields() { |
488 |
return lyrDef.getFieldsDesc();
|
489 |
} |
490 |
|
491 |
public void addField(FieldDescription fieldDesc) { |
492 |
// fieldManager.addField(fieldDesc);
|
493 |
|
494 |
} |
495 |
|
496 |
public FieldDescription removeField(String fieldName) { |
497 |
// return fieldManager.removeField(fieldName);
|
498 |
return null; |
499 |
} |
500 |
|
501 |
public void renameField(String antName, String newName) { |
502 |
// fieldManager.renameField(antName, newName);
|
503 |
|
504 |
} |
505 |
|
506 |
public boolean alterTable() throws EditionException { |
507 |
// return fieldManager.alterTable();
|
508 |
return false; |
509 |
} |
510 |
|
511 |
public FieldDescription[] getFields() { |
512 |
// return fieldManager.getFields();
|
513 |
return lyrDef.getFieldsDesc();
|
514 |
} |
515 |
|
516 |
public boolean canAlterTable() { |
517 |
return true; |
518 |
} |
519 |
|
520 |
public boolean canSaveEdits() { |
521 |
// try {
|
522 |
// return !conex.isReadOnly();
|
523 |
// } catch (SQLException e) {
|
524 |
// // TODO Auto-generated catch block
|
525 |
// e.printStackTrace();
|
526 |
// return false;
|
527 |
// }
|
528 |
return false; |
529 |
} |
530 |
|
531 |
public void setDriver(ArcSdeDriver driver) { |
532 |
this.driver=driver;
|
533 |
|
534 |
} |
535 |
|
536 |
public void setSeLayer(SeLayer layer) { |
537 |
this.selayer=layer;
|
538 |
} |
539 |
|
540 |
} |