|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
SqlTableModel.java | - | 0% | 0% | 0% |
|
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;
|
|
18 |
import java.sql.SQLException;
|
|
19 |
import java.util.Iterator;
|
|
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 Log LOG = LogFactory.getLog(SqlTableModel.class); |
|
60 |
|
|
61 |
private ISqlTableDataSource m_objDataSource;
|
|
62 |
private SqlTableColumnModel m_objColumnModel;
|
|
63 |
|
|
64 |
{ |
|
65 | 0 |
try {
|
66 | 0 |
Class.forName ( "org.hsqldb.jdbcDriver" );
|
67 |
} catch (Exception e) {
|
|
68 | 0 |
System.out.println("ERROR: failed to load HSQLDB JDBC driver.");
|
69 | 0 |
e.printStackTrace(); |
70 |
} |
|
71 |
} |
|
72 |
|
|
73 | 0 |
public SqlTableModel(
|
74 |
ISqlTableDataSource objDataSource, |
|
75 |
SqlTableColumnModel objColumnModel) |
|
76 |
{ |
|
77 | 0 |
this(objDataSource, objColumnModel, new SimpleTableState()); |
78 |
} |
|
79 |
|
|
80 | 0 |
public SqlTableModel(
|
81 |
ISqlTableDataSource objDataSource, |
|
82 |
SqlTableColumnModel objColumnModel, |
|
83 |
SimpleTableState objState) |
|
84 |
{ |
|
85 | 0 |
super(objState);
|
86 | 0 |
m_objDataSource = objDataSource; |
87 | 0 |
m_objColumnModel = objColumnModel; |
88 |
} |
|
89 |
|
|
90 |
/**
|
|
91 |
* @see org.apache.tapestry.contrib.table.model.ITableModel#getColumnModel()
|
|
92 |
*/
|
|
93 | 0 |
public ITableColumnModel getColumnModel()
|
94 |
{ |
|
95 | 0 |
return m_objColumnModel;
|
96 |
} |
|
97 |
|
|
98 | 0 |
public SqlTableColumnModel getSqlColumnModel()
|
99 |
{ |
|
100 | 0 |
return m_objColumnModel;
|
101 |
} |
|
102 |
|
|
103 |
/**
|
|
104 |
* @see org.apache.tapestry.contrib.table.model.ITableModel#getCurrentPageRows()
|
|
105 |
*/
|
|
106 | 0 |
public Iterator getCurrentPageRows()
|
107 |
{ |
|
108 | 0 |
try
|
109 |
{ |
|
110 | 0 |
ResultSet objResultSet = |
111 |
getSqlDataSource().getCurrentRows( |
|
112 |
getSqlColumnModel(), |
|
113 |
getState()); |
|
114 |
|
|
115 | 0 |
return new ResultSetIterator(objResultSet) |
116 |
{ |
|
117 | 0 |
protected void notifyEnd() |
118 |
{ |
|
119 | 0 |
getSqlDataSource().closeResultSet(getResultSet()); |
120 |
} |
|
121 |
}; |
|
122 |
} |
|
123 |
catch (SQLException e)
|
|
124 |
{ |
|
125 | 0 |
LOG.error("Cannot get current page rows", e);
|
126 | 0 |
return new ResultSetIterator(null); |
127 |
} |
|
128 |
} |
|
129 |
|
|
130 |
/**
|
|
131 |
* Returns the dataSource.
|
|
132 |
* @return ISqlTableDataSource
|
|
133 |
*/
|
|
134 | 0 |
public ISqlTableDataSource getSqlDataSource()
|
135 |
{ |
|
136 | 0 |
return m_objDataSource;
|
137 |
} |
|
138 |
|
|
139 |
/**
|
|
140 |
* @see org.apache.tapestry.contrib.table.model.common.AbstractTableModel#getRowCount()
|
|
141 |
*/
|
|
142 | 0 |
protected int getRowCount() |
143 |
{ |
|
144 | 0 |
try
|
145 |
{ |
|
146 | 0 |
return m_objDataSource.getRowCount();
|
147 |
} |
|
148 |
catch (SQLException e)
|
|
149 |
{ |
|
150 | 0 |
LOG.error("Cannot get row count", e);
|
151 | 0 |
return 1;
|
152 |
} |
|
153 |
} |
|
154 |
|
|
155 |
} |
|
156 |
|
|