KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > table > model > sql > SqlTableModel


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.contrib.table.model.sql;
16
17 import java.sql.ResultSet JavaDoc;
18 import java.sql.SQLException JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.tapestry.contrib.table.model.ITableColumnModel;
24 import org.apache.tapestry.contrib.table.model.common.AbstractTableModel;
25 import org.apache.tapestry.contrib.table.model.simple.SimpleTableState;
26
27 /**
28  * An implementation of ITableModel that obtains its data through SQL queries.
29  * This is a very efficient model, since it uses SQL to perform
30  * the data sorting (through ORDER BY) and obtains only the data
31  * on the current page (through LIMIT/OFFSET).
32  * <p>
33  * This object is typically created in the following manner:
34  * <pre>
35  * ISqlConnectionSource objConnSrc =
36  * new SimpleSqlConnectionSource("jdbc:postgresql://localhost/testdb", "testdb", "testdb");
37  *
38  * ISqlTableDataSource objDataSrc =
39  * new SimpleSqlTableDataSource(objConnSrc, "test_table");
40  *
41  * SqlTableColumnModel objColumnModel =
42  * new SqlTableColumnModel(new SqlTableColumn[] {
43  * new SqlTableColumn("language", "Language", true),
44  * new SqlTableColumn("country", "Country", true),
45  * new SqlTableColumn("variant", "Variant", true),
46  * new SqlTableColumn("intvalue", "Integer", true),
47  * new SqlTableColumn("floatvalue", "Float", true)
48  * });
49  *
50  * ITableModel objTableModel = new SqlTableModel(objDataSrc, objColumnModel);
51  *
52  * return objTableModel;
53  * </pre>
54  *
55  * @author mindbridge
56  */

57 public class SqlTableModel extends AbstractTableModel
58 {
59     private static final long serialVersionUID = 1L;
60     private static final Log LOG = LogFactory.getLog(SqlTableModel.class);
61
62     private ISqlTableDataSource m_objDataSource;
63     private SqlTableColumnModel m_objColumnModel;
64     
65     {
66         try {
67             Class.forName ( "org.hsqldb.jdbcDriver" );
68         } catch (Exception JavaDoc e) {
69             System.out.println("ERROR: failed to load HSQLDB JDBC driver.");
70             e.printStackTrace();
71         }
72     }
73
74     public SqlTableModel(
75         ISqlTableDataSource objDataSource,
76         SqlTableColumnModel objColumnModel)
77     {
78         this(objDataSource, objColumnModel, new SimpleTableState());
79     }
80
81     public SqlTableModel(
82         ISqlTableDataSource objDataSource,
83         SqlTableColumnModel objColumnModel,
84         SimpleTableState objState)
85     {
86         super(objState);
87         m_objDataSource = objDataSource;
88         m_objColumnModel = objColumnModel;
89     }
90
91     /**
92      * @see org.apache.tapestry.contrib.table.model.ITableModel#getColumnModel()
93      */

94     public ITableColumnModel getColumnModel()
95     {
96         return m_objColumnModel;
97     }
98
99     public SqlTableColumnModel getSqlColumnModel()
100     {
101         return m_objColumnModel;
102     }
103
104     /**
105      * @see org.apache.tapestry.contrib.table.model.ITableModel#getCurrentPageRows()
106      */

107     public Iterator JavaDoc getCurrentPageRows()
108     {
109         try
110         {
111             ResultSet JavaDoc objResultSet =
112                 getSqlDataSource().getCurrentRows(
113                     getSqlColumnModel(),
114                     getState());
115
116             return new ResultSetIterator(objResultSet)
117             {
118                 protected void notifyEnd()
119                 {
120                     getSqlDataSource().closeResultSet(getResultSet());
121                 }
122             };
123         }
124         catch (SQLException JavaDoc e)
125         {
126             LOG.error("Cannot get current page rows", e);
127             return new ResultSetIterator(null);
128         }
129     }
130
131     /**
132      * Returns the dataSource.
133      * @return ISqlTableDataSource
134      */

135     public ISqlTableDataSource getSqlDataSource()
136     {
137         return m_objDataSource;
138     }
139
140     /**
141      * @see org.apache.tapestry.contrib.table.model.common.AbstractTableModel#getRowCount()
142      */

143     protected int getRowCount()
144     {
145         try
146         {
147             return m_objDataSource.getRowCount();
148         }
149         catch (SQLException JavaDoc e)
150         {
151             LOG.error("Cannot get row count", e);
152             return 1;
153         }
154     }
155
156 }
157
Popular Tags