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