001 package com.sptci.rwt.webui;
002
003 import com.sptci.echo2.table.SortableTableModel;
004
005 import com.sptci.rwt.ConnectionManager;
006 import com.sptci.rwt.QueryException;
007 import com.sptci.rwt.Row;
008
009 /**
010 * A custom table model used to display {@link com.sptci.rwt.Row} objects.
011 *
012 * <p>© Copyright 2007 <a href='http://sptci.com/' target='_new'>Sans Pareil Technologies, Inc.</a></p>
013 * @author Rakesh Vidyadharan 2007-11-13
014 * @version $Id: SortableRowTableModel.java 4123 2008-05-25 21:49:01Z rakesh $
015 * @since Version 1.3
016 */
017 public class SortableRowTableModel extends RowTableModel
018 implements SortableTableModel
019 {
020 /** The name of the column to use in a SQL order by clause. */
021 protected String orderBy;
022
023 /**
024 * Create a new instance of the table model using the specified collection
025 * of {@link com.sptci.rwt.Row} objects.
026 *
027 * @see #fetchData
028 * @see #processColumns
029 * @param query The sql statement that is to be executed to fetch any
030 * additional pages of data.
031 * @param maxRows The maximum number of rows to fetch from the
032 * result set.
033 * @param maxColumnLength The maximum number of characters to display in
034 * a column.
035 * @param manager The manager to use to fetch database connections.
036 * @throws QueryException If errors are encountered while executing
037 * the {@link #query}.
038 */
039 public SortableRowTableModel( final String query, final int maxRows,
040 final int maxColumnLength, final ConnectionManager manager )
041 throws QueryException
042 {
043 super( query, maxRows, maxColumnLength, manager );
044 }
045
046 /**
047 * Sort the data displayed in the table by the specified column.
048 * Re-fetch the data from the {@link #query} ordered by the specified
049 * column.
050 *
051 * @see com.sptci.echo2.table.SortableTableModel#sort( int )
052 * @see #getSortDirection
053 * @see #sort( int, Direction )
054 * @param column The index of the column in {@link #columns} by which
055 * to order the results.
056 * @throws RuntimeException If errors are encountered while fetching
057 * the data.
058 */
059 public void sort( final int column ) throws RuntimeException
060 {
061 Direction direction = getSortDirection( column );
062 sort( column, direction );
063 }
064
065 /**
066 * Sort the data displayed in the table by the specified column.
067 * Re-fetch the data from the {@link #query} ordered by the specified
068 * column. The {@link #page} is set to point to the first page of
069 * data since the re-sorted data will usually have no reference to the
070 * original data.
071 *
072 * @param column The index of the column in {@link #columns} by which
073 * to order the results.
074 * @param direction The direction in which to sort the data.
075 * @throws RuntimeException If errors are encountered while fetching
076 * the data.
077 */
078 public void sort( final int column, final Direction direction )
079 throws RuntimeException
080 {
081 page = 0;
082 sortIndex = column;
083 sortDirection = ( direction == null ) ? Direction.ascending : direction;
084 orderBy = null;
085 String name = columns.get( column ).getName();
086
087 switch ( sortDirection )
088 {
089 case descending:
090 name += " desc";
091 break;
092 }
093
094 orderBy = name;
095
096 try
097 {
098 fetchData();
099 }
100 catch ( Throwable t )
101 {
102 throw new RuntimeException( t );
103 }
104 }
105
106 /**
107 * Returns {@link #query}. Over-ridden to tag on an order by clause
108 * if {@link #orderBy} is not null.
109 *
110 * @return The value/reference of/to query.
111 */
112 @Override
113 public String getQuery()
114 {
115 return ( orderBy == null ) ? query : query + " order by " + orderBy;
116 }
117 }