001: /*
002: * SalomeTMF is a Test Management Framework
003: * Copyright (C) 2005 France Telecom R&D
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * @author Fayçal SOUGRATI, Vincent Pautret, Marche Mikael
020: *
021: * Contact: mikael.marche@rd.francetelecom.com
022: */
023:
024: package org.objectweb.salome_tmf.tools.encrypt_prop;
025:
026: import java.io.BufferedReader;
027: import java.io.File;
028: import java.io.FileReader;
029: import java.sql.Connection;
030: import java.sql.DriverManager;
031: import java.sql.SQLException;
032: import java.sql.Statement;
033: import java.util.StringTokenizer;
034:
035: import javax.swing.JDialog;
036: import javax.swing.JFileChooser;
037: import javax.swing.JOptionPane;
038:
039: import org.objectweb.salome_tmf.tools.languages.Language;
040:
041: public class createDBForm extends JDialog {
042:
043: File scriptFile = null;
044: String jdbcDriver = null;
045: String dbName = null;
046: String salomeUser = null;
047: String salomeUserPwd = null;
048: javax.swing.JFrame parent = null;
049:
050: /** Creates new form createDBForm */
051: public createDBForm(javax.swing.JFrame parent) {
052: super (parent, true);
053: this .parent = parent;
054: initComponents();
055: //this.setLocation(150,150);
056: this .setLocationRelativeTo(this .getParent());
057: }
058:
059: /** This method is called from within the constructor to
060: * initialize the form.
061: * WARNING: Do NOT modify this code. The content of this method is
062: * always regenerated by the Form Editor.
063: */
064: private void initComponents() {//GEN-BEGIN:initComponents
065: jPanel5 = new javax.swing.JPanel();
066: jPanel7 = new javax.swing.JPanel();
067: jPanel6 = new javax.swing.JPanel();
068: buttonsPanel = new javax.swing.JPanel();
069: jPanel4 = new javax.swing.JPanel();
070: createDBButton = new javax.swing.JButton();
071: cancelButton = new javax.swing.JButton();
072: propsFilePanel = new javax.swing.JPanel();
073: propFileLabel = new javax.swing.JLabel();
074: scriptFileField = new javax.swing.JTextField();
075: chooseFileButton = new javax.swing.JButton();
076: jdbcDriverLabel = new javax.swing.JLabel();
077: dbHostField = new javax.swing.JTextField();
078: jPanel1 = new javax.swing.JPanel();
079: jLabel3 = new javax.swing.JLabel();
080: jLabel1 = new javax.swing.JLabel();
081: jLabel6 = new javax.swing.JLabel();
082: jLabel7 = new javax.swing.JLabel();
083: rootField = new javax.swing.JTextField();
084: jPanel8 = new javax.swing.JPanel();
085: jLabel9 = new javax.swing.JLabel();
086: jLabel2 = new javax.swing.JLabel();
087: pwdField = new javax.swing.JTextField();
088: jPanel2 = new javax.swing.JPanel();
089: jLabel4 = new javax.swing.JLabel();
090: jLabel8 = new javax.swing.JLabel();
091: jPanel3 = new javax.swing.JPanel();
092:
093: setTitle(Language
094: .getInstance()
095: .getText(
096: "Paramètres_de_création_d'une_base_de_données_Salomé_TMF"));
097: addWindowListener(new java.awt.event.WindowAdapter() {
098: public void windowClosing(java.awt.event.WindowEvent evt) {
099: exitForm(evt);
100: }
101: });
102:
103: getContentPane().add(jPanel5, java.awt.BorderLayout.NORTH);
104:
105: getContentPane().add(jPanel7, java.awt.BorderLayout.WEST);
106:
107: getContentPane().add(jPanel6, java.awt.BorderLayout.EAST);
108:
109: buttonsPanel.setLayout(new java.awt.BorderLayout());
110:
111: createDBButton
112: .setFont(new java.awt.Font("MS Sans Serif", 1, 11));
113: createDBButton.setForeground(new java.awt.Color(0, 0, 255));
114: createDBButton
115: .setIcon(new javax.swing.ImageIcon(
116: getClass()
117: .getResource(
118: "/org/objectweb/salome_tmf/tools/encrypt_prop/images/db.gif")));
119: createDBButton.setText(Language.getInstance().getText(
120: "Créer_base_de_données"));
121: createDBButton
122: .setPreferredSize(new java.awt.Dimension(240, 41));
123: createDBButton
124: .addActionListener(new java.awt.event.ActionListener() {
125: public void actionPerformed(
126: java.awt.event.ActionEvent evt) {
127: createDBPerformed(evt);
128: }
129: });
130:
131: jPanel4.add(createDBButton);
132:
133: cancelButton.setFont(new java.awt.Font("MS Sans Serif", 0, 10));
134: cancelButton.setForeground(new java.awt.Color(255, 0, 0));
135: cancelButton.setText(Language.getInstance().getText("Annuler"));
136: cancelButton.setMinimumSize(new java.awt.Dimension(121, 41));
137: cancelButton.setPreferredSize(new java.awt.Dimension(121, 41));
138: cancelButton
139: .addActionListener(new java.awt.event.ActionListener() {
140: public void actionPerformed(
141: java.awt.event.ActionEvent evt) {
142: cancelPerformed(evt);
143: }
144: });
145:
146: jPanel4.add(cancelButton);
147:
148: buttonsPanel.add(jPanel4, java.awt.BorderLayout.CENTER);
149:
150: getContentPane().add(buttonsPanel, java.awt.BorderLayout.SOUTH);
151:
152: propsFilePanel.setLayout(new java.awt.GridLayout(5, 3, 3, 3));
153:
154: propsFilePanel.setMinimumSize(new java.awt.Dimension(400, 300));
155: propsFilePanel
156: .setPreferredSize(new java.awt.Dimension(700, 200));
157: propFileLabel
158: .setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
159: propFileLabel.setText(Language.getInstance().getText(
160: "Script_pour_la_cr\u00E9ation_de_la_base_:"));
161: propsFilePanel.add(propFileLabel);
162:
163: scriptFileField.setEditable(false);
164: propsFilePanel.add(scriptFileField);
165:
166: chooseFileButton
167: .setIcon(new javax.swing.ImageIcon(
168: getClass()
169: .getResource(
170: "/org/objectweb/salome_tmf/tools/encrypt_prop/images/openFile.gif")));
171: chooseFileButton.setText(Language.getInstance().getText(
172: "Ouvrir_un_fichier..."));
173: chooseFileButton
174: .addActionListener(new java.awt.event.ActionListener() {
175: public void actionPerformed(
176: java.awt.event.ActionEvent evt) {
177: openScriptPerformed(evt);
178: }
179: });
180:
181: propsFilePanel.add(chooseFileButton);
182:
183: jdbcDriverLabel
184: .setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
185: jdbcDriverLabel.setText(Language.getInstance().getText(
186: "Serveur_de_la_base_de_données_:"));
187: propsFilePanel.add(jdbcDriverLabel);
188:
189: propsFilePanel.add(dbHostField);
190:
191: jPanel1.setLayout(new java.awt.BorderLayout());
192:
193: jLabel3.setText("lat****.rd.francetelecom.fr");
194: jPanel1.add(jLabel3, java.awt.BorderLayout.WEST);
195:
196: jLabel1.setText(Language.getInstance().getText("Exemple_:"));
197: jPanel1.add(jLabel1, java.awt.BorderLayout.NORTH);
198:
199: jPanel1.add(jLabel6, java.awt.BorderLayout.SOUTH);
200:
201: propsFilePanel.add(jPanel1);
202:
203: jLabel7
204: .setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
205: jLabel7.setText(Language.getInstance().getText("root_MySQL:"));
206: propsFilePanel.add(jLabel7);
207:
208: rootField.setText("root");
209: rootField.setToolTipText("");
210: propsFilePanel.add(rootField);
211:
212: jPanel8.setLayout(new java.awt.BorderLayout());
213:
214: jPanel8.add(jLabel9, java.awt.BorderLayout.CENTER);
215:
216: propsFilePanel.add(jPanel8);
217:
218: jLabel2
219: .setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
220: jLabel2.setText(Language.getInstance().getText(
221: "Mot_de_passe_root_:"));
222: propsFilePanel.add(jLabel2);
223:
224: propsFilePanel.add(pwdField);
225:
226: jPanel2.setLayout(new java.awt.BorderLayout());
227:
228: jPanel2.add(jLabel4, java.awt.BorderLayout.CENTER);
229:
230: propsFilePanel.add(jPanel2);
231:
232: propsFilePanel.add(jLabel8);
233:
234: propsFilePanel.add(jPanel3);
235:
236: getContentPane().add(propsFilePanel,
237: java.awt.BorderLayout.CENTER);
238:
239: pack();
240: }//GEN-END:initComponents
241:
242: private void cancelPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelPerformed
243: // TODO add your handling code here:
244: this .dispose();
245: }//GEN-LAST:event_cancelPerformed
246:
247: private void createDBPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createDBPerformed
248: String host = dbHostField.getText();
249: String root = rootField.getText();
250: String pwd = pwdField.getText();
251: // TODO add your handling code here:
252: if (scriptFile == null) {
253: JOptionPane
254: .showMessageDialog(
255: this ,
256: Language
257: .getInstance()
258: .getText(
259: "Vous_devez_choisir_un_fichier_contenant_le_script_de_création_de_la_base_de_données_!"),
260: Language.getInstance().getText("Erreur..."),
261: JOptionPane.ERROR_MESSAGE);
262: } else {
263: if (host == null) {
264: JOptionPane
265: .showMessageDialog(
266: this ,
267: Language
268: .getInstance()
269: .getText(
270: "Vous_devez_saisir_le_nom_du_serveur_de_la_base_de_données_MySQL_!"),
271: Language.getInstance().getText(
272: "Erreur..."),
273: JOptionPane.ERROR_MESSAGE);
274: } else {
275: if (root == null) {
276: JOptionPane
277: .showMessageDialog(
278: this ,
279: Language
280: .getInstance()
281: .getText(
282: "Vous_devez_saisir_le_nom_du_root_MySQL_!"),
283: Language.getInstance().getText(
284: "Erreur..."),
285: JOptionPane.ERROR_MESSAGE);
286: } else {
287: try {
288: Class.forName(jdbcDriver).newInstance();
289: Connection cnt = openDB(host, "mysql", root,
290: pwd);
291: Statement stmt = cnt.createStatement();
292: // CREATE SALOME DATABASE
293: stmt.execute("CREATE DATABASE " + dbName + ";");
294: // ALLOWS USER5 SALOME
295: stmt = cnt.createStatement();
296: stmt
297: .execute("GRANT SELECT,INSERT,UPDATE,DELETE,INDEX, ALTER,CREATE,DROP, "
298: + "LOCK TABLES, CREATE TEMPORARY TABLES,REFERENCES "
299: + "ON "
300: + dbName
301: + ".* TO "
302: + salomeUser
303: + "@'%'"
304: + "IDENTIFIED BY '"
305: + salomeUserPwd + "';");
306: stmt
307: .execute("GRANT SELECT,INSERT,UPDATE,DELETE,INDEX, ALTER,CREATE,DROP, "
308: + "LOCK TABLES, CREATE TEMPORARY TABLES,REFERENCES "
309: + "ON "
310: + dbName
311: + ".* TO "
312: + salomeUser
313: + "@'localhost'"
314: + "IDENTIFIED BY '"
315: + salomeUserPwd + "';");
316: stmt.execute("FLUSH PRIVILEGES;");
317: //cnt = openDB("localhost", dbName, salomeUser, salomeUserPwd);
318: cnt = openDB(host, dbName, root, pwd);
319: // CREATE TABLE
320: String table_script = loadScript();
321: StringTokenizer st = new StringTokenizer(
322: table_script, ";");
323: while (st.hasMoreTokens()) {
324: stmt = cnt.createStatement();
325: stmt.execute(st.nextToken());
326: }
327:
328: // Setting the default language
329:
330: updateLocale(cnt, configDBTool.locale);
331:
332: JOptionPane.showMessageDialog(this , Language
333: .getInstance().getText(
334: "La_base_de_données_")
335: + " "
336: + dbName
337: + " "
338: + Language.getInstance().getText(
339: "_a_été_créee_avec_succès"),
340: Language.getInstance().getText(
341: "Info..."),
342: JOptionPane.INFORMATION_MESSAGE);
343: cnt.close();
344: this .dispose();
345: parent.dispose();
346:
347: } catch (SQLException ex) {
348: ex.printStackTrace();
349: JOptionPane
350: .showMessageDialog(
351: this ,
352: Language
353: .getInstance()
354: .getText(
355: "Echec_de_création_de_la_base_")
356: + dbName
357: + " "
358: + ex.getMessage(),
359: Language.getInstance().getText(
360: "Erreur..."),
361: JOptionPane.ERROR_MESSAGE);
362:
363: } catch (Exception e) {
364: e.printStackTrace();
365: JOptionPane
366: .showMessageDialog(
367: this ,
368: Language
369: .getInstance()
370: .getText(
371: "Echec_de_création_de_la_base_")
372: + dbName
373: + " "
374: + e.getMessage(),
375: Language.getInstance().getText(
376: "Erreur..."),
377: JOptionPane.ERROR_MESSAGE);
378: }
379: }
380: }
381: }
382: }//GEN-LAST:event_createDBPerformed
383:
384: private void updateLocale(Connection cnt, String locale) {
385: String updateLocaleStmt = "UPDATE CONFIG "
386: + "SET VALEUR = '"
387: + locale
388: + "', id_projet = '0', id_personne = '0' "
389: + "WHERE binary CLE = 'Locale' AND id_projet = '0' AND id_personne = '0'";
390:
391: try {
392: Statement stmt = cnt.createStatement();
393: stmt.execute(updateLocaleStmt);
394: } catch (Exception e) {
395: //Util.log("[createDBForm] Problem when updating Locale");
396: e.printStackTrace();
397: }
398: }
399:
400: Connection openDB(String host, String db, String user,
401: String password) throws SQLException {
402: try {
403: String url = "jdbc:mysql://" + host + "/" + db;
404: System.out.println(Language.getInstance().getText(
405: "Tentative_d'ouverture_de_l'url_")
406: + url);
407: Connection cnt = DriverManager.getConnection(url, user,
408: password);
409: return cnt;
410: } catch (SQLException ex) {
411: JOptionPane
412: .showMessageDialog(
413: this ,
414: Language
415: .getInstance()
416: .getText(
417: "Echec_de_la_tentative_d'ouverture_de_la_base_en_root_:")
418: + ex.getMessage(),
419: Language.getInstance().getText("Erreur..."),
420: JOptionPane.ERROR_MESSAGE);
421: throw ex;
422: }
423: }
424:
425: String loadScript() throws Exception {
426: String sql = "";
427: try {
428: String buf;
429: BufferedReader br = new BufferedReader(new FileReader(
430: scriptFile));
431: while ((buf = br.readLine()) != null) {
432: sql += buf;
433: }
434: } catch (Exception e) {
435: JOptionPane
436: .showMessageDialog(
437: this ,
438: Language
439: .getInstance()
440: .getText(
441: "Echec_de_la_lecture_du_script_de_la_base_de_données_:_")
442: + e.getMessage(),
443: Language.getInstance().getText("Erreur..."),
444: JOptionPane.ERROR_MESSAGE);
445: throw e;
446: }
447: return sql;
448: }
449:
450: private void openScriptPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openScriptPerformed
451: // TODO add your handling code here:
452: JFileChooser fileChooser = new JFileChooser();
453: fileChooser.setApproveButtonText(Language.getInstance()
454: .getText("Valider"));
455: if (configDBTool.calledInInstallProc) {
456: String pathToScripts = configDBTool.installDir
457: + java.io.File.separator + "bdd_model";
458: fileChooser.setCurrentDirectory(new File(pathToScripts));
459: } else {
460: fileChooser.setCurrentDirectory(new File("."));
461: }
462: fileChooser.addChoosableFileFilter(new PropsFileFilter(
463: "SQL File [*.sql]", ".sql"));
464: int returnVal = fileChooser.showOpenDialog(createDBForm.this );
465: if (returnVal == JFileChooser.APPROVE_OPTION) {
466: scriptFile = fileChooser.getSelectedFile();
467: scriptFileField.setText(scriptFile.getAbsolutePath());
468: }
469: }//GEN-LAST:event_openScriptPerformed
470:
471: /** Exit the Application */
472: private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
473: System.exit(0);
474: }//GEN-LAST:event_exitForm
475:
476: void setHostName(String host) {
477: dbHostField.setText(host);
478: }
479:
480: void setJDBCdriver(String driver) {
481: jdbcDriver = driver;
482: }
483:
484: void setDBName(String db) {
485: dbName = db;
486: }
487:
488: void setSalomeUser(String user) {
489: salomeUser = user;
490: }
491:
492: void setSalomeUserPwd(String pwd) {
493: salomeUserPwd = pwd;
494: }
495:
496: /**
497: * @param args the command line arguments
498: */
499: /*
500: public static void main(String args[]) {
501: new createDBForm().show();
502: }*/
503:
504: // Variables declaration - do not modify//GEN-BEGIN:variables
505: private javax.swing.JPanel buttonsPanel;
506: private javax.swing.JButton cancelButton;
507: private javax.swing.JButton chooseFileButton;
508: private javax.swing.JButton createDBButton;
509: private javax.swing.JTextField dbHostField;
510: private javax.swing.JLabel jLabel1;
511: private javax.swing.JLabel jLabel2;
512: private javax.swing.JLabel jLabel3;
513: private javax.swing.JLabel jLabel4;
514: private javax.swing.JLabel jLabel6;
515: private javax.swing.JLabel jLabel7;
516: private javax.swing.JLabel jLabel8;
517: private javax.swing.JLabel jLabel9;
518: private javax.swing.JPanel jPanel1;
519: private javax.swing.JPanel jPanel2;
520: private javax.swing.JPanel jPanel3;
521: private javax.swing.JPanel jPanel4;
522: private javax.swing.JPanel jPanel5;
523: private javax.swing.JPanel jPanel6;
524: private javax.swing.JPanel jPanel7;
525: private javax.swing.JPanel jPanel8;
526: private javax.swing.JLabel jdbcDriverLabel;
527: private javax.swing.JLabel propFileLabel;
528: private javax.swing.JPanel propsFilePanel;
529: private javax.swing.JTextField pwdField;
530: private javax.swing.JTextField rootField;
531: private javax.swing.JTextField scriptFileField;
532: // End of variables declaration//GEN-END:variables
533:
534: }
|