Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / TableOperations.java @ 6877

History | View | Annotate | Download (16.5 KB)

1 1103 fjp
/* 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 312 fernando
package com.iver.cit.gvsig;
42
43 6736 caballero
import java.awt.Dimension;
44 1830 fernando
import java.io.IOException;
45
46 1219 vcaballero
import com.hardcode.driverManager.DriverLoadException;
47 884 fernando
import com.hardcode.gdbms.engine.data.DataSource;
48
import com.hardcode.gdbms.engine.data.DataSourceFactory;
49 2217 fernando
import com.hardcode.gdbms.engine.instruction.EvaluationException;
50 884 fernando
import com.hardcode.gdbms.engine.instruction.SemanticException;
51
import com.hardcode.gdbms.parser.ParseException;
52 855 fernando
import com.iver.andami.PluginServices;
53
import com.iver.andami.messages.NotificationManager;
54 596 fernando
import com.iver.andami.plugins.Extension;
55 6877 cesar
import com.iver.andami.ui.mdiManager.IWindow;
56 3940 caballero
import com.iver.cit.gvsig.fmap.edition.EditableAdapter;
57 884 fernando
import com.iver.cit.gvsig.fmap.layers.FBitSet;
58 1774 fernando
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
59 855 fernando
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
60 312 fernando
import com.iver.cit.gvsig.gui.Table;
61 855 fernando
import com.iver.cit.gvsig.gui.filter.ExpressionListener;
62 1830 fernando
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 312 fernando
72
73
/**
74 1219 vcaballero
 * Extensi?n que controla las operaciones realizadas sobre las tablas.
75 312 fernando
 *
76
 * @author Fernando Gonz?lez Cort?s
77
 */
78 5005 jorpiell
public class TableOperations extends Extension implements ExpressionListener {
79 855 fernando
        private SelectableDataSource dataSource = null;
80 2183 fernando
        //private Table vista;
81 312 fernando
82 1219 vcaballero
        /**
83 5005 jorpiell
         * @see com.iver.mdiApp.plugins.IExtension#updateUI(java.lang.String)
84 1219 vcaballero
         */
85
        public void execute(String actionCommand) {
86 2183 fernando
                if ("JOIN".equals(actionCommand)) {
87 1830 fernando
                        ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
88
                        ProjectTable[] pts = (ProjectTable[]) pe.getProject().getTables()
89
                                                                                                        .toArray(new ProjectTable[0]);
90 855 fernando
91 1830 fernando
                        try {
92
                                final ObjectSelectionStep sourceTable = new ObjectSelectionStep();
93
                                sourceTable.setModel(new TableSelectionModel(pts,
94 2337 fernando
                                        PluginServices.getText(this, "seleccione_tabla_origen")));
95 312 fernando
96 1830 fernando
                                final ObjectSelectionStep targetTable = new ObjectSelectionStep();
97
                                targetTable.setModel(new TableSelectionModel(pts,
98 2337 fernando
                                        PluginServices.getText(this, "seleccione_tabla_a_enlazar")));
99 312 fernando
100 1830 fernando
                                final ObjectSelectionStep firstTableField = new ObjectSelectionStep();
101
                                final ObjectSelectionStep secondTableField = new ObjectSelectionStep();
102 2337 fernando
                                final AndamiWizard wiz = new AndamiWizard(PluginServices.getText(this, "back"), PluginServices.getText(this, "next"), PluginServices.getText(this, "finish"), PluginServices.getText(this, "cancel"));
103 6736 caballero
                                wiz.setSize(new Dimension(450,200));
104 1830 fernando
                                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().closeView(wiz);
112
                                                }
113
114
                                                public void finished(WizardEvent w) {
115
                                                        PluginServices.getMDIManager().closeView(wiz);
116
117
                                                        ProjectTable sourceProjectTable = (ProjectTable) sourceTable.getSelected();
118 3940 caballero
                                                        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 1830 fernando
                                                        String tableName1 = sds.getName();
126
127
                                                        ProjectTable targetProjectTable = (ProjectTable) targetTable.getSelected();
128 3940 caballero
                                                        try {
129
                                                                sds = (SelectableDataSource)targetProjectTable.getModelo().getRecordset();
130
                                                        } catch (DriverLoadException e1) {
131
                                                                // TODO Auto-generated catch block
132
                                                                e1.printStackTrace();
133
                                                        }
134 1830 fernando
135
                                                        String tableName2 = sds.getName();
136
137
                                                        String field1 = (String) firstTableField.getSelected();
138
                                                        String field2 = (String) secondTableField.getSelected();
139
140 2183 fernando
                                                        String sql =
141
                                                                "custom com_iver_cit_gvsig_arcjoin tables '" +
142
                                                                tableName1 + "', '" + tableName2 + "' values(" +
143
                                                                field1 + ", " + field2 + ");";
144 1830 fernando
                                                        System.out.println(sql);
145
146
                                                        try {
147 2183 fernando
                                                                SelectableDataSource result = new SelectableDataSource(LayerFactory.getDataSourceFactory()
148
                                                                                                                                                                                                   .executeSQL(sql,
149 2758 fernando
                                                                                        DataSourceFactory.AUTOMATIC_OPENING));
150 1830 fernando
                                                                ProjectTable projectTable = (ProjectTable) sourceTable.getSelected();
151 3940 caballero
                                                                EditableAdapter auxea=new EditableAdapter();
152
                                                                auxea.setOriginalDataSource(result);
153
                                                                projectTable.replaceDataSource(auxea);
154 1830 fernando
                                                        } 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 2217 fernando
                                                        } catch (EvaluationException e) {
168
                                                                NotificationManager.addError("Error de evaluaci?n de la expresi?n",
169
                                                                                e);
170
                            }
171 1830 fernando
                                                }
172
173
                                                public void next(WizardEvent w) {
174
                                                        WizardControl wiz = w.wizard;
175
                                                        wiz.enableBack(true);
176 2183 fernando
                                                        wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
177
178 1830 fernando
                                                        if (w.currentStep == 1) {
179
                                                                ProjectTable pt = (ProjectTable) sourceTable.getSelected();
180
181
                                                                try {
182
                                                                        firstTableField.setModel(new FieldSelectionModel(
183 3940 caballero
                                                                                        pt.getModelo().getRecordset(),
184 2337 fernando
                                                                                        PluginServices.getText(this, "seleccione_campo_enlace"),
185 1830 fernando
                                                                                        -1));
186
                                                                } catch (SelectionException e) {
187
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
188
                                                                                e);
189 3940 caballero
                                                                } catch (DriverLoadException e) {
190
                                                                        // TODO Auto-generated catch block
191
                                                                        e.printStackTrace();
192 1830 fernando
                                                                }
193
                                                        } else if (w.currentStep == 3) {
194
                                                                try {
195
                                                                        //tabla
196
                                                                        ProjectTable pt = (ProjectTable) sourceTable.getSelected();
197
198
                                                                        //?ndice del campo
199 3940 caballero
                                                                        SelectableDataSource sds = (SelectableDataSource)pt.getModelo().getRecordset();
200 1830 fernando
                                                                        String fieldName = (String) firstTableField.getSelected();
201
                                                                        int fieldIndex = sds.getFieldIndexByName(fieldName);
202
                                                                        int type = sds.getFieldType(fieldIndex);
203
204
                                                                        secondTableField.setModel(new FieldSelectionModel(
205 3940 caballero
                                                                                        ((ProjectTable) targetTable.getSelected()).getModelo().getRecordset(),
206 2337 fernando
                                                                                        PluginServices.getText(this, "seleccione_campo_enlace"),
207 1830 fernando
                                                                                        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 3940 caballero
                                                                } catch (DriverLoadException e) {
215
                                                                        // TODO Auto-generated catch block
216
                                                                        e.printStackTrace();
217 1830 fernando
                                                                }
218
                                                        }
219
                                                }
220
221
                                                public void back(WizardEvent w) {
222
                                                        WizardControl wiz = w.wizard;
223
                                                        wiz.enableBack(true);
224 2183 fernando
                                                        wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
225 1830 fernando
                                                }
226
                                        });
227
                                PluginServices.getMDIManager().addView(wiz);
228
                        } catch (SelectionException e) {
229
                                NotificationManager.addError("Error abriendo el asistente", e);
230
                        }
231 2183 fernando
                }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 2337 fernando
                                                PluginServices.getText(this, "seleccione_tabla_origen")));
240 2183 fernando
241
                                final ObjectSelectionStep targetTable = new ObjectSelectionStep();
242
                                targetTable.setModel(new TableSelectionModel(pts,
243 2337 fernando
                                        PluginServices.getText(this, "seleccione_tabla_a_enlazar")));
244 2183 fernando
245
                                final ObjectSelectionStep firstTableField = new ObjectSelectionStep();
246
                                final ObjectSelectionStep secondTableField = new ObjectSelectionStep();
247 2337 fernando
                                final AndamiWizard wiz = new AndamiWizard(PluginServices.getText(this, "back"), PluginServices.getText(this, "next"), PluginServices.getText(this, "finish"), PluginServices.getText(this, "cancel"));
248 6736 caballero
                                wiz.setSize(new Dimension(450,200));
249 2183 fernando
                                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().closeView(wiz);
257
                                                }
258
259
                                                public void finished(WizardEvent w) {
260
                                                        PluginServices.getMDIManager().closeView(wiz);
261
262
                                                        ProjectTable sourceProjectTable = (ProjectTable) sourceTable.getSelected();
263 3940 caballero
                                                        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 2183 fernando
                                                        String tableName1 = sds1.getName();
271
272
                                                        ProjectTable targetProjectTable = (ProjectTable) targetTable.getSelected();
273 3940 caballero
                                                        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 2183 fernando
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 3940 caballero
                                                                                        pt.getModelo().getRecordset(),
300 2337 fernando
                                                                                        PluginServices.getText(this, "seleccione_campo_enlace"),
301 2183 fernando
                                                                                        -1));
302
                                                                } catch (SelectionException e) {
303
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
304
                                                                                e);
305 3940 caballero
                                                                } catch (DriverLoadException e) {
306
                                                                        // TODO Auto-generated catch block
307
                                                                        e.printStackTrace();
308 2183 fernando
                                                                }
309
                                                        } else if (w.currentStep == 3) {
310
                                                                try {
311
                                                                        //tabla
312
                                                                        ProjectTable pt = (ProjectTable) sourceTable.getSelected();
313
314
                                                                        //?ndice del campo
315 3940 caballero
                                                                        SelectableDataSource sds = (SelectableDataSource)pt.getModelo().getRecordset();
316 2183 fernando
                                                                        String fieldName = (String) firstTableField.getSelected();
317
                                                                        int fieldIndex = sds.getFieldIndexByName(fieldName);
318
                                                                        int type = sds.getFieldType(fieldIndex);
319
320
                                                                        secondTableField.setModel(new FieldSelectionModel(
321 3940 caballero
                                                                                        ((ProjectTable) targetTable.getSelected()).getModelo().getRecordset(),
322 2337 fernando
                                                                                        PluginServices.getText(this, "seleccione_campo_enlace"),
323 2183 fernando
                                                                                        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 3940 caballero
                                                                } catch (DriverLoadException e) {
331
                                                                        // TODO Auto-generated catch block
332
                                                                        e.printStackTrace();
333 2183 fernando
                                                                }
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().addView(wiz);
344
                        } catch (SelectionException e) {
345
                                NotificationManager.addError("Error abriendo el asistente", e);
346
                        }
347 3940 caballero
              }
348 1219 vcaballero
        }
349 312 fernando
350 1219 vcaballero
        /**
351 855 fernando
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
352 312 fernando
         */
353
        public void newSet(String expression) {
354 884 fernando
                long[] sel = doSet(expression);
355 1219 vcaballero
356
                if (sel == null) {
357
                        throw new RuntimeException("Not a 'where' clause?");
358
                }
359
360 884 fernando
                FBitSet selection = new FBitSet();
361 1219 vcaballero
362 884 fernando
                for (int i = 0; i < sel.length; i++) {
363
                        selection.set((int) sel[i]);
364
                }
365 1219 vcaballero
366 312 fernando
                dataSource.clearSelection();
367 884 fernando
                dataSource.setSelection(selection);
368 312 fernando
        }
369 1219 vcaballero
370 312 fernando
        /**
371 855 fernando
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
372 312 fernando
         */
373 884 fernando
        private long[] doSet(String expression) {
374
                try {
375 2183 fernando
                        DataSource ds = LayerFactory.getDataSourceFactory().executeSQL(expression,
376 2667 fernando
                                        DataSourceFactory.MANUAL_OPENING);
377 1219 vcaballero
378 884 fernando
                        return ds.getWhereFilter();
379
                } catch (DriverLoadException e) {
380
                        NotificationManager.addError("Error cargando el driver", e);
381 1830 fernando
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
382 884 fernando
                        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 2217 fernando
                } catch (EvaluationException e) {
390
                        NotificationManager.addError("Error con la expresi?n", e);
391
        }
392 1219 vcaballero
393 884 fernando
                return null;
394 312 fernando
        }
395
396 1219 vcaballero
        /**
397
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#addToSet(java.lang.String)
398
         */
399
        public void addToSet(String expression) {
400 884 fernando
                long[] sel = doSet(expression);
401 1219 vcaballero
402
                if (sel == null) {
403
                        throw new RuntimeException("Not a 'where' clause?");
404
                }
405
406 884 fernando
                FBitSet selection = new FBitSet();
407 1219 vcaballero
408 884 fernando
                for (int i = 0; i < sel.length; i++) {
409
                        selection.set((int) sel[i]);
410
                }
411 1219 vcaballero
412 884 fernando
                FBitSet fbs = dataSource.getSelection();
413
                fbs.or(selection);
414
                dataSource.setSelection(fbs);
415 1219 vcaballero
        }
416 312 fernando
417 1219 vcaballero
        /**
418
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#fromSet(java.lang.String)
419
         */
420
        public void fromSet(String expression) {
421 884 fernando
                long[] sel = doSet(expression);
422 1219 vcaballero
423
                if (sel == null) {
424
                        throw new RuntimeException("Not a 'where' clause?");
425
                }
426
427 884 fernando
                FBitSet selection = new FBitSet();
428 1219 vcaballero
429 884 fernando
                for (int i = 0; i < sel.length; i++) {
430
                        selection.set((int) sel[i]);
431 312 fernando
                }
432 1219 vcaballero
433 884 fernando
                FBitSet fbs = dataSource.getSelection();
434
                fbs.and(selection);
435
                dataSource.setSelection(fbs);
436 1219 vcaballero
        }
437 312 fernando
438
        /**
439 5005 jorpiell
         * @see com.iver.mdiApp.plugins.IExtension#isVisible()
440 312 fernando
         */
441
        public boolean isVisible() {
442 6877 cesar
                IWindow v = PluginServices.getMDIManager().getActiveView();
443 312 fernando
444 855 fernando
                if (v == null) {
445 312 fernando
                        return false;
446
                }
447 1219 vcaballero
448 5900 jorpiell
                if (v instanceof Table) {
449 312 fernando
                        return true;
450 2183 fernando
                } /*else {
451 1219 vcaballero
                        if (v instanceof com.iver.cit.gvsig.gui.View) {
452 855 fernando
                                com.iver.cit.gvsig.gui.View view = (com.iver.cit.gvsig.gui.View) v;
453
                                ProjectView pv = view.getModel();
454 1219 vcaballero
                                FLayer[] seleccionadas = pv.getMapContext().getLayers()
455
                                                                                   .getActives();
456

457
                                if (seleccionadas.length == 1) {
458
                                        if (seleccionadas[0] instanceof AlphanumericData) {
459 855 fernando
                                                return true;
460
                                        }
461 312 fernando
                                }
462
                        }
463 2183 fernando
*/
464 312 fernando
                        return false;
465 2183 fernando
                //}
466 312 fernando
        }
467
468 596 fernando
        /**
469 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#initialize()
470 596 fernando
         */
471 5005 jorpiell
        public void initialize() {
472 596 fernando
        }
473
474
        /**
475 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#isEnabled()
476 596 fernando
         */
477
        public boolean isEnabled() {
478 855 fernando
                return true;
479 596 fernando
        }
480 2183 fernando
481 3940 caballero
482 312 fernando
}