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  * ColumnsTableModel.java
26  *
27  * The local model for the Tables Table
28  *****************************************************************************/
29  
30  package zeus.ontology.database;
31  
32  import java.util.*;
33  import java.sql.*;
34  import javax.swing.event.*;
35  import javax.swing.table.*;
36  
37  import zeus.concepts.*;
38  import zeus.ontology.*;
39  import zeus.gui.editors.*;
40  import zeus.ext.DbConnector;
41  
42  
43  public class ColumnsTableModel extends AbstractTableModel
44  {
45    protected String[]     columnNames = { "Column", "Type", "Zeus Equivalent" };
46    protected String[][]   data = null;
47    protected boolean[][]  validityInfo = null; // might use this for unsupported types
48    protected DatabasePane DBPane = null;
49    protected DbConnector  MYDB = null;
50  
51    public ColumnsTableModel(DatabasePane parent)
52    {
53      DBPane = parent;
54      // nothing to refresh as no table is selected yet
55    }
56  
57    void refresh() {
58      // refresh data from source
59      fireTableStructureChanged();
60    }
61  
62    public int      getColumnCount()     { return columnNames.length; }
63    public int      getRowCount()        { return (data != null) ? data.length : 0; }
64    public String[] getRow(int row)      { return data[row]; }
65  
66    public boolean isCellEditable(int r, int c) { return false;}
67    public String  getColumnName(int c)         { return columnNames[c]; }
68  
69    public boolean isValidEntry(int r, int c)   { return validityInfo[r][c]; }
70  
71    public Object  getValueAt(int r, int c)     { return data[r][c]; }
72  
73  
74    public String mapTypeToOntology(int sqlType)
75    {
76      if (sqlType == Types.VARCHAR || sqlType == Types.LONGVARCHAR ||
77          sqlType == Types.CLOB || sqlType == Types.CHAR)
78        return DBPane.ONTDB.STRING;
79      else if (sqlType == Types.BIT || sqlType == Types.VARBINARY)
80        return DBPane.ONTDB.BOOLEAN;
81      else if (sqlType == Types.INTEGER || sqlType == Types.SMALLINT || sqlType == Types.TINYINT)
82        return DBPane.ONTDB.INTEGER;
83      else if (sqlType == Types.DECIMAL || sqlType == Types.DOUBLE || sqlType == Types.FLOAT)
84        return DBPane.ONTDB.REAL;
85      else if (sqlType == Types.DATE)
86        return DBPane.ONTDB.DATE;
87      else if (sqlType == Types.TIME || sqlType == Types.TIMESTAMP )
88        return DBPane.ONTDB.TIME;
89      return "Not supported";
90    }
91  
92    public void setValues(ResultSet rs, int count)
93    {
94      try
95      {
96        //ResultSetMetaData rsmd = rs.getMetaData();
97        data = new String[count][3];
98        int i = 0;
99        while(rs.next())
100       {
101         String cName = rs.getString(4);
102         data[i][0] = cName;
103         String cType = rs.getString(6);
104         data[i][1] = cType;
105         // determine most appropriate type
106         String zType = mapTypeToOntology(rs.getInt(5));
107         data[i][2] = zType;
108         // System.out.println("> " + cName + " " + cType + " " + zType);
109         i++;
110       }
111     }
112     catch(SQLException e)
113     {
114       DBPane.messageArea.append("\nSQL Exception: " + e + "\n");
115     }
116   }
117 
118   public void refreshColumns(String dbTableName)
119   {
120     MYDB = DBPane.MYDB;
121     // get list of columns from named table
122     DBPane.messageArea.append("Getting columns of " + dbTableName);
123     try
124     {
125       DatabaseMetaData dbmd = MYDB.getConnection().getMetaData();
126       ResultSet rs = dbmd.getColumns(null, null, dbTableName, "%");
127 
128       int c = 0;
129       while (rs.next()) c++;
130       rs = dbmd.getColumns(null, null, dbTableName, "%");
131 
132       setValues(rs, c); // convert resultSet into data[][]
133       MYDB.close();
134       DBPane.messageArea.append(" .. OK\n");
135     }
136     catch(SQLException e)
137     {
138       DBPane.messageArea.append("\nSQL Exception: " + e + "\n");
139     }
140     refresh();
141   }
142 
143   String[][] getRows(int[] input)
144   {
145     String[][] result = new String[input.length][columnNames.length];
146     for(int i = 0; i < result.length; i++ )
147       for(int j = 0; j < result[i].length; j++)
148         result[i][j] = data[input[i]][j];
149     return result;
150   }
151 }