Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / TableOperations.java @ 3718

History | View | Annotate | Download (15 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.io.IOException;
44

    
45
import com.hardcode.driverManager.DriverLoadException;
46
import com.hardcode.gdbms.engine.data.DataSource;
47
import com.hardcode.gdbms.engine.data.DataSourceFactory;
48
import com.hardcode.gdbms.engine.data.driver.DriverException;
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.View;
56
import com.iver.cit.gvsig.fmap.layers.FBitSet;
57
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
58
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
59
import com.iver.cit.gvsig.gui.Table;
60
import com.iver.cit.gvsig.gui.filter.ExpressionListener;
61
import com.iver.cit.gvsig.gui.tables.AndamiWizard;
62
import com.iver.cit.gvsig.gui.tables.FieldSelectionModel;
63
import com.iver.cit.gvsig.gui.tables.ObjectSelectionStep;
64
import com.iver.cit.gvsig.gui.tables.Statistics;
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 implements Extension, ExpressionListener {
79
        private SelectableDataSource dataSource = null;
80
        //private Table vista;
81

    
82
        /**
83
         * @see com.iver.mdiApp.plugins.Extension#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

    
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().closeView(wiz);
112
                                                }
113

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

    
117
                                                        ProjectTable sourceProjectTable = (ProjectTable) sourceTable.getSelected();
118
                                                        SelectableDataSource sds = sourceProjectTable.getModelo();
119
                                                        String tableName1 = sds.getName();
120

    
121
                                                        ProjectTable targetProjectTable = (ProjectTable) targetTable.getSelected();
122
                                                        sds = targetProjectTable.getModelo();
123

    
124
                                                        String tableName2 = sds.getName();
125

    
126
                                                        String field1 = (String) firstTableField.getSelected();
127
                                                        String field2 = (String) secondTableField.getSelected();
128

    
129
                                                        String sql =
130
                                                                "custom com_iver_cit_gvsig_arcjoin tables '" +
131
                                                                tableName1 + "', '" + tableName2 + "' values(" +
132
                                                                field1 + ", " + field2 + ");";
133
                                                        System.out.println(sql);
134

    
135
                                                        try {
136
                                                                SelectableDataSource result = new SelectableDataSource(LayerFactory.getDataSourceFactory()
137
                                                                                                                                                                                                   .executeSQL(sql,
138
                                                                                        DataSourceFactory.AUTOMATIC_OPENING));
139
                                                                ProjectTable projectTable = (ProjectTable) sourceTable.getSelected();
140
                                                                projectTable.replaceDataSource(result);
141
                                                        } catch (ParseException e) {
142
                                                                throw new RuntimeException(e);
143
                                                        } catch (DriverLoadException e) {
144
                                                                NotificationManager.addError("Error con la carga de drivers",
145
                                                                        e);
146
                                                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
147
                                                                NotificationManager.addError("Error leyendo del driver",
148
                                                                        e);
149
                                                        } catch (SemanticException e) {
150
                                                                throw new RuntimeException(e);
151
                                                        } catch (IOException e) {
152
                                                                NotificationManager.addError("Error de entrada/salida",
153
                                                                        e);
154
                                                        } catch (EvaluationException e) {
155
                                                                NotificationManager.addError("Error de evaluaci?n de la expresi?n",
156
                                                                                e);
157
                            }
158
                                                }
159

    
160
                                                public void next(WizardEvent w) {
161
                                                        WizardControl wiz = w.wizard;
162
                                                        wiz.enableBack(true);
163
                                                        wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
164

    
165
                                                        if (w.currentStep == 1) {
166
                                                                ProjectTable pt = (ProjectTable) sourceTable.getSelected();
167

    
168
                                                                try {
169
                                                                        firstTableField.setModel(new FieldSelectionModel(
170
                                                                                        pt.getModelo(),
171
                                                                                        PluginServices.getText(this, "seleccione_campo_enlace"),
172
                                                                                        -1));
173
                                                                } catch (SelectionException e) {
174
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
175
                                                                                e);
176
                                                                }
177
                                                        } else if (w.currentStep == 3) {
178
                                                                try {
179
                                                                        //tabla
180
                                                                        ProjectTable pt = (ProjectTable) sourceTable.getSelected();
181

    
182
                                                                        //?ndice del campo
183
                                                                        SelectableDataSource sds = pt.getModelo();
184
                                                                        String fieldName = (String) firstTableField.getSelected();
185
                                                                        int fieldIndex = sds.getFieldIndexByName(fieldName);
186
                                                                        int type = sds.getFieldType(fieldIndex);
187

    
188
                                                                        secondTableField.setModel(new FieldSelectionModel(
189
                                                                                        ((ProjectTable) targetTable.getSelected()).getModelo(),
190
                                                                                        PluginServices.getText(this, "seleccione_campo_enlace"),
191
                                                                                        type));
192
                                                                } catch (SelectionException e) {
193
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
194
                                                                                e);
195
                                                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
196
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
197
                                                                                e);
198
                                                                }
199
                                                        }
200
                                                }
201

    
202
                                                public void back(WizardEvent w) {
203
                                                        WizardControl wiz = w.wizard;
204
                                                        wiz.enableBack(true);
205
                                                        wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
206
                                                }
207
                                        });
208
                                PluginServices.getMDIManager().addView(wiz);
209
                        } catch (SelectionException e) {
210
                                NotificationManager.addError("Error abriendo el asistente", e);
211
                        }
212
                }else if ("LINK".equals(actionCommand)) {
213
                        ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
214
                        ProjectTable[] pts = (ProjectTable[]) pe.getProject().getTables()
215
                                                                                                        .toArray(new ProjectTable[0]);
216

    
217
                        try {
218
                                final ObjectSelectionStep sourceTable = new ObjectSelectionStep();
219
                                sourceTable.setModel(new TableSelectionModel(pts,
220
                                                PluginServices.getText(this, "seleccione_tabla_origen")));
221

    
222
                                final ObjectSelectionStep targetTable = new ObjectSelectionStep();
223
                                targetTable.setModel(new TableSelectionModel(pts,
224
                                        PluginServices.getText(this, "seleccione_tabla_a_enlazar")));
225

    
226
                                final ObjectSelectionStep firstTableField = new ObjectSelectionStep();
227
                                final ObjectSelectionStep secondTableField = new ObjectSelectionStep();
228
                                final AndamiWizard wiz = new AndamiWizard(PluginServices.getText(this, "back"), PluginServices.getText(this, "next"), PluginServices.getText(this, "finish"), PluginServices.getText(this, "cancel"));
229

    
230
                                wiz.addStep(sourceTable);
231
                                wiz.addStep(firstTableField);
232
                                wiz.addStep(targetTable);
233
                                wiz.addStep(secondTableField);
234

    
235
                                wiz.addWizardListener(new WizardListener() {
236
                                                public void cancel(WizardEvent w) {
237
                                                        PluginServices.getMDIManager().closeView(wiz);
238
                                                }
239

    
240
                                                public void finished(WizardEvent w) {
241
                                                        PluginServices.getMDIManager().closeView(wiz);
242

    
243
                                                        ProjectTable sourceProjectTable = (ProjectTable) sourceTable.getSelected();
244
                                                        SelectableDataSource sds1 = sourceProjectTable.getModelo();
245
                                                        String tableName1 = sds1.getName();
246

    
247
                                                        ProjectTable targetProjectTable = (ProjectTable) targetTable.getSelected();
248
                                                        SelectableDataSource sds2 = targetProjectTable.getModelo();
249

    
250
                                                        String tableName2 = sds2.getName();
251

    
252
                                                        String field1 = (String) firstTableField.getSelected();
253
                                                        String field2 = (String) secondTableField.getSelected();
254
                                                        sourceProjectTable.setLinkTable(sds2.getName(),field1,field2);
255
                                                        ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject().setLinkTable();
256
                                                }
257

    
258
                                                public void next(WizardEvent w) {
259
                                                        WizardControl wiz = w.wizard;
260
                                                        wiz.enableBack(true);
261
                                                        wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
262

    
263
                                                        if (w.currentStep == 1) {
264
                                                                ProjectTable pt = (ProjectTable) sourceTable.getSelected();
265

    
266
                                                                try {
267
                                                                        firstTableField.setModel(new FieldSelectionModel(
268
                                                                                        pt.getModelo(),
269
                                                                                        PluginServices.getText(this, "seleccione_campo_enlace"),
270
                                                                                        -1));
271
                                                                } catch (SelectionException e) {
272
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
273
                                                                                e);
274
                                                                }
275
                                                        } else if (w.currentStep == 3) {
276
                                                                try {
277
                                                                        //tabla
278
                                                                        ProjectTable pt = (ProjectTable) sourceTable.getSelected();
279

    
280
                                                                        //?ndice del campo
281
                                                                        SelectableDataSource sds = pt.getModelo();
282
                                                                        String fieldName = (String) firstTableField.getSelected();
283
                                                                        int fieldIndex = sds.getFieldIndexByName(fieldName);
284
                                                                        int type = sds.getFieldType(fieldIndex);
285

    
286
                                                                        secondTableField.setModel(new FieldSelectionModel(
287
                                                                                        ((ProjectTable) targetTable.getSelected()).getModelo(),
288
                                                                                        PluginServices.getText(this, "seleccione_campo_enlace"),
289
                                                                                        type));
290
                                                                } catch (SelectionException e) {
291
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
292
                                                                                e);
293
                                                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
294
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
295
                                                                                e);
296
                                                                }
297
                                                        }
298
                                                }
299

    
300
                                                public void back(WizardEvent w) {
301
                                                        WizardControl wiz = w.wizard;
302
                                                        wiz.enableBack(true);
303
                                                        wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
304
                                                }
305
                                        });
306
                                PluginServices.getMDIManager().addView(wiz);
307
                        } catch (SelectionException e) {
308
                                NotificationManager.addError("Error abriendo el asistente", e);
309
                        }
310
              } 
311
        }
312

    
313
        /**
314
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
315
         */
316
        public void newSet(String expression) {
317
                long[] sel = doSet(expression);
318

    
319
                if (sel == null) {
320
                        throw new RuntimeException("Not a 'where' clause?");
321
                }
322

    
323
                FBitSet selection = new FBitSet();
324

    
325
                for (int i = 0; i < sel.length; i++) {
326
                        selection.set((int) sel[i]);
327
                }
328

    
329
                dataSource.clearSelection();
330
                dataSource.setSelection(selection);
331
        }
332

    
333
        /**
334
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
335
         */
336
        private long[] doSet(String expression) {
337
                try {
338
                        DataSource ds = LayerFactory.getDataSourceFactory().executeSQL(expression,
339
                                        DataSourceFactory.MANUAL_OPENING);
340

    
341
                        return ds.getWhereFilter();
342
                } catch (DriverLoadException e) {
343
                        NotificationManager.addError("Error cargando el driver", e);
344
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
345
                        NotificationManager.addError("Error accediendo al driver", e);
346
                } catch (ParseException e) {
347
                        NotificationManager.addError("Parse error", e);
348
                } catch (SemanticException e) {
349
                        NotificationManager.addError(e.getMessage(), e);
350
                } catch (IOException e) {
351
                        NotificationManager.addError("GDBMS internal error", e);
352
                } catch (EvaluationException e) {
353
                        NotificationManager.addError("Error con la expresi?n", e);
354
        }
355

    
356
                return null;
357
        }
358

    
359
        /**
360
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#addToSet(java.lang.String)
361
         */
362
        public void addToSet(String expression) {
363
                long[] sel = doSet(expression);
364

    
365
                if (sel == null) {
366
                        throw new RuntimeException("Not a 'where' clause?");
367
                }
368

    
369
                FBitSet selection = new FBitSet();
370

    
371
                for (int i = 0; i < sel.length; i++) {
372
                        selection.set((int) sel[i]);
373
                }
374

    
375
                FBitSet fbs = dataSource.getSelection();
376
                fbs.or(selection);
377
                dataSource.setSelection(fbs);
378
        }
379

    
380
        /**
381
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#fromSet(java.lang.String)
382
         */
383
        public void fromSet(String expression) {
384
                long[] sel = doSet(expression);
385

    
386
                if (sel == null) {
387
                        throw new RuntimeException("Not a 'where' clause?");
388
                }
389

    
390
                FBitSet selection = new FBitSet();
391

    
392
                for (int i = 0; i < sel.length; i++) {
393
                        selection.set((int) sel[i]);
394
                }
395

    
396
                FBitSet fbs = dataSource.getSelection();
397
                fbs.and(selection);
398
                dataSource.setSelection(fbs);
399
        }
400

    
401
        /**
402
         * @see com.iver.mdiApp.plugins.Extension#isVisible()
403
         */
404
        public boolean isVisible() {
405
                View v = PluginServices.getMDIManager().getActiveView();
406

    
407
                if (v == null) {
408
                        return false;
409
                }
410

    
411
                if (v.getClass() == Table.class) {
412
                        return true;
413
                } /*else {
414
                        if (v instanceof com.iver.cit.gvsig.gui.View) {
415
                                com.iver.cit.gvsig.gui.View view = (com.iver.cit.gvsig.gui.View) v;
416
                                ProjectView pv = view.getModel();
417
                                FLayer[] seleccionadas = pv.getMapContext().getLayers()
418
                                                                                   .getActives();
419

420
                                if (seleccionadas.length == 1) {
421
                                        if (seleccionadas[0] instanceof AlphanumericData) {
422
                                                return true;
423
                                        }
424
                                }
425
                        }
426
*/
427
                        return false;
428
                //}
429
        }
430

    
431
        /**
432
         * @see com.iver.andami.plugins.Extension#inicializar()
433
         */
434
        public void inicializar() {
435
        }
436

    
437
        /**
438
         * @see com.iver.andami.plugins.Extension#isEnabled()
439
         */
440
        public boolean isEnabled() {
441
                return true;
442
        }
443

    
444
        
445
}