Statistics
| Revision:

root / branches / v10 / extensions / extSDE / src / com / iver / cit / gvsig / sde / gui / sdewizard / WizardSDE.java @ 10942

History | View | Annotate | Download (15.9 KB)

1
package com.iver.cit.gvsig.sde.gui.sdewizard;
2

    
3
import java.awt.BorderLayout;
4
import java.awt.CardLayout;
5
import java.awt.FlowLayout;
6
import java.sql.SQLException;
7
import java.util.ArrayList;
8
import java.util.HashMap;
9
import java.util.TreeMap;
10
import java.util.Vector;
11

    
12
import javax.swing.JOptionPane;
13
import javax.swing.JPanel;
14

    
15
import org.cresques.cts.IProjection;
16
import org.gvsig.gui.beans.swing.JButton;
17

    
18
import com.esri.sde.sdk.client.SeConnection;
19
import com.esri.sde.sdk.client.SeException;
20
import com.esri.sde.sdk.client.SeLayer;
21
import com.esri.sde.sdk.client.SeRegisteredColumn;
22
import com.esri.sde.sdk.client.SeTable;
23
import com.hardcode.driverManager.DriverLoadException;
24
import com.iver.andami.PluginServices;
25
import com.iver.andami.messages.NotificationManager;
26
import com.iver.cit.gvsig.fmap.core.ICanReproject;
27
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
28
import com.iver.cit.gvsig.fmap.drivers.VectorialSDEDriver;
29
import com.iver.cit.gvsig.fmap.drivers.sde.ArcSdeDriver;
30
import com.iver.cit.gvsig.fmap.drivers.sde.SDELayerDefinition;
31
import com.iver.cit.gvsig.fmap.layers.FLayer;
32
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
33
import com.iver.cit.gvsig.gui.WizardPanel;
34
import com.iver.utiles.NotExistInXMLEntity;
35
import com.iver.utiles.XMLEntity;
36
/**
37
 * @author               Vicente Caballero Navarro
38
 * @uml.dependency   supplier="com.iver.cit.gvsig.sde.gui.sdewizard.DBLayerDefinitionPanel"
39
 * @uml.dependency   supplier="com.iver.cit.gvsig.sde.gui.sdewizard.ConnectionPanel"
40
 * @uml.dependency   supplier="com.iver.cit.gvsig.sde.gui.sdewizard.FieldSelection"
41
 * @uml.dependency   supplier="com.iver.cit.gvsig.sde.gui.sdewizard.ConnectionSettings"
42
 * @uml.dependency   supplier="com.iver.cit.gvsig.sde.gui.sdewizard.UniqueFieldSelection"
43
 * @uml.dependency   supplier="com.iver.cit.gvsig.sde.gui.sdewizard.GeomFieldSelection"
44
 */
45
public class WizardSDE extends WizardPanel{
46

    
47
        /**
48
         * @uml.property  name="pnlWizard"
49
         */
50
        private JPanel pnlWizard = null;
51
        /**
52
         * @uml.property  name="jPanel1"
53
         */
54
        private JPanel jPanel1 = null;
55
        /**
56
         * @uml.property  name="btnBack"
57
         */
58
        private JButton btnBack = null;
59
        /**
60
         * @uml.property  name="btnNext"
61
         */
62
        private JButton btnNext = null;
63
        /**
64
         * @uml.property   name="connectionPanel"
65
         */
66
        private ConnectionPanel connectionPanel = null;
67
        /**
68
         * @uml.property   name="dbLayerDefinition"
69
         */
70
        private DBLayerDefinitionPanel dbLayerDefinition = null;
71
        /**
72
         * @uml.property   name="fieldSelection"
73
         */
74
        private FieldSelection fieldSelection = null;
75

    
76
        private int step = 0;
77
        private final int nsteps = 5;
78
        private static final String CONNECTION = "conn";
79
        private static final String LAYER_DEFINITION = "layerdef";
80
        private static final String FIELD_SELECTION= "fieldsel";
81
        /**
82
         * @uml.property   name="driver"
83
         */
84
        private VectorialSDEDriver driver;
85

    
86
        /**
87
         * @uml.property   name="geomFieldSelection"
88
         */
89
        private GeomFieldSelection geomFieldSelection = null;
90
        /**
91
         * @uml.property   name="uniqueFieldSelection"
92
         */
93
        private UniqueFieldSelection uniqueFieldSelection = null;
94

    
95
    private String[] theTables = null;
96
    private SeConnection conex = null;
97

    
98
        private HashMap settings = new HashMap();
99
        /**
100
         * This is the default constructor
101
         */
102
        public WizardSDE() {
103
                super();
104
                initialize();
105
        }
106
        /**
107
         * This method initializes this
108
         *
109
         * @return void
110
         */
111
        private  void initialize() {
112
                setTabName("SDE");
113
                this.setLayout(new BorderLayout());
114
                this.setSize(300, 270);
115
                this.add(getPnlWizard(), java.awt.BorderLayout.CENTER);
116
                this.add(getJPanel1(), java.awt.BorderLayout.SOUTH);
117
                connectionPanel.setDrivers(getDriverNames());
118
                enableButtons();
119

    
120

    
121
        XMLEntity xml = PluginServices.getPluginServices(this)
122
                                      .getPersistentXML();
123

    
124
        if (xml == null) {
125
            xml = new XMLEntity();
126
        }
127

    
128
        if (!xml.contains("esri-sde")) {
129
            String[] servers = new String[0];
130
            xml.putProperty("esri-sde", servers);
131
        }
132

    
133
        try {
134
            String[] servers = xml.getStringArrayProperty("esri-sde");
135

    
136
            for (int i = 0; i < servers.length; i++) {
137
                ConnectionSettings cs = new ConnectionSettings();
138
                cs.setFromString(servers[i]);
139
                settings.put(cs.getName(), cs);
140
            }
141
            getConnectionPanel().setSettings(settings);
142
        } catch (NotExistInXMLEntity e) {
143
        }
144

    
145
        }
146

    
147
        private String[] getDriverNames(){
148
                Class[] classes = new Class[] { VectorialSDEDriver.class };
149

    
150
                ArrayList ret = new ArrayList();
151
                String[] driverNames = LayerFactory.getDM().getDriverNames();
152

    
153
                for (int i = 0; i < driverNames.length; i++) {
154

    
155
                        for (int j = 0; j < classes.length; j++) {
156
                                if (LayerFactory.getDM().isA(driverNames[i], classes[j])) {
157
                                        ret.add(driverNames[i]);
158
                                }
159
                        }
160
                }
161

    
162
                return (String[]) ret.toArray(new String[0]);
163

    
164
        }
165

    
166
        private void enableButtons(){
167
                getBtnBack().setEnabled(step > 0);
168
                getBtnNext().setEnabled(step < nsteps - 1);
169
        }
170

    
171
        /**
172
         * This method initializes pnlWizard
173
         * @return  javax.swing.JPanel
174
         * @uml.property  name="pnlWizard"
175
         */
176
        private JPanel getPnlWizard() {
177
                if (pnlWizard == null) {
178
                        pnlWizard = new JPanel();
179
                        pnlWizard.setLayout(new CardLayout());
180
                        pnlWizard.add(getConnectionPanel(), CONNECTION);
181
                        pnlWizard.add(getDbLayerDefinition(), LAYER_DEFINITION);
182
                        pnlWizard.add(getFieldSelection(), FIELD_SELECTION);
183
                        pnlWizard.add(getGeomFieldSelection(), getGeomFieldSelection()
184
                                        .getName());
185
                        pnlWizard.add(getUniqueFieldSelection(), getUniqueFieldSelection()
186
                                        .getName());
187
                }
188
                return pnlWizard;
189
        }
190
        /**
191
         * This method initializes jPanel1
192
         * @return  javax.swing.JPanel
193
         * @uml.property  name="jPanel1"
194
         */
195
        private JPanel getJPanel1() {
196
                if (jPanel1 == null) {
197
                        FlowLayout flowLayout1 = new FlowLayout(FlowLayout.RIGHT);
198
                        jPanel1 = new JPanel();
199
                        jPanel1.setLayout(flowLayout1);
200
                        jPanel1.setPreferredSize(new java.awt.Dimension(10, 30));
201
                        flowLayout1.setHgap(5);
202
                        flowLayout1.setVgap(4);
203
                        jPanel1.add(getBtnBack(), null);
204
                        jPanel1.add(getBtnNext(), null);
205
                }
206
                return jPanel1;
207
        }
208
        /**
209
         * This method initializes btnBack
210
         * @return  javax.swing.JButton
211
         * @uml.property  name="btnBack"
212
         */
213
        private JButton getBtnBack() {
214
                if (btnBack == null) {
215
                        btnBack = new JButton();
216
                        btnBack.setText(PluginServices.getText(this, "back"));
217
                        //btnBack.setPreferredSize(new java.awt.Dimension(93,18));
218
                        btnBack.addActionListener(new java.awt.event.ActionListener() {
219
                                public void actionPerformed(java.awt.event.ActionEvent e) {
220
                                        step--;
221
                                        enableButtons();
222
                                        ((CardLayout) pnlWizard.getLayout()).previous(pnlWizard);
223
                                }
224
                        });
225
                }
226
                return btnBack;
227
        }
228
        /**
229
         * This method initializes btnNext
230
         * @return  javax.swing.JButton
231
         * @uml.property  name="btnNext"
232
         */
233
        private JButton getBtnNext() {
234
                if (btnNext == null) {
235
                        btnNext = new JButton();
236
                        btnNext.setText(PluginServices.getText(this, "next"));
237
                        //btnNext.setPreferredSize(new java.awt.Dimension(93,18));
238
                        btnNext.addActionListener(new java.awt.event.ActionListener() {
239
                                public void actionPerformed(java.awt.event.ActionEvent e) {
240
                                        boolean done = false;
241
                                        if (step == 0)
242
                                                done = connectionPanel.done();
243
                                        else if (step == 1)
244
                                                done = dbLayerDefinition.done();
245
                                        else if (step == 2)
246
                                                done = fieldSelection.done();
247
                                        else if (step == 3)
248
                                                done = geomFieldSelection.done();
249
                                        if (done) {
250
                                                try {
251
                                                        if (step == 0) {
252
                                                                //catalog = connectionPanel.getDBName();
253
                                                                theTables = getTableNames();
254
                                                                dbLayerDefinition.setTables(theTables);
255
                                                                saveConnection();
256
                                                        } else if (step == 1) {
257
                                                                fieldSelection.setFields(getTableFields());
258
                                                        } else if (step == 2) {
259
                                                                geomFieldSelection.setFields(getTableFields());
260
                                                        } else if (step == 3) {
261
                                                                uniqueFieldSelection.setFields(getTableFields());
262
                                                        }
263
                                                        step++;
264
                                                        enableButtons();
265
                                                        ((CardLayout) pnlWizard.getLayout()).next(pnlWizard);
266
                                                } catch (SQLException e1) {
267
                                                        NotificationManager.addError(PluginServices.getText(
268
                                                                        this, "error_conexion"), e1);
269
                                                } catch (DriverLoadException e1) {
270
                                                        NotificationManager.addError(
271
                                                                        "No se pudo cargar el driver", e1);
272
                                                }
273
                                        } else {
274
                                                JOptionPane.showMessageDialog(WizardSDE.this,
275
                                                                "No estan todos los datos rellenos", "Error",
276
                                                                JOptionPane.ERROR_MESSAGE);
277

    
278
                                        }
279
                                }
280
                        });
281
                }
282
                return btnNext;
283
        }
284

    
285
    private void saveConnection() {
286
        connectionPanel.saveConnectionSettings();
287
    }
288

    
289
    private String[] getTableNames() throws SQLException, DriverLoadException {
290
            try {
291
            conex = new SeConnection(connectionPanel.getHost(), Integer.parseInt(connectionPanel.getPort()), connectionPanel.getDBName(), connectionPanel.getUser(), connectionPanel.getPassword());
292
            Vector theLayers = conex.getLayers();
293
            TreeMap ret = new TreeMap();
294
            for (int i=0; i < theLayers.size(); i++){
295
                SeLayer layer = (SeLayer)theLayers.elementAt(i);
296
                ret.put(layer.getTableName(), layer.getTableName());
297
            }
298
            return (String[]) ret.keySet().toArray(new String[0]);
299
                 }catch (SeException e) {
300
                    e.printStackTrace();
301
                    return null;
302
                }
303
        }
304

    
305
        private String[] getTableFields() throws SQLException, DriverLoadException{
306
                try {
307
                        SeTable table = new SeTable(conex,dbLayerDefinition.getTable());
308
                        SeRegisteredColumn[] columns=table.getColumnList();
309
                        String[] fieldNames=new String[columns.length];
310
                        for (int i=0; i < columns.length; i++){
311
                                fieldNames[i]=columns[i].getName();
312
                        }
313
                        return fieldNames;
314
                } catch (SeException e) {
315
                        e.printStackTrace();
316
                }
317
                return null;
318
        }
319

    
320
        /**
321
         * This method initializes connectionPanel
322
         * @return  com.iver.cit.gvsig.gui.Panels.ConnectionPanel
323
         * @uml.property  name="connectionPanel"
324
         */
325
        private ConnectionPanel getConnectionPanel() {
326
                if (connectionPanel == null) {
327
                        connectionPanel = new ConnectionPanel();
328
                        connectionPanel.setName("connectionPanel");
329
                        connectionPanel.setDrivers(getDriverNames());
330
                }
331
                return connectionPanel;
332
        }
333
        /**
334
         * This method initializes dbLayerDefinition
335
         * @return  com.iver.cit.gvsig.gui.Panels.dbLayerDefinition
336
         * @uml.property  name="dbLayerDefinition"
337
         */
338
        private DBLayerDefinitionPanel getDbLayerDefinition() {
339
                if (dbLayerDefinition == null) {
340
                        dbLayerDefinition = new DBLayerDefinitionPanel();
341
                        dbLayerDefinition.setName("dbLayerDefinition");
342
                }
343
                return dbLayerDefinition;
344
        }
345
        /**
346
         * This method initializes fieldSelection
347
         * @return  com.iver.cit.gvsig.gui.Panels.FieldSelection
348
         * @uml.property  name="fieldSelection"
349
         */
350
        private FieldSelection getFieldSelection() {
351
                if (fieldSelection == null) {
352
                        fieldSelection = new FieldSelection();
353
                        fieldSelection.setName("fieldSelection");
354
                }
355
                return fieldSelection;
356
        }
357

    
358
        /**
359
         * @return
360
         * @throws DriverLoadException
361
         * @uml.property  name="driver"
362
         */
363
        public VectorialSDEDriver getDriver() throws DriverLoadException{
364
                if (driver == null) {
365
                        driver = (VectorialSDEDriver) LayerFactory.getDM().getDriver(
366
                                        connectionPanel.getDriver());
367
                }
368

    
369
                return driver;
370
        }
371

    
372
        public String getConnectionString() throws DriverLoadException{
373
//                String connectionString = getDriver().getConnectionStringBeginning() + "//" + connectionPanel.getHost();
374
//
375
//                if (connectionPanel.getPort().trim().length() > 0) {
376
//                        connectionString += (":" + connectionPanel.getPort());
377
//                } else {
378
//                    connectionString += (":" + driver.getDefaultPort());
379
//                }
380
//
381
//                connectionString += ("/" + connectionPanel.getDBName());
382
//
383
//                return connectionString;
384
                return "";
385
        }
386

    
387
        public String getLayerName(){
388
                return dbLayerDefinition.getLayerName();
389
        }
390

    
391
        /**
392
         * @return ONLY alphanumeric fields. You need to retrieve
393
     * the geometry fields with getGeometryField();
394
         * @throws DriverLoadException
395
         */
396
        public String[] getFields() throws DriverLoadException{
397
                String[] fields = fieldSelection.getFields();
398
                String geomField = geomFieldSelection.getField();
399
                String[] onlyAlphanumericFields = new String[fields.length-1];
400
        int newIndex = 0;
401
                for (int i = 0; i < fields.length; i++) {
402
                        if (!fields[i].equals(geomField))
403
            {
404
                                onlyAlphanumericFields[newIndex] = fields[i];
405
                newIndex++;
406
            }
407
                }
408
                return onlyAlphanumericFields;
409
        }
410

    
411
        /**
412
         * @return WhereClause (doesn't check if correct)
413
         */
414
        public String getWhereClause()
415
        {
416
                return dbLayerDefinition.getWhereClause();
417
        }
418
        /**
419
         * @return
420
         */
421
        public String getUser() {
422
                return connectionPanel.getUser();
423
        }
424
        /**
425
         * @return
426
         */
427
        public String getPassword() {
428
                return connectionPanel.getPassword();
429
        }
430
        /**
431
         * @return
432
         */
433
        public String getTable() {
434
                return dbLayerDefinition.getTable();
435
        }
436

    
437
        /**
438
         * @return Field ID (Unique Value Field we want to use has key)
439
         */
440
        public String getFID() {
441
                return uniqueFieldSelection.getField();
442
        }
443

    
444
    public String getGeomField()
445
    {
446
        return geomFieldSelection.getField();
447
    }
448

    
449
        /**
450
         * This method initializes geomFieldSelection
451
         * @return  com.iver.cit.gvsig.gui.jdbcwizard.GeomFieldSelection
452
         * @uml.property  name="geomFieldSelection"
453
         */
454
        private GeomFieldSelection getGeomFieldSelection() {
455
                if (geomFieldSelection == null) {
456
                        geomFieldSelection = new GeomFieldSelection();
457
                        geomFieldSelection.setName("geomFieldSelection");
458
                }
459
                return geomFieldSelection;
460
        }
461
        /**
462
         * This method initializes uniqueFieldSelection
463
         * @return  com.iver.cit.gvsig.gui.jdbcwizard.UniqueFieldSelection
464
         * @uml.property  name="uniqueFieldSelection"
465
         */
466
        private UniqueFieldSelection getUniqueFieldSelection() {
467
                if (uniqueFieldSelection == null) {
468
                        uniqueFieldSelection = new UniqueFieldSelection();
469
                        uniqueFieldSelection.setName("uniqueFieldSelection");
470
                        uniqueFieldSelection.setWizard(this);
471
                }
472
                return uniqueFieldSelection;
473
        }
474
        public void initWizard() {
475
        }
476

    
477
        /* (non-Javadoc)
478
         * @see com.iver.cit.gvsig.gui.WizardPanel#execute()
479
         */
480
        public void execute() {
481
        }
482

    
483
        /* (non-Javadoc)
484
         * @see com.iver.cit.gvsig.gui.WizardPanel#getLayer()
485
         */
486

    
487
        public FLayer getLayer() {
488
                IProjection proj = null;
489
                WizardSDE arcsde_wizard = this;
490
        String dbHost = connectionPanel.getHost();
491
        String port = connectionPanel.getPort();
492
        String user = arcsde_wizard.getUser();
493
        String pwd = arcsde_wizard.getPassword();
494
        String layerName = arcsde_wizard.getLayerName();
495
        String tableName = arcsde_wizard.getLayerName();
496
        String schema = connectionPanel.getDBName();
497
        String connectionName=connectionPanel.getSettingsName();
498
        String whereClause = "";
499

    
500
        String strEPSG = getMapCtrl().getViewPort()
501
                    .getProjection().getAbrev()
502
                    .substring(5);
503

    
504
        SDELayerDefinition lyrDef = new SDELayerDefinition();
505
        lyrDef.setHost(dbHost);
506
        lyrDef.setPort(port);
507
        lyrDef.setSchema(schema);
508
        lyrDef.setUser(user);
509
        lyrDef.setPassword(pwd);
510
        lyrDef.setName(layerName);
511
        lyrDef.setTableName(tableName);
512
        lyrDef.setWhereClause(whereClause);
513
        lyrDef.setFieldGeometry(arcsde_wizard.getGeomField());
514
        lyrDef.setFieldID(arcsde_wizard.getFID());
515
        lyrDef.setFieldNames(arcsde_wizard.getFields());
516
        lyrDef.setSRID_EPSG(strEPSG);
517
        lyrDef.setConnectionName(connectionName);
518
        ArcSdeDriver driver = new ArcSdeDriver();
519
        if (dbLayerDefinition.getWorkingArea() != null){
520
            driver.setWorkingArea(dbLayerDefinition.getWorkingArea());
521
        }
522

    
523
        if (driver instanceof ICanReproject){
524
            ((ICanReproject)driver).setDestProjection(strEPSG);
525
        }
526
                driver.setData(null, lyrDef);
527
//            driver.setData(dbHost, Integer.parseInt(port), schema, user, pwd, tableName, fields, whereClause);
528
                if (driver instanceof ICanReproject)
529
            {
530
                proj = CRSFactory.getCRS("EPSG:" + ((ICanReproject)driver).getSourceProjection());
531
            }
532
            return LayerFactory.createDBLayer(driver, tableName, proj);
533
        /* } catch (DriverLoadException e) {
534
            // TODO Auto-generated catch block
535
            e.printStackTrace();
536
            return null;
537
        } */
538

    
539
        }
540
}