1 package de.webman.wminstaller.screens; 2 3 import de.webman.wminstaller.app.*; 4 import javax.swing.*; 5 import java.awt.*; 6 import java.awt.event.*; 7 import de.webman.wminstaller.tools.WholeNumberField; 8 import java.sql.*; 9 import de.webman.db.*; 10 11 17 public class ScreenDBSetup extends InstallerScreen 18 { 19 20 21 final JPanel dbPanel = new JPanel(); 22 23 JPanel optionsPanel = null; 24 JComboBox optDBType = null; 25 WholeNumberField optDBLicences = null; 26 JTextField optDBUser = null; 27 JPasswordField optDBPassword1 = null; 28 JPasswordField optDBPassword2 = null; 29 JTextField optDBName = null; 30 JTextField optDBServer = null; 31 WholeNumberField optDBPort = null; 32 33 34 private void addLine(String label, Component c) { 35 JPanel helper = new JPanel( new BorderLayout()); 36 helper.add( c, BorderLayout.WEST); 37 dbPanel.add( new JLabel( label, javax.swing.SwingConstants.RIGHT)); 38 dbPanel.add( helper); 39 } 40 41 public final static String SUPDB_SYBASE = "Sybase"; 42 public final static String SUPDB_ORACLE8I = "Oracle8i"; 43 public final static String SUPDB_POSTGRESQL = "Postgresql"; 44 static String [] dbTypes = {SUPDB_SYBASE, 45 SUPDB_ORACLE8I, 46 SUPDB_POSTGRESQL}; 47 48 public final static int SYBASE_DEFAULT_PORT = 3000; 49 public final static int ORACLE8I_DEFAULT_PORT = 1521; 50 public final static int POSTGRESQL_DEFAULT_PORT = 5432; 51 52 public ScreenDBSetup( Installer wmi, int bt) { 53 super( wmi, bt); 54 55 dbPanel.setLayout( new GridLayout( 8, 2, 5, 8)); 56 57 optDBType = new JComboBox(dbTypes); 58 addLine("DB-Typ", optDBType); 59 60 optDBLicences = new WholeNumberField(0, 4); 61 addLine("Anzahl der DB-Lizenzen", optDBLicences); 62 63 optDBUser = new JTextField( "", 12); 64 addLine("DB-Nutzername", optDBUser); 65 66 optDBPassword1 = new JPasswordField( "", 12); 67 addLine("DB-Passwort", optDBPassword1); 68 69 optDBPassword2 = new JPasswordField( "", 12); 70 addLine("DB-Passwort (nochmals)", optDBPassword2); 71 72 optDBName = new JTextField( "", 12); 73 addLine("DB-Name", optDBName); 74 75 optDBServer = new JTextField( "", 12); 76 addLine("DB-Servername", optDBServer); 77 78 optDBPort = new WholeNumberField(0, 5); 79 addLine("DB-Serverport", optDBPort); 80 81 82 83 optDBType.addActionListener(new ActionListener() { 84 public void actionPerformed(ActionEvent e) { 85 JComboBox cb = (JComboBox)e.getSource(); 86 String dbtype = (String )cb.getSelectedItem(); 87 88 if (SUPDB_SYBASE.equals(dbtype)) { 89 90 int dbport = optDBPort.getValue(); 91 if (dbport == 0) 92 optDBPort.setValue(SYBASE_DEFAULT_PORT); 93 optDBName.setEnabled(false); 94 } 95 else if (SUPDB_ORACLE8I.equals(dbtype)) { 96 97 int dbport = optDBPort.getValue(); 98 if (dbport == 0) 99 optDBPort.setValue(ORACLE8I_DEFAULT_PORT); 100 optDBName.setEnabled(true); 101 } 102 else if (SUPDB_POSTGRESQL.equals(dbtype)) { 103 104 int dbport = optDBPort.getValue(); 105 if (dbport == 0) 106 optDBPort.setValue(POSTGRESQL_DEFAULT_PORT); 107 optDBName.setEnabled(true); 108 } 109 } 110 }); 111 112 113 optionsPanel = new JPanel( new BorderLayout()); 114 optionsPanel.add( dbPanel, BorderLayout.NORTH); 115 addScreen( "Angaben zur Datenbank", optionsPanel); 117 } 118 119 static final int MAX_WIDTH = 40; 120 121 public boolean storeValues() { 122 141 142 143 String dbtype = (String )optDBType.getSelectedItem(); 144 int licences = optDBLicences.getValue(); 145 String dbuser = optDBUser.getText(); 146 String dbname = optDBName.getText(); 147 String dbserver = optDBServer.getText(); 148 int dbport = optDBPort.getValue(); 149 String passwd1 = optDBPassword1.getText(); 150 String passwd2 = optDBPassword2.getText(); 151 152 153 boolean retv = true; 154 StringBuffer errmsgs = new StringBuffer (); 155 156 wmi.getDictionary().put(DictConstants.DB_INSTALL, "true"); 157 158 159 if (dbuser == null || dbuser.length() == 0) { 160 errmsgs.append("- Leerer Benutzername\n"); 161 retv = false; 162 } 163 if (!SUPDB_SYBASE.equals(dbtype) && 164 (dbname == null || dbname.length() == 0)) { 165 errmsgs.append("- kein Datenbank-Name\n"); 166 retv = false; 167 } 168 if (dbserver == null || dbserver.length() == 0) { 169 errmsgs.append("- kein Datenbank-Server\n"); 170 retv = false; 171 } 172 if (passwd1 == null || passwd1.length() == 0 || 173 passwd2 == null || passwd2.length() == 0) { 174 errmsgs.append("- leere Passworte\n"); 175 retv = false; 176 } 177 if (passwd1 != null && passwd2 != null && 178 !passwd1.equals(passwd2)) { 179 errmsgs.append("- Passworte sind nicht identisch\n"); 180 retv = false; 181 } 182 183 if (licences == 0) { 184 errmsgs.append("- keine Lizenzangabe\n"); 185 retv = false; 186 } 187 if (dbport <= 0 || dbport > 65535) { 188 errmsgs.append("- kein oder unmöglicher Datenbank-Port\n"); 189 retv = false; 190 } 191 192 if (!retv) { 193 Object [] options = {"Korregieren", "Abbrechen"}; 194 int n = JOptionPane.showOptionDialog(optionsPanel, 195 "Einige Angaben waren falsch oder nicht\n" + 196 "vollständig. Bitte überprüfen sie die\n" + 197 "folgenden Felder:\n" + errmsgs.toString(), 198 "Fehlerhafte Eingabe", 199 JOptionPane.YES_NO_OPTION, 200 JOptionPane.QUESTION_MESSAGE, 201 null, options, options[0]); switch (n) { 205 case JOptionPane.YES_OPTION: 206 return false; 207 case JOptionPane.NO_OPTION: 208 wmi.leave(-1); 209 break; 210 } 211 } 212 213 215 String [] dberrmsg = new String [1]; 216 if (!isDatabaseOk(dbtype, dbuser, dbname, dbserver, dbport, passwd1, dberrmsg)) { 217 218 Object [] options = {"Korregieren", "Weiter, ohne DB-Install", "Abbrechen"}; 219 220 221 if (dberrmsg[0].length() > MAX_WIDTH) { 222 StringBuffer buf = new StringBuffer (); 223 int c; 224 for (c = 0; c + MAX_WIDTH < dberrmsg[0].length(); c += MAX_WIDTH) { 225 buf.append(dberrmsg[0].substring(c, c + MAX_WIDTH)); 226 buf.append("\n"); 227 } 228 if (c < dberrmsg[0].length()) { 229 buf.append(dberrmsg[0].substring(c)); 230 } 231 dberrmsg[0] = buf.toString(); 232 } 233 234 int n = JOptionPane.showOptionDialog(optionsPanel, 235 "Die Datenbank reagiert nicht\n(" + dberrmsg[0] + ")\n" + 236 "Wenn Sie jetzt fortfahren, kann die Datenbank nicht\n" + 237 "für Einsatz mit Webman vorbereitet werden.", 238 "Datenbankfehler", 239 JOptionPane.YES_NO_CANCEL_OPTION, 240 JOptionPane.QUESTION_MESSAGE, 241 null, options, options[0]); switch (n) { 245 case JOptionPane.YES_OPTION: wmi.getDictionary().put(DictConstants.DB_INSTALL, "true"); 247 retv = false; 248 break; 249 case JOptionPane.NO_OPTION: wmi.getDictionary().put(DictConstants.DB_INSTALL, "false"); 251 break; 252 case JOptionPane.CLOSED_OPTION: wmi.leave(-1); 254 break; 255 } 256 } 257 258 wmi.getDictionary().put(DictConstants.DB_TYPE, dbtype); 259 wmi.getDictionary().put(DictConstants.DB_LICENCES, new Integer (licences)); 260 wmi.getDictionary().put(DictConstants.DB_USER, dbuser); 261 wmi.getDictionary().put(DictConstants.DB_NAME, dbname); 262 wmi.getDictionary().put(DictConstants.DB_SERVER, dbserver); 263 wmi.getDictionary().put(DictConstants.DB_PORT, new Integer (dbport)); 264 wmi.getDictionary().put(DictConstants.DB_PASSWORD, passwd1); 265 266 return retv; 267 } 268 269 public void onEnter() { 270 } 271 272 273 274 275 278 279 private boolean isDatabaseOk(String dbtype, String dbuser, String dbname, String dbserver, 280 int dbport, String passwd, String [] errmsg) 281 { 282 DBConnection con = null; 283 try { 284 con = DBConnectionMgr.getInstance().getConnection(dbtype, 285 dbname, dbserver, dbport, 286 dbuser, passwd); 287 } 288 catch (SQLException e) { 289 System.err.println("[" + e + "]"); 290 errmsg[0] = e.getMessage(); 291 return false; 292 } 293 294 if (con != null) { 295 try { 296 con.close(); 297 } 298 catch (SQLException sqle) { 299 System.err.println("[" + sqle + "]"); 300 errmsg[0] = sqle.getMessage(); 301 return false; 302 } 303 } 304 else { 305 errmsg[0] = "Unbekannte Datenbank"; 306 return false; 307 } 308 309 return true; 310 } 311 312 } 313 | Popular Tags |