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 @ 47336
History | View | Annotate | Download (15.6 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 org.gvsig.xml2db.swing.impl.Task; |
26 |
import java.awt.Color; |
27 |
import java.io.File; |
28 |
import java.util.ArrayList; |
29 |
import java.util.List; |
30 |
import javax.swing.AbstractListModel; |
31 |
import javax.swing.JComponent; |
32 |
import org.cresques.cts.IProjection; |
33 |
import java.nio.charset.Charset; |
34 |
import java.util.Locale; |
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.gvsig.fmap.dal.DALLocator; |
41 |
import org.gvsig.fmap.dal.DataManager; |
42 |
import org.gvsig.fmap.dal.store.h2.H2SpatialUtils; |
43 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
44 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
45 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
46 |
import org.gvsig.fmap.dal.swing.ProjectionPickerController; |
47 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
48 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
49 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
50 |
import org.gvsig.tools.swing.api.pickercontroller.CharsetPickerController; |
51 |
import org.gvsig.tools.swing.api.pickercontroller.FilePickerController; |
52 |
import org.gvsig.tools.swing.api.pickercontroller.PickerController; |
53 |
import org.gvsig.tools.swing.api.task.TaskStatusController; |
54 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
55 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
56 |
import org.gvsig.tools.task.SimpleTaskStatus; |
57 |
import org.gvsig.tools.util.LabeledValue; |
58 |
import org.gvsig.tools.util.LabeledValueImpl; |
59 |
import org.gvsig.xml2db.lib.api.Xml2dbLocator; |
60 |
import org.gvsig.xml2db.lib.api.Xml2dbManager; |
61 |
import org.gvsig.xml2db.lib.api.xmlinfo.XMLInfo; |
62 |
import org.gvsig.xml2db.lib.api.xmlinfo.XMLTableInfo; |
63 |
import org.gvsig.xml2db.swing.Xml2dbPanel; |
64 |
import org.gvsig.xml2db.swing.impl.Xml2dbSwingCommons; |
65 |
import org.slf4j.Logger; |
66 |
import org.slf4j.LoggerFactory; |
67 |
|
68 |
/**
|
69 |
*
|
70 |
* @author jjdelcerro
|
71 |
*/
|
72 |
@SuppressWarnings("UseSpecificCatch") |
73 |
public class CopyXml2dbPanel |
74 |
extends CopyXml2dbPanelView
|
75 |
implements Xml2dbPanel
|
76 |
{ |
77 |
|
78 |
protected static final Logger LOGGER = LoggerFactory.getLogger(CopyXml2dbPanel.class); |
79 |
private PickerController<Locale> pickerLocale; |
80 |
|
81 |
private static class XMLInfoListModel extends AbstractListModel<LabeledValue<XMLTableInfo>> { |
82 |
|
83 |
private final XMLInfo xmlinfo; |
84 |
private final List<LabeledValue<XMLTableInfo>> tables; |
85 |
|
86 |
|
87 |
public XMLInfoListModel() {
|
88 |
this(null); |
89 |
} |
90 |
|
91 |
public XMLInfoListModel(XMLInfo xmlinfo) {
|
92 |
this.xmlinfo = xmlinfo;
|
93 |
if( xmlinfo != null ) { |
94 |
this.tables = new ArrayList<>(xmlinfo.size()); |
95 |
for (XMLTableInfo tableInfo : xmlinfo) {
|
96 |
this.tables.add(new LabeledValueImpl<>(tableInfo.getName(),tableInfo)); |
97 |
} |
98 |
} else {
|
99 |
this.tables = null; |
100 |
} |
101 |
} |
102 |
|
103 |
@Override
|
104 |
public LabeledValue<XMLTableInfo> getElementAt(int index) { |
105 |
if( this.tables == null ) { |
106 |
return null; |
107 |
} |
108 |
return tables.get(index);
|
109 |
} |
110 |
|
111 |
@Override
|
112 |
public int getSize() { |
113 |
if( this.tables == null ) { |
114 |
return 0; |
115 |
} |
116 |
return this.tables.size(); |
117 |
} |
118 |
|
119 |
|
120 |
} |
121 |
|
122 |
|
123 |
private Dialog dialog; |
124 |
private FilePickerController pickerXMLFile;
|
125 |
private FilePickerController pickerDbfile;
|
126 |
private ProjectionPickerController pickerProjection;
|
127 |
private CharsetPickerController pickerCharset;
|
128 |
private TaskStatusController taskStatusController;
|
129 |
private Task lastTask;
|
130 |
private XMLInfo xmlinfo;
|
131 |
|
132 |
public CopyXml2dbPanel() {
|
133 |
this.createComponents();
|
134 |
} |
135 |
|
136 |
private void createComponents() { |
137 |
this.lastTask = Task.FAKE_TASK;
|
138 |
this.xmlinfo = null; |
139 |
this.initComponents();
|
140 |
} |
141 |
|
142 |
private void initComponents() { |
143 |
|
144 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
145 |
DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager(); |
146 |
|
147 |
this.taskStatusController = ToolsSwingLocator.getTaskStatusSwingManager().createTaskStatusController(
|
148 |
lblStatusCaption, |
149 |
lblStatusMsg, |
150 |
pbStatus |
151 |
); |
152 |
this.taskStatusController.setVisible(false); |
153 |
|
154 |
this.translate();
|
155 |
|
156 |
this.pickerXMLFile = toolsSwingManager.createFilePickerController(
|
157 |
this.txtXMLFile,
|
158 |
this.btnXMLFile
|
159 |
); |
160 |
this.pickerXMLFile.addChangeListener((ChangeEvent e) -> { |
161 |
doChangeXMLfile(); |
162 |
}); |
163 |
|
164 |
this.pickerCharset = toolsSwingManager.createCharsetPickerController(
|
165 |
null,
|
166 |
this.cboCharset
|
167 |
); |
168 |
|
169 |
this.pickerProjection = dataSwingManager.createProjectionPickerController(
|
170 |
this.txtProjection,
|
171 |
this.btnProjection
|
172 |
); |
173 |
|
174 |
this.pickerDbfile = toolsSwingManager.createFilePickerController(
|
175 |
this.txtDatabaseFile,
|
176 |
this.btnDataBaseFile
|
177 |
); |
178 |
this.pickerDbfile.addChangeListener((ChangeEvent e) -> { |
179 |
doChangeDBfile(); |
180 |
}); |
181 |
|
182 |
this.pickerCharset.set(Charset.defaultCharset()); |
183 |
|
184 |
this.chkRegisterConnection.setSelected(true); |
185 |
this.chkConnectToRepository.setSelected(true); |
186 |
this.chkOverwriteDatabase.setSelected(false); |
187 |
|
188 |
this.chkOverwriteDatabase.addChangeListener((ChangeEvent e) -> { |
189 |
updateStateComponents(); |
190 |
}); |
191 |
|
192 |
this.cboCharset.setSelectedIndex(-1); |
193 |
|
194 |
this.pickerLocale = toolsSwingManager.createLocalePickerController(this.cboLocale); |
195 |
this.pickerLocale.set(Locale.getDefault()); |
196 |
|
197 |
|
198 |
SwingUtilities.invokeLater(() -> { updateStateComponents(); });
|
199 |
|
200 |
ToolsSwingUtils.ensureRowsCols(this, 14, 80, 16, 100); |
201 |
} |
202 |
|
203 |
private void translate() { |
204 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
205 |
|
206 |
toolsSwingManager.translate(this.lblCharset);
|
207 |
toolsSwingManager.translate(this.lblRepositoryName);
|
208 |
toolsSwingManager.translate(this.lblFileXML);
|
209 |
toolsSwingManager.translate(this.lblDatabaseFile);
|
210 |
toolsSwingManager.translate(this.lblProjection);
|
211 |
toolsSwingManager.translate(this.lblRegisterConnection);
|
212 |
toolsSwingManager.translate(this.lblConnectToRepository);
|
213 |
toolsSwingManager.translate(this.lblOverwriteDatabase);
|
214 |
toolsSwingManager.translate(this.lblLocale);
|
215 |
} |
216 |
|
217 |
private boolean isProcessing() { |
218 |
return this.lastTask.isProcessing(); |
219 |
} |
220 |
|
221 |
private void updateStateComponents() { |
222 |
if( !SwingUtilities.isEventDispatchThread() ) { |
223 |
SwingUtilities.invokeLater(this::updateStateComponents); |
224 |
return;
|
225 |
} |
226 |
if( this.isProcessing() ) { |
227 |
this.pickerXMLFile.setEnabled(false); |
228 |
this.pickerCharset.setEnabled(false); |
229 |
this.pickerProjection.setEnabled(false); |
230 |
if( this.dialog!=null ) { |
231 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false); |
232 |
} |
233 |
return;
|
234 |
} |
235 |
this.pickerXMLFile.setEnabled(true); |
236 |
this.pickerCharset.setEnabled(true); |
237 |
this.pickerProjection.setEnabled(true); |
238 |
|
239 |
File xmlfile = this.pickerXMLFile.get(); |
240 |
if( xmlfile == null || !xmlfile.exists() ) { |
241 |
if( xmlfile==null ) { |
242 |
this.message("_XML_file_is_required", JOptionPane.WARNING_MESSAGE); |
243 |
} else {
|
244 |
this.message("_XML_file_not_exists", JOptionPane.WARNING_MESSAGE); |
245 |
} |
246 |
if( this.dialog!=null ) { |
247 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false); |
248 |
} |
249 |
return;
|
250 |
} |
251 |
File dbfile = this.pickerDbfile.get(); |
252 |
if( dbfile == null ) { |
253 |
this.message("_Database_file_is_required", JOptionPane.WARNING_MESSAGE); |
254 |
if( this.dialog!=null ) { |
255 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false); |
256 |
} |
257 |
return;
|
258 |
} |
259 |
if( H2SpatialUtils.existsH2db(dbfile) && !this.chkOverwriteDatabase.isSelected() ) { |
260 |
this.message("_Database_file_already_exists", JOptionPane.WARNING_MESSAGE); |
261 |
if( this.dialog!=null ) { |
262 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false); |
263 |
} |
264 |
return;
|
265 |
} |
266 |
if( StringUtils.isBlank(this.txtRepositoryName.getText()) ) { |
267 |
this.message("_Repository_name_is_required", JOptionPane.WARNING_MESSAGE); |
268 |
if( this.dialog!=null ) { |
269 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false); |
270 |
} |
271 |
return;
|
272 |
} |
273 |
if( this.dialog!=null ) { |
274 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, true); |
275 |
} |
276 |
if( this.lastTask.needToShowTheStatus()) { |
277 |
this.taskStatusController.setVisible(true); |
278 |
} else {
|
279 |
message(null);
|
280 |
this.taskStatusController.setVisible(false); |
281 |
} |
282 |
} |
283 |
|
284 |
@Override
|
285 |
public void setDialog(Dialog dialog) { |
286 |
this.dialog = dialog;
|
287 |
} |
288 |
|
289 |
@Override
|
290 |
public JComponent asJComponent() { |
291 |
return this; |
292 |
} |
293 |
|
294 |
@Override
|
295 |
public void performAction() { |
296 |
doCopy(); |
297 |
} |
298 |
|
299 |
private void doCopy() { |
300 |
File xmlfile = this.pickerXMLFile.get(); |
301 |
if( xmlfile == null || !xmlfile.exists() ) { |
302 |
return;
|
303 |
} |
304 |
IProjection proj = this.pickerProjection.get();
|
305 |
Charset charset = this.pickerCharset.get(); |
306 |
|
307 |
File dbfile = this.pickerDbfile.get(); |
308 |
if( dbfile==null || dbfile.exists()) { |
309 |
return;
|
310 |
} |
311 |
String s = this.txtRepositoryName.getText(); |
312 |
if( StringUtils.isBlank(s) ) {
|
313 |
s = FilenameUtils.getBaseName(dbfile.getName()); |
314 |
} |
315 |
String repoName = s;
|
316 |
|
317 |
boolean registerConnection = this.chkRegisterConnection.isSelected(); |
318 |
boolean connectToRepository = this.chkConnectToRepository.isSelected(); |
319 |
|
320 |
Xml2dbManager manager = Xml2dbLocator.getXml2dbManager(); |
321 |
|
322 |
if( H2SpatialUtils.existsH2db(dbfile) ) {
|
323 |
if( !this.chkOverwriteDatabase.isSelected() ) { |
324 |
message("Database already exists", JOptionPane.WARNING_MESSAGE); |
325 |
return;
|
326 |
} |
327 |
} |
328 |
|
329 |
Locale locale = this.pickerLocale.get(); |
330 |
|
331 |
this.lastTask = new Task( |
332 |
"Xml2db_copy",
|
333 |
"Copy xml to database",
|
334 |
this::updateStateComponents,
|
335 |
taskStatusController |
336 |
) { |
337 |
@Override
|
338 |
protected void task(SimpleTaskStatus taskStatus) throws Throwable{ |
339 |
try {
|
340 |
if( H2SpatialUtils.existsH2db(dbfile) ) {
|
341 |
taskStatus.message("Removing existing database...");
|
342 |
H2SpatialUtils.server_stop(); |
343 |
H2SpatialUtils.removeH2db(dbfile); |
344 |
H2SpatialUtils.server_start(); |
345 |
} |
346 |
XMLInfo xmlinfo = manager.extractStructure(xmlfile, charset, proj, locale, taskStatus); |
347 |
if( xmlinfo==null ) { |
348 |
throw new RuntimeException("Can't process xml file"); |
349 |
} |
350 |
if( proj==null && xmlinfo.getSrid()!=null ) { |
351 |
pickerProjection.set(xmlinfo.getSrid()); |
352 |
} |
353 |
if( xmlinfo.getCharset()!=null && charset==null ) { |
354 |
pickerCharset.set(xmlinfo.getCharset()); |
355 |
} |
356 |
JDBCServerExplorerParameters params = manager.createDatabase(dbfile, xmlinfo, taskStatus); |
357 |
if( params == null ) { |
358 |
throw new RuntimeException("Can't copy xml to database"); |
359 |
} |
360 |
if( registerConnection ) {
|
361 |
addToConnectionPool(params,repoName); |
362 |
} |
363 |
manager.copyXml2Db(xmlfile, xmlinfo.getCharset(), params, taskStatus); |
364 |
if( connectToRepository) {
|
365 |
DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager(); |
366 |
dataSwingManager.connectToDatabaseWorkspace(params); |
367 |
} |
368 |
} catch(Exception ex) { |
369 |
LOGGER.warn("Can't copy xml to database",ex);
|
370 |
throw ex;
|
371 |
} |
372 |
} |
373 |
|
374 |
@Override
|
375 |
protected void postTask() { |
376 |
// message(null);
|
377 |
} |
378 |
}; |
379 |
|
380 |
this.lastTask.start();
|
381 |
|
382 |
} |
383 |
|
384 |
public void addToConnectionPool(JDBCServerExplorerParameters params, String id) { |
385 |
DataManager dataManager = DALLocator.getDataManager(); |
386 |
dataManager.getDataServerExplorerPool().add(id, params); |
387 |
} |
388 |
|
389 |
private void doChangeXMLfile() { |
390 |
File xmlfile = this.pickerXMLFile.get(); |
391 |
if( xmlfile != null ) { |
392 |
File dbfile = this.pickerDbfile.get(); |
393 |
if( dbfile == null ) { |
394 |
dbfile = new File(FilenameUtils.removeExtension(xmlfile.getAbsolutePath())); |
395 |
this.pickerDbfile.set(dbfile);
|
396 |
this.txtRepositoryName.setText(FilenameUtils.getBaseName(xmlfile.getName()));
|
397 |
} |
398 |
} |
399 |
updateStateComponents(); |
400 |
} |
401 |
|
402 |
private void doChangeDBfile() { |
403 |
File dbfile = this.pickerDbfile.get(); |
404 |
if( dbfile != null ) { |
405 |
String repoName = this.txtRepositoryName.getText(); |
406 |
if( StringUtils.isBlank(repoName) ) {
|
407 |
this.txtRepositoryName.setText(FilenameUtils.getBaseName(dbfile.getName()));
|
408 |
} |
409 |
} |
410 |
updateStateComponents(); |
411 |
} |
412 |
|
413 |
private void message(String msg) { |
414 |
message(msg,JOptionPane.INFORMATION_MESSAGE);
|
415 |
} |
416 |
|
417 |
private void message(final String msg, final int type) { |
418 |
if( !SwingUtilities.isEventDispatchThread() ) { |
419 |
SwingUtilities.invokeLater(() -> { this.message(msg, type);}); |
420 |
return;
|
421 |
} |
422 |
if( StringUtils.isBlank(msg) ) {
|
423 |
this.lblStatusMsg.setText(""); |
424 |
return;
|
425 |
} |
426 |
String s = msg;
|
427 |
if( type == JOptionPane.WARNING_MESSAGE ) { |
428 |
s = Xml2dbSwingCommons.getHTMLColorTag(Color.RED.darker(), msg);
|
429 |
} |
430 |
this.lblStatusMsg.setText(s);
|
431 |
this.lblStatusMsg.setVisible(true); |
432 |
} |
433 |
|
434 |
} |