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