svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.exportto / org.gvsig.exportto.swing / org.gvsig.exportto.swing.prov / org.gvsig.exportto.swing.prov.jdbc / src / main / java / org / gvsig / export / jdbc / swing / panels / SelectTableNamePanel.java @ 46542
History | View | Annotate | Download (19.2 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.export.jdbc.swing.panels; |
24 |
|
25 |
import java.awt.event.ActionEvent; |
26 |
import java.util.ArrayList; |
27 |
import java.util.Collections; |
28 |
import java.util.List; |
29 |
|
30 |
import javax.swing.DefaultListModel; |
31 |
import javax.swing.JComponent; |
32 |
import javax.swing.JOptionPane; |
33 |
import javax.swing.ListModel; |
34 |
import javax.swing.SwingUtilities; |
35 |
import javax.swing.event.AncestorEvent; |
36 |
import javax.swing.event.AncestorListener; |
37 |
import org.apache.commons.lang3.StringUtils; |
38 |
import org.gvsig.export.jdbc.service.ExportJDBCAttributeNamesTranslator; |
39 |
import org.gvsig.export.jdbc.service.ExportJDBCParameters; |
40 |
import org.gvsig.fmap.dal.DALLocator; |
41 |
import org.gvsig.fmap.dal.DataManager; |
42 |
import org.gvsig.fmap.dal.SQLBuilder; |
43 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer; |
44 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
45 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
46 |
import org.gvsig.tools.ToolsLocator; |
47 |
import org.gvsig.tools.i18n.I18nManager; |
48 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
49 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
50 |
import org.gvsig.tools.task.AbstractMonitorableTask; |
51 |
import org.gvsig.tools.task.SimpleTaskStatus; |
52 |
import org.slf4j.Logger; |
53 |
import org.slf4j.LoggerFactory; |
54 |
import org.gvsig.export.swing.JExportProcessPanel; |
55 |
import org.gvsig.export.swing.spi.ExportPanel; |
56 |
import org.gvsig.export.swing.spi.ExportPanelValidationException; |
57 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingLocator; |
58 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
59 |
import org.gvsig.expressionevaluator.swing.ExpressionPickerController; |
60 |
import org.gvsig.fmap.dal.DataStoreParameters; |
61 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
62 |
|
63 |
/**
|
64 |
* @author gvSIG Team
|
65 |
* @version $Id$
|
66 |
*
|
67 |
*/
|
68 |
@SuppressWarnings("UseSpecificCatch") |
69 |
public class SelectTableNamePanel |
70 |
extends SelectTableNamePanelLayout
|
71 |
implements ExportPanel {
|
72 |
|
73 |
private static final Logger logger = LoggerFactory.getLogger(SelectTableNamePanel.class); |
74 |
|
75 |
private static final long serialVersionUID = 6269512983586358017L; |
76 |
private ExpressionPickerController deleteRowsFilterPicker;
|
77 |
|
78 |
private static class TableItem implements Comparable<TableItem> { |
79 |
|
80 |
private JDBCStoreParameters params;
|
81 |
private String label; |
82 |
|
83 |
public TableItem(String label, JDBCStoreParameters params) { |
84 |
this.params = params;
|
85 |
this.label = label;
|
86 |
} |
87 |
|
88 |
public TableItem(JDBCStoreParameters params) {
|
89 |
this(StringUtils.isEmpty(params.getSchema())
|
90 |
? params.getTable() : params.getSchema() + "." + params.getTable(),
|
91 |
params); |
92 |
} |
93 |
|
94 |
@Override
|
95 |
public String toString() { |
96 |
return this.label; |
97 |
} |
98 |
|
99 |
public JDBCStoreParameters getParams() {
|
100 |
return this.params; |
101 |
} |
102 |
|
103 |
@Override
|
104 |
public int compareTo(TableItem o) { |
105 |
return this.label.compareTo(o.label); |
106 |
} |
107 |
} |
108 |
|
109 |
private FillTablesListTask task = null; |
110 |
private SQLBuilder sqlbuilder;
|
111 |
private final ExportJDBCParameters parameters; |
112 |
private final JExportProcessPanel processPanel; |
113 |
|
114 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
115 |
public SelectTableNamePanel(
|
116 |
JExportProcessPanel processPanel, |
117 |
ExportJDBCParameters parameters |
118 |
) { |
119 |
this.processPanel = processPanel;
|
120 |
this.parameters = parameters;
|
121 |
initComponents(); |
122 |
this.addAncestorListener(new AncestorListener() { |
123 |
|
124 |
@Override
|
125 |
public void ancestorAdded(AncestorEvent ae) { |
126 |
} |
127 |
|
128 |
@Override
|
129 |
public void ancestorRemoved(AncestorEvent ae) { |
130 |
cancelTask(); |
131 |
} |
132 |
|
133 |
@Override
|
134 |
public void ancestorMoved(AncestorEvent ae) { |
135 |
} |
136 |
}); |
137 |
} |
138 |
|
139 |
private void initComponents() { |
140 |
this.rdoCreateTable.addActionListener((ActionEvent e) -> { |
141 |
onChangeRadioSelecion(); |
142 |
}); |
143 |
this.rdoInsert.addActionListener((ActionEvent e) -> { |
144 |
onChangeRadioSelecion(); |
145 |
}); |
146 |
this.rdoCreateTable.setSelected(true); |
147 |
this.rdoInsert.setEnabled(false); |
148 |
this.lstTables.setEnabled(false); |
149 |
this.chkDeleteRows.setEnabled(false); |
150 |
try {
|
151 |
this.txtTableName.setText(this.parameters.getSourceFeatureStore().getName()); |
152 |
} catch (Exception ex) { |
153 |
logger.warn("Can't set the default value for the table name", ex);
|
154 |
} |
155 |
ExpressionEvaluatorSwingManager expressionEvaluatorSwingManager = ExpressionEvaluatorSwingLocator.getManager(); |
156 |
this.deleteRowsFilterPicker = expressionEvaluatorSwingManager.createExpressionPickerController(txtDeleteRowsFilter, btnDeleteRowsFilter);
|
157 |
this.deleteRowsFilterPicker.setEnabled(false); |
158 |
|
159 |
ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager(); |
160 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
161 |
swingManager.translate(this.lblHeader);
|
162 |
swingManager.translate(this.lblWarningUseExistingTable, true); |
163 |
swingManager.translate(this.rdoInsert);
|
164 |
swingManager.translate(this.lblSelectTableName);
|
165 |
swingManager.translate(this.rdoCreateTable);
|
166 |
swingManager.translate(this.lblSchema);
|
167 |
swingManager.translate(this.lblTableName);
|
168 |
swingManager.translate(this.lblCaseSensitiveNote,true); |
169 |
swingManager.translate(this.chkDeleteRows);
|
170 |
swingManager.translate(this.lblDeleteRowsFilter);
|
171 |
swingManager.translate(this.txtDeleteRowsFilter);
|
172 |
swingManager.translate(this.btnDeleteRowsFilter);
|
173 |
|
174 |
// this.lblHeader.setText(i18nManager.getTranslation("_Indique_donde_desea_insertar_los_datos"));
|
175 |
// this.lblWarningUseExistingTable.setText(
|
176 |
// "<html>\n"
|
177 |
// + i18nManager.getTranslation("_Los_datos_se_insertaran_usando_los_nombres_de_columna_que_coincidan_con_la_tabla_origen_dejandose_al_valor_por_defecto_para_los_que_no_haya_valores_en_la_tabla_origen")
|
178 |
// + "\n</html>"
|
179 |
// );
|
180 |
// this.rdoInsert.setText(i18nManager.getTranslation("_Insertar_registros_en_una_tabla_existente"));
|
181 |
// this.lblSelectTableName.setText(i18nManager.getTranslation("_Seleccione_la_tabla_a_usar"));
|
182 |
// this.rdoCreateTable.setText(i18nManager.getTranslation("_Crear_una_tabla_nueva"));
|
183 |
// this.lblSchema.setText(i18nManager.getTranslation("_Indique_el_esquema_en_el_que_desea_crear_la_tabla"));
|
184 |
// this.lblTableName.setText(i18nManager.getTranslation("_Indique_el_nombre_de_la_tabla"));
|
185 |
|
186 |
|
187 |
} |
188 |
|
189 |
private void cancelTask() { |
190 |
if (task != null) { |
191 |
task.cancelRequest(); |
192 |
task.getSimpleTaskStatus().remove(); |
193 |
task = null;
|
194 |
} |
195 |
} |
196 |
|
197 |
public boolean canCreateTable() { |
198 |
return this.rdoCreateTable.isSelected(); |
199 |
} |
200 |
|
201 |
public String getSchema() { |
202 |
if (this.canCreateTable()) { |
203 |
return StringUtils.defaultIfBlank(this.txtSchema.getText(), null); |
204 |
} |
205 |
TableItem item = (TableItem) this.lstTables.getSelectedValue();
|
206 |
JDBCStoreParameters tableParameter = item.getParams(); |
207 |
if (tableParameter == null) { |
208 |
return null; |
209 |
} |
210 |
return tableParameter.getSchema();
|
211 |
} |
212 |
|
213 |
public String getTableName() { |
214 |
if (this.canCreateTable()) { |
215 |
return StringUtils.defaultIfBlank(this.txtTableName.getText(), null); |
216 |
} |
217 |
TableItem item = (TableItem) this.lstTables.getSelectedValue();
|
218 |
if (item == null) { |
219 |
return null; |
220 |
} |
221 |
JDBCStoreParameters tableParameter = item.getParams(); |
222 |
|
223 |
if (tableParameter == null) { |
224 |
return null; |
225 |
} |
226 |
return tableParameter.getTable();
|
227 |
} |
228 |
|
229 |
@Override
|
230 |
public String getIdPanel() { |
231 |
return this.getClass().getCanonicalName(); |
232 |
} |
233 |
|
234 |
@Override
|
235 |
public String getTitlePanel() { |
236 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
237 |
return i18nManager.getTranslation("_Tablename"); |
238 |
} |
239 |
|
240 |
@Override
|
241 |
public void previousPanel() { |
242 |
|
243 |
} |
244 |
|
245 |
@Override
|
246 |
public void nextPanel() { |
247 |
this.parameters.setSchema(this.getSchema()); |
248 |
this.parameters.setTableName(this.getTableName()); |
249 |
this.parameters.setCanCreatetable(this.canCreateTable()); |
250 |
if (this.canCreateTable()) { |
251 |
this.parameters.setDeleteRows(false); |
252 |
this.parameters.setDeleteRowsFilter(null); |
253 |
} else {
|
254 |
this.parameters.setDeleteRows(this.chkDeleteRows.isSelected()); |
255 |
this.parameters.setDeleteRowsFilter(this.deleteRowsFilterPicker.get()); |
256 |
} |
257 |
} |
258 |
|
259 |
@Override
|
260 |
public boolean validatePanel() throws ExportPanelValidationException { |
261 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
262 |
String tablename = this.getTableName(); |
263 |
if (tablename == null) { |
264 |
throw new ExportPanelValidationException( |
265 |
i18nManager.getTranslation( |
266 |
"_The_name_of_table_cannot_be_empty"
|
267 |
) |
268 |
); |
269 |
} |
270 |
String schema = this.getSchema(); |
271 |
if (sqlbuilder.support_schemas()) {
|
272 |
if (schema == null) { |
273 |
throw new ExportPanelValidationException( |
274 |
i18nManager.getTranslation( |
275 |
"_The_name_of_schema_cannot_be_empty"
|
276 |
) |
277 |
); |
278 |
} |
279 |
} |
280 |
if (this.rdoCreateTable.isSelected()) { |
281 |
ExportJDBCAttributeNamesTranslator nameTranslator |
282 |
= (ExportJDBCAttributeNamesTranslator) this.parameters.getExportAttributes().getNamesTranslator();
|
283 |
String tablename_tr = nameTranslator.getSuggestion(tablename);
|
284 |
if (!tablename_tr.equals(tablename)) {
|
285 |
String msg = i18nManager.getTranslation(
|
286 |
"_The_name_entered_for_the_table_does_not_correspond_to_the_conditions_selected_in_the_previous_panel_You_want_it_to_be_corrected_automatically"
|
287 |
); |
288 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
289 |
int resp = dialogs.confirmDialog(
|
290 |
msg, |
291 |
i18nManager.getTranslation("_Warning"),
|
292 |
JOptionPane.YES_NO_OPTION,
|
293 |
JOptionPane.WARNING_MESSAGE,
|
294 |
"Exportto_Table_name_with_spaces_or_mixed_case"
|
295 |
); |
296 |
if (resp != JOptionPane.YES_OPTION) { |
297 |
msg = i18nManager.getTranslation( |
298 |
"El_nombre_de_tabla_contiene_caracteres no_validos"
|
299 |
); |
300 |
throw new ExportPanelValidationException(msg); |
301 |
} |
302 |
tablename = tablename_tr; |
303 |
this.txtTableName.setText(tablename);
|
304 |
} |
305 |
ListModel model = this.lstTables.getModel(); |
306 |
for (int i = 0; i < model.getSize(); i++) { |
307 |
TableItem item = (TableItem) model.getElementAt(i); |
308 |
if (StringUtils.equals(schema, item.getParams().getSchema())
|
309 |
&& StringUtils.equals(tablename, item.getParams().getTable())) { |
310 |
String msg = i18nManager.getTranslation(
|
311 |
"_La_tabla_{0}_{1}_ya_existe_en_la_base_de_datos_Seleccione_la_opcion_de_insertar_registros_en_una_tabla_existente_para_a?adir_los_datos_a_esta_o_indique_otro_nombre",
|
312 |
new String[]{schema, tablename} |
313 |
); |
314 |
throw new ExportPanelValidationException(msg); |
315 |
} |
316 |
} |
317 |
} |
318 |
return true; |
319 |
} |
320 |
|
321 |
@Override
|
322 |
public void enterPanel() { |
323 |
JDBCServerExplorerParameters explorerParameters = parameters.getExplorerParameters(); |
324 |
if (explorerParameters == null) { |
325 |
this.processPanel.setButtonEnabled(JExportProcessPanel.BUTTON_NEXT, false); |
326 |
return;
|
327 |
} |
328 |
this.processPanel.setButtonEnabled(JExportProcessPanel.BUTTON_NEXT, true); |
329 |
try {
|
330 |
DataManager dataManager = DALLocator.getDataManager(); |
331 |
JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer( |
332 |
explorerParameters.getExplorerName(), |
333 |
explorerParameters |
334 |
); |
335 |
this.sqlbuilder = explorer.createSQLBuilder();
|
336 |
} catch (Exception ex) { |
337 |
throw new RuntimeException("Can't retrieve the sqlbuilder", ex); |
338 |
} |
339 |
this.fillTablesList();
|
340 |
|
341 |
// set values from params
|
342 |
this.rdoCreateTable.setSelected(this.parameters.canCreatetable()); |
343 |
if (this.parameters.canCreatetable() == true) { |
344 |
if (this.parameters.getSchema() != null) { |
345 |
this.txtSchema.setText(this.parameters.getSchema()); |
346 |
this.txtTableName.setText(this.parameters.getTableName()); |
347 |
} |
348 |
this.chkDeleteRows.setSelected(false); |
349 |
this.deleteRowsFilterPicker.set(null); |
350 |
} else {
|
351 |
String schema = this.parameters.getSchema(); |
352 |
if (schema != null) { |
353 |
//this.lstTables;
|
354 |
for (int i = 0; i < this.lstTables.getModel().getSize(); i++) { |
355 |
TableItem item = (TableItem) this.lstTables.getModel().getElementAt(i);
|
356 |
JDBCStoreParameters tableParameter = item.getParams(); |
357 |
if ( StringUtils.equals(tableParameter.getSchema(),schema) ) {
|
358 |
this.lstTables.setSelectedIndex(i);
|
359 |
} |
360 |
} |
361 |
} |
362 |
this.chkDeleteRows.setSelected(this.parameters.isDeleteRows()); |
363 |
this.deleteRowsFilterPicker.set(this.parameters.getDeleteRowsFilter()); |
364 |
|
365 |
} |
366 |
} |
367 |
|
368 |
@Override
|
369 |
public JComponent asJComponent() { |
370 |
return this; |
371 |
} |
372 |
|
373 |
public void onChangeRadioSelecion() { |
374 |
if (this.rdoCreateTable.isSelected()) { |
375 |
this.txtSchema.setEnabled(true); |
376 |
this.txtTableName.setEnabled(true); |
377 |
this.lstTables.setEnabled(false); |
378 |
this.chkDeleteRows.setEnabled(false); |
379 |
this.txtDeleteRowsFilter.setEnabled(false); |
380 |
this.btnDeleteRowsFilter.setEnabled(false); |
381 |
} else {
|
382 |
this.txtSchema.setEnabled(false); |
383 |
this.txtTableName.setEnabled(false); |
384 |
this.lstTables.setEnabled(true); |
385 |
this.chkDeleteRows.setEnabled(true); |
386 |
this.txtDeleteRowsFilter.setEnabled(true); |
387 |
this.btnDeleteRowsFilter.setEnabled(true); |
388 |
} |
389 |
} |
390 |
|
391 |
private void fillTablesList() { |
392 |
|
393 |
JDBCServerExplorerParameters explorerParameters = this.parameters.getExplorerParameters();
|
394 |
if (explorerParameters == null) { |
395 |
return;
|
396 |
} |
397 |
cancelTask(); |
398 |
this.task = new FillTablesListTask(); |
399 |
task.setDaemon(true);
|
400 |
task.start(); |
401 |
} |
402 |
|
403 |
private class FillTablesListTask extends AbstractMonitorableTask { |
404 |
|
405 |
public FillTablesListTask() {
|
406 |
super("Export"); |
407 |
} |
408 |
|
409 |
@Override
|
410 |
protected SimpleTaskStatus getSimpleTaskStatus() {
|
411 |
return (SimpleTaskStatus) this.getTaskStatus(); |
412 |
} |
413 |
|
414 |
@Override
|
415 |
public void run() { |
416 |
|
417 |
JDBCServerExplorerParameters explorerParameters = parameters.getExplorerParameters(); |
418 |
if (parameters.getExplorerParameters() == null) { |
419 |
return;
|
420 |
} |
421 |
final SimpleTaskStatus status = this.getSimpleTaskStatus(); |
422 |
try {
|
423 |
status.setAutoremove(true);
|
424 |
|
425 |
DataManager dataManager = DALLocator.getDataManager(); |
426 |
|
427 |
this.getSimpleTaskStatus().message("Connecting server"); |
428 |
explorerParameters.setShowInformationDBTables(false);
|
429 |
final JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer(
|
430 |
explorerParameters.getExplorerName(), |
431 |
explorerParameters |
432 |
); |
433 |
SwingUtilities.invokeAndWait(() -> {
|
434 |
if (sqlbuilder.support_schemas()) {
|
435 |
txtSchema.setText(sqlbuilder.default_schema()); |
436 |
} else {
|
437 |
txtSchema.setText("");
|
438 |
txtSchema.setEnabled(false);
|
439 |
} |
440 |
}); |
441 |
|
442 |
this.getSimpleTaskStatus().message("Retrieving tables"); |
443 |
final List<DataStoreParameters> parameters = explorer.list(); |
444 |
final List<TableItem> tables = new ArrayList<>(); |
445 |
for (DataStoreParameters table : parameters) {
|
446 |
if (status.isCancelled()) {
|
447 |
status.cancel(); |
448 |
break;
|
449 |
} |
450 |
tables.add(new TableItem((JDBCStoreParameters)table));
|
451 |
} |
452 |
Collections.sort(tables);
|
453 |
|
454 |
this.getSimpleTaskStatus().message("Add tables"); |
455 |
|
456 |
final DefaultListModel lmodel = new DefaultListModel(); |
457 |
for (TableItem table : tables) {
|
458 |
lmodel.addElement(table); |
459 |
} |
460 |
|
461 |
SwingUtilities.invokeAndWait(() -> {
|
462 |
lstTables.setModel(lmodel); |
463 |
//lstTables.setEnabled(true);
|
464 |
rdoInsert.setEnabled(true);
|
465 |
}); |
466 |
|
467 |
status.message("finish");
|
468 |
status.terminate(); |
469 |
|
470 |
} catch (final Exception ex) { |
471 |
logger.warn("Fail to fill tables list", ex);
|
472 |
if (status.isCancellationRequested()) {
|
473 |
status.cancel(); |
474 |
} |
475 |
if (status.isRunning()) {
|
476 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
477 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
478 |
dialogs.messageDialog( |
479 |
i18nManager.getTranslation("_There_have_been_problems_filling_data_in_panel")
|
480 |
+ " (" + getTitlePanel() + ")", |
481 |
null,
|
482 |
i18nManager.getTranslation("_Warning"),
|
483 |
JOptionPane.WARNING_MESSAGE,
|
484 |
"ProblemsFillingTableNamePanel"
|
485 |
); |
486 |
} |
487 |
|
488 |
} finally {
|
489 |
status.terminate(); |
490 |
status.remove(); |
491 |
} |
492 |
|
493 |
} |
494 |
|
495 |
} |
496 |
|
497 |
} |