Statistics
| Revision:

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

History | View | Annotate | Download (14.2 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.instruction.SemanticException;
49
import com.hardcode.gdbms.parser.ParseException;
50
import com.iver.andami.PluginServices;
51
import com.iver.andami.messages.NotificationManager;
52
import com.iver.andami.plugins.Extension;
53
import com.iver.andami.ui.mdiManager.View;
54
import com.iver.cit.gvsig.fmap.layers.FBitSet;
55
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
56
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
57
import com.iver.cit.gvsig.gui.Table;
58
import com.iver.cit.gvsig.gui.filter.ExpressionListener;
59
import com.iver.cit.gvsig.gui.tables.AndamiWizard;
60
import com.iver.cit.gvsig.gui.tables.FieldSelectionModel;
61
import com.iver.cit.gvsig.gui.tables.ObjectSelectionStep;
62
import com.iver.cit.gvsig.gui.tables.TableSelectionModel;
63
import com.iver.cit.gvsig.project.ProjectTable;
64
import com.iver.utiles.swing.objectSelection.SelectionException;
65
import com.iver.utiles.swing.wizard.WizardControl;
66
import com.iver.utiles.swing.wizard.WizardEvent;
67
import com.iver.utiles.swing.wizard.WizardListener;
68

    
69

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

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

    
88
                        try {
89
                                final ObjectSelectionStep sourceTable = new ObjectSelectionStep();
90
                                sourceTable.setModel(new TableSelectionModel(pts,
91
                                                "Selecciona la tabla origen"));
92

    
93
                                final ObjectSelectionStep targetTable = new ObjectSelectionStep();
94
                                targetTable.setModel(new TableSelectionModel(pts,
95
                                                "Selecciona la tabla a enlazar"));
96

    
97
                                final ObjectSelectionStep firstTableField = new ObjectSelectionStep();
98
                                final ObjectSelectionStep secondTableField = new ObjectSelectionStep();
99
                                final AndamiWizard wiz = new AndamiWizard();
100

    
101
                                wiz.addStep(sourceTable);
102
                                wiz.addStep(firstTableField);
103
                                wiz.addStep(targetTable);
104
                                wiz.addStep(secondTableField);
105

    
106
                                wiz.addWizardListener(new WizardListener() {
107
                                                public void cancel(WizardEvent w) {
108
                                                        PluginServices.getMDIManager().closeView(wiz);
109
                                                }
110

    
111
                                                public void finished(WizardEvent w) {
112
                                                        PluginServices.getMDIManager().closeView(wiz);
113

    
114
                                                        ProjectTable sourceProjectTable = (ProjectTable) sourceTable.getSelected();
115
                                                        SelectableDataSource sds = sourceProjectTable.getModelo();
116
                                                        String tableName1 = sds.getName();
117

    
118
                                                        ProjectTable targetProjectTable = (ProjectTable) targetTable.getSelected();
119
                                                        sds = targetProjectTable.getModelo();
120

    
121
                                                        String tableName2 = sds.getName();
122

    
123
                                                        String field1 = (String) firstTableField.getSelected();
124
                                                        String field2 = (String) secondTableField.getSelected();
125

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

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

    
154
                                                public void next(WizardEvent w) {
155
                                                        WizardControl wiz = w.wizard;
156
                                                        wiz.enableBack(true);
157
                                                        wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
158

    
159
                                                        if (w.currentStep == 1) {
160
                                                                ProjectTable pt = (ProjectTable) sourceTable.getSelected();
161

    
162
                                                                try {
163
                                                                        firstTableField.setModel(new FieldSelectionModel(
164
                                                                                        pt.getModelo(),
165
                                                                                        "Selecciona el campo sobre el que hacer el join",
166
                                                                                        -1));
167
                                                                } catch (SelectionException e) {
168
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
169
                                                                                e);
170
                                                                }
171
                                                        } else if (w.currentStep == 3) {
172
                                                                try {
173
                                                                        //tabla
174
                                                                        ProjectTable pt = (ProjectTable) sourceTable.getSelected();
175

    
176
                                                                        //?ndice del campo
177
                                                                        SelectableDataSource sds = pt.getModelo();
178
                                                                        String fieldName = (String) firstTableField.getSelected();
179
                                                                        int fieldIndex = sds.getFieldIndexByName(fieldName);
180
                                                                        int type = sds.getFieldType(fieldIndex);
181

    
182
                                                                        secondTableField.setModel(new FieldSelectionModel(
183
                                                                                        ((ProjectTable) targetTable.getSelected()).getModelo(),
184
                                                                                        "Selecciona el campo sobre el que hacer el join",
185
                                                                                        type));
186
                                                                } catch (SelectionException e) {
187
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
188
                                                                                e);
189
                                                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
190
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
191
                                                                                e);
192
                                                                }
193
                                                        }
194
                                                }
195

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

    
211
                        try {
212
                                final ObjectSelectionStep sourceTable = new ObjectSelectionStep();
213
                                sourceTable.setModel(new TableSelectionModel(pts,
214
                                                "Selecciona la tabla origen"));
215

    
216
                                final ObjectSelectionStep targetTable = new ObjectSelectionStep();
217
                                targetTable.setModel(new TableSelectionModel(pts,
218
                                                "Selecciona la tabla a enlazar"));
219

    
220
                                final ObjectSelectionStep firstTableField = new ObjectSelectionStep();
221
                                final ObjectSelectionStep secondTableField = new ObjectSelectionStep();
222
                                final AndamiWizard wiz = new AndamiWizard();
223

    
224
                                wiz.addStep(sourceTable);
225
                                wiz.addStep(firstTableField);
226
                                wiz.addStep(targetTable);
227
                                wiz.addStep(secondTableField);
228

    
229
                                wiz.addWizardListener(new WizardListener() {
230
                                                public void cancel(WizardEvent w) {
231
                                                        PluginServices.getMDIManager().closeView(wiz);
232
                                                }
233

    
234
                                                public void finished(WizardEvent w) {
235
                                                        PluginServices.getMDIManager().closeView(wiz);
236

    
237
                                                        ProjectTable sourceProjectTable = (ProjectTable) sourceTable.getSelected();
238
                                                        SelectableDataSource sds1 = sourceProjectTable.getModelo();
239
                                                        String tableName1 = sds1.getName();
240

    
241
                                                        ProjectTable targetProjectTable = (ProjectTable) targetTable.getSelected();
242
                                                        SelectableDataSource sds2 = targetProjectTable.getModelo();
243

    
244
                                                        String tableName2 = sds2.getName();
245

    
246
                                                        String field1 = (String) firstTableField.getSelected();
247
                                                        String field2 = (String) secondTableField.getSelected();
248
                                                        sourceProjectTable.setLinkTable(sds2.getName(),field1,field2);
249
                                                        ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject().setLinkTable();
250
                                                }
251

    
252
                                                public void next(WizardEvent w) {
253
                                                        WizardControl wiz = w.wizard;
254
                                                        wiz.enableBack(true);
255
                                                        wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
256

    
257
                                                        if (w.currentStep == 1) {
258
                                                                ProjectTable pt = (ProjectTable) sourceTable.getSelected();
259

    
260
                                                                try {
261
                                                                        firstTableField.setModel(new FieldSelectionModel(
262
                                                                                        pt.getModelo(),
263
                                                                                        "Selecciona el campo sobre el que hacer el Enlace",
264
                                                                                        -1));
265
                                                                } catch (SelectionException e) {
266
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
267
                                                                                e);
268
                                                                }
269
                                                        } else if (w.currentStep == 3) {
270
                                                                try {
271
                                                                        //tabla
272
                                                                        ProjectTable pt = (ProjectTable) sourceTable.getSelected();
273

    
274
                                                                        //?ndice del campo
275
                                                                        SelectableDataSource sds = pt.getModelo();
276
                                                                        String fieldName = (String) firstTableField.getSelected();
277
                                                                        int fieldIndex = sds.getFieldIndexByName(fieldName);
278
                                                                        int type = sds.getFieldType(fieldIndex);
279

    
280
                                                                        secondTableField.setModel(new FieldSelectionModel(
281
                                                                                        ((ProjectTable) targetTable.getSelected()).getModelo(),
282
                                                                                        "Selecciona el campo sobre el que hacer el Enlace",
283
                                                                                        type));
284
                                                                } catch (SelectionException e) {
285
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
286
                                                                                e);
287
                                                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
288
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
289
                                                                                e);
290
                                                                }
291
                                                        }
292
                                                }
293

    
294
                                                public void back(WizardEvent w) {
295
                                                        WizardControl wiz = w.wizard;
296
                                                        wiz.enableBack(true);
297
                                                        wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
298
                                                }
299
                                        });
300
                                PluginServices.getMDIManager().addView(wiz);
301
                        } catch (SelectionException e) {
302
                                NotificationManager.addError("Error abriendo el asistente", e);
303
                        }
304
                }
305
        }
306

    
307
        /**
308
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
309
         */
310
        public void newSet(String expression) {
311
                long[] sel = doSet(expression);
312

    
313
                if (sel == null) {
314
                        throw new RuntimeException("Not a 'where' clause?");
315
                }
316

    
317
                FBitSet selection = new FBitSet();
318

    
319
                for (int i = 0; i < sel.length; i++) {
320
                        selection.set((int) sel[i]);
321
                }
322

    
323
                dataSource.clearSelection();
324
                dataSource.setSelection(selection);
325
        }
326

    
327
        /**
328
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
329
         */
330
        private long[] doSet(String expression) {
331
                try {
332
                        DataSource ds = LayerFactory.getDataSourceFactory().executeSQL(expression,
333
                                        DataSourceFactory.CLOSING_MANUAL_MODE);
334

    
335
                        return ds.getWhereFilter();
336
                } catch (DriverLoadException e) {
337
                        NotificationManager.addError("Error cargando el driver", e);
338
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
339
                        NotificationManager.addError("Error accediendo al driver", e);
340
                } catch (ParseException e) {
341
                        NotificationManager.addError("Parse error", e);
342
                } catch (SemanticException e) {
343
                        NotificationManager.addError(e.getMessage(), e);
344
                } catch (IOException e) {
345
                        NotificationManager.addError("GDBMS internal error", e);
346
                }
347

    
348
                return null;
349
        }
350

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

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

    
361
                FBitSet selection = new FBitSet();
362

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

    
367
                FBitSet fbs = dataSource.getSelection();
368
                fbs.or(selection);
369
                dataSource.setSelection(fbs);
370
        }
371

    
372
        /**
373
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#fromSet(java.lang.String)
374
         */
375
        public void fromSet(String expression) {
376
                long[] sel = doSet(expression);
377

    
378
                if (sel == null) {
379
                        throw new RuntimeException("Not a 'where' clause?");
380
                }
381

    
382
                FBitSet selection = new FBitSet();
383

    
384
                for (int i = 0; i < sel.length; i++) {
385
                        selection.set((int) sel[i]);
386
                }
387

    
388
                FBitSet fbs = dataSource.getSelection();
389
                fbs.and(selection);
390
                dataSource.setSelection(fbs);
391
        }
392

    
393
        /**
394
         * @see com.iver.mdiApp.plugins.Extension#isVisible()
395
         */
396
        public boolean isVisible() {
397
                View v = PluginServices.getMDIManager().getActiveView();
398

    
399
                if (v == null) {
400
                        return false;
401
                }
402

    
403
                if (v.getClass() == Table.class) {
404
                        return true;
405
                } /*else {
406
                        if (v instanceof com.iver.cit.gvsig.gui.View) {
407
                                com.iver.cit.gvsig.gui.View view = (com.iver.cit.gvsig.gui.View) v;
408
                                ProjectView pv = view.getModel();
409
                                FLayer[] seleccionadas = pv.getMapContext().getLayers()
410
                                                                                   .getActives();
411

412
                                if (seleccionadas.length == 1) {
413
                                        if (seleccionadas[0] instanceof AlphanumericData) {
414
                                                return true;
415
                                        }
416
                                }
417
                        }
418
*/
419
                        return false;
420
                //}
421
        }
422

    
423
        /**
424
         * @see com.iver.andami.plugins.Extension#inicializar()
425
         */
426
        public void inicializar() {
427
        }
428

    
429
        /**
430
         * @see com.iver.andami.plugins.Extension#isEnabled()
431
         */
432
        public boolean isEnabled() {
433
                return true;
434
        }
435

    
436
        
437
}