Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extGeoDB / src / com / prodevelop / cit / gvsig / vectorialdb / wizard / WizardVectorialDB.java @ 29199

History | View | Annotate | Download (10.7 KB)

1 11971 caballero
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Prodevelop 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
 *   Prodevelop Integraci?n de Tecnolog?as SL
34
 *   Conde Salvatierra de ?lava , 34-10
35
 *   46004 Valencia
36
 *   Spain
37
 *
38
 *   +34 963 510 612
39
 *   +34 963 510 968
40
 *   gis@prodevelop.es
41
 *   http://www.prodevelop.es
42
 */
43
package com.prodevelop.cit.gvsig.vectorialdb.wizard;
44
45 29199 jmvivo
import java.awt.Window;
46 12826 jaume
import java.util.ArrayList;
47 27917 jmvivo
import java.util.Iterator;
48 24760 jmvivo
import java.util.List;
49 11971 caballero
50 27953 jmvivo
import org.cresques.cts.IProjection;
51 29199 jmvivo
import org.gvsig.AppGvSigLocator;
52
import org.gvsig.AppGvSigManager;
53
import org.gvsig.PrepareContext;
54
import org.gvsig.PrepareContextView;
55 24759 jmvivo
import org.gvsig.fmap.dal.DALLocator;
56
import org.gvsig.fmap.dal.DataManager;
57
import org.gvsig.fmap.dal.DataStoreParameters;
58 27917 jmvivo
import org.gvsig.fmap.dal.exception.DataException;
59 28073 jmvivo
import org.gvsig.fmap.dal.exception.ReadException;
60 29199 jmvivo
import org.gvsig.fmap.dal.feature.FeatureStore;
61 27917 jmvivo
import org.gvsig.fmap.dal.serverexplorer.db.DBServerExplorer;
62 27932 jmvivo
import org.gvsig.fmap.dal.store.db.DBStoreParameters;
63 22035 vcaballero
import org.gvsig.fmap.geom.primitive.Envelope;
64 27953 jmvivo
import org.gvsig.fmap.mapcontext.MapContext;
65
import org.gvsig.fmap.mapcontext.layers.CancelationException;
66
import org.gvsig.fmap.mapcontext.layers.FLayer;
67
import org.gvsig.fmap.mapcontext.layers.FLayers;
68
import org.gvsig.fmap.mapcontext.layers.LayerFactory;
69
import org.gvsig.fmap.mapcontrol.MapControl;
70 29199 jmvivo
import org.slf4j.Logger;
71
import org.slf4j.LoggerFactory;
72 12826 jaume
73 11971 caballero
import com.iver.andami.PluginServices;
74
import com.iver.andami.messages.NotificationManager;
75
import com.iver.andami.ui.mdiManager.IWindow;
76 15684 vcaballero
import com.iver.cit.gvsig.SingleVectorialDBConnectionExtension;
77 11971 caballero
import com.iver.cit.gvsig.project.documents.view.gui.View;
78
79
80
/**
81 14366 jldominguez
 * Driver-independent GeoDB wizard. Queries the drivers to fill GUI controls.
82 11971 caballero
 * Multi-table selection available.
83
 *
84
 * @author jldominguez
85
 *
86
 */
87 29199 jmvivo
public class WizardVectorialDB extends WizardDB {
88
    private static Logger logger = LoggerFactory
89
                        .getLogger(WizardVectorialDB.class.getName());
90 15684 vcaballero
91 14366 jldominguez
    private static final String GEODB_WIZARD_TAB_NAME = "GeoDB";
92 29199 jmvivo
        private View view = null;
93 11971 caballero
94 29199 jmvivo
        private PrepareContextView prepareContext;
95 11971 caballero
96
    /**
97
     * This method initializes this
98
     *
99
     * @return void
100
     */
101 29199 jmvivo
    protected void initialize() {
102
                super.initialize();
103 14366 jldominguez
        setTabName(GEODB_WIZARD_TAB_NAME);
104 11971 caballero
        setLayout(null);
105
        setSize(512, 478);
106
107
        IWindow iw = PluginServices.getMDIManager().getActiveWindow();
108
109
        if (iw == null) {
110
            return;
111
        }
112
113 22653 vcaballero
        if ((iw instanceof View)) {
114
                 view = (View) iw;
115
             setMapCtrl(view.getMapControl());
116 11971 caballero
        }
117 29199 jmvivo
    }
118 11971 caballero
119
120 29199 jmvivo
        protected TablesListItem createTabeListItem(DBServerExplorer dbExplorer,
121
                        DBStoreParameters param) {
122
                return new TablesListItemVectorial(dbExplorer, param, getMapCtrl(),
123
                                this);
124
        }
125 11971 caballero
126 29199 jmvivo
        public void setSettingsPanels(TablesListItem actTable) {
127
                super.setSettingsPanels(actTable);
128
        }
129
130
        protected UserTableSettingsPanel createSettingsPanel(
131
                        TablesListItem actTable) {
132
                if (actTable == null) {
133
                        return new UserTableSettingsVectorialPanel(null, null, "",
134
                                        getMapCtrl(), true, this, null, null);
135 22142 jmvivo
                }
136 29199 jmvivo
                String abrev = null;
137
                if (getMapCtrl() != null) {
138
                        abrev = getMapCtrl().getViewPort().getProjection().getAbrev();
139
                }
140 22035 vcaballero
141 29199 jmvivo
                return ((TablesListItemVectorial) actTable)
142
                                .getUserTableSettingsPanel(abrev);
143
        }
144 11971 caballero
145
146 29199 jmvivo
        public DataStoreParameters[] getParameters() {
147
                try {
148
                        TablesListItem[] selected = getSelectedTables();
149
                        int count = selected.length;
150
                        DBStoreParameters[] dbParameters = new DBStoreParameters[count];
151
                        String strEPSG = null;
152
                        if (getMapCtrl() != null) {
153
                                strEPSG = getMapCtrl().getViewPort().getProjection().getAbrev();
154
                        }
155 11971 caballero
156 29199 jmvivo
                        for (int i = 0; i < count; i++) {
157
                                TablesListItemVectorial item = (TablesListItemVectorial) selected[i];
158 27953 jmvivo
159 29199 jmvivo
                                dbParameters[i] = getParameterForTable(item);
160
                        }
161
162
                        return dbParameters;// layerArrayToGroup(all_layers, groupName);
163
                } catch (Exception e) {
164
                        logger.error("While creating jdbc layer: " + e.getMessage(), e);
165
                        NotificationManager.addError("Error al cargar la capa: "
166
                                        + e.getMessage(), e);
167 27953 jmvivo
                }
168 29199 jmvivo
169
                return null;
170
        }
171
172
        public void execute() {
173
                SingleVectorialDBConnectionExtension.saveAllToPersistence();
174
175
                MapControl mapControl = this.getMapCtrl();
176
                IProjection proj = null;
177
                TablesListItem[] tables = getSelectedTables();
178
179
180 28073 jmvivo
                List all_layers = new ArrayList();
181 27953 jmvivo
                String strEPSG = mapControl.getViewPort().getProjection().getAbrev();
182
                LayerFactory layerFactory = LayerFactory.getInstance();
183
                String groupName = null;
184 28073 jmvivo
                Envelope env = null;
185 29199 jmvivo
                DBStoreParameters parameter;
186
                TablesListItem table;
187
                FeatureStore store, storeToAdd;
188 28073 jmvivo
189 29199 jmvivo
                DataManager man = DALLocator.getDataManager();
190
191
                AppGvSigManager appGvSIGMan = AppGvSigLocator.getAppGvSigManager();
192
                PrepareContext context = this.getPrepareDataStoreContext();
193
194
195 28073 jmvivo
                FLayer layer;
196 29199 jmvivo
                for (int i = 0; i < tables.length; i++) {
197
                        table = tables[i];
198
                        UserTableSettingsVectorialPanel userTableSettingsPanel = (UserTableSettingsVectorialPanel) table
199
                                        .getUserTableSettingsPanel();
200
                        parameter = getParameterForTable(table);
201 27953 jmvivo
                        if (i == 0) {
202 29199 jmvivo
                                groupName = parameter.getDBName() + " (" + parameter.getHost()
203
                                                + ")";
204 27953 jmvivo
                        }
205
                        try {
206 29199 jmvivo
                                parameter = (DBStoreParameters) appGvSIGMan
207
                                                .prepareOpenDataStoreParameters(parameter, context);
208
209
                        } catch (Exception e2) {
210
                                NotificationManager.addError(e2);
211
                                continue;
212
                        }
213
214
                        try {
215
                                store = (FeatureStore) man.createStore(parameter);
216
                        } catch (Exception e) {
217 27953 jmvivo
                                NotificationManager.addError(e);
218 29199 jmvivo
                                return;
219
                        }
220
221
                        try {
222
                                storeToAdd = (FeatureStore) appGvSIGMan.pepareOpenDataSource(
223
                                                store, context);
224
                        } catch (Exception e) {
225 28073 jmvivo
                                NotificationManager.addError(e);
226 29199 jmvivo
                                try {
227
                                        store.dispose();
228
                                } catch (DataException e1) {
229
                                        logger
230
                                                        .error(
231
                                                                        "Exception when disposing a store after prepareStore exception",
232
                                                                        e1);
233
                                }
234
                                continue;
235 27953 jmvivo
                        }
236 29199 jmvivo
237
                        try {
238
239
                                layer = layerFactory.createLayer(userTableSettingsPanel
240
                                                .getUserLayerName(), storeToAdd);
241
                                all_layers.add(layer);
242
                                if (env == null) {
243
                                        env = layer.getFullEnvelope();
244
                                } else {
245
                                        env.add(layer.getFullEnvelope());
246
                                }
247
                        } catch (Exception e) {
248
                                try {
249
                                        storeToAdd.dispose();
250
                                } catch (DataException e1) {
251
                                        logger
252
                                                        .error(
253
                                                                        "Exception when disposing a store after createLayer exception",
254
                                                                        e1);
255
                                }
256
257
                                NotificationManager.addError(e);
258
                        }
259 27953 jmvivo
                }
260
261
                MapContext mc = mapControl.getMapContext();
262
                FLayers root = mapControl.getMapContext().getLayers();
263 28073 jmvivo
                if (all_layers.size() > 1) {
264 27953 jmvivo
                        FLayers group = new FLayers();// (mc,root);
265
                        group.setMapContext(mc);
266
                        group.setParentLayer(root);
267
                        group.setName(groupName);
268
269 28073 jmvivo
                        Iterator iter = all_layers.iterator();
270
                        while (iter.hasNext()) {
271
                                group.addLayer((FLayer) iter.next());
272 27953 jmvivo
                        }
273
274
                        if ((group != null) && !(group.isOk())) {
275
                                // if the layer is not okay (it has errors) process them
276
                                processErrorsOfLayer(group, mapControl);
277
                        }
278
279
                        if (group != null) {
280
                                group.setVisible(true);
281
                                mapControl.getMapContext().beginAtomicEvent();
282
                                try {
283 28073 jmvivo
                                        // checkProjection(group, mapControl.getViewPort());
284
                                        try {
285
                                                mapControl.getMapContext().getLayers().addLayer(group);
286
                                        } catch (CancelationException e) {
287
                                                // TODO Auto-generated catch block
288
                                                e.printStackTrace();
289
                                        }
290
                                        if (mapControl.getViewPort().getExtent() == null) {
291
                                                mapControl.getViewPort().setEnvelope(env);
292
                                        }
293
                                } finally {
294
                                        mapControl.getMapContext().endAtomicEvent();
295 27953 jmvivo
                                }
296
                                return;
297
                        }
298 28073 jmvivo
                } else if (all_layers.size() == 1) {
299
                        layer = (FLayer) all_layers.get(0);
300
                        if (!(layer.isOk())) {
301 27953 jmvivo
                                // if the layer is not okay (it has errors) process them
302 28073 jmvivo
                                processErrorsOfLayer(layer, mapControl);
303 27953 jmvivo
                        }
304
305 28073 jmvivo
                        layer.setVisible(true);
306
                        mapControl.getMapContext().beginAtomicEvent();
307
                        // checkProjection(all_layers[0], mapControl.getViewPort());
308
                        try {
309 27953 jmvivo
                                try {
310 28073 jmvivo
                                        mapControl.getMapContext().getLayers().addLayer(layer);
311 27953 jmvivo
                                } catch (CancelationException e) {
312 28073 jmvivo
                                        return;
313 27953 jmvivo
                                }
314 28073 jmvivo
315
                                if (mapControl.getViewPort().getExtent() == null) {
316
                                        try {
317
                                                mapControl.getViewPort().setEnvelope(
318
                                                                layer.getFullEnvelope());
319
                                        } catch (ReadException e) {
320
                                                NotificationManager.addError(e);
321
                                                return;
322
                                        }
323
                                }
324
                        } finally {
325
326 29199 jmvivo
                                mapControl.getMapContext().endAtomicEvent();
327 27953 jmvivo
                        }
328 28073 jmvivo
                        return;
329 27953 jmvivo
                }
330 29199 jmvivo
        }
331 11971 caballero
332 29199 jmvivo
        protected PrepareContext getPrepareDataStoreContext() {
333
                if (this.prepareContext == null) {
334
                        this.prepareContext = new PrepareContextView() {
335
                                public Window getOwnerWindow() {
336
                                        return null;
337
                                }
338 11971 caballero
339 29199 jmvivo
                                public MapControl getMapControl() {
340
                                        return WizardVectorialDB.this
341
                                                        .getMapCtrl();
342
                                }
343 11971 caballero
344 29199 jmvivo
                        };
345
                }
346
                return this.prepareContext;
347
        }
348 11971 caballero
349 29199 jmvivo
        protected DBStoreParameters getParameterForTable(TablesListItem table) {
350
                DBStoreParameters parameters = table.getParameters();
351 11971 caballero
352 29199 jmvivo
                String strEPSG = null;
353
                if (getMapCtrl() != null) {
354
                        strEPSG = getMapCtrl().getViewPort().getProjection().getAbrev();
355
                }
356 11971 caballero
357 29199 jmvivo
                UserTableSettingsVectorialPanel userTableSettingsPanel = (UserTableSettingsVectorialPanel) table
358
                                .getUserTableSettingsPanel();
359 11971 caballero
360 29199 jmvivo
                Envelope _wa = userTableSettingsPanel.getWorkingArea();
361 11971 caballero
362 29199 jmvivo
                String geomField = userTableSettingsPanel.getGeoFieldName();
363
                String fidField = userTableSettingsPanel.getIdFieldName();
364
                if (!(fidField.startsWith("{") && fidField.endsWith("}"))) {
365
                        parameters.setPkFields(new String[] { fidField });
366
                        fidField = null;
367
                }
368
                String[] fields = table.getUserSelectedFieldsPanel()
369
                                .getUserSelectedFields(fidField, geomField);
370 11971 caballero
371 29199 jmvivo
                if (userTableSettingsPanel.isSqlActive()) {
372
                        String whereClause = userTableSettingsPanel.getWhereClause();
373
                        parameters.setInitialFilter(whereClause);
374
                } else {
375
                        parameters.setInitialFilter("");
376 22142 jmvivo
                }
377 11971 caballero
378 29199 jmvivo
                parameters.setFields(fields);
379
                parameters.setDefaultGeometry(geomField);
380
                if (_wa != null) {
381
                        parameters.setWorkingArea(_wa);
382 27917 jmvivo
                }
383 11971 caballero
384 29199 jmvivo
                parameters.setSRSID(strEPSG);
385 11971 caballero
386 29199 jmvivo
                return parameters;
387 11971 caballero
388 22653 vcaballero
        }
389 27953 jmvivo
390
391 29199 jmvivo
}