Statistics
| Revision:

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

History | View | Annotate | Download (13.7 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 22477 vcaballero
import java.awt.Component;
44 25750 vcaballero
import java.awt.Dimension;
45
import java.util.ArrayList;
46 22477 vcaballero
import java.util.Iterator;
47 1830 fernando
48 22477 vcaballero
import javax.swing.JOptionPane;
49
50 25068 vcaballero
import org.gvsig.fmap.dal.DALLocator;
51 25750 vcaballero
import org.gvsig.fmap.dal.DataManager;
52
import org.gvsig.fmap.dal.DataTypes;
53 24759 jmvivo
import org.gvsig.fmap.dal.exception.DataException;
54 25750 vcaballero
import org.gvsig.fmap.dal.exception.ReadException;
55 24759 jmvivo
import org.gvsig.fmap.dal.feature.Feature;
56 25750 vcaballero
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
57 25089 jmvivo
import org.gvsig.fmap.dal.feature.FeatureQuery;
58
import org.gvsig.fmap.dal.feature.FeatureSelection;
59
import org.gvsig.fmap.dal.feature.FeatureSet;
60 24759 jmvivo
import org.gvsig.fmap.dal.feature.FeatureStore;
61 25774 jmvivo
import org.gvsig.fmap.dal.feature.transform.join.JoinTransform;
62 25750 vcaballero
import org.gvsig.project.document.table.FeatureTableDocument;
63
import org.gvsig.project.document.table.FeatureTableDocumentFactory;
64 25068 vcaballero
import org.gvsig.project.document.table.gui.FeatureTableDocumentPanel;
65 26325 jmvivo
import org.gvsig.project.document.table.gui.JoinWizardController;
66 24759 jmvivo
import org.opengis.feature.FeatureCollection;
67 20994 jmvivo
68 855 fernando
import com.iver.andami.PluginServices;
69 25750 vcaballero
import com.iver.andami.messages.NotificationManager;
70 596 fernando
import com.iver.andami.plugins.Extension;
71 6877 cesar
import com.iver.andami.ui.mdiManager.IWindow;
72 855 fernando
import com.iver.cit.gvsig.gui.filter.ExpressionListener;
73 26325 jmvivo
import com.iver.cit.gvsig.project.documents.gui.AndamiWizard;
74
import com.iver.cit.gvsig.project.documents.gui.ObjectSelectionStep;
75 25750 vcaballero
import com.iver.cit.gvsig.project.documents.table.FieldSelectionModel;
76
import com.iver.cit.gvsig.project.documents.table.TableSelectionModel;
77
import com.iver.utiles.swing.objectSelection.SelectionException;
78
import com.iver.utiles.swing.wizard.WizardControl;
79
import com.iver.utiles.swing.wizard.WizardEvent;
80
import com.iver.utiles.swing.wizard.WizardListener;
81 312 fernando
82
83
/**
84 1219 vcaballero
 * Extensi?n que controla las operaciones realizadas sobre las tablas.
85 312 fernando
 *
86
 * @author Fernando Gonz?lez Cort?s
87
 */
88 5005 jorpiell
public class TableOperations extends Extension implements ExpressionListener {
89 21665 vcaballero
        private FeatureStore featureStore = null;
90 312 fernando
91 1219 vcaballero
        /**
92 5005 jorpiell
         * @see com.iver.mdiApp.plugins.IExtension#updateUI(java.lang.String)
93 1219 vcaballero
         */
94
        public void execute(String actionCommand) {
95 25750 vcaballero
                                ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
96
                                com.iver.cit.gvsig.project.Project project=pe.getProject();
97
                                FeatureTableDocument[] pts = project.getDocumentsByType(FeatureTableDocumentFactory.registerName)
98
                                        .toArray(new FeatureTableDocument[0]);
99
                                if ("JOIN".equals(actionCommand)) {
100 26325 jmvivo
                                        JoinWizardController wizardController = new JoinWizardController(
101
                                        this);
102
                        wizardController.runWizard(pts);
103 25750 vcaballero
                }else if ("LINK".equals(actionCommand)) {
104
                        try {
105
                                final ObjectSelectionStep sourceTable = new ObjectSelectionStep();
106
                                sourceTable.setModel(new TableSelectionModel(pts,
107
                                                PluginServices.getText(this, "seleccione_tabla_origen")));
108
109
                                final ObjectSelectionStep targetTable = new ObjectSelectionStep();
110
                                targetTable.setModel(new TableSelectionModel(pts,
111
                                        PluginServices.getText(this, "seleccione_tabla_a_enlazar")));
112
113
                                final ObjectSelectionStep firstTableField = new ObjectSelectionStep();
114
                                final ObjectSelectionStep secondTableField = new ObjectSelectionStep();
115
                                final AndamiWizard wiz = new AndamiWizard(PluginServices.getText(this, "back"), PluginServices.getText(this, "next"), PluginServices.getText(this, "finish"), PluginServices.getText(this, "cancel"));
116
                                wiz.setSize(new Dimension(450,200));
117
                                wiz.addStep(sourceTable);
118
                                wiz.addStep(firstTableField);
119
                                wiz.addStep(targetTable);
120
                                wiz.addStep(secondTableField);
121
122
                                wiz.addWizardListener(new WizardListener() {
123
                                                public void cancel(WizardEvent w) {
124
                                                        PluginServices.getMDIManager().closeWindow(wiz);
125
                                                }
126
127
                                                public void finished(WizardEvent w) {
128
                                                        PluginServices.getMDIManager().closeWindow(wiz);
129
130
                                                        FeatureTableDocument sourceProjectTable = (FeatureTableDocument) sourceTable.getSelected();
131
                                                        FeatureStore sds1 = sourceProjectTable.getStore();
132
133
                                                        FeatureTableDocument targetProjectTable = (FeatureTableDocument) targetTable.getSelected();
134
                                                        FeatureStore sds2 = targetProjectTable.getStore();
135
136
                                                        String field1 = (String) firstTableField.getSelected();
137
                                                        String field2 = (String) secondTableField.getSelected();
138
                                                        sourceProjectTable.setLinkTable(sds2.getName(),field1,field2);
139
                                                        ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject().setLinkTable();
140
141
                                                }
142
143
                                                public void next(WizardEvent w) {
144
                                                        WizardControl wiz = w.wizard;
145
                                                        wiz.enableBack(true);
146
                                                        wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
147
148
                                                        if (w.currentStep == 1) {
149
                                                                FeatureTableDocument pt = (FeatureTableDocument) sourceTable.getSelected();
150
151
                                                                try {
152
                                                                        firstTableField.setModel(new FieldSelectionModel(
153
                                                                                        pt.getStore(),
154
                                                                                        PluginServices.getText(this, "seleccione_campo_enlace"),
155
                                                                                        DataTypes.STRING));
156
                                                                } catch (SelectionException e) {
157
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
158
                                                                                e);
159
                                                                }
160
                                                        } else if (w.currentStep == 3) {
161
                                                                try {
162
                                                                        //tabla
163
                                                                        FeatureTableDocument pt = (FeatureTableDocument) sourceTable.getSelected();
164
165
                                                                        //?ndice del campo
166
                                                                        FeatureStore fs = pt.getStore();
167
                                                                        String fieldName = (String) firstTableField.getSelected();
168
                                                                        int type = ((FeatureAttributeDescriptor)fs.getDefaultFeatureType().get(fieldName)).getDataType();
169
170
                                                                        secondTableField.setModel(new FieldSelectionModel(
171
                                                                                        ((FeatureTableDocument) targetTable
172
                                                                                                                .getSelected())
173
                                                                                                                .getStore(),
174
                                                                                        PluginServices.getText(this, "seleccione_campo_enlace"),
175
                                                                                        type));
176
                                                                } catch (SelectionException e) {
177
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
178
                                                                                e);
179
                                                                } catch (DataException e) {
180
                                                                        NotificationManager.addError("Error obteniendo los campos de la tabla",
181
                                                                                        e);
182
                                                                }
183
                                                        }
184
                                                }
185
186
                                                public void back(WizardEvent w) {
187
                                                        WizardControl wiz = w.wizard;
188
                                                        wiz.enableBack(true);
189
                                                        wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
190
                                                }
191
                                        });
192
                                project.setModified(true);
193
                                PluginServices.getMDIManager().addWindow(wiz);
194
                        } catch (SelectionException e) {
195
                                NotificationManager.addError("Error abriendo el asistente", e);
196
                        }
197
              }
198 1219 vcaballero
        }
199 312 fernando
200 1219 vcaballero
        /**
201 855 fernando
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
202 312 fernando
         */
203 22353 jmvivo
        public void newSet(String expression) throws DataException {
204 22477 vcaballero
                // By Pablo: if no filter expression -> no element selected
205 9494 ppiqueras
                if (! this.filterExpressionFromWhereIsEmpty(expression)) {
206 22477 vcaballero
                        try {
207 25089 jmvivo
                                FeatureSet set = doSet(expression);
208 9532 caballero
209 25089 jmvivo
                                if (set == null) {
210 22477 vcaballero
                                        throw new RuntimeException("Not a 'where' clause?");
211
                                }
212 25089 jmvivo
                                FeatureSelection newSel =featureStore.createFeatureSelection();
213
                                newSel.select(set);
214
                                featureStore.setSelection(newSel);
215
                                set.dispose();
216 22477 vcaballero
                        }catch(Exception e){
217
                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), "Asegurate de que la consulta es correcta.");
218 9494 ppiqueras
                        }
219 1219 vcaballero
                }
220 9494 ppiqueras
                else {
221
                        // By Pablo: if no expression -> no element selected
222 25089 jmvivo
                        featureStore.getFeatureSelection().deselectAll();
223 884 fernando
                }
224 312 fernando
        }
225 1219 vcaballero
226 312 fernando
        /**
227 855 fernando
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
228 312 fernando
         */
229 25089 jmvivo
        private FeatureSet doSet(String expression) throws DataException {
230
                FeatureQuery query = featureStore.createFeatureQuery();
231
                query
232
                                .setFilter(DALLocator.getDataManager().createExpresion(
233
                                                expression));
234
                return featureStore.getFeatureSet(query);
235 312 fernando
        }
236 1219 vcaballero
        /**
237
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#addToSet(java.lang.String)
238
         */
239 22353 jmvivo
        public void addToSet(String expression) throws DataException {
240 22477 vcaballero
                // By Pablo: if no filter expression -> don't add more elements to set
241 9494 ppiqueras
                if (! this.filterExpressionFromWhereIsEmpty(expression)) {
242 25089 jmvivo
                        FeatureSet set = null;
243
                        set = doSet(expression);
244 9532 caballero
245 25089 jmvivo
                        featureStore.getFeatureSelection().select(set);
246
                        set.dispose();
247 1219 vcaballero
                }
248
        }
249 312 fernando
250 1219 vcaballero
        /**
251
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#fromSet(java.lang.String)
252
         */
253 22353 jmvivo
        public void fromSet(String expression) throws DataException {
254 22477 vcaballero
                // By Pablo: if no filter expression -> no element selected
255
                        if (! this.filterExpressionFromWhereIsEmpty(expression)) {
256 9532 caballero
257 25089 jmvivo
                                FeatureSet set = null;
258
                        set = doSet(expression);
259 9532 caballero
260 25089 jmvivo
                                if (set == null) {
261 22477 vcaballero
                                        throw new RuntimeException("Not a 'where' clause?");
262
                                }
263
                                FeatureCollection oldSelection=(FeatureCollection)featureStore.getSelection();
264 9532 caballero
265 25089 jmvivo
                                FeatureSelection newSelection = featureStore
266
                                        .createFeatureSelection();
267
                                Iterator<Feature> iterator = set.iterator();
268 22477 vcaballero
                                while (iterator.hasNext()) {
269 25089 jmvivo
                                        Feature feature = iterator.next();
270 22477 vcaballero
                                        if (oldSelection.contains(feature)){
271 25089 jmvivo
                                                newSelection.select(feature);
272 22477 vcaballero
                                        }
273
                                }
274 25089 jmvivo
                                featureStore.setSelection(newSelection);
275
                                set.dispose();
276 22477 vcaballero
                        } else {
277
                                        // By Pablo: if no expression -> no element selected
278
                                        featureStore.setSelection(featureStore.createSelection());
279
                                }
280 1219 vcaballero
        }
281 9532 caballero
282 9494 ppiqueras
        /**
283
         * Returns true if the WHERE subconsultation of the filterExpression is empty ("")
284 9532 caballero
         *
285 9494 ppiqueras
         * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
286
         * @param expression An string
287 9532 caballero
         * @return A boolean value
288 9494 ppiqueras
         */
289
        private boolean filterExpressionFromWhereIsEmpty(String expression) {
290
                String subExpression = expression.trim();
291 9532 caballero
                int pos;
292
293 9494 ppiqueras
                // Remove last ';' if exists
294 22931 jmvivo
                if (subExpression.charAt(subExpression.length() -1) == ';') {
295 9494 ppiqueras
                        subExpression = subExpression.substring(0, subExpression.length() -1).trim();
296 22931 jmvivo
                }
297 9532 caballero
298 9494 ppiqueras
                // If there is no 'where' clause
299 22931 jmvivo
                if ((pos = subExpression.indexOf("where")) == -1) {
300 9494 ppiqueras
                        return false;
301 22931 jmvivo
                }
302 9532 caballero
303 9494 ppiqueras
                // If there is no subexpression in the WHERE clause -> true
304
                subExpression = subExpression.substring(pos + 5, subExpression.length()).trim(); // + 5 is the length of 'where'
305 22931 jmvivo
                if ( subExpression.length() == 0 ) {
306 9494 ppiqueras
                        return true;
307 22931 jmvivo
                } else {
308 9494 ppiqueras
                        return false;
309 22931 jmvivo
                }
310 9494 ppiqueras
        }
311 312 fernando
312
        /**
313 5005 jorpiell
         * @see com.iver.mdiApp.plugins.IExtension#isVisible()
314 312 fernando
         */
315
        public boolean isVisible() {
316 6880 cesar
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
317 312 fernando
318 855 fernando
                if (v == null) {
319 312 fernando
                        return false;
320
                }
321 1219 vcaballero
322 25068 vcaballero
                if (v instanceof FeatureTableDocumentPanel) {
323 312 fernando
                        return true;
324 2183 fernando
                } /*else {
325 1219 vcaballero
                        if (v instanceof com.iver.cit.gvsig.gui.View) {
326 855 fernando
                                com.iver.cit.gvsig.gui.View view = (com.iver.cit.gvsig.gui.View) v;
327
                                ProjectView pv = view.getModel();
328 1219 vcaballero
                                FLayer[] seleccionadas = pv.getMapContext().getLayers()
329
                                                                                   .getActives();
330

331
                                if (seleccionadas.length == 1) {
332
                                        if (seleccionadas[0] instanceof AlphanumericData) {
333 855 fernando
                                                return true;
334
                                        }
335 312 fernando
                                }
336
                        }
337 2183 fernando
*/
338 312 fernando
                        return false;
339 2183 fernando
                //}
340 312 fernando
        }
341
342 596 fernando
        /**
343 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#initialize()
344 596 fernando
         */
345 5005 jorpiell
        public void initialize() {
346 14821 jmvivo
                registerIcons();
347 25089 jmvivo
//                FIXME
348
//                org.gvsig.fmap.data.feature.joinstore.Register.selfRegister();
349 22477 vcaballero
350
351 596 fernando
        }
352
353 14821 jmvivo
        private void registerIcons(){
354 15647 jmvivo
                PluginServices.getIconTheme().registerDefault(
355 14821 jmvivo
                                "table-join",
356
                                this.getClass().getClassLoader().getResource("images/tablejoin.png")
357
                        );
358
359 15647 jmvivo
                PluginServices.getIconTheme().registerDefault(
360 14821 jmvivo
                                "table-link",
361
                                this.getClass().getClassLoader().getResource("images/tablelink.png")
362
                        );
363
        }
364
365 596 fernando
        /**
366 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#isEnabled()
367 596 fernando
         */
368
        public boolean isEnabled() {
369 855 fernando
                return true;
370 596 fernando
        }
371 26325 jmvivo
        public void execJoin(FeatureTableDocument sourceProjectTable,
372
                        String field1, String prefix1,
373
                        FeatureTableDocument targetProjectTable, String field2,
374
                        String prefix2) {
375
376
                FeatureStore fs1 = sourceProjectTable.getStore();
377
378
                FeatureStore fs2 = targetProjectTable.getStore();
379
380
                if (fs1 == fs2) {
381
                        NotificationManager
382
                                        .addInfo("no_es_posible_aplicar_join_sobre_la_misma_fuente");
383 26241 vcaballero
                }
384 2183 fernando
385 26241 vcaballero
                try {
386 26325 jmvivo
                        DataManager dm = DALLocator.getDataManager();
387
                        JoinTransform jt = new JoinTransform();
388
                        ArrayList<String> fields = new ArrayList<String>();
389
                        Iterator iterator2 = fs2.getDefaultFeatureType().iterator();
390
                        while (iterator2.hasNext()) {
391
                                FeatureAttributeDescriptor descriptor = (FeatureAttributeDescriptor) iterator2
392
                                                .next();
393
                                String name = descriptor.getName();
394
                                if (!name.equals(field2)) {
395
                                        fields.add(name);
396
                                }
397
                        }
398
                        jt.initialize(fs1, fs2, field1, field2, prefix1, prefix2, fields
399
                                        .toArray(new String[0]));
400
                        fs1.getTransforms().add(jt);
401
                        // featureStore=fs1;
402
403 26241 vcaballero
                } catch (ReadException e) {
404 26325 jmvivo
                        NotificationManager.addError("Error leyendo del driver", e);
405
                } catch (DataException e) {
406
                        NotificationManager.addError(e);
407 26241 vcaballero
                }
408 26325 jmvivo
409 26241 vcaballero
        }
410
        /**
411
         * Ensure that field name only has 'safe' characters
412
         * (no spaces, special characters, etc).
413
         */
414
        public String sanitizeFieldName(String fieldName) {
415
                return fieldName.replaceAll("\\W", "_"); // replace any non-word character by an underscore
416
        }
417 3940 caballero
418 26241 vcaballero
419 312 fernando
}