Clover coverage report - Code Coverage for tapestry-contrib release 4.0.1
Coverage timestamp: Fri Mar 31 2006 09:15:40 EST
file stats: LOC: 156   Methods: 8
NCLOC: 85   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
SqlTableModel.java - 0% 0% 0%
coverage
 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 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  0 try {
 67  0 Class.forName ( "org.hsqldb.jdbcDriver" );
 68    } catch (Exception e) {
 69  0 System.out.println("ERROR: failed to load HSQLDB JDBC driver.");
 70  0 e.printStackTrace();
 71    }
 72    }
 73   
 74  0 public SqlTableModel(
 75    ISqlTableDataSource objDataSource,
 76    SqlTableColumnModel objColumnModel)
 77    {
 78  0 this(objDataSource, objColumnModel, new SimpleTableState());
 79    }
 80   
 81  0 public SqlTableModel(
 82    ISqlTableDataSource objDataSource,
 83    SqlTableColumnModel objColumnModel,
 84    SimpleTableState objState)
 85    {
 86  0 super(objState);
 87  0 m_objDataSource = objDataSource;
 88  0 m_objColumnModel = objColumnModel;
 89    }
 90   
 91    /**
 92    * @see org.apache.tapestry.contrib.table.model.ITableModel#getColumnModel()
 93    */
 94  0 public ITableColumnModel getColumnModel()
 95    {
 96  0 return m_objColumnModel;
 97    }
 98   
 99  0 public SqlTableColumnModel getSqlColumnModel()
 100    {
 101  0 return m_objColumnModel;
 102    }
 103   
 104    /**
 105    * @see org.apache.tapestry.contrib.table.model.ITableModel#getCurrentPageRows()
 106    */
 107  0 public Iterator getCurrentPageRows()
 108    {
 109  0 try
 110    {
 111  0 ResultSet objResultSet =
 112    getSqlDataSource().getCurrentRows(
 113    getSqlColumnModel(),
 114    getState());
 115   
 116  0 return new ResultSetIterator(objResultSet)
 117    {
 118  0 protected void notifyEnd()
 119    {
 120  0 getSqlDataSource().closeResultSet(getResultSet());
 121    }
 122    };
 123    }
 124    catch (SQLException e)
 125    {
 126  0 LOG.error("Cannot get current page rows", e);
 127  0 return new ResultSetIterator(null);
 128    }
 129    }
 130   
 131    /**
 132    * Returns the dataSource.
 133    * @return ISqlTableDataSource
 134    */
 135  0 public ISqlTableDataSource getSqlDataSource()
 136    {
 137  0 return m_objDataSource;
 138    }
 139   
 140    /**
 141    * @see org.apache.tapestry.contrib.table.model.common.AbstractTableModel#getRowCount()
 142    */
 143  0 protected int getRowCount()
 144    {
 145  0 try
 146    {
 147  0 return m_objDataSource.getRowCount();
 148    }
 149    catch (SQLException e)
 150    {
 151  0 LOG.error("Cannot get row count", e);
 152  0 return 1;
 153    }
 154    }
 155   
 156    }