View Javadoc

1   package org.paneris.jal.model;
2   
3   import java.sql.Connection;
4   import java.sql.ResultSet;
5   import java.sql.Statement;
6   
7   import org.paneris.util.ExtendedHash;
8   import org.webmacro.servlet.HandlerException;
9   
10  /**
11   * A TableMetaData encapsulates meta data for a single table in the datadictionary.
12   * It has a hashtable for datadictionary fields.
13   *
14   * <p>It can be constructed from the table name or from the table id number.</p>
15   *
16   * <p>You need to pass a table identifier and a database to the constructors </p>
17   *
18   */
19  public class TableMetaData {
20  
21      private String tableName;
22      private String displayName;
23      private ExtendedHash fields;
24      private Integer displayOrder;
25      private Integer resultsPerPage;
26      private Integer maxResults;
27      private boolean cached;
28      private boolean listEdit;
29      private Integer tableNumber;
30      private String type;
31      private String updateController;
32      private String database;
33      private DBConnectionManager connMgr;
34      private DataCache dataCache = DataCache.getInstance();
35  
36  /**
37   Constructor to build a table using a table name
38  **/
39      public TableMetaData(String db, String tn) throws Exception {
40          database = db;
41          connMgr = DBConnectionManager.getInstance();
42          Connection conn = connMgr.getConnection("TableMetaData",database);
43          Statement s = conn.createStatement();
44          String sqlString = "SELECT * FROM datadictionarytables WHERE tablename = '";
45          sqlString = sqlString.concat(tn + "'");
46          try {
47              ResultSet rs = s.executeQuery(sqlString);
48              if (rs.next()) {
49                  Integer tableNumber = new Integer(rs.getInt("id"));
50                  init(tableNumber, conn);
51              } else {
52                  throw new Exception("Tablename not found in DatadictionaryTables Table: " + tn);
53              }
54          } catch (Exception e) {
55              throw new HandlerException(e.toString() + " sqlString: " + sqlString);
56          }
57          s.close();
58          connMgr.freeConnection(database, conn);
59      }
60  
61  /**
62   constructor to build a table given it's id in the datadictionarytables table
63  **/
64      public TableMetaData(String db, Integer tn) throws Exception {
65          database = db;
66          connMgr = DBConnectionManager.getInstance();
67          Connection conn = connMgr.getConnection("TableMetaData",db);
68          init(tn, conn);
69          connMgr.freeConnection(database, conn);
70      }
71  
72  /**
73  initise the datadictionary data structure
74  **/
75      private void init(Integer tn, Connection c) throws Exception {
76          Statement s = c.createStatement();
77          Statement s2 = c.createStatement();
78          tableNumber = tn;
79          String sqlString = new String("SELECT * FROM datadictionarytables WHERE id = " + tableNumber);
80          try {
81          //
82          //get the stuff for the table
83              ResultSet rs = s.executeQuery(sqlString);
84              rs.next();
85              tableName = rs.getString("tablename").trim();
86              displayName = rs.getString("displayname").trim();
87              displayOrder = new Integer(rs.getInt("displayorder"));
88              Integer shutUp = displayOrder;
89              displayOrder = shutUp;
90              try {
91                  resultsPerPage = new Integer(rs.getInt("defaultresultsperpage"));
92              } catch (Exception e) {
93                  resultsPerPage = new Integer(0);
94              }
95              try {
96                  maxResults = new Integer(rs.getInt("maxresults"));
97              } catch (Exception e) {
98                  maxResults = new Integer(0);
99              }
100             cached = rs.getBoolean("cached");
101             try {
102                 listEdit = rs.getBoolean("listedit");
103             } catch (Exception e) {
104                 listEdit = true;
105             }
106             try {
107                 updateController = rs.getString("updatecontroller");
108             } catch (Exception e) {
109                 updateController = null;
110             }
111             if (updateController == null) updateController = "";
112             int typeInt = rs.getInt("type");
113             if (typeInt != 0) {
114                 sqlString = new String("SELECT type FROM datadictionarytabletypes WHERE id = " + typeInt);
115                 ResultSet rs2 = s2.executeQuery(sqlString);
116                 rs2.next();
117                 type = rs2.getString("type").trim();
118             } else {
119                 type = "";
120             }
121             //
122             // get the stuff for the fields
123             sqlString = "SELECT id FROM datadictionary WHERE tablename = ";
124             sqlString = sqlString.concat(tableNumber.toString());
125             sqlString = sqlString.concat(" ORDER BY displayorder");
126             rs = s.executeQuery(sqlString);
127             fields = new ExtendedHash();
128             while (rs.next()) {
129                 FieldMetaData fieldMetaData = dataCache.getFieldMetaData(database, new Integer(rs.getInt("id")));
130                 fields.put(fieldMetaData.getFieldName(),fieldMetaData);
131             }
132         } catch (Exception e) {
133             throw new Exception(e.toString() + " TableMetaData SQL: " + sqlString);
134         }
135         s.close();
136     }
137 
138 /**
139  * @return a hash of field data for this table
140  */
141     public ExtendedHash getFields() {
142         return fields;
143     }
144 
145 /**
146  * get the tablename
147  */
148     public String getTableName() {
149         return tableName;
150     }
151 
152 /**
153  * allow this table to be edited as part of a list
154  */
155     public boolean getListEdit() {
156         return listEdit;
157     }
158 
159 /**
160  * allow this table to be edited as part of a list
161  */
162     public String getUpdateController() {
163         return updateController;
164     }
165 
166 /**
167  * get the type of the table
168  */
169     public String getType() {
170         return type;
171     }
172 
173 /**
174  * get the tablenumber
175  */
176     public Integer getTableNumber() {
177         return tableNumber;
178     }
179 
180 /**
181  * get the default results per page
182  */
183     public Integer getResultsPerPage() {
184         return resultsPerPage;
185     }
186 
187 /**
188  * get the maximum number of results for this table
189  */
190     public int getMaxResults() {
191         return maxResults.intValue();
192     }
193 
194 /**
195  * apply the max results if appropriate
196  */
197     public String applyMaxResults(String db, String sql) throws Exception {
198         String sqlString = sql;
199         if (getMaxResults() > 0) {
200             if (connMgr.getDatabaseEngineType(db) == DBConnectionManager.POSTGRES && 
201                 !connMgr.getDatabaseProductVersion(database).equals("6.4")) {
202                 sqlString += " LIMIT " + getMaxResults();
203             }
204         }
205         return sqlString;
206     }
207 
208 /**
209  * Get the display name for the table.
210  */
211     public String getDisplayName() {
212         return displayName;
213     }
214 
215 /**
216  * find out if the table is cached
217  */
218     public boolean getCached() {
219         return cached;
220     }
221 }