Coverage Report - org.apache.tapestry.contrib.table.model.sql.SqlTableModel
 
Classes in this File Line Coverage Branch Coverage Complexity
SqlTableModel
0%
0/24
N/A
1.5
SqlTableModel$1
0%
0/3
N/A
1.5
 
 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 org.apache.commons.logging.Log;
 18  
 import org.apache.commons.logging.LogFactory;
 19  
 import org.apache.tapestry.contrib.table.model.ITableColumnModel;
 20  
 import org.apache.tapestry.contrib.table.model.common.AbstractTableModel;
 21  
 import org.apache.tapestry.contrib.table.model.simple.SimpleTableState;
 22  
 
 23  
 import java.sql.ResultSet;
 24  
 import java.sql.SQLException;
 25  
 import java.util.Iterator;
 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 the data sorting
 30  
  * (through ORDER BY) and obtains only the data on the current page (through
 31  
  * LIMIT/OFFSET).
 32  
  * <p>
 33  
  * This object is typically created in the following manner:
 34  
  * 
 35  
  * <pre>
 36  
  * ISqlConnectionSource objConnSrc = new SimpleSqlConnectionSource(
 37  
  *         &quot;jdbc:postgresql://localhost/testdb&quot;, &quot;testdb&quot;, &quot;testdb&quot;);
 38  
  * 
 39  
  * ISqlTableDataSource objDataSrc = new SimpleSqlTableDataSource(objConnSrc,
 40  
  *         &quot;test_table&quot;);
 41  
  * 
 42  
  * SqlTableColumnModel objColumnModel = new SqlTableColumnModel(
 43  
  *         new SqlTableColumn[] {
 44  
  *                 new SqlTableColumn(&quot;language&quot;, &quot;Language&quot;, true),
 45  
  *                 new SqlTableColumn(&quot;country&quot;, &quot;Country&quot;, true),
 46  
  *                 new SqlTableColumn(&quot;variant&quot;, &quot;Variant&quot;, true),
 47  
  *                 new SqlTableColumn(&quot;intvalue&quot;, &quot;Integer&quot;, true),
 48  
  *                 new SqlTableColumn(&quot;floatvalue&quot;, &quot;Float&quot;, true) });
 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  
 
 60  
     private static final long serialVersionUID = 1L;
 61  0
     private static final Log LOG = LogFactory.getLog(SqlTableModel.class);
 62  
 
 63  
     private ISqlTableDataSource m_objDataSource;
 64  
     private SqlTableColumnModel m_objColumnModel;
 65  
 
 66  
     {
 67  
         try
 68  
         {
 69  0
             Class.forName("org.hsqldb.jdbcDriver");
 70  
         }
 71  0
         catch (Exception e)
 72  
         {
 73  0
             System.out.println("ERROR: failed to load HSQLDB JDBC driver.");
 74  0
             e.printStackTrace();
 75  0
         }
 76  
     }
 77  
 
 78  
     public SqlTableModel(ISqlTableDataSource objDataSource,
 79  
             SqlTableColumnModel objColumnModel)
 80  
     {
 81  0
         this(objDataSource, objColumnModel, new SimpleTableState());
 82  0
     }
 83  
 
 84  
     public SqlTableModel(ISqlTableDataSource objDataSource,
 85  
             SqlTableColumnModel objColumnModel, SimpleTableState objState)
 86  
     {
 87  0
         super(objState);
 88  0
         m_objDataSource = objDataSource;
 89  0
         m_objColumnModel = objColumnModel;
 90  0
     }
 91  
 
 92  
     /**
 93  
      * @see org.apache.tapestry.contrib.table.model.ITableModel#getColumnModel()
 94  
      */
 95  
     public ITableColumnModel getColumnModel()
 96  
     {
 97  0
         return m_objColumnModel;
 98  
     }
 99  
 
 100  
     public SqlTableColumnModel getSqlColumnModel()
 101  
     {
 102  0
         return m_objColumnModel;
 103  
     }
 104  
 
 105  
     /**
 106  
      * @see org.apache.tapestry.contrib.table.model.ITableModel#getCurrentPageRows()
 107  
      */
 108  
     public Iterator getCurrentPageRows()
 109  
     {
 110  
         try
 111  
         {
 112  0
             ResultSet objResultSet = getSqlDataSource().getCurrentRows(getSqlColumnModel(), getState());
 113  
 
 114  0
             return new ResultSetIterator(objResultSet)
 115  0
             {
 116  
 
 117  
                 protected void notifyEnd()
 118  
                 {
 119  0
                     getSqlDataSource().closeResultSet(getResultSet());
 120  0
                 }
 121  
             };
 122  
         }
 123  0
         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  
      * 
 133  
      * @return ISqlTableDataSource
 134  
      */
 135  
     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  
     public int getRowCount()
 144  
     {
 145  
         try
 146  
         {
 147  0
             return m_objDataSource.getRowCount();
 148  
         }
 149  0
         catch (SQLException e)
 150  
         {
 151  0
             LOG.error("Cannot get row count", e);
 152  0
             return 1;
 153  
         }
 154  
     }
 155  
 
 156  
 }