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 @ 43020
History | View | Annotate | Download (14.3 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
6 | 41486 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
11 | 41486 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
16 | 41486 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
20 | 41486 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 40435 | jjdelcerro | */
|
23 | package org.gvsig.exportto.swing.prov.jdbc.panel; |
||
24 | |||
25 | 41486 | jjdelcerro | import java.awt.event.ActionEvent; |
26 | import java.awt.event.ActionListener; |
||
27 | 41492 | jjdelcerro | import java.awt.event.ComponentEvent; |
28 | import java.awt.event.ComponentListener; |
||
29 | import java.awt.event.ContainerEvent; |
||
30 | import java.awt.event.ContainerListener; |
||
31 | 41486 | jjdelcerro | import java.util.Iterator; |
32 | import java.util.List; |
||
33 | |||
34 | import javax.swing.DefaultListModel; |
||
35 | import javax.swing.JComponent; |
||
36 | 41598 | jjdelcerro | import javax.swing.JOptionPane; |
37 | 41534 | jjdelcerro | import javax.swing.ListModel; |
38 | 41492 | jjdelcerro | import javax.swing.SwingUtilities; |
39 | import javax.swing.event.AncestorEvent; |
||
40 | import javax.swing.event.AncestorListener; |
||
41 | 41486 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
42 | 41632 | jjdelcerro | //import org.gvsig.app.ApplicationLocator;
|
43 | //import org.gvsig.app.ApplicationManager;
|
||
44 | 41492 | jjdelcerro | import org.gvsig.exportto.swing.ExporttoSwingLocator; |
45 | import org.gvsig.exportto.swing.ExporttoSwingManager; |
||
46 | 41598 | jjdelcerro | import org.gvsig.exportto.swing.prov.jdbc.ExporttoJDBCOptions; |
47 | 40435 | jjdelcerro | import org.gvsig.exportto.swing.spi.ExporttoPanelValidationException; |
48 | 41486 | jjdelcerro | import org.gvsig.exportto.swing.spi.ExporttoSwingProviderPanel; |
49 | import org.gvsig.fmap.dal.DALLocator; |
||
50 | import org.gvsig.fmap.dal.DataManager; |
||
51 | import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer; |
||
52 | import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
||
53 | import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
||
54 | import org.gvsig.tools.ToolsLocator; |
||
55 | import org.gvsig.tools.i18n.I18nManager; |
||
56 | 41492 | jjdelcerro | import org.gvsig.tools.task.AbstractMonitorableTask; |
57 | import org.gvsig.tools.task.SimpleTaskStatus; |
||
58 | 41486 | jjdelcerro | import org.slf4j.Logger; |
59 | import org.slf4j.LoggerFactory; |
||
60 | 40435 | jjdelcerro | |
61 | /**
|
||
62 | * @author gvSIG Team
|
||
63 | * @version $Id$
|
||
64 | 41486 | jjdelcerro | *
|
65 | 40435 | jjdelcerro | */
|
66 | 41486 | jjdelcerro | public class SelectTableNamePanel extends SelectTableNamePanelLayout implements ExporttoSwingProviderPanel { |
67 | 41534 | jjdelcerro | |
68 | 41486 | jjdelcerro | private static final Logger logger = LoggerFactory.getLogger(SelectTableNamePanel.class); |
69 | 41534 | jjdelcerro | |
70 | 40435 | jjdelcerro | private static final long serialVersionUID = 6269512983586358017L; |
71 | 41598 | jjdelcerro | private final ExporttoJDBCOptions provider; |
72 | 41534 | jjdelcerro | |
73 | 41492 | jjdelcerro | private FillTablesListTask task = null; |
74 | 41534 | jjdelcerro | |
75 | 41486 | jjdelcerro | private static class TableItem { |
76 | 41534 | jjdelcerro | |
77 | 41486 | jjdelcerro | private JDBCStoreParameters params;
|
78 | private String label; |
||
79 | 41534 | jjdelcerro | |
80 | 41486 | jjdelcerro | public TableItem(String label, JDBCStoreParameters params) { |
81 | this.params = params;
|
||
82 | this.label = label;
|
||
83 | } |
||
84 | 41534 | jjdelcerro | |
85 | 41486 | jjdelcerro | public TableItem(JDBCStoreParameters params) {
|
86 | this(params.getSchema() + "." + params.getTable(), params); |
||
87 | } |
||
88 | 41534 | jjdelcerro | |
89 | 41486 | jjdelcerro | public String toString() { |
90 | return this.label; |
||
91 | } |
||
92 | 41534 | jjdelcerro | |
93 | 41486 | jjdelcerro | public JDBCStoreParameters getParams() {
|
94 | return this.params; |
||
95 | } |
||
96 | } |
||
97 | 41534 | jjdelcerro | |
98 | 41598 | jjdelcerro | public SelectTableNamePanel(ExporttoJDBCOptions provider) {
|
99 | 41486 | jjdelcerro | this.provider = provider;
|
100 | 41488 | jjdelcerro | initComponents(); |
101 | 41492 | jjdelcerro | this.addAncestorListener(new AncestorListener() { |
102 | 41534 | jjdelcerro | |
103 | 41492 | jjdelcerro | public void ancestorAdded(AncestorEvent ae) { |
104 | } |
||
105 | 41534 | jjdelcerro | |
106 | 41492 | jjdelcerro | public void ancestorRemoved(AncestorEvent ae) { |
107 | cancelTask(); |
||
108 | } |
||
109 | 41534 | jjdelcerro | |
110 | 41492 | jjdelcerro | public void ancestorMoved(AncestorEvent ae) { |
111 | } |
||
112 | }); |
||
113 | 41486 | jjdelcerro | } |
114 | 41534 | jjdelcerro | |
115 | 41486 | jjdelcerro | private void initComponents() { |
116 | this.rdoCreateTable.addActionListener(new ActionListener() { |
||
117 | public void actionPerformed(ActionEvent e) { |
||
118 | onChangeRadioSelecion(); |
||
119 | } |
||
120 | }); |
||
121 | this.rdoInsert.addActionListener(new ActionListener() { |
||
122 | public void actionPerformed(ActionEvent e) { |
||
123 | onChangeRadioSelecion(); |
||
124 | } |
||
125 | }); |
||
126 | 41488 | jjdelcerro | this.rdoCreateTable.setSelected(true); |
127 | 41492 | jjdelcerro | this.rdoInsert.setEnabled(false); |
128 | this.lstTables.setEnabled(false); |
||
129 | try {
|
||
130 | this.txtTableName.setText(this.provider.getSource().getName()); |
||
131 | } catch (Exception ex) { |
||
132 | logger.warn("Can't set the default value for the table name", ex);
|
||
133 | } |
||
134 | 41560 | jjdelcerro | |
135 | 41534 | jjdelcerro | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
136 | this.lblHeader.setText(i18nManager.getTranslation("_Indique_donde_desea_insertar_los_datos")); |
||
137 | this.lblWarningUseExistingTable.setText(
|
||
138 | 41560 | jjdelcerro | "<html>\n"
|
139 | + 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")
|
||
140 | + "\n</html>"
|
||
141 | 41534 | jjdelcerro | ); |
142 | this.rdoInsert.setText(i18nManager.getTranslation("_Insertar_registros_en_una_tabla_existente")); |
||
143 | this.lblSelectTableName.setText(i18nManager.getTranslation("_Seleccione_la_tabla_a_usar")); |
||
144 | this.rdoCreateTable.setText(i18nManager.getTranslation("_Crear_una_tabla_nueva")); |
||
145 | this.lblSchema.setText(i18nManager.getTranslation("_Indique_el_esquema_en_el_que_desea_crear_la_tabla")); |
||
146 | this.lblTableName.setText(i18nManager.getTranslation("_Indique_el_nombre_de_la_tabla")); |
||
147 | 41486 | jjdelcerro | } |
148 | 41534 | jjdelcerro | |
149 | 41492 | jjdelcerro | private void cancelTask() { |
150 | 41534 | jjdelcerro | if (task != null) { |
151 | 41492 | jjdelcerro | task.cancelRequest(); |
152 | task.getSimpleTaskStatus().remove(); |
||
153 | task = null;
|
||
154 | 41534 | jjdelcerro | } |
155 | 41492 | jjdelcerro | } |
156 | 41534 | jjdelcerro | |
157 | 41486 | jjdelcerro | public boolean canCreateTable() { |
158 | return this.rdoCreateTable.isSelected(); |
||
159 | } |
||
160 | 41534 | jjdelcerro | |
161 | 41486 | jjdelcerro | public String getSchema() { |
162 | 41534 | jjdelcerro | if (this.canCreateTable()) { |
163 | 41486 | jjdelcerro | return StringUtils.defaultIfBlank(this.txtSchema.getText(), null); |
164 | } |
||
165 | 41509 | jjdelcerro | TableItem item = (TableItem) this.lstTables.getSelectedValue();
|
166 | JDBCStoreParameters tableParameter = item.getParams(); |
||
167 | 41534 | jjdelcerro | if (tableParameter == null) { |
168 | 41486 | jjdelcerro | return null; |
169 | } |
||
170 | 41509 | jjdelcerro | return tableParameter.getSchema();
|
171 | 41486 | jjdelcerro | } |
172 | 41534 | jjdelcerro | |
173 | 41486 | jjdelcerro | public String getTableName() { |
174 | 41534 | jjdelcerro | if (this.canCreateTable()) { |
175 | 41492 | jjdelcerro | return StringUtils.defaultIfBlank(this.txtTableName.getText(), null); |
176 | 41486 | jjdelcerro | } |
177 | 41509 | jjdelcerro | TableItem item = (TableItem) this.lstTables.getSelectedValue();
|
178 | 41560 | jjdelcerro | if (item == null) { |
179 | return null; |
||
180 | } |
||
181 | 41509 | jjdelcerro | JDBCStoreParameters tableParameter = item.getParams(); |
182 | 41534 | jjdelcerro | |
183 | if (tableParameter == null) { |
||
184 | 41486 | jjdelcerro | return null; |
185 | } |
||
186 | 41509 | jjdelcerro | return tableParameter.getTable();
|
187 | 41486 | jjdelcerro | } |
188 | 41534 | jjdelcerro | |
189 | 40435 | jjdelcerro | public String getPanelTitle() { |
190 | 41486 | jjdelcerro | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
191 | 41534 | jjdelcerro | return i18nManager.getTranslation("_Tablename"); |
192 | 40435 | jjdelcerro | } |
193 | 41534 | jjdelcerro | |
194 | 40435 | jjdelcerro | public boolean isValidPanel() throws ExporttoPanelValidationException { |
195 | 41534 | jjdelcerro | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
196 | String tablename = this.getTableName(); |
||
197 | 41560 | jjdelcerro | if (tablename == null) { |
198 | 40435 | jjdelcerro | throw new ExporttoPanelValidationException( |
199 | 41560 | jjdelcerro | i18nManager.getTranslation( |
200 | "_The_name_of_table_cannot_be_empty"
|
||
201 | ) |
||
202 | 41534 | jjdelcerro | ); |
203 | 40435 | jjdelcerro | } |
204 | 41534 | jjdelcerro | String schema = this.getSchema(); |
205 | if (schema == null) { |
||
206 | 41486 | jjdelcerro | throw new ExporttoPanelValidationException( |
207 | 41560 | jjdelcerro | i18nManager.getTranslation( |
208 | "_The_name_of_schema_cannot_be_empty"
|
||
209 | ) |
||
210 | 41534 | jjdelcerro | ); |
211 | 41486 | jjdelcerro | } |
212 | 41560 | jjdelcerro | if (this.rdoCreateTable.isSelected()) { |
213 | 41598 | jjdelcerro | String tablename_tr = tablename;
|
214 | if( this.provider.getTranslateIdentifiersToLowerCase() ) { |
||
215 | tablename_tr = tablename_tr.toLowerCase(); |
||
216 | } |
||
217 | if( this.provider.getRemoveSpacesInIdentifiers() ) { |
||
218 | tablename_tr = StringUtils.normalizeSpace(tablename_tr).replace(" ", "_"); |
||
219 | } |
||
220 | if( !tablename_tr.equals(tablename) ) {
|
||
221 | String msg = i18nManager.getTranslation(
|
||
222 | "Ha_utilizado_espacios_en_blanco_o_mayusculas_en_el_nombre_de_la_tabla_Desea_que_se_corrija_de_forma_automatica"
|
||
223 | ); |
||
224 | 41632 | jjdelcerro | int resp = JOptionPane.showConfirmDialog( |
225 | this,
|
||
226 | 41598 | jjdelcerro | "Ha utilizado espacios en blanco o mayusculas en el nombre de la tabla.\n? Desea que se corrija de forma automatica ?",
|
227 | i18nManager.getTranslation("_Warning"),
|
||
228 | JOptionPane.YES_NO_OPTION,
|
||
229 | JOptionPane.WARNING_MESSAGE
|
||
230 | ); |
||
231 | if( resp != JOptionPane.YES_OPTION ) { |
||
232 | msg = i18nManager.getTranslation( |
||
233 | "El_nombre_de_tabla_contiene_caracteres no_validos"
|
||
234 | ); |
||
235 | throw new ExporttoPanelValidationException(msg); |
||
236 | } |
||
237 | tablename = tablename_tr; |
||
238 | this.txtTableName.setText(tablename);
|
||
239 | } |
||
240 | 41560 | jjdelcerro | ListModel model = this.lstTables.getModel(); |
241 | for (int i = 0; i < model.getSize(); i++) { |
||
242 | TableItem item = (TableItem) model.getElementAt(i); |
||
243 | if (schema.equals(item.getParams().getSchema())
|
||
244 | && tablename.equals(item.getParams().getTable())) { |
||
245 | String msg = i18nManager.getTranslation(
|
||
246 | "_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",
|
||
247 | new String[]{schema, tablename} |
||
248 | ); |
||
249 | throw new ExporttoPanelValidationException(msg); |
||
250 | } |
||
251 | 41534 | jjdelcerro | } |
252 | } |
||
253 | 40435 | jjdelcerro | return true; |
254 | } |
||
255 | 41534 | jjdelcerro | |
256 | 41488 | jjdelcerro | public void enterPanel() { |
257 | this.fillTablesList();
|
||
258 | } |
||
259 | 41534 | jjdelcerro | |
260 | 41486 | jjdelcerro | public JComponent asJComponent() { |
261 | return this; |
||
262 | } |
||
263 | 41534 | jjdelcerro | |
264 | 41486 | jjdelcerro | public void onChangeRadioSelecion() { |
265 | 41534 | jjdelcerro | if (this.rdoCreateTable.isSelected()) { |
266 | 41486 | jjdelcerro | this.txtSchema.setEnabled(true); |
267 | this.txtTableName.setEnabled(true); |
||
268 | this.lstTables.setEnabled(false); |
||
269 | } else {
|
||
270 | this.txtSchema.setEnabled(false); |
||
271 | this.txtTableName.setEnabled(false); |
||
272 | this.lstTables.setEnabled(true); |
||
273 | } |
||
274 | } |
||
275 | 41534 | jjdelcerro | |
276 | 41486 | jjdelcerro | private void fillTablesList() { |
277 | 41534 | jjdelcerro | |
278 | 41486 | jjdelcerro | JDBCServerExplorerParameters explorerParameters = this.provider.getExplorerParameters();
|
279 | 41534 | jjdelcerro | if (explorerParameters == null) { |
280 | 41486 | jjdelcerro | return;
|
281 | } |
||
282 | 41492 | jjdelcerro | cancelTask(); |
283 | this.task = new FillTablesListTask(); |
||
284 | task.setDaemon(true);
|
||
285 | task.start(); |
||
286 | } |
||
287 | 41534 | jjdelcerro | |
288 | 41492 | jjdelcerro | private class FillTablesListTask extends AbstractMonitorableTask { |
289 | 41534 | jjdelcerro | |
290 | 41492 | jjdelcerro | public FillTablesListTask() {
|
291 | super("Export"); |
||
292 | } |
||
293 | 41534 | jjdelcerro | |
294 | 41492 | jjdelcerro | protected SimpleTaskStatus getSimpleTaskStatus() {
|
295 | return (SimpleTaskStatus) this.getTaskStatus(); |
||
296 | } |
||
297 | 41534 | jjdelcerro | |
298 | 41492 | jjdelcerro | public void run() { |
299 | 41534 | jjdelcerro | |
300 | 41492 | jjdelcerro | JDBCServerExplorerParameters explorerParameters = provider.getExplorerParameters(); |
301 | 41534 | jjdelcerro | if (provider.getExplorerParameters() == null) { |
302 | 41492 | jjdelcerro | return;
|
303 | 41486 | jjdelcerro | } |
304 | 41492 | jjdelcerro | final SimpleTaskStatus status = this.getSimpleTaskStatus(); |
305 | try {
|
||
306 | status.setAutoremove(true);
|
||
307 | 41534 | jjdelcerro | |
308 | 41492 | jjdelcerro | DataManager dataManager = DALLocator.getDataManager(); |
309 | 41534 | jjdelcerro | |
310 | 41492 | jjdelcerro | this.getSimpleTaskStatus().message("Connecting server"); |
311 | explorerParameters.setShowInformationDBTables(false);
|
||
312 | 43020 | jjdelcerro | final JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer(
|
313 | 41492 | jjdelcerro | explorerParameters.getExplorerName(), |
314 | explorerParameters |
||
315 | ); |
||
316 | 41534 | jjdelcerro | |
317 | 41492 | jjdelcerro | this.getSimpleTaskStatus().message("Retrieving tables"); |
318 | final List<JDBCStoreParameters> tables = explorer.list(); |
||
319 | 41534 | jjdelcerro | |
320 | 41492 | jjdelcerro | this.getSimpleTaskStatus().message("Add tables"); |
321 | 41534 | jjdelcerro | |
322 | 41492 | jjdelcerro | SwingUtilities.invokeAndWait(new Runnable() { |
323 | public void run() { |
||
324 | 43020 | jjdelcerro | txtSchema.setText(explorer.createSQLBuilder().default_schema()); |
325 | |||
326 | 41492 | jjdelcerro | DefaultListModel lmodel = new DefaultListModel(); |
327 | Iterator<JDBCStoreParameters> it = tables.iterator();
|
||
328 | 41534 | jjdelcerro | while (it.hasNext()) {
|
329 | if (status.isCancelled()) {
|
||
330 | 41492 | jjdelcerro | status.cancel(); |
331 | break;
|
||
332 | } |
||
333 | JDBCStoreParameters table = it.next(); |
||
334 | lmodel.addElement(new TableItem(table));
|
||
335 | } |
||
336 | lstTables.setModel(lmodel); |
||
337 | 41509 | jjdelcerro | //lstTables.setEnabled(true);
|
338 | 41492 | jjdelcerro | rdoInsert.setEnabled(true);
|
339 | } |
||
340 | }); |
||
341 | 41534 | jjdelcerro | |
342 | 41492 | jjdelcerro | status.message("finish");
|
343 | status.terminate(); |
||
344 | 41534 | jjdelcerro | |
345 | 41492 | jjdelcerro | } catch (final Exception ex) { |
346 | logger.warn("Fail to fill tables list", ex);
|
||
347 | 41534 | jjdelcerro | if (status.isCancellationRequested()) {
|
348 | 41492 | jjdelcerro | status.cancel(); |
349 | } |
||
350 | 41534 | jjdelcerro | if (status.isRunning()) {
|
351 | 41492 | jjdelcerro | try {
|
352 | SwingUtilities.invokeAndWait(new Runnable() { |
||
353 | public void run() { |
||
354 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
355 | ExporttoSwingManager manager = ExporttoSwingLocator.getSwingManager(); |
||
356 | 41534 | jjdelcerro | |
357 | 41492 | jjdelcerro | manager.showMessage( |
358 | i18nManager.getTranslation("_Warning"),
|
||
359 | i18nManager.getTranslation("_There_have_been_problems_filling_data_in_panel")
|
||
360 | + " (" + getPanelTitle() + ")", |
||
361 | ex, |
||
362 | null
|
||
363 | ); |
||
364 | } |
||
365 | }); |
||
366 | } catch (Exception ex2) { |
||
367 | logger.warn("Can't show error message", ex2);
|
||
368 | } |
||
369 | } |
||
370 | } finally {
|
||
371 | status.terminate(); |
||
372 | status.remove(); |
||
373 | } |
||
374 | 41534 | jjdelcerro | |
375 | 41486 | jjdelcerro | } |
376 | 41534 | jjdelcerro | |
377 | 41486 | jjdelcerro | } |
378 | 41534 | jjdelcerro | |
379 | 40435 | jjdelcerro | } |