svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.xml2db / org.gvsig.xml2db.swing / org.gvsig.xml2db.swing.impl / src / main / java / org / gvsig / xml2db / swing / impl / copyxml2db / CopyXml2dbPanel.java @ 47607
History | View | Annotate | Download (19.1 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright (c) 2007-2023 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.xml2db.swing.impl.copyxml2db; |
24 |
|
25 |
import java.awt.Color; |
26 |
import java.awt.event.ActionEvent; |
27 |
import java.io.File; |
28 |
import java.nio.charset.Charset; |
29 |
import java.util.ArrayList; |
30 |
import java.util.List; |
31 |
import java.util.Locale; |
32 |
import javax.swing.AbstractListModel; |
33 |
import javax.swing.ImageIcon; |
34 |
import javax.swing.JComponent; |
35 |
import javax.swing.JOptionPane; |
36 |
import javax.swing.SwingUtilities; |
37 |
import javax.swing.event.ChangeEvent; |
38 |
import org.apache.commons.io.FilenameUtils; |
39 |
import org.apache.commons.lang3.StringUtils; |
40 |
import org.cresques.cts.IProjection; |
41 |
import org.gvsig.fmap.dal.DALLocator; |
42 |
import org.gvsig.fmap.dal.DataManager; |
43 |
import org.gvsig.fmap.dal.PersonalDatabaseServerExplorerFactory; |
44 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
45 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
46 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
47 |
import org.gvsig.fmap.dal.swing.ProjectionPickerController; |
48 |
import org.gvsig.tools.ToolsLocator; |
49 |
import org.gvsig.tools.i18n.I18nManager; |
50 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
51 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
52 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
53 |
import org.gvsig.tools.swing.api.pickercontroller.CharsetPickerController; |
54 |
import org.gvsig.tools.swing.api.pickercontroller.FilePickerController; |
55 |
import org.gvsig.tools.swing.api.pickercontroller.PickerController; |
56 |
import org.gvsig.tools.swing.api.task.TaskStatusController; |
57 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
58 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
59 |
import org.gvsig.tools.task.SimpleTaskStatus; |
60 |
import org.gvsig.tools.util.LabeledValue; |
61 |
import org.gvsig.tools.util.LabeledValueImpl; |
62 |
import org.gvsig.xml2db.lib.api.Xml2dbLocator; |
63 |
import org.gvsig.xml2db.lib.api.Xml2dbManager; |
64 |
import org.gvsig.xml2db.lib.api.xmlinfo.XMLInfo; |
65 |
import org.gvsig.xml2db.lib.api.xmlinfo.XMLTableInfo; |
66 |
import org.gvsig.xml2db.swing.Xml2dbPanel; |
67 |
import org.gvsig.xml2db.swing.impl.Task; |
68 |
import org.gvsig.xml2db.swing.impl.Xml2dbSwingCommons; |
69 |
import static org.gvsig.xml2db.swing.impl.Xml2dbSwingCommons.getPersonalDatabaseServerExplorersComboModel; |
70 |
import org.slf4j.Logger; |
71 |
import org.slf4j.LoggerFactory; |
72 |
|
73 |
/**
|
74 |
*
|
75 |
* @author jjdelcerro
|
76 |
*/
|
77 |
@SuppressWarnings("UseSpecificCatch") |
78 |
public class CopyXml2dbPanel |
79 |
extends CopyXml2dbPanelView
|
80 |
implements Xml2dbPanel
|
81 |
{ |
82 |
|
83 |
protected static final Logger LOGGER = LoggerFactory.getLogger(CopyXml2dbPanel.class); |
84 |
private PickerController<Locale> pickerLocale; |
85 |
|
86 |
private static class XMLInfoListModel extends AbstractListModel<LabeledValue<XMLTableInfo>> { |
87 |
|
88 |
private final XMLInfo xmlinfo; |
89 |
private final List<LabeledValue<XMLTableInfo>> tables; |
90 |
|
91 |
|
92 |
public XMLInfoListModel() {
|
93 |
this(null); |
94 |
} |
95 |
|
96 |
public XMLInfoListModel(XMLInfo xmlinfo) {
|
97 |
this.xmlinfo = xmlinfo;
|
98 |
if( xmlinfo != null ) { |
99 |
this.tables = new ArrayList<>(xmlinfo.size()); |
100 |
for (XMLTableInfo tableInfo : xmlinfo) {
|
101 |
this.tables.add(new LabeledValueImpl<>(tableInfo.getName(),tableInfo)); |
102 |
} |
103 |
} else {
|
104 |
this.tables = null; |
105 |
} |
106 |
} |
107 |
|
108 |
@Override
|
109 |
public LabeledValue<XMLTableInfo> getElementAt(int index) { |
110 |
if( this.tables == null ) { |
111 |
return null; |
112 |
} |
113 |
return tables.get(index);
|
114 |
} |
115 |
|
116 |
@Override
|
117 |
public int getSize() { |
118 |
if( this.tables == null ) { |
119 |
return 0; |
120 |
} |
121 |
return this.tables.size(); |
122 |
} |
123 |
|
124 |
|
125 |
} |
126 |
|
127 |
|
128 |
private Dialog dialog; |
129 |
private FilePickerController pickerXMLFile;
|
130 |
private FilePickerController pickerDbfile;
|
131 |
private ProjectionPickerController pickerProjection;
|
132 |
private CharsetPickerController pickerCharset;
|
133 |
private TaskStatusController taskStatusController;
|
134 |
private Task lastTask;
|
135 |
private XMLInfo xmlinfo;
|
136 |
|
137 |
public CopyXml2dbPanel() {
|
138 |
this.createComponents();
|
139 |
} |
140 |
|
141 |
private void createComponents() { |
142 |
this.lastTask = Task.FAKE_TASK;
|
143 |
this.xmlinfo = null; |
144 |
this.initComponents();
|
145 |
} |
146 |
|
147 |
private void initComponents() { |
148 |
|
149 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
150 |
DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager(); |
151 |
|
152 |
this.taskStatusController = ToolsSwingLocator.getTaskStatusSwingManager().createTaskStatusController(
|
153 |
lblStatusCaption, |
154 |
lblStatusMsg, |
155 |
pbStatus |
156 |
); |
157 |
this.taskStatusController.setVisible(false); |
158 |
|
159 |
this.translate();
|
160 |
|
161 |
this.pickerXMLFile = toolsSwingManager.createFilePickerController(
|
162 |
this.txtXMLFile,
|
163 |
this.btnXMLFile,
|
164 |
null,
|
165 |
"Xml2db_xmlFile",
|
166 |
null,
|
167 |
true
|
168 |
); |
169 |
this.pickerXMLFile.addChangeListener((ChangeEvent e) -> { |
170 |
doChangeXMLfile(); |
171 |
}); |
172 |
|
173 |
this.pickerCharset = toolsSwingManager.createCharsetPickerController(
|
174 |
null,
|
175 |
this.cboCharset
|
176 |
); |
177 |
|
178 |
this.btnDetectCharset.addActionListener((ActionEvent e) -> { |
179 |
doDetectCharset(); |
180 |
}); |
181 |
|
182 |
this.pickerProjection = dataSwingManager.createProjectionPickerController(
|
183 |
this.txtProjection,
|
184 |
this.btnProjection
|
185 |
); |
186 |
|
187 |
this.btnDetectProjection.addActionListener((ActionEvent e) -> { |
188 |
doDetectProjection(); |
189 |
}); |
190 |
|
191 |
this.cboDatabaseType.setModel(getPersonalDatabaseServerExplorersComboModel());
|
192 |
|
193 |
this.pickerDbfile = toolsSwingManager.createFilePickerController(
|
194 |
this.txtDatabaseFile,
|
195 |
this.btnDataBaseFile
|
196 |
); |
197 |
this.pickerDbfile.addChangeListener((ChangeEvent e) -> { |
198 |
doChangeDBfile(); |
199 |
}); |
200 |
|
201 |
this.pickerCharset.set(Charset.defaultCharset()); |
202 |
|
203 |
this.chkRegisterConnection.setSelected(true); |
204 |
this.chkConnectToRepository.setSelected(true); |
205 |
this.chkOverwriteDatabase.setSelected(false); |
206 |
|
207 |
this.chkOverwriteDatabase.addChangeListener((ChangeEvent e) -> { |
208 |
updateStateComponents(); |
209 |
}); |
210 |
|
211 |
this.cboCharset.setSelectedIndex(-1); |
212 |
|
213 |
this.pickerLocale = toolsSwingManager.createLocalePickerController(this.cboLocale); |
214 |
this.pickerLocale.set(Locale.getDefault()); |
215 |
|
216 |
toolsSwingManager.addClearButton(this.txtPrefixesForTables);
|
217 |
|
218 |
SwingUtilities.invokeLater(() -> { updateStateComponents(); });
|
219 |
|
220 |
ToolsSwingUtils.ensureRowsCols(this, 15, 80, 17, 100); |
221 |
} |
222 |
|
223 |
private void translate() { |
224 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
225 |
|
226 |
toolsSwingManager.translate(this.lblCharset);
|
227 |
toolsSwingManager.translate(this.lblRepositoryName);
|
228 |
toolsSwingManager.translate(this.lblFileXML);
|
229 |
toolsSwingManager.translate(this.lblDatabaseFile);
|
230 |
toolsSwingManager.translate(this.lblProjection);
|
231 |
toolsSwingManager.translate(this.lblRegisterConnection);
|
232 |
toolsSwingManager.translate(this.lblConnectToRepository);
|
233 |
toolsSwingManager.translate(this.lblOverwriteDatabase);
|
234 |
toolsSwingManager.translate(this.lblLocale);
|
235 |
toolsSwingManager.translate(this.btnDetectProjection);
|
236 |
toolsSwingManager.translate(this.btnDetectCharset);
|
237 |
toolsSwingManager.translate(this.lblPrefixesForTables);
|
238 |
} |
239 |
|
240 |
private boolean isProcessing() { |
241 |
return this.lastTask.isProcessing(); |
242 |
} |
243 |
|
244 |
private void updateStateComponents() { |
245 |
if( !SwingUtilities.isEventDispatchThread() ) { |
246 |
SwingUtilities.invokeLater(this::updateStateComponents); |
247 |
return;
|
248 |
} |
249 |
if( this.isProcessing() ) { |
250 |
this.pickerXMLFile.setEnabled(false); |
251 |
this.pickerCharset.setEnabled(false); |
252 |
this.pickerProjection.setEnabled(false); |
253 |
this.btnDetectCharset.setEnabled(false); |
254 |
this.btnDetectProjection.setEnabled(false); |
255 |
if( this.dialog!=null ) { |
256 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false); |
257 |
} |
258 |
return;
|
259 |
} |
260 |
PersonalDatabaseServerExplorerFactory explorerFactory = this.getExplorerFactory();
|
261 |
|
262 |
this.pickerXMLFile.setEnabled(true); |
263 |
this.pickerCharset.setEnabled(true); |
264 |
this.pickerProjection.setEnabled(true); |
265 |
|
266 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
267 |
|
268 |
File xmlfile = this.pickerXMLFile.get(); |
269 |
if( xmlfile == null || !xmlfile.exists() ) { |
270 |
this.btnDetectCharset.setEnabled(false); |
271 |
this.btnDetectProjection.setEnabled(false); |
272 |
if( xmlfile==null ) { |
273 |
this.message(i18n.getTranslation("_XML_file_is_required"), JOptionPane.WARNING_MESSAGE); |
274 |
} else {
|
275 |
this.message(i18n.getTranslation("_XML_file_not_exists"), JOptionPane.WARNING_MESSAGE); |
276 |
} |
277 |
if( this.dialog!=null ) { |
278 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false); |
279 |
} |
280 |
return;
|
281 |
} |
282 |
this.btnDetectCharset.setEnabled(true); |
283 |
this.btnDetectProjection.setEnabled(true); |
284 |
File dbfile = this.pickerDbfile.get(); |
285 |
if( dbfile == null ) { |
286 |
this.message(i18n.getTranslation("_Database_file_is_required"), JOptionPane.WARNING_MESSAGE); |
287 |
if( this.dialog!=null ) { |
288 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false); |
289 |
} |
290 |
return;
|
291 |
} |
292 |
if( explorerFactory.existsdb(dbfile) && !this.chkOverwriteDatabase.isSelected() ) { |
293 |
this.message(i18n.getTranslation("_Database_file_already_exists"), JOptionPane.WARNING_MESSAGE); |
294 |
if( this.dialog!=null ) { |
295 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false); |
296 |
} |
297 |
return;
|
298 |
} |
299 |
if( StringUtils.isBlank(this.txtRepositoryName.getText()) ) { |
300 |
this.message(i18n.getTranslation("_Repository_name_is_required"), JOptionPane.WARNING_MESSAGE); |
301 |
if( this.dialog!=null ) { |
302 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false); |
303 |
} |
304 |
return;
|
305 |
} |
306 |
if( this.dialog!=null ) { |
307 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, true); |
308 |
} |
309 |
if( this.lastTask.needToShowTheStatus()) { |
310 |
this.taskStatusController.setVisible(true); |
311 |
} else {
|
312 |
message(null);
|
313 |
this.taskStatusController.setVisible(false); |
314 |
} |
315 |
} |
316 |
|
317 |
@Override
|
318 |
public void setDialog(Dialog dialog) { |
319 |
this.dialog = dialog;
|
320 |
} |
321 |
|
322 |
@Override
|
323 |
public JComponent asJComponent() { |
324 |
return this; |
325 |
} |
326 |
|
327 |
@Override
|
328 |
public void performAction() { |
329 |
doCopy(); |
330 |
} |
331 |
|
332 |
private void doCopy() { |
333 |
File xmlfile = this.pickerXMLFile.get(); |
334 |
if( xmlfile == null || !xmlfile.exists() ) { |
335 |
return;
|
336 |
} |
337 |
IProjection userproj = this.pickerProjection.get();
|
338 |
Charset charset = this.pickerCharset.get(); |
339 |
|
340 |
File dbfile = this.pickerDbfile.get(); |
341 |
if( dbfile==null || dbfile.exists()) { |
342 |
return;
|
343 |
} |
344 |
String s = this.txtRepositoryName.getText(); |
345 |
if( StringUtils.isBlank(s) ) {
|
346 |
s = FilenameUtils.getBaseName(dbfile.getName()); |
347 |
} |
348 |
String repoName = s;
|
349 |
|
350 |
boolean registerConnection = this.chkRegisterConnection.isSelected(); |
351 |
boolean connectToRepository = this.chkConnectToRepository.isSelected(); |
352 |
|
353 |
PersonalDatabaseServerExplorerFactory explorerFactory = this.getExplorerFactory();
|
354 |
|
355 |
Xml2dbManager manager = Xml2dbLocator.getXml2dbManager(); |
356 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
357 |
|
358 |
if( explorerFactory.existsdb(dbfile) ) {
|
359 |
if( !this.chkOverwriteDatabase.isSelected() ) { |
360 |
message(i18n.getTranslation("_Database_file_already_exists"), JOptionPane.WARNING_MESSAGE); |
361 |
return;
|
362 |
} |
363 |
} |
364 |
|
365 |
String tablesPrefix = StringUtils.defaultIfBlank(this.txtPrefixesForTables.getText(), null); |
366 |
|
367 |
Locale locale = this.pickerLocale.get(); |
368 |
|
369 |
this.lastTask = new Task( |
370 |
"Xml2db_copy",
|
371 |
i18n.getTranslation("_Copy_xml_to_database"),
|
372 |
this::updateStateComponents,
|
373 |
taskStatusController |
374 |
) { |
375 |
@Override
|
376 |
protected void task(SimpleTaskStatus taskStatus) throws Throwable{ |
377 |
try {
|
378 |
if( explorerFactory.existsdb(dbfile) ) {
|
379 |
taskStatus.message(i18n.getTranslation("_Removing_existing_database"));
|
380 |
explorerFactory.serverStop(); |
381 |
explorerFactory.removedb(dbfile); |
382 |
explorerFactory.serverStart(); |
383 |
} |
384 |
XMLInfo xmlinfo = manager.extractStructure(xmlfile, charset, userproj, locale, tablesPrefix, taskStatus); |
385 |
if( xmlinfo==null ) { |
386 |
throw new RuntimeException("Can't process xml file"); |
387 |
} |
388 |
if( userproj==null ) { |
389 |
if( xmlinfo.getSrid()==null ) { |
390 |
if( xmlinfo.hasGeometries() ) {
|
391 |
throw new RuntimeException("_Projection_is_required"); |
392 |
} |
393 |
} else {
|
394 |
pickerProjection.set(xmlinfo.getSrid()); |
395 |
} |
396 |
} |
397 |
if( xmlinfo.getCharset()!=null && charset==null ) { |
398 |
pickerCharset.set(xmlinfo.getCharset()); |
399 |
} |
400 |
JDBCServerExplorerParameters params = manager.createDatabase(explorerFactory.getName(), dbfile, xmlinfo, taskStatus); |
401 |
if( params == null ) { |
402 |
throw new RuntimeException("Can't copy xml to database"); |
403 |
} |
404 |
if( registerConnection ) {
|
405 |
addToConnectionPool(params,repoName); |
406 |
} |
407 |
manager.copyXml2Db(xmlfile, xmlinfo.getCharset(), params, tablesPrefix, taskStatus); |
408 |
if( connectToRepository) {
|
409 |
DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager(); |
410 |
dataSwingManager.connectToDatabaseWorkspace(params); |
411 |
} |
412 |
} catch(Exception ex) { |
413 |
LOGGER.warn("Can't copy xml to database",ex);
|
414 |
throw ex;
|
415 |
} |
416 |
} |
417 |
|
418 |
@Override
|
419 |
protected void postTask() { |
420 |
// message(null);
|
421 |
} |
422 |
}; |
423 |
|
424 |
this.lastTask.start();
|
425 |
|
426 |
} |
427 |
|
428 |
public void addToConnectionPool(JDBCServerExplorerParameters params, String id) { |
429 |
DataManager dataManager = DALLocator.getDataManager(); |
430 |
dataManager.getDataServerExplorerPool().add(id, params); |
431 |
} |
432 |
|
433 |
private void doChangeXMLfile() { |
434 |
File xmlfile = this.pickerXMLFile.get(); |
435 |
if( xmlfile != null ) { |
436 |
File dbfile = this.pickerDbfile.get(); |
437 |
if( dbfile == null ) { |
438 |
dbfile = new File(FilenameUtils.removeExtension(xmlfile.getAbsolutePath())); |
439 |
this.pickerDbfile.set(dbfile);
|
440 |
this.txtRepositoryName.setText(FilenameUtils.getBaseName(xmlfile.getName()));
|
441 |
} |
442 |
} |
443 |
updateStateComponents(); |
444 |
} |
445 |
|
446 |
private void doChangeDBfile() { |
447 |
File dbfile = this.pickerDbfile.get(); |
448 |
if( dbfile != null ) { |
449 |
String repoName = this.txtRepositoryName.getText(); |
450 |
if( StringUtils.isBlank(repoName) ) {
|
451 |
this.txtRepositoryName.setText(FilenameUtils.getBaseName(dbfile.getName()));
|
452 |
} |
453 |
} |
454 |
updateStateComponents(); |
455 |
} |
456 |
|
457 |
private void doDetectCharset() { |
458 |
Xml2dbManager manager = Xml2dbLocator.getXml2dbManager(); |
459 |
this.pickerCharset.set(manager.detectCharset(this.pickerXMLFile.get())); |
460 |
} |
461 |
|
462 |
private void doDetectProjection() { |
463 |
Xml2dbManager manager = Xml2dbLocator.getXml2dbManager(); |
464 |
|
465 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
466 |
|
467 |
Task task = new Task(
|
468 |
"Xml2db_detectProjection",
|
469 |
i18n.getTranslation("_Detecting_projection"),
|
470 |
this::updateStateComponents,
|
471 |
taskStatusController |
472 |
) { |
473 |
@Override
|
474 |
protected void task(SimpleTaskStatus taskStatus) throws Throwable{ |
475 |
try {
|
476 |
pickerProjection.set(manager.detectProjection(pickerXMLFile.get(),taskStatus)); |
477 |
} catch(Exception ex) { |
478 |
LOGGER.warn("Can't detect projection",ex);
|
479 |
throw ex;
|
480 |
} |
481 |
} |
482 |
}; |
483 |
|
484 |
task.start(); |
485 |
|
486 |
} |
487 |
|
488 |
private void message(String msg) { |
489 |
message(msg,JOptionPane.INFORMATION_MESSAGE);
|
490 |
} |
491 |
|
492 |
private void message(final String msg, final int type) { |
493 |
if( !SwingUtilities.isEventDispatchThread() ) { |
494 |
SwingUtilities.invokeLater(() -> { this.message(msg, type);}); |
495 |
return;
|
496 |
} |
497 |
if( StringUtils.isBlank(msg) ) {
|
498 |
this.lblStatusMsg.setText(""); |
499 |
return;
|
500 |
} |
501 |
String s = msg;
|
502 |
if( type == JOptionPane.WARNING_MESSAGE ) { |
503 |
s = Xml2dbSwingCommons.getHTMLColorTag(Color.RED.darker(), msg);
|
504 |
} |
505 |
this.lblStatusMsg.setText(s);
|
506 |
this.lblStatusMsg.setVisible(true); |
507 |
} |
508 |
|
509 |
@Override
|
510 |
public ImageIcon loadImage(String imageName) { |
511 |
return ToolsSwingUtils.loadImage(this, imageName); |
512 |
} |
513 |
|
514 |
private PersonalDatabaseServerExplorerFactory getExplorerFactory() {
|
515 |
return (PersonalDatabaseServerExplorerFactory) this.cboDatabaseType.getSelectedItem(); |
516 |
} |
517 |
} |