svn-gvsig-desktop / trunk / extensions / extJDBC / src / com / iver / cit / gvsig / jdbc_spatial / gui / jdbcwizard / WizardJDBC.java @ 2269
History | View | Annotate | Download (16.4 KB)
1 |
package com.iver.cit.gvsig.jdbc_spatial.gui.jdbcwizard; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.CardLayout; |
5 |
import java.awt.FlowLayout; |
6 |
import java.io.IOException; |
7 |
import java.sql.Connection; |
8 |
import java.sql.DatabaseMetaData; |
9 |
import java.sql.DriverManager; |
10 |
import java.sql.ResultSet; |
11 |
import java.sql.ResultSetMetaData; |
12 |
import java.sql.SQLException; |
13 |
import java.sql.Statement; |
14 |
import java.util.ArrayList; |
15 |
import java.util.HashMap; |
16 |
|
17 |
import javax.swing.JButton; |
18 |
import javax.swing.JOptionPane; |
19 |
import javax.swing.JPanel; |
20 |
|
21 |
import com.hardcode.driverManager.DriverLoadException; |
22 |
import com.hardcode.gdbms.engine.data.NoSuchTableException; |
23 |
import com.iver.andami.PluginServices; |
24 |
import com.iver.andami.messages.NotificationManager; |
25 |
import com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver; |
26 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
27 |
import com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver; |
28 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
29 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
30 |
import com.iver.cit.gvsig.gui.WizardPanel; |
31 |
import com.iver.utiles.NotExistInXMLEntity; |
32 |
import com.iver.utiles.StringUtilities; |
33 |
import com.iver.utiles.XMLEntity; |
34 |
/**
|
35 |
* @author Fernando Gonz?lez Cort?s
|
36 |
*/
|
37 |
public class WizardJDBC extends WizardPanel{ |
38 |
|
39 |
private JPanel pnlWizard = null; |
40 |
private JPanel jPanel1 = null; |
41 |
private JButton btnBack = null; |
42 |
private JButton btnNext = null; |
43 |
private ConnectionPanel connectionPanel = null; |
44 |
private DBLayerDefinition dbLayerDefinition = null; |
45 |
private FieldSelection fieldSelection = null; |
46 |
|
47 |
private int step = 0; |
48 |
private final int nsteps = 5; |
49 |
private static final String CONNECTION = "conn"; |
50 |
private static final String LAYER_DEFINITION = "layerdef"; |
51 |
private static final String FIELD_SELECTION= "fieldsel"; |
52 |
private VectorialJDBCDriver driver;
|
53 |
|
54 |
private GeomFieldSelection geomFieldSelection = null; |
55 |
private UniqueFieldSelection uniqueFieldSelection = null; |
56 |
|
57 |
private HashMap settings = new HashMap(); |
58 |
/**
|
59 |
* This is the default constructor
|
60 |
*/
|
61 |
public WizardJDBC() {
|
62 |
super();
|
63 |
initialize(); |
64 |
} |
65 |
/**
|
66 |
* This method initializes this
|
67 |
*
|
68 |
* @return void
|
69 |
*/
|
70 |
private void initialize() { |
71 |
setTabName("JDBC");
|
72 |
this.setLayout(new BorderLayout()); |
73 |
this.setSize(300, 270); |
74 |
this.add(getPnlWizard(), java.awt.BorderLayout.CENTER);
|
75 |
this.add(getJPanel1(), java.awt.BorderLayout.SOUTH);
|
76 |
connectionPanel.setDrivers(getDriverNames()); |
77 |
enableButtons(); |
78 |
|
79 |
|
80 |
XMLEntity xml = PluginServices.getPluginServices(this)
|
81 |
.getPersistentXML(); |
82 |
|
83 |
if (xml == null) { |
84 |
xml = new XMLEntity();
|
85 |
} |
86 |
|
87 |
if (!xml.contains("jdbc-connections")) { |
88 |
String[] servers = new String[0]; |
89 |
xml.putProperty("jdbc-connections", servers);
|
90 |
} |
91 |
|
92 |
try {
|
93 |
String[] servers = xml.getStringArrayProperty("jdbc-connections"); |
94 |
|
95 |
for (int i = 0; i < servers.length; i++) { |
96 |
ConnectionSettings cs = new ConnectionSettings();
|
97 |
cs.setFromString(servers[i]); |
98 |
settings.put(cs.getName(), cs); |
99 |
} |
100 |
getConnectionPanel().setSettings(settings); |
101 |
} catch (NotExistInXMLEntity e) {
|
102 |
} |
103 |
|
104 |
} |
105 |
|
106 |
private String[] getDriverNames(){ |
107 |
Class[] classes = new Class[] { VectorialJDBCDriver.class }; |
108 |
|
109 |
ArrayList ret = new ArrayList(); |
110 |
String[] driverNames = LayerFactory.getDM().getDriverNames(); |
111 |
|
112 |
for (int i = 0; i < driverNames.length; i++) { |
113 |
boolean is = false; |
114 |
|
115 |
for (int j = 0; j < classes.length; j++) { |
116 |
if (LayerFactory.getDM().isA(driverNames[i], classes[j])) {
|
117 |
ret.add(driverNames[i]); |
118 |
} |
119 |
} |
120 |
} |
121 |
|
122 |
return (String[]) ret.toArray(new String[0]); |
123 |
|
124 |
} |
125 |
|
126 |
private void enableButtons(){ |
127 |
getBtnBack().setEnabled(step > 0);
|
128 |
getBtnNext().setEnabled(step < nsteps - 1);
|
129 |
} |
130 |
|
131 |
/**
|
132 |
* This method initializes pnlWizard
|
133 |
*
|
134 |
* @return javax.swing.JPanel
|
135 |
*/
|
136 |
private JPanel getPnlWizard() { |
137 |
if (pnlWizard == null) { |
138 |
pnlWizard = new JPanel(); |
139 |
pnlWizard.setLayout(new CardLayout()); |
140 |
pnlWizard.add(getConnectionPanel(), CONNECTION); |
141 |
pnlWizard.add(getDbLayerDefinition(), LAYER_DEFINITION); |
142 |
pnlWizard.add(getFieldSelection(), FIELD_SELECTION); |
143 |
pnlWizard.add(getGeomFieldSelection(), getGeomFieldSelection().getName()); |
144 |
pnlWizard.add(getUniqueFieldSelection(), getUniqueFieldSelection().getName()); |
145 |
} |
146 |
return pnlWizard;
|
147 |
} |
148 |
/**
|
149 |
* This method initializes jPanel1
|
150 |
*
|
151 |
* @return javax.swing.JPanel
|
152 |
*/
|
153 |
private JPanel getJPanel1() { |
154 |
if (jPanel1 == null) { |
155 |
FlowLayout flowLayout1 = new FlowLayout(); |
156 |
jPanel1 = new JPanel(); |
157 |
jPanel1.setLayout(flowLayout1); |
158 |
jPanel1.setPreferredSize(new java.awt.Dimension(10,25)); |
159 |
flowLayout1.setHgap(0);
|
160 |
flowLayout1.setVgap(4);
|
161 |
jPanel1.add(getBtnBack(), null);
|
162 |
jPanel1.add(getBtnNext(), null);
|
163 |
} |
164 |
return jPanel1;
|
165 |
} |
166 |
/**
|
167 |
* This method initializes btnBack
|
168 |
*
|
169 |
* @return javax.swing.JButton
|
170 |
*/
|
171 |
private JButton getBtnBack() { |
172 |
if (btnBack == null) { |
173 |
btnBack = new JButton(); |
174 |
btnBack.setText(PluginServices.getText(this, "back")); |
175 |
btnBack.setPreferredSize(new java.awt.Dimension(63,18)); |
176 |
btnBack.addActionListener(new java.awt.event.ActionListener() {
|
177 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
178 |
step--; |
179 |
enableButtons(); |
180 |
((CardLayout)pnlWizard.getLayout()).previous(pnlWizard);
|
181 |
} |
182 |
}); |
183 |
} |
184 |
return btnBack;
|
185 |
} |
186 |
/**
|
187 |
* This method initializes btnNext
|
188 |
*
|
189 |
* @return javax.swing.JButton
|
190 |
*/
|
191 |
private JButton getBtnNext() { |
192 |
if (btnNext == null) { |
193 |
btnNext = new JButton(); |
194 |
btnNext.setText(PluginServices.getText(this, "next")); |
195 |
btnNext.setPreferredSize(new java.awt.Dimension(62,18)); |
196 |
btnNext.addActionListener(new java.awt.event.ActionListener() {
|
197 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
198 |
boolean done = false; |
199 |
if (step == 0) done = connectionPanel.done(); |
200 |
else if (step == 1) done = dbLayerDefinition.done(); |
201 |
else if (step == 2) done = fieldSelection.done(); |
202 |
else if (step == 3) done = geomFieldSelection.done(); |
203 |
if (done){
|
204 |
try {
|
205 |
if (step == 0){ |
206 |
dbLayerDefinition.setTables(getTableNames()); |
207 |
saveConnection(); |
208 |
} |
209 |
else if (step == 1) { |
210 |
fieldSelection.setFields(getTableFields()); |
211 |
}else if (step == 2) { |
212 |
geomFieldSelection.setFields(getFields()); |
213 |
}else if (step == 3) { |
214 |
uniqueFieldSelection.setFields(getTableFields()); |
215 |
} |
216 |
step++; |
217 |
enableButtons(); |
218 |
((CardLayout)pnlWizard.getLayout()).next(pnlWizard);
|
219 |
} catch (SQLException e1) { |
220 |
NotificationManager.addError("No se pudieron obtener los metadatos de la base de datos", e1);
|
221 |
} catch (DriverLoadException e1) {
|
222 |
NotificationManager.addError("No se pudo cargar el driver", e1);
|
223 |
} |
224 |
}else{
|
225 |
JOptionPane.showMessageDialog(WizardJDBC.this, "No estan todos los datos rellenos", "Error", JOptionPane.ERROR_MESSAGE); |
226 |
|
227 |
} |
228 |
} |
229 |
}); |
230 |
} |
231 |
return btnNext;
|
232 |
} |
233 |
|
234 |
private void saveConnection() { |
235 |
ConnectionSettings cs = new ConnectionSettings();
|
236 |
cs.setDb(connectionPanel.getDBName()); |
237 |
cs.setDriver(connectionPanel.getDriver()); |
238 |
cs.setHost(connectionPanel.getHost()); |
239 |
cs.setPort(connectionPanel.getPort()); |
240 |
cs.setUser(connectionPanel.getUser()); |
241 |
cs.setName(connectionPanel.getSettingsName()); |
242 |
|
243 |
// if (settings.get(connectionPanel.getSettingsName())== null){
|
244 |
settings.put(connectionPanel.getSettingsName(), cs); |
245 |
// connectionPanel.setSettings(settings);
|
246 |
//}
|
247 |
|
248 |
PluginServices ps = PluginServices.getPluginServices(this);
|
249 |
XMLEntity xml = ps.getPersistentXML(); |
250 |
|
251 |
try {
|
252 |
String[] connections = xml.getStringArrayProperty("jdbc-connections"); |
253 |
String[] newConnections = new String[connections.length + 1]; |
254 |
System.arraycopy(connections, 0, newConnections, 0, connections.length); |
255 |
newConnections[connections.length] = cs.toString(); |
256 |
xml.putProperty("jdbc-connections", newConnections);
|
257 |
} catch (NotExistInXMLEntity e) {
|
258 |
xml.putProperty("jdbc-connections", new String[] { cs.toString() }); |
259 |
} |
260 |
|
261 |
ps.setPersistentXML(xml); |
262 |
|
263 |
} |
264 |
|
265 |
private String[] getTableNames() throws SQLException, DriverLoadException { |
266 |
Connection c = DriverManager.getConnection(getConnectionString(), connectionPanel.getUser(), |
267 |
connectionPanel.getPassword()); |
268 |
|
269 |
DatabaseMetaData dbmd = c.getMetaData();
|
270 |
ResultSet rs = dbmd.getTables(null, null, null, null); |
271 |
ArrayList ret = new ArrayList(); |
272 |
while (rs.next()){
|
273 |
ret.add(rs.getString("TABLE_NAME"));
|
274 |
} |
275 |
|
276 |
return (String[]) ret.toArray(new String[0]); |
277 |
} |
278 |
|
279 |
private String[] getTableFields() throws SQLException, DriverLoadException{ |
280 |
Connection c = DriverManager.getConnection(getConnectionString(), connectionPanel.getUser(), |
281 |
connectionPanel.getPassword()); |
282 |
Statement st = c.createStatement();
|
283 |
ResultSet rs = st.executeQuery("select * from " + dbLayerDefinition.getTable()); |
284 |
ResultSetMetaData rsmd = rs.getMetaData();
|
285 |
|
286 |
String[] ret = new String[rsmd.getColumnCount()]; |
287 |
|
288 |
for (int i = 0; i < ret.length; i++) { |
289 |
ret[i] = rsmd.getColumnName(i+1);
|
290 |
} |
291 |
|
292 |
return ret;
|
293 |
} |
294 |
|
295 |
/**
|
296 |
* This method initializes connectionPanel
|
297 |
*
|
298 |
* @return com.iver.cit.gvsig.gui.Panels.ConnectionPanel
|
299 |
*/
|
300 |
private ConnectionPanel getConnectionPanel() {
|
301 |
if (connectionPanel == null) { |
302 |
connectionPanel = new ConnectionPanel();
|
303 |
connectionPanel.setName("connectionPanel");
|
304 |
connectionPanel.setDrivers(getDriverNames()); |
305 |
} |
306 |
return connectionPanel;
|
307 |
} |
308 |
/**
|
309 |
* This method initializes dbLayerDefinition
|
310 |
*
|
311 |
* @return com.iver.cit.gvsig.gui.Panels.dbLayerDefinition
|
312 |
*/
|
313 |
private DBLayerDefinition getDbLayerDefinition() {
|
314 |
if (dbLayerDefinition == null) { |
315 |
dbLayerDefinition = new DBLayerDefinition();
|
316 |
dbLayerDefinition.setName("dbLayerDefinition");
|
317 |
} |
318 |
return dbLayerDefinition;
|
319 |
} |
320 |
/**
|
321 |
* This method initializes fieldSelection
|
322 |
*
|
323 |
* @return com.iver.cit.gvsig.gui.Panels.FieldSelection
|
324 |
*/
|
325 |
private FieldSelection getFieldSelection() {
|
326 |
if (fieldSelection == null) { |
327 |
fieldSelection = new FieldSelection();
|
328 |
fieldSelection.setName("fieldSelection");
|
329 |
} |
330 |
return fieldSelection;
|
331 |
} |
332 |
|
333 |
public VectorialJDBCDriver getDriver() throws DriverLoadException{ |
334 |
if (driver == null){ |
335 |
driver = (VectorialJDBCDriver) LayerFactory.getDM().getDriver(connectionPanel.getDriver()); |
336 |
} |
337 |
|
338 |
return driver;
|
339 |
} |
340 |
|
341 |
public String getConnectionString() throws DriverLoadException{ |
342 |
String connectionString = getDriver().getConnectionStringBeginning() + "//" + connectionPanel.getHost(); |
343 |
|
344 |
if (connectionPanel.getPort().trim().length() > 0) { |
345 |
connectionString += (":" + connectionPanel.getPort());
|
346 |
} else {
|
347 |
connectionString += (":" + driver.getDefaultPort());
|
348 |
} |
349 |
|
350 |
connectionString += ("/" + connectionPanel.getDBName());
|
351 |
|
352 |
return connectionString;
|
353 |
} |
354 |
|
355 |
public String getLayerName(){ |
356 |
return dbLayerDefinition.getLayerName();
|
357 |
} |
358 |
|
359 |
public String[] getFields() throws DriverLoadException{ |
360 |
String[] fields = fieldSelection.getFields(); |
361 |
String geomField = geomFieldSelection.getField();
|
362 |
int geomIndex = 0; |
363 |
for (int i = 0; i < fields.length; i++) { |
364 |
if (fields[i].equals(geomField)){
|
365 |
geomIndex = i; |
366 |
} |
367 |
} |
368 |
|
369 |
String aux = fields[0]; |
370 |
fields[0] = getDriver().getGeometryField(fields[geomIndex]);
|
371 |
fields[geomIndex] = aux; |
372 |
|
373 |
return fields;
|
374 |
} |
375 |
/**
|
376 |
* @return
|
377 |
*/
|
378 |
public String getUser() { |
379 |
return connectionPanel.getUser();
|
380 |
} |
381 |
/**
|
382 |
* @return
|
383 |
*/
|
384 |
public String getPassword() { |
385 |
return connectionPanel.getPassword();
|
386 |
} |
387 |
/**
|
388 |
* @return
|
389 |
*/
|
390 |
public String getTable() { |
391 |
return dbLayerDefinition.getTable();
|
392 |
} |
393 |
|
394 |
/**
|
395 |
* @return
|
396 |
* @throws DriverLoadException
|
397 |
*/
|
398 |
public int getFID() throws DriverLoadException { |
399 |
String[] fields = getFields(); |
400 |
for (int i = 0; i < fields.length; i++) { |
401 |
if (fields[i].equals(uniqueFieldSelection.getField())){
|
402 |
return i+1; |
403 |
} |
404 |
} |
405 |
|
406 |
throw new RuntimeException(); |
407 |
} |
408 |
|
409 |
/**
|
410 |
* This method initializes geomFieldSelection
|
411 |
*
|
412 |
* @return com.iver.cit.gvsig.gui.jdbcwizard.GeomFieldSelection
|
413 |
*/
|
414 |
private GeomFieldSelection getGeomFieldSelection() {
|
415 |
if (geomFieldSelection == null) { |
416 |
geomFieldSelection = new GeomFieldSelection();
|
417 |
geomFieldSelection.setName("geomFieldSelection");
|
418 |
} |
419 |
return geomFieldSelection;
|
420 |
} |
421 |
/**
|
422 |
* This method initializes uniqueFieldSelection
|
423 |
*
|
424 |
* @return com.iver.cit.gvsig.gui.jdbcwizard.UniqueFieldSelection
|
425 |
*/
|
426 |
private UniqueFieldSelection getUniqueFieldSelection() {
|
427 |
if (uniqueFieldSelection == null) { |
428 |
uniqueFieldSelection = new UniqueFieldSelection();
|
429 |
uniqueFieldSelection.setName("uniqueFieldSelection");
|
430 |
uniqueFieldSelection.setWizard(this);
|
431 |
} |
432 |
return uniqueFieldSelection;
|
433 |
} |
434 |
public void initWizard() { |
435 |
} |
436 |
|
437 |
/* (non-Javadoc)
|
438 |
* @see com.iver.cit.gvsig.gui.WizardPanel#execute()
|
439 |
*/
|
440 |
public void execute() { |
441 |
} |
442 |
|
443 |
/* (non-Javadoc)
|
444 |
* @see com.iver.cit.gvsig.gui.WizardPanel#getLayer()
|
445 |
*/
|
446 |
public FLayer getLayer() {
|
447 |
WizardJDBC wiz = this;
|
448 |
//true -> desconectado
|
449 |
if (false){ |
450 |
try {
|
451 |
String dbURL = wiz.getConnectionString();
|
452 |
String user = wiz.getUser();
|
453 |
String pwd = wiz.getPassword();
|
454 |
String layerName = wiz.getLayerName();
|
455 |
String fields = StringUtilities.getComaSeparated(wiz.getFields());
|
456 |
String tableName = wiz.getTable();
|
457 |
String whereClause = ""; |
458 |
int fidField = wiz.getFID();
|
459 |
|
460 |
|
461 |
// TODO: ?C?mo se hace esto para que sea gen?rico?.
|
462 |
// Supongo que el wizard te tiene que decir qu? driver
|
463 |
// quieres usar....
|
464 |
Connection conn = DriverManager.getConnection(dbURL, user, pwd); |
465 |
conn.setAutoCommit(false);
|
466 |
|
467 |
VectorialJDBCDriver driver = wiz.getDriver(); |
468 |
driver.setData(conn, tableName, fields, whereClause, fidField); |
469 |
if (dbLayerDefinition.getWorkingArea() != null){ |
470 |
driver.setWorkingArea(dbLayerDefinition.getWorkingArea()); |
471 |
} |
472 |
/* Disconnected d = new Disconnected(driver, layerName);
|
473 |
PluginServices.backgroundExecution(d);
|
474 |
lyr = d.getLayer();
|
475 |
*/
|
476 |
FLayer lyr = LayerFactory.createDisconnectedDBLayer(driver, layerName, null, null); |
477 |
if (lyr != null) { |
478 |
lyr.setVisible(true);
|
479 |
} |
480 |
|
481 |
return lyr;
|
482 |
} catch (SQLException e) { |
483 |
// TODO Auto-generated catch block
|
484 |
e.printStackTrace(); |
485 |
} catch (DriverLoadException e) {
|
486 |
e.printStackTrace(); |
487 |
} catch (IOException e) { |
488 |
e.printStackTrace(); |
489 |
} catch (DriverIOException e) {
|
490 |
e.printStackTrace(); |
491 |
} catch (NoSuchTableException e) {
|
492 |
e.printStackTrace(); |
493 |
} catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
494 |
e.printStackTrace(); |
495 |
} catch (ClassNotFoundException e) { |
496 |
e.printStackTrace(); |
497 |
} |
498 |
}else{
|
499 |
try {
|
500 |
String dbURL = wiz.getConnectionString();
|
501 |
String user = wiz.getUser();
|
502 |
String pwd = wiz.getPassword();
|
503 |
String layerName = wiz.getLayerName();
|
504 |
String fields = StringUtilities.getComaSeparated(wiz.getFields());
|
505 |
String tableName = wiz.getTable();
|
506 |
String whereClause = ""; |
507 |
int fidField = wiz.getFID();;
|
508 |
|
509 |
|
510 |
// TODO: ?C?mo se hace esto para que sea gen?rico?.
|
511 |
// Supongo que el wizard te tiene que decir qu? driver
|
512 |
// quieres usar....
|
513 |
// Class.forName("org.postgresql.Driver");
|
514 |
// Class.forName("com.mysql.jdbc.Driver");
|
515 |
Connection conn = DriverManager.getConnection(dbURL, user, pwd); |
516 |
conn.setAutoCommit(false);
|
517 |
|
518 |
VectorialJDBCDriver driver = wiz.getDriver(); |
519 |
if (driver instanceof DefaultDBDriver) |
520 |
{ |
521 |
DefaultDBDriver dbDriver = (DefaultDBDriver) driver; |
522 |
dbDriver.setData(conn, tableName, fields, whereClause, fidField); |
523 |
if (dbLayerDefinition.getWorkingArea() != null){ |
524 |
driver.setWorkingArea(dbLayerDefinition.getWorkingArea()); |
525 |
} |
526 |
} |
527 |
|
528 |
return LayerFactory.createDBLayer(driver, layerName, null); |
529 |
} catch (SQLException e) { |
530 |
// TODO Auto-generated catch block
|
531 |
e.printStackTrace(); |
532 |
/* } catch (ClassNotFoundException e) {
|
533 |
// TODO Auto-generated catch block
|
534 |
e.printStackTrace(); */
|
535 |
} catch (DriverLoadException e) {
|
536 |
e.printStackTrace(); |
537 |
} |
538 |
} |
539 |
return null; |
540 |
} |
541 |
} |