Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_1_RELEASE / extensions / extJDBC / src / com / iver / cit / gvsig / jdbc_spatial / gui / jdbcwizard / WizardJDBC.java @ 9531

History | View | Annotate | Download (14.7 KB)

1 2269 fjp
package com.iver.cit.gvsig.jdbc_spatial.gui.jdbcwizard;
2
3
import java.awt.BorderLayout;
4
import java.awt.CardLayout;
5
import java.awt.FlowLayout;
6
import java.sql.Connection;
7
import java.sql.DatabaseMetaData;
8
import java.sql.DriverManager;
9
import java.sql.ResultSet;
10
import java.sql.ResultSetMetaData;
11
import java.sql.SQLException;
12
import java.sql.Statement;
13
import java.util.ArrayList;
14
import java.util.HashMap;
15 3319 fjp
import java.util.TreeMap;
16 2269 fjp
17
import javax.swing.JOptionPane;
18
import javax.swing.JPanel;
19
20 3095 fjp
import org.cresques.cts.IProjection;
21 6703 jmvivo
import org.gvsig.gui.beans.swing.JButton;
22 3095 fjp
23 2269 fjp
import com.hardcode.driverManager.DriverLoadException;
24
import com.iver.andami.PluginServices;
25
import com.iver.andami.messages.NotificationManager;
26 3095 fjp
import com.iver.cit.gvsig.fmap.core.ICanReproject;
27 7703 luisw2
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
28 3251 fjp
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
29 2269 fjp
import com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver;
30
import com.iver.cit.gvsig.fmap.layers.FLayer;
31
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
32
import com.iver.cit.gvsig.gui.WizardPanel;
33
import com.iver.utiles.NotExistInXMLEntity;
34
import com.iver.utiles.XMLEntity;
35
/**
36
 * @author Fernando Gonz?lez Cort?s
37
 */
38
public class WizardJDBC extends WizardPanel{
39
40
        private JPanel pnlWizard = null;
41
        private JPanel jPanel1 = null;
42
        private JButton btnBack = null;
43
        private JButton btnNext = null;
44
        private ConnectionPanel connectionPanel = null;
45 3207 fjp
        private DBLayerDefinitionPanel dbLayerDefinition = null;
46 2269 fjp
        private FieldSelection fieldSelection = null;
47 8765 jjdelcerro
48 2269 fjp
        private int step = 0;
49
        private final int nsteps = 5;
50
        private static final String CONNECTION = "conn";
51
        private static final String LAYER_DEFINITION = "layerdef";
52
        private static final String FIELD_SELECTION= "fieldsel";
53
        private VectorialJDBCDriver driver;
54 8765 jjdelcerro
55 2269 fjp
        private GeomFieldSelection geomFieldSelection = null;
56
        private UniqueFieldSelection uniqueFieldSelection = null;
57 8765 jjdelcerro
58 3076 fjp
    private DatabaseMetaData dbmd = null;
59
    private String catalog = null;
60
    private String selectTable = null;
61
    private String[] theTables = null;
62 8765 jjdelcerro
    private Connection conex = null;
63
64 2269 fjp
        private HashMap settings = new HashMap();
65
        /**
66
         * This is the default constructor
67
         */
68
        public WizardJDBC() {
69
                super();
70
                initialize();
71
        }
72
        /**
73
         * This method initializes this
74 8765 jjdelcerro
         *
75 2269 fjp
         * @return void
76
         */
77
        private  void initialize() {
78
                setTabName("JDBC");
79
                this.setLayout(new BorderLayout());
80
                this.setSize(300, 270);
81
                this.add(getPnlWizard(), java.awt.BorderLayout.CENTER);
82
                this.add(getJPanel1(), java.awt.BorderLayout.SOUTH);
83
                connectionPanel.setDrivers(getDriverNames());
84
                enableButtons();
85
86 8765 jjdelcerro
87 2269 fjp
        XMLEntity xml = PluginServices.getPluginServices(this)
88
                                      .getPersistentXML();
89
90
        if (xml == null) {
91
            xml = new XMLEntity();
92
        }
93
94
        if (!xml.contains("jdbc-connections")) {
95
            String[] servers = new String[0];
96
            xml.putProperty("jdbc-connections", servers);
97
        }
98
99
        try {
100
            String[] servers = xml.getStringArrayProperty("jdbc-connections");
101
102
            for (int i = 0; i < servers.length; i++) {
103
                ConnectionSettings cs = new ConnectionSettings();
104
                cs.setFromString(servers[i]);
105
                settings.put(cs.getName(), cs);
106
            }
107
            getConnectionPanel().setSettings(settings);
108
        } catch (NotExistInXMLEntity e) {
109
        }
110 8765 jjdelcerro
111 2269 fjp
        }
112 8765 jjdelcerro
113 2269 fjp
        private String[] getDriverNames(){
114
                Class[] classes = new Class[] { VectorialJDBCDriver.class };
115
116
                ArrayList ret = new ArrayList();
117
                String[] driverNames = LayerFactory.getDM().getDriverNames();
118
119
                for (int i = 0; i < driverNames.length; i++) {
120
                        boolean is = false;
121
122
                        for (int j = 0; j < classes.length; j++) {
123
                                if (LayerFactory.getDM().isA(driverNames[i], classes[j])) {
124
                                        ret.add(driverNames[i]);
125
                                }
126
                        }
127
                }
128
129
                return (String[]) ret.toArray(new String[0]);
130 8765 jjdelcerro
131 2269 fjp
        }
132 8765 jjdelcerro
133 2269 fjp
        private void enableButtons(){
134
                getBtnBack().setEnabled(step > 0);
135
                getBtnNext().setEnabled(step < nsteps - 1);
136
        }
137 8765 jjdelcerro
138 2269 fjp
        /**
139 8765 jjdelcerro
         * This method initializes pnlWizard
140
         *
141
         * @return javax.swing.JPanel
142
         */
143 2269 fjp
        private JPanel getPnlWizard() {
144
                if (pnlWizard == null) {
145
                        pnlWizard = new JPanel();
146
                        pnlWizard.setLayout(new CardLayout());
147
                        pnlWizard.add(getConnectionPanel(), CONNECTION);
148
                        pnlWizard.add(getDbLayerDefinition(), LAYER_DEFINITION);
149
                        pnlWizard.add(getFieldSelection(), FIELD_SELECTION);
150
                        pnlWizard.add(getGeomFieldSelection(), getGeomFieldSelection().getName());
151
                        pnlWizard.add(getUniqueFieldSelection(), getUniqueFieldSelection().getName());
152
                }
153
                return pnlWizard;
154
        }
155
        /**
156 8765 jjdelcerro
         * This method initializes jPanel1
157
         *
158
         * @return javax.swing.JPanel
159
         */
160 2269 fjp
        private JPanel getJPanel1() {
161
                if (jPanel1 == null) {
162 6703 jmvivo
                        FlowLayout flowLayout1 = new FlowLayout(FlowLayout.RIGHT);
163 2269 fjp
                        jPanel1 = new JPanel();
164
                        jPanel1.setLayout(flowLayout1);
165 6703 jmvivo
                        jPanel1.setPreferredSize(new java.awt.Dimension(10,30));
166
                        flowLayout1.setHgap(5);
167 2269 fjp
                        flowLayout1.setVgap(4);
168
                        jPanel1.add(getBtnBack(), null);
169
                        jPanel1.add(getBtnNext(), null);
170
                }
171
                return jPanel1;
172
        }
173
        /**
174 8765 jjdelcerro
         * This method initializes btnBack
175
         *
176
         * @return javax.swing.JButton
177
         */
178 2269 fjp
        private JButton getBtnBack() {
179
                if (btnBack == null) {
180
                        btnBack = new JButton();
181
                        btnBack.setText(PluginServices.getText(this, "back"));
182 6703 jmvivo
                        //btnBack.setPreferredSize(new java.awt.Dimension(93,18));
183 8765 jjdelcerro
                        btnBack.addActionListener(new java.awt.event.ActionListener() {
184
                                public void actionPerformed(java.awt.event.ActionEvent e) {
185 2269 fjp
                                        step--;
186
                                        enableButtons();
187
                                        ((CardLayout)pnlWizard.getLayout()).previous(pnlWizard);
188
                                }
189
                        });
190
                }
191
                return btnBack;
192
        }
193
        /**
194 8765 jjdelcerro
         * This method initializes btnNext
195
         *
196
         * @return javax.swing.JButton
197
         */
198 2269 fjp
        private JButton getBtnNext() {
199
                if (btnNext == null) {
200
                        btnNext = new JButton();
201
                        btnNext.setText(PluginServices.getText(this, "next"));
202 6703 jmvivo
                        //btnNext.setPreferredSize(new java.awt.Dimension(93,18));
203 8765 jjdelcerro
                        btnNext.addActionListener(new java.awt.event.ActionListener() {
204 2269 fjp
                                public void actionPerformed(java.awt.event.ActionEvent e) {
205
                                        boolean done = false;
206
                                        if (step == 0) done = connectionPanel.done();
207
                                        else if (step == 1) done = dbLayerDefinition.done();
208
                                        else if (step == 2) done = fieldSelection.done();
209
                                        else if (step == 3) done = geomFieldSelection.done();
210
                                        if (done){
211
                                                try {
212
                                                        if (step == 0){
213 3076 fjp
                                catalog = connectionPanel.getDBName();
214
                                theTables = getTableNames();
215
                                                                dbLayerDefinition.setTables(theTables);
216 2269 fjp
                                                            saveConnection();
217
                                                        }
218
                                                        else if (step == 1) {
219
                                                                fieldSelection.setFields(getTableFields());
220
                                                        }else if (step == 2) {
221 3095 fjp
                                                                geomFieldSelection.setFields(getTableFields());
222 2269 fjp
                                                        }else if (step == 3) {
223
                                                                uniqueFieldSelection.setFields(getTableFields());
224
                                                        }
225
                                                        step++;
226
                                                        enableButtons();
227
                                                        ((CardLayout)pnlWizard.getLayout()).next(pnlWizard);
228
                                                } catch (SQLException e1) {
229 3076 fjp
                                                        NotificationManager.addError(PluginServices.getText(this,"error_conexion"), e1);
230 2269 fjp
                                                } catch (DriverLoadException e1) {
231
                                                        NotificationManager.addError("No se pudo cargar el driver", e1);
232
                                                }
233
                                        }else{
234
                                                JOptionPane.showMessageDialog(WizardJDBC.this, "No estan todos los datos rellenos", "Error", JOptionPane.ERROR_MESSAGE);
235 8765 jjdelcerro
236 2269 fjp
                                        }
237
                                }
238
                        });
239
                }
240
                return btnNext;
241
        }
242
243
    private void saveConnection() {
244 3207 fjp
        connectionPanel.saveConnectionSettings();
245 2269 fjp
246 3207 fjp
        // settings.put(connectionPanel.getSettingsName(), cs);
247 8765 jjdelcerro
248
249 2269 fjp
    }
250
251
    private String[] getTableNames() throws SQLException, DriverLoadException {
252 3076 fjp
                conex = DriverManager.getConnection(getConnectionString(), connectionPanel.getUser(),
253 2269 fjp
                                connectionPanel.getPassword());
254
255 3076 fjp
                dbmd = conex.getMetaData();
256 3319 fjp
        String[] types = {"TABLE", "VIEW"};
257
                ResultSet rs = dbmd.getTables(catalog, null, null, types);
258
                TreeMap ret = new TreeMap();
259 2269 fjp
                while (rs.next()){
260 3319 fjp
                        ret.put(rs.getString("TABLE_NAME"), rs.getString("TABLE_NAME"));
261 2269 fjp
                }
262 8765 jjdelcerro
263 3319 fjp
                return (String[]) ret.keySet().toArray(new String[0]);
264 2269 fjp
        }
265 8765 jjdelcerro
266 2269 fjp
        private String[] getTableFields() throws SQLException, DriverLoadException{
267 3076 fjp
                Statement st = conex.createStatement();
268
        // ResultSet rs = dbmd.getTables(catalog, null, dbLayerDefinition.getTable(), null);
269
                ResultSet rs = st.executeQuery("select * from " + dbLayerDefinition.getTable() + " LIMIT 1");
270 2269 fjp
                ResultSetMetaData rsmd = rs.getMetaData();
271
272
                String[] ret = new String[rsmd.getColumnCount()];
273
274
                for (int i = 0; i < ret.length; i++) {
275
                        ret[i] = rsmd.getColumnName(i+1);
276
                }
277
278
                return ret;
279
        }
280 8765 jjdelcerro
281 2269 fjp
        /**
282 8765 jjdelcerro
         * This method initializes connectionPanel
283
         *
284
         * @return com.iver.cit.gvsig.gui.Panels.ConnectionPanel
285
         */
286 2269 fjp
        private ConnectionPanel getConnectionPanel() {
287
                if (connectionPanel == null) {
288
                        connectionPanel = new ConnectionPanel();
289
                        connectionPanel.setName("connectionPanel");
290
                        connectionPanel.setDrivers(getDriverNames());
291
                }
292
                return connectionPanel;
293
        }
294
        /**
295 8765 jjdelcerro
         * This method initializes dbLayerDefinition
296
         *
297
         * @return com.iver.cit.gvsig.gui.Panels.dbLayerDefinition
298
         */
299 3207 fjp
        private DBLayerDefinitionPanel getDbLayerDefinition() {
300 2269 fjp
                if (dbLayerDefinition == null) {
301 3207 fjp
                        dbLayerDefinition = new DBLayerDefinitionPanel();
302 2269 fjp
                        dbLayerDefinition.setName("dbLayerDefinition");
303
                }
304
                return dbLayerDefinition;
305
        }
306
        /**
307 8765 jjdelcerro
         * This method initializes fieldSelection
308
         *
309
         * @return com.iver.cit.gvsig.gui.Panels.FieldSelection
310
         */
311 2269 fjp
        private FieldSelection getFieldSelection() {
312
                if (fieldSelection == null) {
313
                        fieldSelection = new FieldSelection();
314
                        fieldSelection.setName("fieldSelection");
315
                }
316
                return fieldSelection;
317
        }
318 8765 jjdelcerro
319 2269 fjp
        public VectorialJDBCDriver getDriver() throws DriverLoadException{
320
                if (driver == null){
321
                        driver = (VectorialJDBCDriver) LayerFactory.getDM().getDriver(connectionPanel.getDriver());
322
                }
323 8765 jjdelcerro
324 2269 fjp
                return driver;
325
        }
326 8765 jjdelcerro
327 2269 fjp
        public String getConnectionString() throws DriverLoadException{
328
                String connectionString = getDriver().getConnectionStringBeginning() + "//" + connectionPanel.getHost();
329
330
                if (connectionPanel.getPort().trim().length() > 0) {
331
                        connectionString += (":" + connectionPanel.getPort());
332
                } else {
333
                    connectionString += (":" + driver.getDefaultPort());
334
                }
335
336
                connectionString += ("/" + connectionPanel.getDBName());
337 8765 jjdelcerro
338 2269 fjp
                return connectionString;
339
        }
340 8765 jjdelcerro
341 2269 fjp
        public String getLayerName(){
342
                return dbLayerDefinition.getLayerName();
343
        }
344 8765 jjdelcerro
345 3251 fjp
        /**
346
         * @return ONLY alphanumeric fields. You need to retrieve
347
     * the geometry fields with getGeometryField();
348
         * @throws DriverLoadException
349
         */
350 2269 fjp
        public String[] getFields() throws DriverLoadException{
351
                String[] fields = fieldSelection.getFields();
352
                String geomField = geomFieldSelection.getField();
353 3251 fjp
                String[] onlyAlphanumericFields = new String[fields.length-1];
354
        int newIndex = 0;
355 2269 fjp
                for (int i = 0; i < fields.length; i++) {
356 3251 fjp
                        if (!fields[i].equals(geomField))
357
            {
358
                                onlyAlphanumericFields[newIndex] = fields[i];
359
                newIndex++;
360
            }
361 8765 jjdelcerro
                }
362 3251 fjp
                return onlyAlphanumericFields;
363 2269 fjp
        }
364 8765 jjdelcerro
365 2269 fjp
        /**
366 6788 fjp
         * @return WhereClause (doesn't check if correct)
367
         */
368
        public String getWhereClause()
369
        {
370
                return dbLayerDefinition.getWhereClause();
371
        }
372
        /**
373 2269 fjp
         * @return
374
         */
375
        public String getUser() {
376
                return connectionPanel.getUser();
377
        }
378
        /**
379
         * @return
380
         */
381
        public String getPassword() {
382
                return connectionPanel.getPassword();
383
        }
384
        /**
385
         * @return
386
         */
387
        public String getTable() {
388
                return dbLayerDefinition.getTable();
389
        }
390 8765 jjdelcerro
391 2269 fjp
        /**
392 3251 fjp
         * @return Field ID (Unique Value Field we want to use has key)
393 2269 fjp
         */
394 3251 fjp
        public String getFID() {
395
                return uniqueFieldSelection.getField();
396 2269 fjp
        }
397 8765 jjdelcerro
398 3251 fjp
    public String getGeomField()
399
    {
400
        return geomFieldSelection.getField();
401
    }
402 8765 jjdelcerro
403 2269 fjp
        /**
404 8765 jjdelcerro
         * This method initializes geomFieldSelection
405
         *
406
         * @return com.iver.cit.gvsig.gui.jdbcwizard.GeomFieldSelection
407
         */
408 2269 fjp
        private GeomFieldSelection getGeomFieldSelection() {
409
                if (geomFieldSelection == null) {
410
                        geomFieldSelection = new GeomFieldSelection();
411
                        geomFieldSelection.setName("geomFieldSelection");
412
                }
413
                return geomFieldSelection;
414
        }
415
        /**
416 8765 jjdelcerro
         * This method initializes uniqueFieldSelection
417
         *
418
         * @return com.iver.cit.gvsig.gui.jdbcwizard.UniqueFieldSelection
419
         */
420 2269 fjp
        private UniqueFieldSelection getUniqueFieldSelection() {
421
                if (uniqueFieldSelection == null) {
422
                        uniqueFieldSelection = new UniqueFieldSelection();
423
                        uniqueFieldSelection.setName("uniqueFieldSelection");
424
                        uniqueFieldSelection.setWizard(this);
425
                }
426
                return uniqueFieldSelection;
427
        }
428
        public void initWizard() {
429
        }
430
431
        /* (non-Javadoc)
432
         * @see com.iver.cit.gvsig.gui.WizardPanel#execute()
433
         */
434
        public void execute() {
435
        }
436
437
        /* (non-Javadoc)
438
         * @see com.iver.cit.gvsig.gui.WizardPanel#getLayer()
439
         */
440
        public FLayer getLayer() {
441
                WizardJDBC wiz = this;
442 2352 fjp
            //true -> Modo desconectado. Por ahora, no se usa
443 3095 fjp
        IProjection proj = null;
444 3251 fjp
        String dbURL;
445
        try {
446
            dbURL = wiz.getConnectionString();
447
            String user = wiz.getUser();
448
            String pwd = wiz.getPassword();
449
            String layerName = wiz.getLayerName();
450
            String[] fields = wiz.getFields();
451
            String tableName = wiz.getTable();
452 6788 fjp
            String whereClause = wiz.getWhereClause();
453 3251 fjp
            String fidField = wiz.getFID();
454
            String geomField = wiz.getGeomField();
455
            Connection conn = DriverManager.getConnection(dbURL, user, pwd);
456
            conn.setAutoCommit(false);
457 8765 jjdelcerro
458 3251 fjp
            VectorialJDBCDriver driver = wiz.getDriver();
459
            if (dbLayerDefinition.getWorkingArea() != null){
460
                driver.setWorkingArea(dbLayerDefinition.getWorkingArea());
461
            }
462
            String strEPSG = getMapCtrl().getViewPort()
463
                        .getProjection().getAbrev()
464
                        .substring(5);
465 8765 jjdelcerro
            DBLayerDefinition lyrDef = new DBLayerDefinition();
466 4420 fjp
            lyrDef.setName(layerName);
467 3251 fjp
            lyrDef.setTableName(tableName);
468
            lyrDef.setWhereClause(whereClause);
469
            lyrDef.setFieldNames(fields);
470
            lyrDef.setFieldGeometry(geomField);
471
            lyrDef.setFieldID(fidField);
472
            if (dbLayerDefinition.getWorkingArea() != null)
473
                lyrDef.setWorkingArea(dbLayerDefinition.getWorkingArea());
474 8765 jjdelcerro
475 3251 fjp
            lyrDef.setSRID_EPSG(strEPSG);
476
            if (driver instanceof ICanReproject)
477 8765 jjdelcerro
            {
478 3251 fjp
                ((ICanReproject)driver).setDestProjection(strEPSG);
479
            }
480
            driver.setData(conn, lyrDef);
481
            if (driver instanceof ICanReproject)
482 8765 jjdelcerro
            {
483
                proj = CRSFactory.getCRS("EPSG:" + ((ICanReproject)driver).getSourceProjection());
484 3251 fjp
            }
485 8765 jjdelcerro
486 3251 fjp
                if (false){
487 3095 fjp
                FLayer lyr = LayerFactory.createDisconnectedDBLayer(driver, layerName, proj, null);
488 3251 fjp
                            if (lyr != null) {
489
                                    lyr.setVisible(true);
490
                            }
491 8765 jjdelcerro
492 3251 fjp
                            return lyr;
493 8765 jjdelcerro
                }else{ // MODO CONECTADO
494 3251 fjp
                return LayerFactory.createDBLayer(driver, layerName, proj);
495
                }
496
        } catch (Exception e) {
497
            e.printStackTrace();
498
            NotificationManager.addError("Error al cargar la capa.", e);
499
        }
500 8765 jjdelcerro
501 2269 fjp
                return null;
502
        }
503
}