svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.geodb.app / org.gvsig.geodb.app.mainplugin / src / main / java / org / gvsig / geodb / vectorialdb / wizard / WizardVectorialDB.java @ 43377
History | View | Annotate | Download (11.6 KB)
1 | 40557 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40557 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | 40557 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * of the License, or (at your option) any later version.
|
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | 40557 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | 40435 | jjdelcerro | *
|
21 | 40557 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | package org.gvsig.geodb.vectorialdb.wizard; |
||
25 | |||
26 | import java.awt.Window; |
||
27 | import java.util.ArrayList; |
||
28 | import java.util.List; |
||
29 | 43377 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
30 | 40435 | jjdelcerro | |
31 | import org.cresques.cts.IProjection; |
||
32 | import org.slf4j.Logger; |
||
33 | import org.slf4j.LoggerFactory; |
||
34 | |||
35 | import org.gvsig.andami.PluginServices; |
||
36 | import org.gvsig.andami.messages.NotificationManager; |
||
37 | import org.gvsig.andami.ui.mdiManager.IWindow; |
||
38 | import org.gvsig.app.prepareAction.PrepareContext; |
||
39 | import org.gvsig.app.prepareAction.PrepareContextView; |
||
40 | import org.gvsig.app.project.documents.view.gui.DefaultViewPanel; |
||
41 | import org.gvsig.fmap.dal.DALLocator; |
||
42 | import org.gvsig.fmap.dal.DataManager; |
||
43 | import org.gvsig.fmap.dal.DataStoreParameters; |
||
44 | import org.gvsig.fmap.dal.exception.DataException; |
||
45 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
46 | import org.gvsig.fmap.dal.serverexplorer.db.DBServerExplorer; |
||
47 | import org.gvsig.fmap.dal.store.db.DBStoreParameters; |
||
48 | import org.gvsig.fmap.geom.primitive.Envelope; |
||
49 | import org.gvsig.fmap.mapcontext.MapContext; |
||
50 | 43359 | jjdelcerro | import org.gvsig.fmap.mapcontext.MapContextLocator; |
51 | import org.gvsig.fmap.mapcontext.MapContextManager; |
||
52 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontext.layers.CancelationException; |
53 | import org.gvsig.fmap.mapcontext.layers.FLayer; |
||
54 | import org.gvsig.fmap.mapcontext.layers.FLayers; |
||
55 | import org.gvsig.fmap.mapcontrol.MapControl; |
||
56 | |||
57 | |||
58 | |||
59 | /**
|
||
60 | * Driver-independent GeoDB wizard. Queries the drivers to fill GUI controls.
|
||
61 | * Multi-table selection available.
|
||
62 | *
|
||
63 | * @author jldominguez
|
||
64 | *
|
||
65 | */
|
||
66 | public class WizardVectorialDB extends WizardDB { |
||
67 | private static final long serialVersionUID = -5002685263220038989L; |
||
68 | |||
69 | 43359 | jjdelcerro | private static final Logger logger = LoggerFactory.getLogger(WizardVectorialDB.class); |
70 | 40435 | jjdelcerro | |
71 | private static final String GEODB_WIZARD_TAB_NAME = "GeoDB"; |
||
72 | private DefaultViewPanel view = null; |
||
73 | |||
74 | private PrepareContextView prepareContext;
|
||
75 | |||
76 | /**
|
||
77 | * This method initializes this
|
||
78 | *
|
||
79 | */
|
||
80 | 43359 | jjdelcerro | @Override
|
81 | 40435 | jjdelcerro | protected void initialize() { |
82 | super.initialize();
|
||
83 | setTabName(GEODB_WIZARD_TAB_NAME); |
||
84 | setLayout(null);
|
||
85 | setSize(512, 478); |
||
86 | |||
87 | IWindow iw = PluginServices.getMDIManager().getActiveWindow(); |
||
88 | |||
89 | if (iw == null) { |
||
90 | return;
|
||
91 | } |
||
92 | |||
93 | if ((iw instanceof DefaultViewPanel)) { |
||
94 | view = (DefaultViewPanel) iw; |
||
95 | setMapCtrl(view.getMapControl()); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | 43359 | jjdelcerro | @Override
|
100 | 40435 | jjdelcerro | protected List getTableList(DBServerExplorer explorer) throws DataException { |
101 | return explorer.list(DBServerExplorer.MODE_GEOMETRY);
|
||
102 | } |
||
103 | |||
104 | 43359 | jjdelcerro | @Override
|
105 | 41258 | jjdelcerro | protected TablesListItem createTabeListItem(DBStoreParameters param) {
|
106 | return new TablesListItemVectorial(param, getMapCtrl(),this); |
||
107 | 40435 | jjdelcerro | } |
108 | |||
109 | 43359 | jjdelcerro | @Override
|
110 | 40435 | jjdelcerro | public void setSettingsPanels(TablesListItem actTable) { |
111 | super.setSettingsPanels(actTable);
|
||
112 | } |
||
113 | |||
114 | 43359 | jjdelcerro | @Override
|
115 | 40435 | jjdelcerro | protected UserTableSettingsPanel createSettingsPanel(
|
116 | TablesListItem actTable) { |
||
117 | if (actTable == null) { |
||
118 | return new UserTableSettingsVectorialPanel(null, null, "", |
||
119 | getMapCtrl(), true, this, null, null); |
||
120 | } |
||
121 | String abrev = null; |
||
122 | if (getMapCtrl() != null) { |
||
123 | abrev = getMapCtrl().getViewPort().getProjection().getAbrev(); |
||
124 | } |
||
125 | |||
126 | return ((TablesListItemVectorial) actTable)
|
||
127 | .getUserTableSettingsPanel(abrev); |
||
128 | } |
||
129 | |||
130 | 43359 | jjdelcerro | @Override
|
131 | 40435 | jjdelcerro | public DataStoreParameters[] getParameters() { |
132 | try {
|
||
133 | TablesListItem[] selected = getSelectedTables();
|
||
134 | int count = selected.length;
|
||
135 | DBStoreParameters[] dbParameters = new DBStoreParameters[count]; |
||
136 | String strEPSG = null; |
||
137 | if (getMapCtrl() != null) { |
||
138 | strEPSG = getMapCtrl().getViewPort().getProjection().getAbrev(); |
||
139 | } |
||
140 | |||
141 | for (int i = 0; i < count; i++) { |
||
142 | TablesListItemVectorial item = (TablesListItemVectorial) selected[i]; |
||
143 | |||
144 | dbParameters[i] = getParameterForTable(item); |
||
145 | } |
||
146 | |||
147 | 43359 | jjdelcerro | return dbParameters;
|
148 | 40435 | jjdelcerro | } catch (Exception e) { |
149 | 43359 | jjdelcerro | logger.warn("Error while creating jdbc layer: " + e.getMessage(), e);
|
150 | 40435 | jjdelcerro | } |
151 | |||
152 | return null; |
||
153 | } |
||
154 | |||
155 | 43359 | jjdelcerro | @Override
|
156 | 40435 | jjdelcerro | public void execute() { |
157 | MapControl mapControl = this.getMapCtrl();
|
||
158 | 43359 | jjdelcerro | MapContext mc = mapControl.getMapContext(); |
159 | MapContextManager mapContextManager = MapContextLocator.getMapContextManager(); |
||
160 | 40435 | jjdelcerro | TablesListItem[] tables = getSelectedTables();
|
161 | |||
162 | 43359 | jjdelcerro | List<FLayer> all_layers = new ArrayList(); |
163 | 40435 | jjdelcerro | String groupName = null; |
164 | 43359 | jjdelcerro | Boolean needCalculateEnvelope = mc.getViewPort().getEnvelope() == null; |
165 | 40435 | jjdelcerro | Envelope env = null;
|
166 | DBStoreParameters parameter; |
||
167 | TablesListItem table; |
||
168 | 43359 | jjdelcerro | FeatureStore store; |
169 | 40435 | jjdelcerro | |
170 | DataManager man = DALLocator.getDataManager(); |
||
171 | |||
172 | try {
|
||
173 | |||
174 | FLayer layer; |
||
175 | for (int i = 0; i < tables.length; i++) { |
||
176 | table = tables[i]; |
||
177 | 43359 | jjdelcerro | UserTableSettingsPanel userTableSettingsPanel = table.getUserTableSettingsPanel(); |
178 | 40435 | jjdelcerro | parameter = getParameterForTable(table); |
179 | if (i == 0) { |
||
180 | 43377 | jjdelcerro | String dbname = parameter.getDBName();
|
181 | if( StringUtils.isEmpty(dbname) ) {
|
||
182 | dbname = "Group";
|
||
183 | } |
||
184 | String host = parameter.getHost();
|
||
185 | if( StringUtils.isEmpty(host) ) {
|
||
186 | groupName = dbname; |
||
187 | } else {
|
||
188 | groupName = dbname + " (" + host + ")"; |
||
189 | } |
||
190 | 40435 | jjdelcerro | } |
191 | try {
|
||
192 | 40904 | jldominguez | store = (FeatureStore) man.openStore( |
193 | parameter.getDataStoreName(), |
||
194 | parameter); |
||
195 | 40435 | jjdelcerro | } catch (Exception e) { |
196 | 43359 | jjdelcerro | logger.warn("While opening DB store.", e);
|
197 | 40435 | jjdelcerro | return;
|
198 | } |
||
199 | try {
|
||
200 | |||
201 | 43359 | jjdelcerro | layer = mapContextManager.createLayer( |
202 | userTableSettingsPanel.getUserLayerName(), |
||
203 | store |
||
204 | ); |
||
205 | 40435 | jjdelcerro | all_layers.add(layer); |
206 | 43359 | jjdelcerro | if( needCalculateEnvelope ) {
|
207 | if (env == null) { |
||
208 | env = layer.getFullEnvelope(); |
||
209 | } else {
|
||
210 | env.add(layer.getFullEnvelope()); |
||
211 | } |
||
212 | 40435 | jjdelcerro | } |
213 | } catch (Exception e) { |
||
214 | 43359 | jjdelcerro | logger.warn("Can't create layer '"+userTableSettingsPanel.getUserLayerName()+"'.",e); |
215 | 40435 | jjdelcerro | } |
216 | } |
||
217 | |||
218 | if (all_layers.size() > 1) { |
||
219 | 43359 | jjdelcerro | FLayers group = new FLayers();
|
220 | 40435 | jjdelcerro | group.setMapContext(mc); |
221 | 43359 | jjdelcerro | group.setParentLayer(mc.getLayers()); |
222 | 40435 | jjdelcerro | group.setName(groupName); |
223 | |||
224 | 43359 | jjdelcerro | for( FLayer aLayer : all_layers) {
|
225 | group.addLayer(aLayer); |
||
226 | 40435 | jjdelcerro | } |
227 | 43359 | jjdelcerro | if ( !(group.isOk())) {
|
228 | 40435 | jjdelcerro | // if the layer is not okay (it has errors) process them
|
229 | processErrorsOfLayer(group, mapControl); |
||
230 | } |
||
231 | |||
232 | 43359 | jjdelcerro | group.setVisible(true);
|
233 | mc.beginAtomicEvent(); |
||
234 | try {
|
||
235 | 40435 | jjdelcerro | try {
|
236 | 43359 | jjdelcerro | mc.getLayers().addLayer(group); |
237 | group.dispose(); |
||
238 | } catch (CancelationException e) {
|
||
239 | logger.warn("Can't add group to mapcontext layers.",e);
|
||
240 | 40435 | jjdelcerro | } |
241 | 43359 | jjdelcerro | if ( needCalculateEnvelope && env!=null ) { |
242 | mapControl.getViewPort().setEnvelope(env); |
||
243 | } |
||
244 | } finally {
|
||
245 | mc.endAtomicEvent(); |
||
246 | 40435 | jjdelcerro | } |
247 | 43359 | jjdelcerro | return;
|
248 | } else {
|
||
249 | 40435 | jjdelcerro | if (all_layers.size() == 1) { |
250 | layer = (FLayer) all_layers.get(0);
|
||
251 | if (!(layer.isOk())) {
|
||
252 | // if the layer is not okay (it has errors) process them
|
||
253 | processErrorsOfLayer(layer, mapControl); |
||
254 | } |
||
255 | |||
256 | layer.setVisible(true);
|
||
257 | 43359 | jjdelcerro | mc.beginAtomicEvent(); |
258 | 40435 | jjdelcerro | try {
|
259 | try {
|
||
260 | 43359 | jjdelcerro | mc.getLayers().addLayer(layer); |
261 | 40435 | jjdelcerro | } catch (CancelationException e) {
|
262 | return;
|
||
263 | } |
||
264 | |||
265 | 43359 | jjdelcerro | if ( needCalculateEnvelope && env!=null ) { |
266 | mapControl.getViewPort().setEnvelope(env); |
||
267 | 40435 | jjdelcerro | } |
268 | } finally {
|
||
269 | 43359 | jjdelcerro | mc.endAtomicEvent(); |
270 | 40435 | jjdelcerro | } |
271 | return;
|
||
272 | } |
||
273 | 43359 | jjdelcerro | } |
274 | 40435 | jjdelcerro | } finally {
|
275 | // Dispose all created layers. If they have been included into
|
||
276 | // a FLayers object, they will have been binded there.
|
||
277 | for (int i = 0; i < all_layers.size(); i++) { |
||
278 | FLayer layer = (FLayer) all_layers.get(i); |
||
279 | layer.dispose(); |
||
280 | } |
||
281 | } |
||
282 | } |
||
283 | |||
284 | 43359 | jjdelcerro | @Override
|
285 | 40435 | jjdelcerro | protected PrepareContext getPrepareDataStoreContext() {
|
286 | if (this.prepareContext == null) { |
||
287 | this.prepareContext = new PrepareContextView() { |
||
288 | 43359 | jjdelcerro | @Override
|
289 | 40435 | jjdelcerro | public Window getOwnerWindow() { |
290 | return null; |
||
291 | } |
||
292 | |||
293 | 43359 | jjdelcerro | @Override
|
294 | 40435 | jjdelcerro | public MapControl getMapControl() {
|
295 | return WizardVectorialDB.this
|
||
296 | .getMapCtrl(); |
||
297 | } |
||
298 | |||
299 | 43359 | jjdelcerro | @Override
|
300 | 40435 | jjdelcerro | public IProjection getViewProjection() {
|
301 | MapControl mapControl = getMapControl(); |
||
302 | if (mapControl!=null){ |
||
303 | return mapControl.getProjection();
|
||
304 | } |
||
305 | return null; |
||
306 | } |
||
307 | |||
308 | }; |
||
309 | } |
||
310 | return this.prepareContext; |
||
311 | } |
||
312 | |||
313 | 43359 | jjdelcerro | @Override
|
314 | 40435 | jjdelcerro | protected DBStoreParameters getParameterForTable(TablesListItem table) {
|
315 | DBStoreParameters parameters = table.getParameters(); |
||
316 | |||
317 | UserTableSettingsVectorialPanel userTableSettingsPanel = (UserTableSettingsVectorialPanel) table |
||
318 | .getUserTableSettingsPanel(); |
||
319 | |||
320 | Envelope _wa = userTableSettingsPanel.getWorkingArea(); |
||
321 | |||
322 | String geomField = userTableSettingsPanel.getGeoFieldName();
|
||
323 | String fidField = userTableSettingsPanel.getIdFieldName();
|
||
324 | //IF is a multiple PK, remove the {} symbols
|
||
325 | if (fidField.startsWith("{") && fidField.endsWith("}")) { |
||
326 | fidField = fidField.substring(1, fidField.length()-1); |
||
327 | } |
||
328 | String[] pkFields = fidField.split(","); |
||
329 | |||
330 | String[] fields = table.getUserSelectedFieldsPanel() |
||
331 | .getUserSelectedFields(pkFields, geomField); |
||
332 | |||
333 | if (userTableSettingsPanel.isSqlActive()) {
|
||
334 | String whereClause = userTableSettingsPanel.getWhereClause();
|
||
335 | parameters.setBaseFilter(whereClause); |
||
336 | } else {
|
||
337 | parameters.setBaseFilter("");
|
||
338 | } |
||
339 | |||
340 | parameters.setFields(fields); |
||
341 | parameters.setDefaultGeometryField(geomField); |
||
342 | if (_wa != null) { |
||
343 | parameters.setWorkingArea(_wa); |
||
344 | } |
||
345 | 40952 | jldominguez | |
346 | // ======= Setting PK from combo box
|
||
347 | if (pkFields != null) { |
||
348 | parameters.setPkFields(pkFields); |
||
349 | } |
||
350 | // =========================
|
||
351 | 40435 | jjdelcerro | |
352 | IProjection proj_selected_by_user = userTableSettingsPanel.getProjection(); |
||
353 | parameters.setCRS(proj_selected_by_user); |
||
354 | |||
355 | return parameters;
|
||
356 | |||
357 | } |
||
358 | |||
359 | |||
360 | } |