Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / applications / appgvSIG / src / com / iver / cit / gvsig / TableOperations.java @ 7539

History | View | Annotate | Download (16.5 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig;
42

    
43
import java.awt.Dimension;
44
import java.io.IOException;
45

    
46
import com.hardcode.driverManager.DriverLoadException;
47
import com.hardcode.gdbms.engine.data.DataSource;
48
import com.hardcode.gdbms.engine.data.DataSourceFactory;
49
import com.hardcode.gdbms.engine.instruction.EvaluationException;
50
import com.hardcode.gdbms.engine.instruction.SemanticException;
51
import com.hardcode.gdbms.parser.ParseException;
52
import com.iver.andami.PluginServices;
53
import com.iver.andami.messages.NotificationManager;
54
import com.iver.andami.plugins.Extension;
55
import com.iver.andami.ui.mdiManager.IWindow;
56
import com.iver.cit.gvsig.fmap.edition.EditableAdapter;
57
import com.iver.cit.gvsig.fmap.layers.FBitSet;
58
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
59
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
60
import com.iver.cit.gvsig.gui.Table;
61
import com.iver.cit.gvsig.gui.filter.ExpressionListener;
62
import com.iver.cit.gvsig.gui.tables.AndamiWizard;
63
import com.iver.cit.gvsig.gui.tables.FieldSelectionModel;
64
import com.iver.cit.gvsig.gui.tables.ObjectSelectionStep;
65
import com.iver.cit.gvsig.gui.tables.TableSelectionModel;
66
import com.iver.cit.gvsig.project.ProjectTable;
67
import com.iver.utiles.swing.objectSelection.SelectionException;
68
import com.iver.utiles.swing.wizard.WizardControl;
69
import com.iver.utiles.swing.wizard.WizardEvent;
70
import com.iver.utiles.swing.wizard.WizardListener;
71

    
72

    
73
/**
74
 * Extensi?n que controla las operaciones realizadas sobre las tablas.
75
 *
76
 * @author Fernando Gonz?lez Cort?s
77
 */
78
public class TableOperations extends Extension implements ExpressionListener {
79
        private SelectableDataSource dataSource = null;
80
        //private Table vista;
81

    
82
        /**
83
         * @see com.iver.mdiApp.plugins.IExtension#updateUI(java.lang.String)
84
         */
85
        public void execute(String actionCommand) {
86
                if ("JOIN".equals(actionCommand)) {
87
                        ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
88
                        ProjectTable[] pts = (ProjectTable[]) pe.getProject().getTables()
89
                                                                                                        .toArray(new ProjectTable[0]);
90

    
91
                        try {
92
                                final ObjectSelectionStep sourceTable = new ObjectSelectionStep();
93
                                sourceTable.setModel(new TableSelectionModel(pts,
94
                                        PluginServices.getText(this, "seleccione_tabla_origen")));
95

    
96
                                final ObjectSelectionStep targetTable = new ObjectSelectionStep();
97
                                targetTable.setModel(new TableSelectionModel(pts,
98
                                        PluginServices.getText(this, "seleccione_tabla_a_enlazar")));
99

    
100
                                final ObjectSelectionStep firstTableField = new ObjectSelectionStep();
101
                                final ObjectSelectionStep secondTableField = new ObjectSelectionStep();
102
                                final AndamiWizard wiz = new AndamiWizard(PluginServices.getText(this, "back"), PluginServices.getText(this, "next"), PluginServices.getText(this, "finish"), PluginServices.getText(this, "cancel"));
103
                                wiz.setSize(new Dimension(450,200));
104
                                wiz.addStep(sourceTable);
105
                                wiz.addStep(firstTableField);
106
                                wiz.addStep(targetTable);
107
                                wiz.addStep(secondTableField);
108

    
109
                                wiz.addWizardListener(new WizardListener() {
110
                                                public void cancel(WizardEvent w) {
111
                                                        PluginServices.getMDIManager().closeWindow(wiz);
112
                                                }
113

    
114
                                                public void finished(WizardEvent w) {
115
                                                        PluginServices.getMDIManager().closeWindow(wiz);
116

    
117
                                                        ProjectTable sourceProjectTable = (ProjectTable) sourceTable.getSelected();
118
                                                        SelectableDataSource sds=null;
119
                                                        try {
120
                                                                sds =(SelectableDataSource) sourceProjectTable.getModelo().getRecordset();
121
                                                        } catch (DriverLoadException e1) {
122
                                                                // TODO Auto-generated catch block
123
                                                                e1.printStackTrace();
124
                                                        }
125
                                                        String tableName1 = sds.getName();
126

    
127
                                                        ProjectTable targetProjectTable = (ProjectTable) targetTable.getSelected();
128
                                                        try {
129
                                                                sds = (SelectableDataSource)targetProjectTable.getModelo().getRecordset();
130
                                                        } catch (DriverLoadException e1) {
131
                                                                // TODO Auto-generated catch block
132
                                                                e1.printStackTrace();
133
                                                        }
134

    
135
                                                        String tableName2 = sds.getName();
136

    
137
                                                        String field1 = (String) firstTableField.getSelected();
138
                                                        String field2 = (String) secondTableField.getSelected();
139

    
140
                                                        String sql =
141
                                                                "custom com_iver_cit_gvsig_arcjoin tables '" +
142
                                                                tableName1 + "', '" + tableName2 + "' values(" +
143
                                                                field1 + ", " + field2 + ");";
144
                                                        System.out.println(sql);
145

    
146
                                                        try {
147
                                                                SelectableDataSource result = new SelectableDataSource(LayerFactory.getDataSourceFactory()
148
                                                                                                                                                                                                   .executeSQL(sql,
149
                                                                                        DataSourceFactory.AUTOMATIC_OPENING));
150
                                                                ProjectTable projectTable = (ProjectTable) sourceTable.getSelected();
151
                                                                EditableAdapter auxea=new EditableAdapter();
152
                                                                auxea.setOriginalDataSource(result);
153
                                                                projectTable.replaceDataSource(auxea);
154
                                                        } catch (ParseException e) {
155
                                                                throw new RuntimeException(e);
156
                                                        } catch (DriverLoadException e) {
157
                                                                NotificationManager.addError("Error con la carga de drivers",
158
                                                                        e);
159
                                                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
160
                                                                NotificationManager.addError("Error leyendo del driver",
161
                                                                        e);
162
                                                        } catch (SemanticException e) {
163
                                                                throw new RuntimeException(e);
164
                                                        } catch (IOException e) {
165
                                                                NotificationManager.addError("Error de entrada/salida",
166
                                                                        e);
167
                                                        } catch (EvaluationException e) {
168
                                                                NotificationManager.addError("Error de evaluaci?n de la expresi?n",
169
                                                                                e);
170
                            }
171
                                                }
172

    
173
                                                public void next(WizardEvent w) {
174
                                                        WizardControl wiz = w.wizard;
175
                                                        wiz.enableBack(true);
176
                                                        wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
177

    
178
                                                        if (w.currentStep == 1) {
179
                                                                ProjectTable pt = (ProjectTable) sourceTable.getSelected();
180

    
181
                                                                try {
182
                                                                        firstTableField.setModel(new FieldSelectionModel(
183
                                                                                        pt.getModelo().getRecordset(),
184
                                                                                        PluginServices.getText(this, "seleccione_campo_enlace"),
185
                                                                                        -1));
186
                                                                } catch (SelectionException e) {
187
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
188
                                                                                e);
189
                                                                } catch (DriverLoadException e) {
190
                                                                        // TODO Auto-generated catch block
191
                                                                        e.printStackTrace();
192
                                                                }
193
                                                        } else if (w.currentStep == 3) {
194
                                                                try {
195
                                                                        //tabla
196
                                                                        ProjectTable pt = (ProjectTable) sourceTable.getSelected();
197

    
198
                                                                        //?ndice del campo
199
                                                                        SelectableDataSource sds = (SelectableDataSource)pt.getModelo().getRecordset();
200
                                                                        String fieldName = (String) firstTableField.getSelected();
201
                                                                        int fieldIndex = sds.getFieldIndexByName(fieldName);
202
                                                                        int type = sds.getFieldType(fieldIndex);
203

    
204
                                                                        secondTableField.setModel(new FieldSelectionModel(
205
                                                                                        ((ProjectTable) targetTable.getSelected()).getModelo().getRecordset(),
206
                                                                                        PluginServices.getText(this, "seleccione_campo_enlace"),
207
                                                                                        type));
208
                                                                } catch (SelectionException e) {
209
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
210
                                                                                e);
211
                                                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
212
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
213
                                                                                e);
214
                                                                } catch (DriverLoadException e) {
215
                                                                        // TODO Auto-generated catch block
216
                                                                        e.printStackTrace();
217
                                                                }
218
                                                        }
219
                                                }
220

    
221
                                                public void back(WizardEvent w) {
222
                                                        WizardControl wiz = w.wizard;
223
                                                        wiz.enableBack(true);
224
                                                        wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
225
                                                }
226
                                        });
227
                                PluginServices.getMDIManager().addWindow(wiz);
228
                        } catch (SelectionException e) {
229
                                NotificationManager.addError("Error abriendo el asistente", e);
230
                        }
231
                }else if ("LINK".equals(actionCommand)) {
232
                        ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
233
                        ProjectTable[] pts = (ProjectTable[]) pe.getProject().getTables()
234
                                                                                                        .toArray(new ProjectTable[0]);
235

    
236
                        try {
237
                                final ObjectSelectionStep sourceTable = new ObjectSelectionStep();
238
                                sourceTable.setModel(new TableSelectionModel(pts,
239
                                                PluginServices.getText(this, "seleccione_tabla_origen")));
240

    
241
                                final ObjectSelectionStep targetTable = new ObjectSelectionStep();
242
                                targetTable.setModel(new TableSelectionModel(pts,
243
                                        PluginServices.getText(this, "seleccione_tabla_a_enlazar")));
244

    
245
                                final ObjectSelectionStep firstTableField = new ObjectSelectionStep();
246
                                final ObjectSelectionStep secondTableField = new ObjectSelectionStep();
247
                                final AndamiWizard wiz = new AndamiWizard(PluginServices.getText(this, "back"), PluginServices.getText(this, "next"), PluginServices.getText(this, "finish"), PluginServices.getText(this, "cancel"));
248
                                wiz.setSize(new Dimension(450,200));
249
                                wiz.addStep(sourceTable);
250
                                wiz.addStep(firstTableField);
251
                                wiz.addStep(targetTable);
252
                                wiz.addStep(secondTableField);
253

    
254
                                wiz.addWizardListener(new WizardListener() {
255
                                                public void cancel(WizardEvent w) {
256
                                                        PluginServices.getMDIManager().closeWindow(wiz);
257
                                                }
258

    
259
                                                public void finished(WizardEvent w) {
260
                                                        PluginServices.getMDIManager().closeWindow(wiz);
261

    
262
                                                        ProjectTable sourceProjectTable = (ProjectTable) sourceTable.getSelected();
263
                                                        SelectableDataSource sds1=null;;
264
                                                        try {
265
                                                                sds1 = (SelectableDataSource)sourceProjectTable.getModelo().getRecordset();
266
                                                        } catch (DriverLoadException e) {
267
                                                                // TODO Auto-generated catch block
268
                                                                e.printStackTrace();
269
                                                        }
270
                                                        String tableName1 = sds1.getName();
271

    
272
                                                        ProjectTable targetProjectTable = (ProjectTable) targetTable.getSelected();
273
                                                        SelectableDataSource sds2=null;
274
                                                        try {
275
                                                                sds2 = (SelectableDataSource)targetProjectTable.getModelo().getRecordset();
276
                                                        } catch (DriverLoadException e) {
277
                                                                // TODO Auto-generated catch block
278
                                                                e.printStackTrace();
279
                                                        }
280

    
281
                                                        String tableName2 = sds2.getName();
282

    
283
                                                        String field1 = (String) firstTableField.getSelected();
284
                                                        String field2 = (String) secondTableField.getSelected();
285
                                                        sourceProjectTable.setLinkTable(sds2.getName(),field1,field2);
286
                                                        ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject().setLinkTable();
287
                                                }
288

    
289
                                                public void next(WizardEvent w) {
290
                                                        WizardControl wiz = w.wizard;
291
                                                        wiz.enableBack(true);
292
                                                        wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
293

    
294
                                                        if (w.currentStep == 1) {
295
                                                                ProjectTable pt = (ProjectTable) sourceTable.getSelected();
296

    
297
                                                                try {
298
                                                                        firstTableField.setModel(new FieldSelectionModel(
299
                                                                                        pt.getModelo().getRecordset(),
300
                                                                                        PluginServices.getText(this, "seleccione_campo_enlace"),
301
                                                                                        -1));
302
                                                                } catch (SelectionException e) {
303
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
304
                                                                                e);
305
                                                                } catch (DriverLoadException e) {
306
                                                                        // TODO Auto-generated catch block
307
                                                                        e.printStackTrace();
308
                                                                }
309
                                                        } else if (w.currentStep == 3) {
310
                                                                try {
311
                                                                        //tabla
312
                                                                        ProjectTable pt = (ProjectTable) sourceTable.getSelected();
313

    
314
                                                                        //?ndice del campo
315
                                                                        SelectableDataSource sds = (SelectableDataSource)pt.getModelo().getRecordset();
316
                                                                        String fieldName = (String) firstTableField.getSelected();
317
                                                                        int fieldIndex = sds.getFieldIndexByName(fieldName);
318
                                                                        int type = sds.getFieldType(fieldIndex);
319

    
320
                                                                        secondTableField.setModel(new FieldSelectionModel(
321
                                                                                        ((ProjectTable) targetTable.getSelected()).getModelo().getRecordset(),
322
                                                                                        PluginServices.getText(this, "seleccione_campo_enlace"),
323
                                                                                        type));
324
                                                                } catch (SelectionException e) {
325
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
326
                                                                                e);
327
                                                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
328
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
329
                                                                                e);
330
                                                                } catch (DriverLoadException e) {
331
                                                                        // TODO Auto-generated catch block
332
                                                                        e.printStackTrace();
333
                                                                }
334
                                                        }
335
                                                }
336

    
337
                                                public void back(WizardEvent w) {
338
                                                        WizardControl wiz = w.wizard;
339
                                                        wiz.enableBack(true);
340
                                                        wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
341
                                                }
342
                                        });
343
                                PluginServices.getMDIManager().addWindow(wiz);
344
                        } catch (SelectionException e) {
345
                                NotificationManager.addError("Error abriendo el asistente", e);
346
                        }
347
              }
348
        }
349

    
350
        /**
351
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
352
         */
353
        public void newSet(String expression) {
354
                long[] sel = doSet(expression);
355

    
356
                if (sel == null) {
357
                        throw new RuntimeException("Not a 'where' clause?");
358
                }
359

    
360
                FBitSet selection = new FBitSet();
361

    
362
                for (int i = 0; i < sel.length; i++) {
363
                        selection.set((int) sel[i]);
364
                }
365

    
366
                dataSource.clearSelection();
367
                dataSource.setSelection(selection);
368
        }
369

    
370
        /**
371
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
372
         */
373
        private long[] doSet(String expression) {
374
                try {
375
                        DataSource ds = LayerFactory.getDataSourceFactory().executeSQL(expression,
376
                                        DataSourceFactory.MANUAL_OPENING);
377

    
378
                        return ds.getWhereFilter();
379
                } catch (DriverLoadException e) {
380
                        NotificationManager.addError("Error cargando el driver", e);
381
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
382
                        NotificationManager.addError("Error accediendo al driver", e);
383
                } catch (ParseException e) {
384
                        NotificationManager.addError("Parse error", e);
385
                } catch (SemanticException e) {
386
                        NotificationManager.addError(e.getMessage(), e);
387
                } catch (IOException e) {
388
                        NotificationManager.addError("GDBMS internal error", e);
389
                } catch (EvaluationException e) {
390
                        NotificationManager.addError("Error con la expresi?n", e);
391
        }
392

    
393
                return null;
394
        }
395

    
396
        /**
397
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#addToSet(java.lang.String)
398
         */
399
        public void addToSet(String expression) {
400
                long[] sel = doSet(expression);
401

    
402
                if (sel == null) {
403
                        throw new RuntimeException("Not a 'where' clause?");
404
                }
405

    
406
                FBitSet selection = new FBitSet();
407

    
408
                for (int i = 0; i < sel.length; i++) {
409
                        selection.set((int) sel[i]);
410
                }
411

    
412
                FBitSet fbs = dataSource.getSelection();
413
                fbs.or(selection);
414
                dataSource.setSelection(fbs);
415
        }
416

    
417
        /**
418
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#fromSet(java.lang.String)
419
         */
420
        public void fromSet(String expression) {
421
                long[] sel = doSet(expression);
422

    
423
                if (sel == null) {
424
                        throw new RuntimeException("Not a 'where' clause?");
425
                }
426

    
427
                FBitSet selection = new FBitSet();
428

    
429
                for (int i = 0; i < sel.length; i++) {
430
                        selection.set((int) sel[i]);
431
                }
432

    
433
                FBitSet fbs = dataSource.getSelection();
434
                fbs.and(selection);
435
                dataSource.setSelection(fbs);
436
        }
437

    
438
        /**
439
         * @see com.iver.mdiApp.plugins.IExtension#isVisible()
440
         */
441
        public boolean isVisible() {
442
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
443

    
444
                if (v == null) {
445
                        return false;
446
                }
447

    
448
                if (v instanceof Table) {
449
                        return true;
450
                } /*else {
451
                        if (v instanceof com.iver.cit.gvsig.gui.View) {
452
                                com.iver.cit.gvsig.gui.View view = (com.iver.cit.gvsig.gui.View) v;
453
                                ProjectView pv = view.getModel();
454
                                FLayer[] seleccionadas = pv.getMapContext().getLayers()
455
                                                                                   .getActives();
456

457
                                if (seleccionadas.length == 1) {
458
                                        if (seleccionadas[0] instanceof AlphanumericData) {
459
                                                return true;
460
                                        }
461
                                }
462
                        }
463
*/
464
                        return false;
465
                //}
466
        }
467

    
468
        /**
469
         * @see com.iver.andami.plugins.IExtension#initialize()
470
         */
471
        public void initialize() {
472
        }
473

    
474
        /**
475
         * @see com.iver.andami.plugins.IExtension#isEnabled()
476
         */
477
        public boolean isEnabled() {
478
                return true;
479
        }
480

    
481

    
482
}