You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Daniel Jue <te...@gmail.com> on 2006/12/29 16:45:44 UTC

Using a CachedRowSet to supply data to a table

Hello list,  I am trying to use a CachedRowSet to supply data to a
table.  I implemented a CachedRowSetIterator class, but it doesn't
seem to play well with the Contrib:Table.  (I am using Tap 4.0.2)

I am also Persisting the CachedRowSet in my page using this notation:

@Persist("session")
public abstract CachedRowSet getCRS();
public abstract void setCRS(CachedRowSet crs_in);

I am not persisting the CachedRowSetIterator.

I am also telling Tapestry to use the persistance available to the
Table.  Not sure if this is overkill, or interfering:

<table class="mytable" jwcid="table2@Contrib:Table"
source="ognl:CRSIterator" columns="ognl:tableColumns"
rowsClass="ognl:beans.evenOdd.next" pageSize="999"
name="tableSessionStateManager" value="ognl:new
org.apache.tapestry.contrib.table.model.common.FullTableSessionStateManager()">
</table>

The getTableColumns method returns a string like this:

public String getTableColumns() {
  return "=getColumn('SHOP_CODE')," + "=getColumn('SHOP_NAME'),"
  + "=getColumn('COMPLETED_WITH_COSTS'),"
  + "=getColumn('AVG_DAYS_TO_COMPLETE'),"
  + "=getColumn('COMPLETED_IN_HOUSE'),"
  + "=getColumn('AVG_IN_HOUSE_HOURS'),"
  + "=getColumn('COMPLETED_WORK_ORDER_COSTS'),"
  + "=getColumn('AVG_COST')";
}

getColumn is like this:

public ITableColumn getColumn(String name) {
  SimpleTableColumn TempColumn = new SimpleTableColumn(name, name
	+ " Label", new StandardColumnEvaluator(), true);
   TempColumn.setColumnComparator(new StandardComparator());
   return TempColumn;
}

the StandardColumnEvaluator class is a private class to the page, and
looks like this:
private class StandardColumnEvaluator implements ITableColumnEvaluator {
/**
 * @see org.apache.tapestry.contrib.table.model.simple.ITableColumnEvaluator#getColumnValue(ITableColumn,
*      Object)
*/
  public Object getColumnValue(ITableColumn objColumn, Object objRow) {
    try {
      return ((CachedRowSet) objRow).getObject(objColumn.getColumnName());
    } catch (SQLException e) {
       System.out.println(e.getMessage());// e.printStackTrace();
    }
  return null;
  }
}

StandardComparator just does a string comparison on each if it can.

So what happens is :

All the getColumn(XYZ)'s get called, once for each column
A CRSIterator is created.
getColumnValue(a), getColumnValue(b),....getColumnValue(n) is called
for each row, but on each row the Iterator is on the last position.

i.e. the Iterator is not iterating.
Any ideas on what is wrong, or a better way to go about this?  I
_could_ create an ArrayList of row objects and send that to the table,
but the whole reason for using the CachedRowSet was to have the data
only stored in one object.

Here is my CachedRowSetIterator:

//------------------------------------------------
import java.io.Serializable;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import javax.sql.rowset.CachedRowSet;

public class CachedRowSetIterator implements Serializable, Iterator<Object> {
private static final long serialVersionUID = 8121123424550983513L;
private CachedRowSet crs;

	/**
	 * Empty constructor required by JavaBean specification.
	 */
	public CachedRowSetIterator() {
	}

	/**
	 * Convenience constructor to instantiate object with values.
	 */
	public CachedRowSetIterator(CachedRowSet crs) {
		set(crs);
	}

	public CachedRowSet getValues() {
		return this.crs;
	}

	// --------------------------------------------------------- Public
	// Methods
	/*
	 * Returns true if the iteration has more elements.
	 */
	public boolean hasNext() {
		try {
			return (!this.crs.isLast());
		} catch (SQLException sqle) {
			return false;
		}
	}

	/*
	 * Returns the next element in the interation. - iteration has no more
	 * elements
	 */
	public Object next() throws NoSuchElementException {
		try {
			if (this.crs.next()) {
				return this.crs;
			}
		} catch (SQLException sqle) {
			return null;
		}
		return null;
	}

	/*
	 * Not implemented.
	 */
	public void remove() throws UnsupportedOperationException,
			IllegalStateException {
		throw new UnsupportedOperationException();
	}

	/**
	 * Convenience constructor to set object values.
	 */
	public void set(CachedRowSet crsIn) {
		this.crs = crsIn;
	}
}
//------------------------------------------------

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org