Statistics
| Revision:

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

History | View | Annotate | Download (16.4 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 1830 fernando
import java.io.IOException;
44
45 1219 vcaballero
import com.hardcode.driverManager.DriverLoadException;
46 884 fernando
import com.hardcode.gdbms.engine.data.DataSource;
47
import com.hardcode.gdbms.engine.data.DataSourceFactory;
48 2217 fernando
import com.hardcode.gdbms.engine.instruction.EvaluationException;
49 884 fernando
import com.hardcode.gdbms.engine.instruction.SemanticException;
50
import com.hardcode.gdbms.parser.ParseException;
51 855 fernando
import com.iver.andami.PluginServices;
52
import com.iver.andami.messages.NotificationManager;
53 596 fernando
import com.iver.andami.plugins.Extension;
54 855 fernando
import com.iver.andami.ui.mdiManager.View;
55 3940 caballero
import com.iver.cit.gvsig.fmap.edition.EditableAdapter;
56 884 fernando
import com.iver.cit.gvsig.fmap.layers.FBitSet;
57 1774 fernando
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
58 855 fernando
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
59 312 fernando
import com.iver.cit.gvsig.gui.Table;
60 855 fernando
import com.iver.cit.gvsig.gui.filter.ExpressionListener;
61 1830 fernando
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.TableSelectionModel;
65
import com.iver.cit.gvsig.project.ProjectTable;
66
import com.iver.utiles.swing.objectSelection.SelectionException;
67
import com.iver.utiles.swing.wizard.WizardControl;
68
import com.iver.utiles.swing.wizard.WizardEvent;
69
import com.iver.utiles.swing.wizard.WizardListener;
70 312 fernando
71
72
/**
73 1219 vcaballero
 * Extensi?n que controla las operaciones realizadas sobre las tablas.
74 312 fernando
 *
75
 * @author Fernando Gonz?lez Cort?s
76
 */
77 5005 jorpiell
public class TableOperations extends Extension implements ExpressionListener {
78 855 fernando
        private SelectableDataSource dataSource = null;
79 2183 fernando
        //private Table vista;
80 312 fernando
81 1219 vcaballero
        /**
82 5005 jorpiell
         * @see com.iver.mdiApp.plugins.IExtension#updateUI(java.lang.String)
83 1219 vcaballero
         */
84
        public void execute(String actionCommand) {
85 2183 fernando
                if ("JOIN".equals(actionCommand)) {
86 1830 fernando
                        ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
87
                        ProjectTable[] pts = (ProjectTable[]) pe.getProject().getTables()
88
                                                                                                        .toArray(new ProjectTable[0]);
89 855 fernando
90 1830 fernando
                        try {
91
                                final ObjectSelectionStep sourceTable = new ObjectSelectionStep();
92
                                sourceTable.setModel(new TableSelectionModel(pts,
93 2337 fernando
                                        PluginServices.getText(this, "seleccione_tabla_origen")));
94 312 fernando
95 1830 fernando
                                final ObjectSelectionStep targetTable = new ObjectSelectionStep();
96
                                targetTable.setModel(new TableSelectionModel(pts,
97 2337 fernando
                                        PluginServices.getText(this, "seleccione_tabla_a_enlazar")));
98 312 fernando
99 1830 fernando
                                final ObjectSelectionStep firstTableField = new ObjectSelectionStep();
100
                                final ObjectSelectionStep secondTableField = new ObjectSelectionStep();
101 2337 fernando
                                final AndamiWizard wiz = new AndamiWizard(PluginServices.getText(this, "back"), PluginServices.getText(this, "next"), PluginServices.getText(this, "finish"), PluginServices.getText(this, "cancel"));
102 1830 fernando
103
                                wiz.addStep(sourceTable);
104
                                wiz.addStep(firstTableField);
105
                                wiz.addStep(targetTable);
106
                                wiz.addStep(secondTableField);
107
108
                                wiz.addWizardListener(new WizardListener() {
109
                                                public void cancel(WizardEvent w) {
110
                                                        PluginServices.getMDIManager().closeView(wiz);
111
                                                }
112
113
                                                public void finished(WizardEvent w) {
114
                                                        PluginServices.getMDIManager().closeView(wiz);
115
116
                                                        ProjectTable sourceProjectTable = (ProjectTable) sourceTable.getSelected();
117 3940 caballero
                                                        SelectableDataSource sds=null;
118
                                                        try {
119
                                                                sds =(SelectableDataSource) sourceProjectTable.getModelo().getRecordset();
120
                                                        } catch (DriverLoadException e1) {
121
                                                                // TODO Auto-generated catch block
122
                                                                e1.printStackTrace();
123
                                                        }
124 1830 fernando
                                                        String tableName1 = sds.getName();
125
126
                                                        ProjectTable targetProjectTable = (ProjectTable) targetTable.getSelected();
127 3940 caballero
                                                        try {
128
                                                                sds = (SelectableDataSource)targetProjectTable.getModelo().getRecordset();
129
                                                        } catch (DriverLoadException e1) {
130
                                                                // TODO Auto-generated catch block
131
                                                                e1.printStackTrace();
132
                                                        }
133 1830 fernando
134
                                                        String tableName2 = sds.getName();
135
136
                                                        String field1 = (String) firstTableField.getSelected();
137
                                                        String field2 = (String) secondTableField.getSelected();
138
139 2183 fernando
                                                        String sql =
140
                                                                "custom com_iver_cit_gvsig_arcjoin tables '" +
141
                                                                tableName1 + "', '" + tableName2 + "' values(" +
142
                                                                field1 + ", " + field2 + ");";
143 1830 fernando
                                                        System.out.println(sql);
144
145
                                                        try {
146 2183 fernando
                                                                SelectableDataSource result = new SelectableDataSource(LayerFactory.getDataSourceFactory()
147
                                                                                                                                                                                                   .executeSQL(sql,
148 2758 fernando
                                                                                        DataSourceFactory.AUTOMATIC_OPENING));
149 1830 fernando
                                                                ProjectTable projectTable = (ProjectTable) sourceTable.getSelected();
150 3940 caballero
                                                                EditableAdapter auxea=new EditableAdapter();
151
                                                                auxea.setOriginalDataSource(result);
152
                                                                projectTable.replaceDataSource(auxea);
153 1830 fernando
                                                        } catch (ParseException e) {
154
                                                                throw new RuntimeException(e);
155
                                                        } catch (DriverLoadException e) {
156
                                                                NotificationManager.addError("Error con la carga de drivers",
157
                                                                        e);
158
                                                        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
159
                                                                NotificationManager.addError("Error leyendo del driver",
160
                                                                        e);
161
                                                        } catch (SemanticException e) {
162
                                                                throw new RuntimeException(e);
163
                                                        } catch (IOException e) {
164
                                                                NotificationManager.addError("Error de entrada/salida",
165
                                                                        e);
166 2217 fernando
                                                        } catch (EvaluationException e) {
167
                                                                NotificationManager.addError("Error de evaluaci?n de la expresi?n",
168
                                                                                e);
169
                            }
170 1830 fernando
                                                }
171
172
                                                public void next(WizardEvent w) {
173
                                                        WizardControl wiz = w.wizard;
174
                                                        wiz.enableBack(true);
175 2183 fernando
                                                        wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
176
177 1830 fernando
                                                        if (w.currentStep == 1) {
178
                                                                ProjectTable pt = (ProjectTable) sourceTable.getSelected();
179
180
                                                                try {
181
                                                                        firstTableField.setModel(new FieldSelectionModel(
182 3940 caballero
                                                                                        pt.getModelo().getRecordset(),
183 2337 fernando
                                                                                        PluginServices.getText(this, "seleccione_campo_enlace"),
184 1830 fernando
                                                                                        -1));
185
                                                                } catch (SelectionException e) {
186
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
187
                                                                                e);
188 3940 caballero
                                                                } catch (DriverLoadException e) {
189
                                                                        // TODO Auto-generated catch block
190
                                                                        e.printStackTrace();
191 1830 fernando
                                                                }
192
                                                        } else if (w.currentStep == 3) {
193
                                                                try {
194
                                                                        //tabla
195
                                                                        ProjectTable pt = (ProjectTable) sourceTable.getSelected();
196
197
                                                                        //?ndice del campo
198 3940 caballero
                                                                        SelectableDataSource sds = (SelectableDataSource)pt.getModelo().getRecordset();
199 1830 fernando
                                                                        String fieldName = (String) firstTableField.getSelected();
200
                                                                        int fieldIndex = sds.getFieldIndexByName(fieldName);
201
                                                                        int type = sds.getFieldType(fieldIndex);
202
203
                                                                        secondTableField.setModel(new FieldSelectionModel(
204 3940 caballero
                                                                                        ((ProjectTable) targetTable.getSelected()).getModelo().getRecordset(),
205 2337 fernando
                                                                                        PluginServices.getText(this, "seleccione_campo_enlace"),
206 1830 fernando
                                                                                        type));
207
                                                                } catch (SelectionException e) {
208
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
209
                                                                                e);
210
                                                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
211
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
212
                                                                                e);
213 3940 caballero
                                                                } catch (DriverLoadException e) {
214
                                                                        // TODO Auto-generated catch block
215
                                                                        e.printStackTrace();
216 1830 fernando
                                                                }
217
                                                        }
218
                                                }
219
220
                                                public void back(WizardEvent w) {
221
                                                        WizardControl wiz = w.wizard;
222
                                                        wiz.enableBack(true);
223 2183 fernando
                                                        wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
224 1830 fernando
                                                }
225
                                        });
226
                                PluginServices.getMDIManager().addView(wiz);
227
                        } catch (SelectionException e) {
228
                                NotificationManager.addError("Error abriendo el asistente", e);
229
                        }
230 2183 fernando
                }else if ("LINK".equals(actionCommand)) {
231
                        ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
232
                        ProjectTable[] pts = (ProjectTable[]) pe.getProject().getTables()
233
                                                                                                        .toArray(new ProjectTable[0]);
234
235
                        try {
236
                                final ObjectSelectionStep sourceTable = new ObjectSelectionStep();
237
                                sourceTable.setModel(new TableSelectionModel(pts,
238 2337 fernando
                                                PluginServices.getText(this, "seleccione_tabla_origen")));
239 2183 fernando
240
                                final ObjectSelectionStep targetTable = new ObjectSelectionStep();
241
                                targetTable.setModel(new TableSelectionModel(pts,
242 2337 fernando
                                        PluginServices.getText(this, "seleccione_tabla_a_enlazar")));
243 2183 fernando
244
                                final ObjectSelectionStep firstTableField = new ObjectSelectionStep();
245
                                final ObjectSelectionStep secondTableField = new ObjectSelectionStep();
246 2337 fernando
                                final AndamiWizard wiz = new AndamiWizard(PluginServices.getText(this, "back"), PluginServices.getText(this, "next"), PluginServices.getText(this, "finish"), PluginServices.getText(this, "cancel"));
247 2183 fernando
248
                                wiz.addStep(sourceTable);
249
                                wiz.addStep(firstTableField);
250
                                wiz.addStep(targetTable);
251
                                wiz.addStep(secondTableField);
252
253
                                wiz.addWizardListener(new WizardListener() {
254
                                                public void cancel(WizardEvent w) {
255
                                                        PluginServices.getMDIManager().closeView(wiz);
256
                                                }
257
258
                                                public void finished(WizardEvent w) {
259
                                                        PluginServices.getMDIManager().closeView(wiz);
260
261
                                                        ProjectTable sourceProjectTable = (ProjectTable) sourceTable.getSelected();
262 3940 caballero
                                                        SelectableDataSource sds1=null;;
263
                                                        try {
264
                                                                sds1 = (SelectableDataSource)sourceProjectTable.getModelo().getRecordset();
265
                                                        } catch (DriverLoadException e) {
266
                                                                // TODO Auto-generated catch block
267
                                                                e.printStackTrace();
268
                                                        }
269 2183 fernando
                                                        String tableName1 = sds1.getName();
270
271
                                                        ProjectTable targetProjectTable = (ProjectTable) targetTable.getSelected();
272 3940 caballero
                                                        SelectableDataSource sds2=null;
273
                                                        try {
274
                                                                sds2 = (SelectableDataSource)targetProjectTable.getModelo().getRecordset();
275
                                                        } catch (DriverLoadException e) {
276
                                                                // TODO Auto-generated catch block
277
                                                                e.printStackTrace();
278
                                                        }
279 2183 fernando
280
                                                        String tableName2 = sds2.getName();
281
282
                                                        String field1 = (String) firstTableField.getSelected();
283
                                                        String field2 = (String) secondTableField.getSelected();
284
                                                        sourceProjectTable.setLinkTable(sds2.getName(),field1,field2);
285
                                                        ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject().setLinkTable();
286
                                                }
287
288
                                                public void next(WizardEvent w) {
289
                                                        WizardControl wiz = w.wizard;
290
                                                        wiz.enableBack(true);
291
                                                        wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
292
293
                                                        if (w.currentStep == 1) {
294
                                                                ProjectTable pt = (ProjectTable) sourceTable.getSelected();
295
296
                                                                try {
297
                                                                        firstTableField.setModel(new FieldSelectionModel(
298 3940 caballero
                                                                                        pt.getModelo().getRecordset(),
299 2337 fernando
                                                                                        PluginServices.getText(this, "seleccione_campo_enlace"),
300 2183 fernando
                                                                                        -1));
301
                                                                } catch (SelectionException e) {
302
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
303
                                                                                e);
304 3940 caballero
                                                                } catch (DriverLoadException e) {
305
                                                                        // TODO Auto-generated catch block
306
                                                                        e.printStackTrace();
307 2183 fernando
                                                                }
308
                                                        } else if (w.currentStep == 3) {
309
                                                                try {
310
                                                                        //tabla
311
                                                                        ProjectTable pt = (ProjectTable) sourceTable.getSelected();
312
313
                                                                        //?ndice del campo
314 3940 caballero
                                                                        SelectableDataSource sds = (SelectableDataSource)pt.getModelo().getRecordset();
315 2183 fernando
                                                                        String fieldName = (String) firstTableField.getSelected();
316
                                                                        int fieldIndex = sds.getFieldIndexByName(fieldName);
317
                                                                        int type = sds.getFieldType(fieldIndex);
318
319
                                                                        secondTableField.setModel(new FieldSelectionModel(
320 3940 caballero
                                                                                        ((ProjectTable) targetTable.getSelected()).getModelo().getRecordset(),
321 2337 fernando
                                                                                        PluginServices.getText(this, "seleccione_campo_enlace"),
322 2183 fernando
                                                                                        type));
323
                                                                } catch (SelectionException e) {
324
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
325
                                                                                e);
326
                                                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
327
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
328
                                                                                e);
329 3940 caballero
                                                                } catch (DriverLoadException e) {
330
                                                                        // TODO Auto-generated catch block
331
                                                                        e.printStackTrace();
332 2183 fernando
                                                                }
333
                                                        }
334
                                                }
335
336
                                                public void back(WizardEvent w) {
337
                                                        WizardControl wiz = w.wizard;
338
                                                        wiz.enableBack(true);
339
                                                        wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
340
                                                }
341
                                        });
342
                                PluginServices.getMDIManager().addView(wiz);
343
                        } catch (SelectionException e) {
344
                                NotificationManager.addError("Error abriendo el asistente", e);
345
                        }
346 3940 caballero
              }
347 1219 vcaballero
        }
348 312 fernando
349 1219 vcaballero
        /**
350 855 fernando
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
351 312 fernando
         */
352
        public void newSet(String expression) {
353 884 fernando
                long[] sel = doSet(expression);
354 1219 vcaballero
355
                if (sel == null) {
356
                        throw new RuntimeException("Not a 'where' clause?");
357
                }
358
359 884 fernando
                FBitSet selection = new FBitSet();
360 1219 vcaballero
361 884 fernando
                for (int i = 0; i < sel.length; i++) {
362
                        selection.set((int) sel[i]);
363
                }
364 1219 vcaballero
365 312 fernando
                dataSource.clearSelection();
366 884 fernando
                dataSource.setSelection(selection);
367 312 fernando
        }
368 1219 vcaballero
369 312 fernando
        /**
370 855 fernando
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
371 312 fernando
         */
372 884 fernando
        private long[] doSet(String expression) {
373
                try {
374 2183 fernando
                        DataSource ds = LayerFactory.getDataSourceFactory().executeSQL(expression,
375 2667 fernando
                                        DataSourceFactory.MANUAL_OPENING);
376 1219 vcaballero
377 884 fernando
                        return ds.getWhereFilter();
378
                } catch (DriverLoadException e) {
379
                        NotificationManager.addError("Error cargando el driver", e);
380 1830 fernando
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
381 884 fernando
                        NotificationManager.addError("Error accediendo al driver", e);
382
                } catch (ParseException e) {
383
                        NotificationManager.addError("Parse error", e);
384
                } catch (SemanticException e) {
385
                        NotificationManager.addError(e.getMessage(), e);
386
                } catch (IOException e) {
387
                        NotificationManager.addError("GDBMS internal error", e);
388 2217 fernando
                } catch (EvaluationException e) {
389
                        NotificationManager.addError("Error con la expresi?n", e);
390
        }
391 1219 vcaballero
392 884 fernando
                return null;
393 312 fernando
        }
394
395 1219 vcaballero
        /**
396
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#addToSet(java.lang.String)
397
         */
398
        public void addToSet(String expression) {
399 884 fernando
                long[] sel = doSet(expression);
400 1219 vcaballero
401
                if (sel == null) {
402
                        throw new RuntimeException("Not a 'where' clause?");
403
                }
404
405 884 fernando
                FBitSet selection = new FBitSet();
406 1219 vcaballero
407 884 fernando
                for (int i = 0; i < sel.length; i++) {
408
                        selection.set((int) sel[i]);
409
                }
410 1219 vcaballero
411 884 fernando
                FBitSet fbs = dataSource.getSelection();
412
                fbs.or(selection);
413
                dataSource.setSelection(fbs);
414 1219 vcaballero
        }
415 312 fernando
416 1219 vcaballero
        /**
417
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#fromSet(java.lang.String)
418
         */
419
        public void fromSet(String expression) {
420 884 fernando
                long[] sel = doSet(expression);
421 1219 vcaballero
422
                if (sel == null) {
423
                        throw new RuntimeException("Not a 'where' clause?");
424
                }
425
426 884 fernando
                FBitSet selection = new FBitSet();
427 1219 vcaballero
428 884 fernando
                for (int i = 0; i < sel.length; i++) {
429
                        selection.set((int) sel[i]);
430 312 fernando
                }
431 1219 vcaballero
432 884 fernando
                FBitSet fbs = dataSource.getSelection();
433
                fbs.and(selection);
434
                dataSource.setSelection(fbs);
435 1219 vcaballero
        }
436 312 fernando
437
        /**
438 5005 jorpiell
         * @see com.iver.mdiApp.plugins.IExtension#isVisible()
439 312 fernando
         */
440
        public boolean isVisible() {
441 855 fernando
                View v = PluginServices.getMDIManager().getActiveView();
442 312 fernando
443 855 fernando
                if (v == null) {
444 312 fernando
                        return false;
445
                }
446 1219 vcaballero
447 855 fernando
                if (v.getClass() == Table.class) {
448 312 fernando
                        return true;
449 2183 fernando
                } /*else {
450 1219 vcaballero
                        if (v instanceof com.iver.cit.gvsig.gui.View) {
451 855 fernando
                                com.iver.cit.gvsig.gui.View view = (com.iver.cit.gvsig.gui.View) v;
452
                                ProjectView pv = view.getModel();
453 1219 vcaballero
                                FLayer[] seleccionadas = pv.getMapContext().getLayers()
454
                                                                                   .getActives();
455

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