View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street,
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  /******************************************************************************
25  * DatabasePane.java
26  *
27  * The Viewer/Controller for the Database Import Pane
28  *****************************************************************************/
29  
30  package zeus.ontology.database;
31  
32  import javax.swing.*;
33  import javax.swing.table.*;
34  import javax.swing.border.*;
35  import javax.swing.event.*;
36  
37  import java.awt.*;
38  import java.awt.event.*;
39  import java.util.*;
40  
41  import zeus.util.*;
42  import zeus.concepts.*;
43  import zeus.ontology.*;
44  import zeus.generator.GeneratorModel;
45  import zeus.gui.help.*;
46  import zeus.gui.editors.*;
47  import zeus.gui.fields.*;
48  
49  import zeus.ext.DbConnector;
50  import java.sql.*;
51  
52  
53  public class DatabasePane extends JPanel implements ActionListener
54  {
55    static final String[] ERROR_MESSAGE = {
56       "Please select a row before\ncalling this operation"
57    };
58  
59    public DbConnector    MYDB     = null;
60    public OntologyDb     ONTDB    = null;
61    public GeneratorModel genModel = null;
62  
63    protected TablesTableModel  tablesModel;
64    protected ColumnsTableModel columnsModel;
65    protected ColumnsTableUI    columnsUI;
66    protected TablesTableUI     tablesUI;
67    protected Vector            tablesList = new Vector();
68  
69    protected JTextField       dbNameField;
70    protected JTextField       dbDriverField;
71    protected JTextField       dbUsernameField;
72    protected JTextField       dbPasswordField;
73    protected JButton          connBtn;
74  
75    public JButton   importBtn;
76    public JTextArea messageArea;
77  
78  
79    public DatabasePane(OntologyDb ontologyDb, GeneratorModel genmodel)
80    {
81      setLayout(new BorderLayout());
82      ONTDB = ontologyDb;
83      genModel = genmodel;
84  
85      JPanel dbParamsPanel = new JPanel();
86      TitledBorder border = BorderFactory.createTitledBorder("Database Parameters");
87      border.setTitlePosition(TitledBorder.TOP);
88      border.setTitleJustification(TitledBorder.RIGHT);
89      border.setTitleFont(new Font("Helvetica", Font.BOLD, 12));
90      border.setTitleColor(Color.blue);
91      dbParamsPanel.setBorder(border);
92      add(BorderLayout.NORTH, dbParamsPanel);
93  
94      GridBagLayout gb = new GridBagLayout();
95      GridBagConstraints gbc = new GridBagConstraints();
96      dbParamsPanel.setLayout(gb);
97      gbc = new GridBagConstraints();
98  
99      dbNameField = new JTextField(20);
100     JLabel label1 = new JLabel("Database");
101     label1.setFont(new Font("Helvetica",Font.PLAIN, 12));
102     label1.setToolTipText("Enter the path to your database, consult your JDBC driver documentation for syntax");
103     gbc.insets = new Insets(4,8,4,0);
104     gbc.fill = GridBagConstraints.NONE;
105     gbc.anchor = GridBagConstraints.WEST;
106     gb.setConstraints(label1,gbc);
107     dbParamsPanel.add(label1);
108 
109     gbc.gridwidth = GridBagConstraints.REMAINDER;
110     gbc.fill = GridBagConstraints.NONE;
111     gbc.insets = new Insets(4,8,4,8);
112     gb.setConstraints(dbNameField,gbc);
113     dbParamsPanel.add(dbNameField);
114 
115     dbDriverField = new JTextField(20);
116     JLabel label2 = new JLabel("JDBC Driver");
117     label2.setFont(new Font("Helvetica",Font.PLAIN, 12));
118     label2.setToolTipText("Enter the full classname of the JDBC driver used to access your database");
119     gbc.gridwidth = 1;
120     gbc.fill = GridBagConstraints.NONE;
121     gbc.anchor = GridBagConstraints.WEST;
122     gbc.insets = new Insets(4,8,4,0);
123     gb.setConstraints(label2,gbc);
124     dbParamsPanel.add(label2);
125 
126     gbc.gridwidth = GridBagConstraints.REMAINDER;
127     gbc.fill = GridBagConstraints.NONE;
128     gbc.insets = new Insets(4,8,4,8);
129     gbc.weightx = 1;
130     gb.setConstraints(dbDriverField,gbc);
131     dbParamsPanel.add(dbDriverField);
132 
133     dbUsernameField = new JTextField(20);
134     JLabel label3 = new JLabel("Username");
135     label3.setFont(new Font("Helvetica",Font.PLAIN, 12));
136     label3.setToolTipText("Provide this if you need to be authenticated when connecting to your database");
137     gbc.gridwidth = 1;
138     gbc.insets = new Insets(4,8,4,0);
139     gbc.fill = GridBagConstraints.NONE;
140     gbc.anchor = GridBagConstraints.WEST;
141     gb.setConstraints(label3,gbc);
142     dbParamsPanel.add(label3);
143 
144     gbc.gridwidth = GridBagConstraints.REMAINDER;
145     gbc.fill = GridBagConstraints.NONE;
146     gbc.insets = new Insets(4,8,4,8);
147     gbc.weightx = 1;
148     gb.setConstraints(dbUsernameField,gbc);
149     dbParamsPanel.add(dbUsernameField);
150 
151     dbPasswordField = new JTextField(20);
152     JLabel label4 = new JLabel("Password");
153     label4.setFont(new Font("Helvetica",Font.PLAIN, 12));
154     label4.setToolTipText("Provide this if you need to be authenticated when connecting to your database");
155     gbc.gridwidth = 1;
156     gbc.insets = new Insets(4,8,4,0);
157     gbc.fill = GridBagConstraints.NONE;
158     gbc.anchor = GridBagConstraints.WEST;
159     gb.setConstraints(label4,gbc);
160     dbParamsPanel.add(label4);
161 
162     gbc.gridwidth = GridBagConstraints.NONE;
163     gbc.anchor = GridBagConstraints.WEST;
164     gbc.fill = GridBagConstraints.NONE;
165     gbc.insets = new Insets(4,8,4,8);
166     gbc.weightx = 1;
167     gb.setConstraints(dbPasswordField,gbc);
168     dbParamsPanel.add(dbPasswordField);
169 
170     connBtn = new JButton("Connect");
171     connBtn.addActionListener(this);
172     gbc.insets = new Insets(4,8,4,0);
173     gbc.fill = GridBagConstraints.REMAINDER;
174     gbc.anchor = GridBagConstraints.EAST;
175     gb.setConstraints(connBtn,gbc);
176     dbParamsPanel.add(connBtn);
177 
178     // The Database Tables Panel - shows all the tables and columns in the connected database
179     JPanel dbTablesPanel = new JPanel();
180     dbTablesPanel.setLayout(new BorderLayout());
181 
182     columnsUI = new ColumnsTableUI(this);
183     columnsModel = columnsUI.getModel();
184     tablesUI = new TablesTableUI(columnsModel);
185     tablesModel = tablesUI.getModel();
186 
187     dbTablesPanel.add(BorderLayout.WEST, tablesUI);
188     dbTablesPanel.add(BorderLayout.EAST, columnsUI);
189 
190     importBtn = new JButton("Import Selected");
191     importBtn.addActionListener(this);
192     if (tablesList.size() == 0)
193       importBtn.setEnabled(false);
194 
195     dbTablesPanel.add(new JLabel());
196     dbTablesPanel.add(BorderLayout.SOUTH, importBtn);
197 
198     TitledBorder border2 = BorderFactory.createTitledBorder("Database Schema");
199     border2.setTitlePosition(TitledBorder.TOP);
200     border2.setTitleJustification(TitledBorder.RIGHT);
201     border2.setTitleFont(new Font("Helvetica", Font.BOLD, 12));
202     border2.setTitleColor(Color.blue);
203     dbTablesPanel.setBorder(border2);
204     add(BorderLayout.CENTER, dbTablesPanel);
205 
206     // The Database Messages Panel - prints results of JBDC operations
207     JPanel dbMessagesPanel = new JPanel();
208     TitledBorder border3 = BorderFactory.createTitledBorder("Messages");
209     border3.setTitlePosition(TitledBorder.TOP);
210     border3.setTitleJustification(TitledBorder.RIGHT);
211     border3.setTitleFont(new Font("Helvetica", Font.BOLD, 12));
212     border3.setTitleColor(Color.blue);
213     dbMessagesPanel.setBorder(border3);
214 
215     dbMessagesPanel.setLayout(new GridLayout(1,1));
216 
217     messageArea = new JTextArea(8,200);
218     messageArea.setEditable(false);
219     messageArea.setLineWrap(true);
220     messageArea.setBackground(Color.white);
221 
222     JScrollPane scrollPane = new JScrollPane(messageArea);
223     scrollPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
224     scrollPane.setPreferredSize(new Dimension(300,160));
225     dbMessagesPanel.add(scrollPane);
226     add(BorderLayout.SOUTH, dbMessagesPanel);
227 
228     initParams();
229   }
230 
231   private void initParams()
232   {
233     dbNameField.setText("jdbc:mysql://steerpike:3306/test");
234     dbDriverField.setText("org.gjt.mm.mysql.Driver");
235   }
236 
237   private void errorMsg(int tag) {
238     JOptionPane.showMessageDialog(this,ERROR_MESSAGE[tag],
239                                   "Error", JOptionPane.ERROR_MESSAGE);
240   }
241 
242   public void actionPerformed(ActionEvent e) 
243   {
244     Object src = e.getSource();
245     if (src == connBtn)
246       makeDbConnection();
247     else if (src == importBtn)
248       importTable();
249   }
250 
251 
252   private void makeDbConnection()
253   {
254     String dbName     = null;
255     String dbDriver   = null;
256     String dbUsername = null;
257     String dbPassword = null;
258 
259     dbName     = dbNameField.getText();
260     dbDriver   = dbDriverField.getText();
261     dbUsername = dbUsernameField.getText();
262     dbPassword = dbPasswordField.getText();
263 
264     // store JDBC parameters - need to write them into the jdbc.data file
265     if (genModel != null)
266     {
267       genModel.dbName     = dbName;
268       genModel.dbDriver   = dbDriver;
269       genModel.dbUsername = dbUsername;
270       genModel.dbPassword = dbPassword;
271     }
272     MYDB = new DbConnector(dbUsername, dbPassword, dbDriver, dbName);
273 
274     messageArea.append("** Reading table information from " + dbName);
275     try
276     {
277       //  populate the known tables table
278       DatabaseMetaData dmeta = MYDB.getConnection().getMetaData();
279       String[] types = {"TABLE"};
280       ResultSet rsTables = dmeta.getTables(null,null,null,types);
281       while(rsTables.next())
282       {
283         String strTable = rsTables.getString(3);
284         //System.out.println("> " + strTable);
285         tablesList.add(strTable);
286       }
287       tablesModel.setValues(tablesList);
288       MYDB.close();
289       messageArea.append(" OK \n");
290     }
291     catch(SQLException e)
292     {
293       messageArea.append("\nSQL Exception: " + e + "\n");
294     }
295   }
296 
297 
298   private void importTable()
299   {
300     if (columnsUI.getTable().getSelectedRow() == -1 )
301     {
302        messageArea.append("Error - no selected columns to import\n");
303        return;
304     }
305     String[][] selectedrows = columnsUI.getSelectedRows();
306 
307     String importEntityName = tablesUI.getSelectedRow();
308     TreeNode parent = ONTDB.getRoot();
309     boolean ok = ONTDB.addNamedChildFact(parent, importEntityName);
310     if (ok)
311       messageArea.append("Created new entity: " + importEntityName + "\n");
312     else
313       messageArea.append("Entity: " + importEntityName + " already exists\n");
314 
315     messageArea.append("Importing: " + selectedrows.length + " attribute(s) \n");
316 
317     // create a new attribute for each imported column
318     for(int i = 0; i < selectedrows.length; i++ )
319     {
320       String cName = selectedrows[i][0];
321       String cType = selectedrows[i][1];
322       String zType = selectedrows[i][2];
323      // System.out.println("> " + cName + " " + cType + " " + zType);
324       ONTDB.addNewAttribute(importEntityName, cName, zType);
325     }
326 
327     messageArea.append("Import successful. \n");
328     messageArea.append("The imported fact and its attributes are now in the fact tree \n\n");
329   }
330 }