Statistics
| Revision:

root / trunk / extensions / extSDE / src / com / iver / cit / gvsig / fmap / drivers / sde / ArcSdeWriter.java @ 11193

History | View | Annotate | Download (18.3 KB)

1
package com.iver.cit.gvsig.fmap.drivers.sde;
2

    
3
import java.io.IOException;
4
import java.sql.Types;
5
import java.util.Calendar;
6
import java.util.Date;
7

    
8
import com.esri.sde.sdk.client.SeColumnDefinition;
9
import com.esri.sde.sdk.client.SeConnection;
10
import com.esri.sde.sdk.client.SeCoordinateReference;
11
import com.esri.sde.sdk.client.SeDelete;
12
import com.esri.sde.sdk.client.SeException;
13
import com.esri.sde.sdk.client.SeInsert;
14
import com.esri.sde.sdk.client.SeLayer;
15
import com.esri.sde.sdk.client.SeObjectId;
16
import com.esri.sde.sdk.client.SeRow;
17
import com.esri.sde.sdk.client.SeShape;
18
import com.esri.sde.sdk.client.SeUpdate;
19
import com.iver.cit.gvsig.fmap.core.FShape;
20
import com.iver.cit.gvsig.fmap.core.IFeature;
21
import com.iver.cit.gvsig.fmap.core.IGeometry;
22
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
23
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
24
import com.iver.cit.gvsig.fmap.edition.EditionException;
25
import com.iver.cit.gvsig.fmap.edition.IFieldManager;
26
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
27
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter;
28
import com.iver.cit.gvsig.fmap.edition.writers.AbstractWriter;
29

    
30

    
31
/**
32
 * DOCUMENT ME!
33
 *
34
 * @author Vicente Caballero Navarro
35
 */
36
public class ArcSdeWriter extends AbstractWriter implements ISpatialWriter,
37
    IFieldManager {
38
    private int numRows;
39
    private ArcSDELayerDefinition lyrDef;
40
    private SeConnection conex;
41

    
42
    //private Statement st;
43
    private boolean bCreateTable;
44
    private ArcSdeDriver driver;
45
    private SeLayer selayer;
46

    
47
    //private ArcSde postGisSQL = new ArcSde();
48
    // private double flatness;
49
    //private JdbcFieldManager fieldManager;
50

    
51
    /**
52
     * Useful to create a layer from scratch Call setFile before using this
53
     * function
54
     *
55
     * @param lyrD
56
     *
57
     * @throws EditionException
58
     */
59
    public void initialize(ITableDefinition lyrD) throws EditionException {
60
        super.initialize(lyrD);
61
        this.lyrDef = (ArcSDELayerDefinition) lyrD;
62
        conex = (SeConnection) lyrDef.getConnection();
63

    
64
        //
65
        //                try {
66
        //                        //st = conex.createStatement();
67
        //
68
        //                        if (bCreateTable) {
69
        //                                try {
70
        //                                        st.execute("DROP TABLE " + lyrDef.getTableName() + ";");
71
        //                                } catch (SQLException e1) {
72
        //                                        // Si no existe la tabla, no hay que borrarla.
73
        //                                }
74
        //
75
        //                                String sqlCreate = postGisSQL.getSqlCreateSpatialTable(lyrDef,
76
        //                                                lyrDef.getFieldsDesc(), true);
77
        //                                System.out.println("sqlCreate =" + sqlCreate);
78
        //                                st.execute(sqlCreate);
79
        //
80
        //                                String sqlAlter = postGisSQL.getSqlAlterTable(lyrDef);
81
        //                                System.out.println("sqlAlter =" + sqlAlter);
82
        //                                st.execute(sqlAlter);
83
        //                                // CREATE TABLE PARKS ( PARK_ID int4, PARK_NAME varchar(128),
84
        //                                // PARK_DATE date, PARK_TYPE varchar(2) );
85
        //                                // SELECT AddGeometryColumn('parks_db', 'parks', 'park_geom',
86
        //                                // 128,
87
        //                                // 'MULTIPOLYGON', 2 );
88
        //
89
        //                                /*
90
        //                                 * BEGIN; INSERT INTO ROADS_GEOM (ID,GEOM,NAME ) VALUES
91
        //                                 * (1,GeometryFromText('LINESTRING(191232 243118,191108
92
        //                                 * 243242)',-1),'Jeff Rd'); INSERT INTO ROADS_GEOM (ID,GEOM,NAME )
93
        //                                 * VALUES (2,GeometryFromText('LINESTRING(189141 244158,189265
94
        //                                 * 244817)',-1),'Geordie Rd'); COMMIT;
95
        //                                 */
96
        //                                conex.commit();
97
        //                        }
98
        //                        conex.setAutoCommit(false);
99
        //                        fieldManager = new JdbcFieldManager(conex, lyrDef.getTableName());
100
        //
101
        //                } catch (SQLException e) {
102
        //                        e.printStackTrace();
103
        //                        throw new EditionException(e);
104
        //                }
105
    }
106

    
107
    /**
108
     * DOCUMENT ME!
109
     *
110
     * @throws EditionException DOCUMENT ME!
111
     */
112
    public void preProcess() throws EditionException {
113
        numRows = 0;
114

    
115
        // ATENTION: We will transform (in PostGIS class; doubleQuote())
116
        // to UTF-8 strings. Then, we tell the PostgreSQL server
117
        // that we will use UTF-8, and it can translate
118
        // to its charset
119
        // Note: we have to translate to UTF-8 because
120
        // the server cannot manage UTF-16
121
        //ResultSet rsAux;
122
        try {
123
            //conex.rollbackTransaction();
124
            conex.startTransaction();
125
            alterTable();
126

    
127
            //rsAux = conex.getRelease().st.executeQuery("SHOW server_encoding;");
128
            //rsAux.next();
129
            //String serverEncoding = rsAux.getString(1);
130
            //System.out.println("Server encoding = " + serverEncoding);
131
            // st.execute("SET CLIENT_ENCODING TO 'UNICODE';");
132
            // Intentamos convertir nuestras cadenas a ese encode.
133
            //                postGisSQL.setEncoding(serverEncoding);
134
        } catch (SeException e) {
135
            // TODO Auto-generated catch block
136
            e.printStackTrace();
137
        }
138
    }
139

    
140
    /**
141
     * DOCUMENT ME!
142
     *
143
     * @param _row DOCUMENT ME!
144
     *
145
     * @throws EditionException DOCUMENT ME!
146
     */
147
    public void process(IRowEdited _row) throws EditionException {
148
        int status = _row.getStatus();
149

    
150
        try {
151
            switch (status) {
152
            case IRowEdited.STATUS_ADDED:
153
                addRow(_row);
154

    
155
                /*
156
                 // TODO when addRowInCreation() is implemented:
157
                if (tableCreation) {
158
                        addRowInCreation(_row);
159
                } else {
160
                        addRow(_row);
161
                }
162
                */
163
                break;
164

    
165
            case IRowEdited.STATUS_DELETED:
166
                deleteRow(_row);
167

    
168
                break;
169

    
170
            case IRowEdited.STATUS_MODIFIED:
171
                updateRow(_row);
172

    
173
                break;
174

    
175
            case IRowEdited.STATUS_ORIGINAL:
176
                originalRow(_row);
177

    
178
                break;
179
            }
180
        } catch (SeException e) {
181
            e.printStackTrace();
182
            throw new EditionException();
183
        } catch (IOException e) {
184
            e.printStackTrace();
185
            throw new EditionException();
186
        }
187
    }
188

    
189
    /**
190
     * DOCUMENT ME!
191
     *
192
     * @param _row DOCUMENT ME!
193
     */
194
    private void originalRow(IRowEdited _row) {
195
        // TODO Auto-generated method stub
196
    }
197

    
198
    /**
199
     * DOCUMENT ME!
200
     *
201
     * @param irow DOCUMENT ME!
202
     *
203
     * @throws SeException DOCUMENT ME!
204
     * @throws IOException DOCUMENT ME!
205
     */
206
    private void updateRow(IRowEdited irow) throws SeException, IOException {
207
        //                 the values associated with the given record.
208
        SeUpdate update = new SeUpdate(conex);
209
        String[] cols = lyrDef.getFieldNames();
210

    
211
        //                        String featureId = feature.getID().substring(
212
        //                                        feature.getID().lastIndexOf('.') + 1,
213
        //                                        feature.getID().length());
214
        update.toTable(selayer.getQualifiedName(), cols,
215
            "SHAPE" + " = " + irow.getID());
216
        update.setWriteMode(true);
217

    
218
        SeRow row = update.getRowToSet();
219

    
220
        // Set values on rows here.....
221
        for (int i = 0; i < cols.length; i++) {
222
            if (cols[i].equals("SHAPE")) {
223
                setRowValue(row, i,
224
                    ((IFeature) irow.getLinkedRow()).getGeometry());
225
            } else {
226
                setRowValue(row, i, irow.getAttribute(i));
227
            }
228
        }
229

    
230
        update.execute();
231
        update.close();
232
    }
233

    
234
    /**
235
     * DOCUMENT ME!
236
     *
237
     * @param _row DOCUMENT ME!
238
     *
239
     * @throws SeException DOCUMENT ME!
240
     */
241
    private void deleteRow(IRowEdited _row) throws SeException {
242
        //                                if ((this.features == null)
243
        //                                                || (this.currentIndex >= this.features.size())) {
244
        //                                        throw new IOException("No current feature available.");
245
        //                                }
246
        //                                if (this.notInserted) {
247
        //                                        this.features.remove(this.currentIndex--);
248
        //                                        this.notInserted = false;
249
        //                                } else {
250
        //                                        Feature feature = (Feature) this.features.get(this.currentIndex);
251
        //                                        PooledConnection connection = null;
252
        //                                                connection = getConnection();
253
        SeDelete seDelete = new SeDelete(conex);
254

    
255
        long featureId = Long.parseLong(_row.getID());
256
        SeObjectId objectID = new SeObjectId(featureId);
257
        seDelete.byId(selayer.getQualifiedName(), objectID);
258

    
259
        //this.dataStore.fireRemoved(feature);
260
        //                                        } catch (Exception e) {
261
        //                                                throw new SOException(e.getMessage());
262
        //                                        } finally {
263
        conex.close();
264

    
265
        //                                        }
266
        //                                }
267
    }
268

    
269
    /**
270
     * DOCUMENT ME!
271
     *
272
     * @param irow DOCUMENT ME!
273
     *
274
     * @throws EditionException DOCUMENT ME!
275
     */
276
    private void addRow(IRowEdited irow) throws EditionException {
277
        try {
278
            //Feature feature = (Feature) this.features.get(this.currentIndex);
279
            //FeatureType featureType = feature.getFeatureType();
280
            //AttributeType[] attributeTypes = featureType.getAttributeTypes();
281
            //connection = getConnection();
282
            //if (this.notInserted) {
283
            // We must insert the record into ArcSDE
284
            SeInsert insert = new SeInsert(conex);
285
            String[] cols = lyrDef.getFieldNames();
286
            insert.intoTable(selayer.getQualifiedName(), cols);
287
            insert.setWriteMode(true);
288

    
289
            SeRow row = insert.getRowToSet();
290

    
291
            // Now set the values for the new row here...
292
            for (int i = 0; i < cols.length; i++) {
293
                if (cols[i].equals("SHAPE")) {
294
                    setRowValue(row, i,
295
                        ((IFeature) irow.getLinkedRow()).getGeometry());
296
                } else {
297
                    setRowValue(row, i, irow.getAttribute(i));
298
                }
299
            }
300

    
301
            // Now "commit" the changes.
302
            insert.execute();
303
            insert.close();
304

    
305
            //this.dataStore.fireAdded(feature);
306
            //}
307
            //                 else {
308
            //                                        // The record is already inserted, so we will be updating
309
            //                                        // the values associated with the given record.
310
            //                                        SeUpdate update = new SeUpdate(connection);
311
            //                                        String[] cols = getColumns(attributeTypes, connection);
312
            //                                        String featureId = feature.getID().substring(
313
            //                                                        feature.getID().lastIndexOf('.') + 1,
314
            //                                                        feature.getID().length());
315
            //                                        update.toTable(this.layer.getQualifiedName(), cols,
316
            //                                                        this.spatialColumnName + " = " + featureId);
317
            //                                        update.setWriteMode(true);
318
            //
319
            //                                        SeRow row = update.getRowToSet();
320
            //
321
            //                                        // Set values on rows here.....
322
            //                                        for (int i = 0; i < cols.length; i++) {
323
            //                                                Object value = feature
324
            //                                                                .getAttribute(this.mutableAttributeIndexes[i]
325
            //                                                                                .intValue());
326
            //                                                setRowValue(row, i, value);
327
            //                                        }
328
            //
329
            //                                        update.execute();
330
            //                                        update.close();
331
            //
332
            //                                }
333
        } catch (Exception e) {
334
            //                                LOGGER.log(Level.WARNING, e.getMessage(), e);
335
            //                                if (LOGGER.isLoggable(Level.FINE)) {
336
            //                                        e.printStackTrace();
337
            //                                }
338
            //                                throw new DataSourceException(e.getMessage(), e);
339
        } finally {
340
            try {
341
                conex.close();
342
            } catch (SeException e) {
343
                // TODO Auto-generated catch block
344
                e.printStackTrace();
345
            }
346
        }
347
    }
348

    
349
    /**
350
     * Used to set a value on an SeRow object. The values is converted to the
351
     * appropriate type based on an inspection of the SeColumnDefintion
352
     * object.
353
     *
354
     * @param row
355
     * @param index
356
     * @param value
357
     *
358
     * @throws SeException DOCUMENT ME!
359
     * @throws IOException DOCUMENT ME!
360
     */
361
    private void setRowValue(SeRow row, int index, Object value)
362
        throws SeException, IOException {
363
        SeColumnDefinition seColumnDefinition = null;
364
        seColumnDefinition = row.getColumnDef(index);
365

    
366
        switch (seColumnDefinition.getType()) {
367
        case SeColumnDefinition.TYPE_INTEGER: {
368
            if (value != null) {
369
                row.setInteger(index, new Integer(value.toString()));
370
            } else {
371
                row.setInteger(index, null);
372
            }
373

    
374
            break;
375
        }
376

    
377
        case SeColumnDefinition.TYPE_SMALLINT: {
378
            if (value != null) {
379
                row.setShort(index, new Short(value.toString()));
380
            } else {
381
                row.setShort(index, null);
382
            }
383

    
384
            break;
385
        }
386

    
387
        case SeColumnDefinition.TYPE_FLOAT: {
388
            if (value != null) {
389
                row.setFloat(index, new Float(value.toString()));
390
            } else {
391
                row.setFloat(index, null);
392
            }
393

    
394
            break;
395
        }
396

    
397
        case SeColumnDefinition.TYPE_DOUBLE: {
398
            if (value != null) {
399
                row.setDouble(index, new Double(value.toString()));
400
            } else {
401
                row.setDouble(index, null);
402
            }
403

    
404
            break;
405
        }
406

    
407
        case SeColumnDefinition.TYPE_STRING: {
408
            if (value != null) {
409
                row.setString(index, value.toString());
410
            } else {
411
                row.setString(index, null);
412
            }
413

    
414
            break;
415
        }
416

    
417
        case SeColumnDefinition.TYPE_DATE: {
418
            if (value != null) {
419
                Calendar calendar = Calendar.getInstance();
420
                calendar.setTime((Date) value);
421
                row.setTime(index, calendar);
422
            } else {
423
                row.setTime(index, null);
424
            }
425

    
426
            break;
427
        }
428

    
429
        case SeColumnDefinition.TYPE_SHAPE: {
430
            if (value != null) {
431
                try {
432
                    SeCoordinateReference coordRef = selayer.getCoordRef();
433
                    IGeometry geom = (IGeometry) value;
434
                    SeShape shape = ArcSdeFeatureIterator.constructShape(geom,
435
                            coordRef);
436
                    row.setShape(index, shape);
437
                } catch (Exception e) {
438
                    e.printStackTrace();
439
                }
440
            } else {
441
                row.setShape(index, null);
442
            }
443

    
444
            break;
445
        }
446
        }
447
    }
448

    
449
    /**
450
     * DOCUMENT ME!
451
     *
452
     * @throws EditionException DOCUMENT ME!
453
     */
454
    public void postProcess() throws EditionException {
455
        try {
456
            conex.commitTransaction();
457
        } catch (SeException e) {
458
            e.printStackTrace();
459
            throw new EditionException(e);
460
        }
461
    }
462

    
463
    /**
464
     * DOCUMENT ME!
465
     *
466
     * @return DOCUMENT ME!
467
     */
468
    public String getName() {
469
        return "gvSIG SDE Writer";
470
    }
471

    
472
    /**
473
     * DOCUMENT ME!
474
     *
475
     * @param gvSIGgeometryType DOCUMENT ME!
476
     *
477
     * @return DOCUMENT ME!
478
     */
479
    public boolean canWriteGeometry(int gvSIGgeometryType) {
480
        switch (gvSIGgeometryType) {
481
        case FShape.POINT:
482
            return true;
483

    
484
        case FShape.LINE:
485
            return true;
486

    
487
        case FShape.POLYGON:
488
            return true;
489

    
490
        case FShape.ARC:
491
            return false;
492

    
493
        case FShape.ELLIPSE:
494
            return false;
495

    
496
        case FShape.MULTIPOINT:
497
            return true;
498

    
499
        case FShape.TEXT:
500
            return false;
501
        }
502

    
503
        return false;
504
    }
505

    
506
    /**
507
     * DOCUMENT ME!
508
     *
509
     * @param sqlType DOCUMENT ME!
510
     *
511
     * @return DOCUMENT ME!
512
     */
513
    public boolean canWriteAttribute(int sqlType) {
514
        switch (sqlType) {
515
        case Types.DOUBLE:
516
        case Types.FLOAT:
517
        case Types.INTEGER:
518
        case Types.BIGINT:
519
            return true;
520

    
521
        case Types.DATE:
522
            return true;
523

    
524
        case Types.BIT:
525
        case Types.BOOLEAN:
526
            return true;
527

    
528
        case Types.VARCHAR:
529
        case Types.CHAR:
530
        case Types.LONGVARCHAR:
531
            return true;
532
        }
533

    
534
        return false;
535
    }
536

    
537
    /**
538
     * DOCUMENT ME!
539
     *
540
     * @return Returns the bCreateTable.
541
     */
542
    public boolean isCreateTable() {
543
        return bCreateTable;
544
    }
545

    
546
    /**
547
     * DOCUMENT ME!
548
     *
549
     * @param createTable The bCreateTable to set.
550
     */
551
    public void setCreateTable(boolean createTable) {
552
        bCreateTable = createTable;
553
    }
554

    
555
    /**
556
     * DOCUMENT ME!
557
     *
558
     * @return DOCUMENT ME!
559
     */
560
    public FieldDescription[] getOriginalFields() {
561
        return lyrDef.getFieldsDesc();
562
    }
563

    
564
    /**
565
     * DOCUMENT ME!
566
     *
567
     * @param fieldDesc DOCUMENT ME!
568
     */
569
    public void addField(FieldDescription fieldDesc) {
570
        //                fieldManager.addField(fieldDesc);
571
    }
572

    
573
    /**
574
     * DOCUMENT ME!
575
     *
576
     * @param fieldName DOCUMENT ME!
577
     *
578
     * @return DOCUMENT ME!
579
     */
580
    public FieldDescription removeField(String fieldName) {
581
        //                return fieldManager.removeField(fieldName);
582
        return null;
583
    }
584

    
585
    /**
586
     * DOCUMENT ME!
587
     *
588
     * @param antName DOCUMENT ME!
589
     * @param newName DOCUMENT ME!
590
     */
591
    public void renameField(String antName, String newName) {
592
        //                fieldManager.renameField(antName, newName);
593
    }
594

    
595
    /**
596
     * DOCUMENT ME!
597
     *
598
     * @return DOCUMENT ME!
599
     *
600
     * @throws EditionException DOCUMENT ME!
601
     */
602
    public boolean alterTable() throws EditionException {
603
        //                return fieldManager.alterTable();
604
        return false;
605
    }
606

    
607
    /**
608
     * DOCUMENT ME!
609
     *
610
     * @return DOCUMENT ME!
611
     */
612
    public FieldDescription[] getFields() {
613
        //                return fieldManager.getFields();
614
        return lyrDef.getFieldsDesc();
615
    }
616

    
617
    /**
618
     * DOCUMENT ME!
619
     *
620
     * @return DOCUMENT ME!
621
     */
622
    public boolean canAlterTable() {
623
        return true;
624
    }
625

    
626
    /**
627
     * DOCUMENT ME!
628
     *
629
     * @return DOCUMENT ME!
630
     */
631
    public boolean canSaveEdits() {
632
        //                try {
633
        //                        return !conex.isReadOnly();
634
        //                } catch (SQLException e) {
635
        //                        // TODO Auto-generated catch block
636
        //                        e.printStackTrace();
637
        //                        return false;
638
        //                }
639
        return false;
640
    }
641

    
642
    /**
643
     * DOCUMENT ME!
644
     *
645
     * @param driver DOCUMENT ME!
646
     */
647
    public void setDriver(ArcSdeDriver driver) {
648
        this.driver = driver;
649
    }
650

    
651
    /**
652
     * DOCUMENT ME!
653
     *
654
     * @param layer DOCUMENT ME!
655
     */
656
    public void setSeLayer(SeLayer layer) {
657
        this.selayer = layer;
658
    }
659
}