1 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 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 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 94 public ITableColumnModel getColumnModel() 95 { 96 return m_objColumnModel; 97 } 98 99 public SqlTableColumnModel getSqlColumnModel() 100 { 101 return m_objColumnModel; 102 } 103 104 107 public Iterator getCurrentPageRows() 108 { 109 try 110 { 111 ResultSet 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 e) 125 { 126 LOG.error("Cannot get current page rows", e); 127 return new ResultSetIterator(null); 128 } 129 } 130 131 135 public ISqlTableDataSource getSqlDataSource() 136 { 137 return m_objDataSource; 138 } 139 140 143 protected int getRowCount() 144 { 145 try 146 { 147 return m_objDataSource.getRowCount(); 148 } 149 catch (SQLException e) 150 { 151 LOG.error("Cannot get row count", e); 152 return 1; 153 } 154 } 155 156 } 157 | Popular Tags |