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 @ 44386
History | View | Annotate | Download (17.1 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.awt.event.ActionListener; |
27 |
import java.util.Iterator; |
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.fmap.dal.DataStoreParameters; |
58 |
|
59 |
/**
|
60 |
* @author gvSIG Team
|
61 |
* @version $Id$
|
62 |
*
|
63 |
*/
|
64 |
public class SelectTableNamePanel |
65 |
extends SelectTableNamePanelLayout
|
66 |
implements ExportPanel {
|
67 |
|
68 |
private static final Logger logger = LoggerFactory.getLogger(SelectTableNamePanel.class); |
69 |
|
70 |
private static final long serialVersionUID = 6269512983586358017L; |
71 |
|
72 |
private static class TableItem { |
73 |
|
74 |
private JDBCStoreParameters params;
|
75 |
private String label; |
76 |
|
77 |
public TableItem(String label, JDBCStoreParameters params) { |
78 |
this.params = params;
|
79 |
this.label = label;
|
80 |
} |
81 |
|
82 |
public TableItem(JDBCStoreParameters params) {
|
83 |
this(StringUtils.isEmpty(params.getSchema())
|
84 |
? params.getTable() : params.getSchema() + "." + params.getTable(),
|
85 |
params); |
86 |
} |
87 |
|
88 |
@Override
|
89 |
public String toString() { |
90 |
return this.label; |
91 |
} |
92 |
|
93 |
public JDBCStoreParameters getParams() {
|
94 |
return this.params; |
95 |
} |
96 |
} |
97 |
|
98 |
private FillTablesListTask task = null; |
99 |
private SQLBuilder sqlbuilder;
|
100 |
private final ExportJDBCParameters parameters; |
101 |
private final JExportProcessPanel processPanel; |
102 |
|
103 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
104 |
public SelectTableNamePanel(
|
105 |
JExportProcessPanel processPanel, |
106 |
ExportJDBCParameters parameters |
107 |
) { |
108 |
this.processPanel = processPanel;
|
109 |
this.parameters = parameters;
|
110 |
initComponents(); |
111 |
this.addAncestorListener(new AncestorListener() { |
112 |
|
113 |
@Override
|
114 |
public void ancestorAdded(AncestorEvent ae) { |
115 |
} |
116 |
|
117 |
@Override
|
118 |
public void ancestorRemoved(AncestorEvent ae) { |
119 |
cancelTask(); |
120 |
} |
121 |
|
122 |
@Override
|
123 |
public void ancestorMoved(AncestorEvent ae) { |
124 |
} |
125 |
}); |
126 |
} |
127 |
|
128 |
private void initComponents() { |
129 |
this.rdoCreateTable.addActionListener(new ActionListener() { |
130 |
@Override
|
131 |
public void actionPerformed(ActionEvent e) { |
132 |
onChangeRadioSelecion(); |
133 |
} |
134 |
}); |
135 |
this.rdoInsert.addActionListener(new ActionListener() { |
136 |
@Override
|
137 |
public void actionPerformed(ActionEvent e) { |
138 |
onChangeRadioSelecion(); |
139 |
} |
140 |
}); |
141 |
this.rdoCreateTable.setSelected(true); |
142 |
this.rdoInsert.setEnabled(false); |
143 |
this.lstTables.setEnabled(false); |
144 |
try {
|
145 |
this.txtTableName.setText(this.parameters.getSourceFeatureStore().getName()); |
146 |
} catch (Exception ex) { |
147 |
logger.warn("Can't set the default value for the table name", ex);
|
148 |
} |
149 |
|
150 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
151 |
this.lblHeader.setText(i18nManager.getTranslation("_Indique_donde_desea_insertar_los_datos")); |
152 |
this.lblWarningUseExistingTable.setText(
|
153 |
"<html>\n"
|
154 |
+ 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")
|
155 |
+ "\n</html>"
|
156 |
); |
157 |
this.rdoInsert.setText(i18nManager.getTranslation("_Insertar_registros_en_una_tabla_existente")); |
158 |
this.lblSelectTableName.setText(i18nManager.getTranslation("_Seleccione_la_tabla_a_usar")); |
159 |
this.rdoCreateTable.setText(i18nManager.getTranslation("_Crear_una_tabla_nueva")); |
160 |
this.lblSchema.setText(i18nManager.getTranslation("_Indique_el_esquema_en_el_que_desea_crear_la_tabla")); |
161 |
this.lblTableName.setText(i18nManager.getTranslation("_Indique_el_nombre_de_la_tabla")); |
162 |
} |
163 |
|
164 |
private void cancelTask() { |
165 |
if (task != null) { |
166 |
task.cancelRequest(); |
167 |
task.getSimpleTaskStatus().remove(); |
168 |
task = null;
|
169 |
} |
170 |
} |
171 |
|
172 |
public boolean canCreateTable() { |
173 |
return this.rdoCreateTable.isSelected(); |
174 |
} |
175 |
|
176 |
public String getSchema() { |
177 |
if (this.canCreateTable()) { |
178 |
return StringUtils.defaultIfBlank(this.txtSchema.getText(), null); |
179 |
} |
180 |
TableItem item = (TableItem) this.lstTables.getSelectedValue();
|
181 |
JDBCStoreParameters tableParameter = item.getParams(); |
182 |
if (tableParameter == null) { |
183 |
return null; |
184 |
} |
185 |
return tableParameter.getSchema();
|
186 |
} |
187 |
|
188 |
public String getTableName() { |
189 |
if (this.canCreateTable()) { |
190 |
return StringUtils.defaultIfBlank(this.txtTableName.getText(), null); |
191 |
} |
192 |
TableItem item = (TableItem) this.lstTables.getSelectedValue();
|
193 |
if (item == null) { |
194 |
return null; |
195 |
} |
196 |
JDBCStoreParameters tableParameter = item.getParams(); |
197 |
|
198 |
if (tableParameter == null) { |
199 |
return null; |
200 |
} |
201 |
return tableParameter.getTable();
|
202 |
} |
203 |
|
204 |
@Override
|
205 |
public String getIdPanel() { |
206 |
return this.getClass().getCanonicalName(); |
207 |
} |
208 |
|
209 |
@Override
|
210 |
public String getTitlePanel() { |
211 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
212 |
return i18nManager.getTranslation("_Tablename"); |
213 |
} |
214 |
|
215 |
@Override
|
216 |
public void previousPanel() { |
217 |
|
218 |
} |
219 |
|
220 |
@Override
|
221 |
public void nextPanel() { |
222 |
this.parameters.setSchema(this.getSchema()); |
223 |
this.parameters.setTableName(this.getTableName()); |
224 |
this.parameters.setCanCreatetable(this.canCreateTable()); |
225 |
} |
226 |
|
227 |
@Override
|
228 |
public boolean validatePanel() throws ExportPanelValidationException { |
229 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
230 |
String tablename = this.getTableName(); |
231 |
if (tablename == null) { |
232 |
throw new ExportPanelValidationException( |
233 |
i18nManager.getTranslation( |
234 |
"_The_name_of_table_cannot_be_empty"
|
235 |
) |
236 |
); |
237 |
} |
238 |
String schema = this.getSchema(); |
239 |
if (sqlbuilder.support_schemas()) {
|
240 |
if (schema == null) { |
241 |
throw new ExportPanelValidationException( |
242 |
i18nManager.getTranslation( |
243 |
"_The_name_of_schema_cannot_be_empty"
|
244 |
) |
245 |
); |
246 |
} |
247 |
} |
248 |
if (this.rdoCreateTable.isSelected()) { |
249 |
ExportJDBCAttributeNamesTranslator nameTranslator |
250 |
= (ExportJDBCAttributeNamesTranslator) this.parameters.getExportAttributes().getNamesTranslator();
|
251 |
String tablename_tr = nameTranslator.getNameSuggestion(tablename);
|
252 |
if (!tablename_tr.equals(tablename)) {
|
253 |
String msg = i18nManager.getTranslation(
|
254 |
"Ha_utilizado_espacios_en_blanco_o_mayusculas_en_el_nombre_de_la_tabla_Desea_que_se_corrija_de_forma_automatica"
|
255 |
); |
256 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
257 |
int resp = dialogs.confirmDialog(
|
258 |
msg, |
259 |
i18nManager.getTranslation("_Warning"),
|
260 |
JOptionPane.YES_NO_OPTION,
|
261 |
JOptionPane.WARNING_MESSAGE,
|
262 |
"Exportto_Table_name_with_spaces_or_mixed_case"
|
263 |
); |
264 |
if (resp != JOptionPane.YES_OPTION) { |
265 |
msg = i18nManager.getTranslation( |
266 |
"El_nombre_de_tabla_contiene_caracteres no_validos"
|
267 |
); |
268 |
throw new ExportPanelValidationException(msg); |
269 |
} |
270 |
tablename = tablename_tr; |
271 |
this.txtTableName.setText(tablename);
|
272 |
} |
273 |
ListModel model = this.lstTables.getModel(); |
274 |
for (int i = 0; i < model.getSize(); i++) { |
275 |
TableItem item = (TableItem) model.getElementAt(i); |
276 |
if (StringUtils.equals(schema, item.getParams().getSchema())
|
277 |
&& StringUtils.equals(tablename, item.getParams().getTable())) { |
278 |
String msg = i18nManager.getTranslation(
|
279 |
"_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",
|
280 |
new String[]{schema, tablename} |
281 |
); |
282 |
throw new ExportPanelValidationException(msg); |
283 |
} |
284 |
} |
285 |
} |
286 |
return true; |
287 |
} |
288 |
|
289 |
@Override
|
290 |
public void enterPanel() { |
291 |
JDBCServerExplorerParameters explorerParameters = parameters.getExplorerParameters(); |
292 |
if (explorerParameters == null) { |
293 |
this.processPanel.setButtonEnabled(JExportProcessPanel.BUTTON_NEXT, false); |
294 |
return;
|
295 |
} |
296 |
this.processPanel.setButtonEnabled(JExportProcessPanel.BUTTON_NEXT, true); |
297 |
try {
|
298 |
DataManager dataManager = DALLocator.getDataManager(); |
299 |
JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer( |
300 |
explorerParameters.getExplorerName(), |
301 |
explorerParameters |
302 |
); |
303 |
this.sqlbuilder = explorer.createSQLBuilder();
|
304 |
} catch (Exception ex) { |
305 |
throw new RuntimeException("Can't retrieve the sqlbuilder", ex); |
306 |
} |
307 |
this.fillTablesList();
|
308 |
|
309 |
// set values from params
|
310 |
this.rdoCreateTable.setSelected(this.parameters.canCreatetable()); |
311 |
if (this.parameters.canCreatetable() == true) { |
312 |
if (this.parameters.getSchema() != null) { |
313 |
this.txtSchema.setText(this.parameters.getSchema()); |
314 |
this.txtTableName.setText(this.parameters.getTableName()); |
315 |
} |
316 |
} else {
|
317 |
String schema = this.parameters.getSchema(); |
318 |
if (schema != null) { |
319 |
//this.lstTables;
|
320 |
for (int i = 0; i < this.lstTables.getModel().getSize(); i++) { |
321 |
TableItem item = (TableItem) this.lstTables.getModel().getElementAt(i);
|
322 |
JDBCStoreParameters tableParameter = item.getParams(); |
323 |
if (tableParameter.getSchema() == null ? schema == null : tableParameter.getSchema().equals(schema)) { |
324 |
this.lstTables.setSelectedIndex(i);
|
325 |
} |
326 |
} |
327 |
} |
328 |
} |
329 |
} |
330 |
|
331 |
@Override
|
332 |
public JComponent asJComponent() { |
333 |
return this; |
334 |
} |
335 |
|
336 |
public void onChangeRadioSelecion() { |
337 |
if (this.rdoCreateTable.isSelected()) { |
338 |
this.txtSchema.setEnabled(true); |
339 |
this.txtTableName.setEnabled(true); |
340 |
this.lstTables.setEnabled(false); |
341 |
} else {
|
342 |
this.txtSchema.setEnabled(false); |
343 |
this.txtTableName.setEnabled(false); |
344 |
this.lstTables.setEnabled(true); |
345 |
} |
346 |
} |
347 |
|
348 |
private void fillTablesList() { |
349 |
|
350 |
JDBCServerExplorerParameters explorerParameters = this.parameters.getExplorerParameters();
|
351 |
if (explorerParameters == null) { |
352 |
return;
|
353 |
} |
354 |
cancelTask(); |
355 |
this.task = new FillTablesListTask(); |
356 |
task.setDaemon(true);
|
357 |
task.start(); |
358 |
} |
359 |
|
360 |
private class FillTablesListTask extends AbstractMonitorableTask { |
361 |
|
362 |
public FillTablesListTask() {
|
363 |
super("Export"); |
364 |
} |
365 |
|
366 |
@Override
|
367 |
protected SimpleTaskStatus getSimpleTaskStatus() {
|
368 |
return (SimpleTaskStatus) this.getTaskStatus(); |
369 |
} |
370 |
|
371 |
@Override
|
372 |
public void run() { |
373 |
|
374 |
JDBCServerExplorerParameters explorerParameters = parameters.getExplorerParameters(); |
375 |
if (parameters.getExplorerParameters() == null) { |
376 |
return;
|
377 |
} |
378 |
final SimpleTaskStatus status = this.getSimpleTaskStatus(); |
379 |
try {
|
380 |
status.setAutoremove(true);
|
381 |
|
382 |
DataManager dataManager = DALLocator.getDataManager(); |
383 |
|
384 |
this.getSimpleTaskStatus().message("Connecting server"); |
385 |
explorerParameters.setShowInformationDBTables(false);
|
386 |
final JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer(
|
387 |
explorerParameters.getExplorerName(), |
388 |
explorerParameters |
389 |
); |
390 |
SwingUtilities.invokeAndWait(new Runnable() { |
391 |
|
392 |
@Override
|
393 |
public void run() { |
394 |
if (sqlbuilder.support_schemas()) {
|
395 |
txtSchema.setText(sqlbuilder.default_schema()); |
396 |
} else {
|
397 |
txtSchema.setText("");
|
398 |
txtSchema.setEnabled(false);
|
399 |
} |
400 |
} |
401 |
}); |
402 |
|
403 |
this.getSimpleTaskStatus().message("Retrieving tables"); |
404 |
final List<DataStoreParameters> tables = explorer.list(); |
405 |
|
406 |
this.getSimpleTaskStatus().message("Add tables"); |
407 |
|
408 |
SwingUtilities.invokeAndWait(new Runnable() { |
409 |
@Override
|
410 |
public void run() { |
411 |
DefaultListModel lmodel = new DefaultListModel(); |
412 |
Iterator<DataStoreParameters> it = tables.iterator();
|
413 |
while (it.hasNext()) {
|
414 |
if (status.isCancelled()) {
|
415 |
status.cancel(); |
416 |
break;
|
417 |
} |
418 |
JDBCStoreParameters table = (JDBCStoreParameters) it.next(); |
419 |
lmodel.addElement(new TableItem(table));
|
420 |
} |
421 |
lstTables.setModel(lmodel); |
422 |
//lstTables.setEnabled(true);
|
423 |
rdoInsert.setEnabled(true);
|
424 |
} |
425 |
}); |
426 |
|
427 |
status.message("finish");
|
428 |
status.terminate(); |
429 |
|
430 |
} catch (final Exception ex) { |
431 |
logger.warn("Fail to fill tables list", ex);
|
432 |
if (status.isCancellationRequested()) {
|
433 |
status.cancel(); |
434 |
} |
435 |
if (status.isRunning()) {
|
436 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
437 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
438 |
dialogs.messageDialog( |
439 |
i18nManager.getTranslation("_There_have_been_problems_filling_data_in_panel")
|
440 |
+ " (" + getTitlePanel() + ")", |
441 |
null,
|
442 |
i18nManager.getTranslation("_Warning"),
|
443 |
JOptionPane.WARNING_MESSAGE,
|
444 |
"ProblemsFillingTableNamePanel"
|
445 |
); |
446 |
} |
447 |
|
448 |
} finally {
|
449 |
status.terminate(); |
450 |
status.remove(); |
451 |
} |
452 |
|
453 |
} |
454 |
|
455 |
} |
456 |
|
457 |
} |