001    package hep.aida.ref.sql.JAS3Plugin;
002    
003    import hep.aida.ref.sql.AIDAURL;
004    
005    // AIDA
006    import hep.aida.IAnalysisFactory;
007    
008    // FreeHEP
009    import org.freehep.application.Application;
010    import org.freehep.swing.wizard.Finishable;
011    import org.freehep.swing.wizard.WizardPage;
012    import org.freehep.application.RecentItemTextField;
013    
014    // Java
015    import java.io.IOException;
016    import java.awt.GridBagConstraints;
017    import java.awt.GridBagLayout;
018    import java.awt.Insets;
019    import javax.swing.JLabel;
020    import javax.swing.border.EmptyBorder;
021    import javax.swing.JTextField;
022    import javax.swing.JPasswordField;
023    
024    /** <code>SQLTupleWizardPage</code> is a Wizard to establish connection
025      * to SQL database using SQLTuple.
026      * <p><font color="#880088">
027      * $Id: SQLTupleWizardPage.java,v 1.21 2007/06/11 14:02:29 hrivnac Exp $
028      * <pre>
029      * $Log: SQLTupleWizardPage.java,v $
030      * Revision 1.21  2007/06/11 14:02:29  hrivnac
031      * better support for tags in Oracle
032      *
033      * Revision 1.20  2007/05/23 16:38:45  hrivnac
034      * logical connections for Plotter; better UML
035      *
036      * Revision 1.19  2006/12/19 14:31:08  hrivnac
037      * news added into WS
038      *
039      * Revision 1.18  2006/12/13 16:38:51  hrivnac
040      * derby works in JAS3 Plugin
041      *
042      * Revision 1.17  2005/09/28 16:21:06  hrivnac
043      * code cleaning
044      *
045      * Revision 1.16  2005/03/08 17:05:51  hrivnac
046      * ...
047      *
048      * Revision 1.15  2004/12/08 10:56:51  hrivnac
049      * ITree created with sensible name for Oracle
050      *
051      * Revision 1.14  2004/12/08 10:25:19  hrivnac
052      * ITree created with sensible name for Oracle
053      *
054      * Revision 1.13  2004/12/08 10:15:39  hrivnac
055      * ITree created with sensible name for Oracle
056      *
057      * Revision 1.12  2004/12/07 01:04:56  hrivnac
058      * name pattern can be specified
059      *
060      * Revision 1.11  2004/12/01 16:07:15  hrivnac
061      * better support for ColMan JAS3 plugin
062      *
063      * Revision 1.10  2004/11/30 15:13:20  hrivnac
064      * support for ColMan JAS3Plugin
065      *
066      * Revision 1.9  2004/11/26 17:52:28  hrivnac
067      * JAS3Plugin can use customised StmtSrc
068      *
069      * Revision 1.8  2004/11/24 14:32:32  hrivnac
070      * better support for schemas
071      *
072      * Revision 1.7  2004/11/19 16:04:39  hrivnac
073      * JAS3 plugin supports empty password
074      *
075      * Revision 1.6  2004/10/29 22:27:25  hrivnac
076      * imports corrected
077      *
078      * Revision 1.5  2004/10/22 15:33:00  hrivnac
079      * cleaned
080      *
081      * Revision 1.4  2004/10/20 23:02:29  hrivnac
082      * Types mapping simplified
083      *
084      * Revision 1.3  2004/10/13 12:25:40  hrivnac
085      * defaults work fine
086      *
087      * Revision 1.2  2004/10/12 19:26:59  hrivnac
088      * MySQL Connector 3.0.14
089      *
090      * Revision 1.1  2004/10/08 22:20:50  hrivnac
091      * JAS3Plugin works from JAS3 Menu
092      *
093      * </pre>
094      * </font></p>
095      * @opt attributes
096      * @opt operations
097      * @opt types
098      * @opt visibility
099      * @version $Id: SQLTupleWizardPage.java,v 1.21 2007/06/11 14:02:29 hrivnac Exp $
100      * @author <a href="mailto:Julius.Hrivnac@cern.ch">J.Hrivnac</a> */
101    public class SQLTupleWizardPage extends WizardPage 
102                                    implements Finishable {
103    
104      /** Construct. */
105      public SQLTupleWizardPage() {
106        initComponents();
107        }
108    
109      /** Connect to the SQL database when "Finish" is requested. */
110      public void onFinish() {
111        AIDAURL aidaurl = new AIDAURL(_urlField.getText());
112        String db      = aidaurl.db();
113        String schema  = _schemaField.getText();
114        String name    = _nameField.getText();
115        String user    = _userField.getText();
116        String passwd  = new String(_passwordField.getPassword());
117        String stmtSrc = null;
118        if (_stmtSrcField != null) {
119          stmtSrc = _stmtSrcField.getText();
120          }
121        String options = "hep.aida.ref.sql.db="   + db + ";"
122                       + "hep.aida.ref.sql.user=" + user;
123        if (schema != null && !schema.trim().equals("")) {
124          options += ";hep.aida.ref.sql.schema=" + schema;
125          }
126        else if (aidaurl.protocol().equals("oracle")) {
127          options += ";hep.aida.ref.sql.schema=" + user.toUpperCase();
128          }
129        if (name != null && !name.trim().equals("")) {
130          options += ";hep.aida.ref.sql.name=" + name;
131          }
132        if (passwd != null && !passwd.trim().equals("")) {
133          options += ";hep.aida.ref.sql.passwd=" + passwd;
134          }
135        if (stmtSrc != null && !stmtSrc.trim().equals("")) {
136          options += ";hep.aida.ref.sql.src=" + stmtSrc;
137          }
138        options += ";hep.aida.ref.sql.forwardonly=true";
139        _urlField.saveState();
140        _schemaField.saveState();
141        _nameField.saveState();
142        _userField.saveState();
143        String id = aidaurl.name();
144        if (schema != null && !schema.trim().equals("")) {
145          id += "_" + schema;
146          }
147        if (aidaurl.protocol() != null && !aidaurl.protocol().trim().equals("")) {
148          id += "(" + aidaurl.protocol() + ")";
149          }
150        try {
151          IAnalysisFactory.create().createTreeFactory().create(id, "sql", true, false, options);
152          }
153        catch (IOException e) {
154          Application.getApplication().error(this, "Error connect to " + db, e);
155          }
156        dispose();
157        }
158    
159      /** Create panel. */
160      private void initComponents() {
161    
162        GridBagConstraints gridBagConstraints;
163        setLayout(new GridBagLayout());
164    
165        JLabel urlLabel = new JLabel();
166        urlLabel.setText("Database URL:");
167        setBorder(new EmptyBorder(new Insets(10, 10, 10, 10)));
168        gridBagConstraints = new GridBagConstraints();
169        gridBagConstraints.insets = new Insets(0, 0, 0, 5);
170        gridBagConstraints.anchor = GridBagConstraints.EAST;
171        add(urlLabel, gridBagConstraints);
172    
173        _urlField = new RecentItemTextField("sqltuple.url.last", 10, true);
174        _urlField.setMaxWidth(50);
175        gridBagConstraints = new GridBagConstraints();
176        gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
177        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
178        gridBagConstraints.weightx = 1.0;
179        add(_urlField, gridBagConstraints);
180    
181        JLabel schemaLabel = new JLabel();
182        schemaLabel.setText("Schema Pattern (optional):");
183        setBorder(new EmptyBorder(new Insets(10, 10, 10, 10)));
184        gridBagConstraints = new GridBagConstraints();
185        gridBagConstraints.insets = new Insets(0, 0, 0, 5);
186        gridBagConstraints.anchor = GridBagConstraints.EAST;
187        add(schemaLabel, gridBagConstraints);
188    
189        _schemaField = new RecentItemTextField("sqltuple.schema.last", 10, true);
190        gridBagConstraints = new GridBagConstraints();
191        gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
192        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
193        gridBagConstraints.weightx = 1.0;
194        add(_schemaField, gridBagConstraints);
195    
196        JLabel nameLabel = new JLabel();
197        nameLabel.setText("Name Pattern (optional):");
198        setBorder(new EmptyBorder(new Insets(10, 10, 10, 10)));
199        gridBagConstraints = new GridBagConstraints();
200        gridBagConstraints.insets = new Insets(0, 0, 0, 5);
201        gridBagConstraints.anchor = GridBagConstraints.EAST;
202        add(nameLabel, gridBagConstraints);
203    
204        _nameField = new RecentItemTextField("sqltuple.name.last", 10, true);
205        gridBagConstraints = new GridBagConstraints();
206        gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
207        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
208        gridBagConstraints.weightx = 1.0;
209        add(_nameField, gridBagConstraints);
210    
211        JLabel userLabel = new JLabel();
212        userLabel.setText("Username:");
213        gridBagConstraints = new GridBagConstraints();
214        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
215        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
216        add(userLabel, gridBagConstraints);
217    
218        _userField = new RecentItemTextField("sqltuple.user.last", 10, true);
219        if (_userField.getText().equals("")) {
220          _userField.setText(System.getProperty("user.name"));
221          }
222        gridBagConstraints = new GridBagConstraints();
223        gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
224        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
225        gridBagConstraints.weightx = 1.0;
226        add(_userField, gridBagConstraints);
227    
228        JLabel passwordLabel = new JLabel();
229        passwordLabel.setText("Password (optional):");
230        gridBagConstraints = new GridBagConstraints();
231        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
232        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
233        add(passwordLabel, gridBagConstraints);
234    
235        _passwordField = new javax.swing.JPasswordField();
236        gridBagConstraints = new GridBagConstraints();
237        gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
238        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
239        gridBagConstraints.weightx = 1.0;
240        add(_passwordField, gridBagConstraints);
241    
242        if (_stmtSrc != null) {
243          JLabel stmtSrcLabel = new JLabel();
244          stmtSrcLabel.setText("Customised StmtSrc (optional):");
245          gridBagConstraints = new GridBagConstraints();
246          gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
247          gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
248          add(stmtSrcLabel, gridBagConstraints);
249          _stmtSrcField = new JTextField();
250          _stmtSrcField.setText(_stmtSrc);
251          gridBagConstraints = new GridBagConstraints();
252          gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
253          gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
254          gridBagConstraints.weightx = 1.0;
255          add(_stmtSrcField, gridBagConstraints);
256          }
257    
258        }
259    
260       /** Set customised {@link hep.aida.ref.sql.StmtSrc} properties.
261         * @param stmtSrc The customised {@link hep.aida.ref.sql.StmtSrc} properties. */
262       public static void setStmtSrc(String stmtSrc) {
263         _stmtSrc = stmtSrc;
264         }
265    
266       private static String _stmtSrc; 
267    
268       private RecentItemTextField _urlField;
269    
270       private RecentItemTextField _schemaField;
271    
272       private RecentItemTextField _nameField;
273    
274       private RecentItemTextField _userField;
275    
276       private JPasswordField      _passwordField;
277    
278       private JTextField          _stmtSrcField;
279    
280      }