Yes,
I saw the issue :) few days ago ...

ok....
The grid docs should mention following caveat:

If you have a datasource that is filtered and the size changes
with some user action like submitting a search form...
having pager set to a some page can brake the grid
if number of rows gets smaller and the page does not exist an more.

for this reason it is recommendable to set pager to page:1 after
search form gets submitted (search criteria changes)
this can be achieved by calling         grid.setCurrentPage(1);


.... while I'm still talking about the grid, there is a neat (for me
at least :D )
GridDataSource I've made for Hibernate in my apps.

it uses Criteria for filtering, and gets sorting criteria from the grid,
it loads only the results that will be shown,
it caches result from the count query so instance is not reusable (but
can be made so)

I'm putting it on wiki, but belive it could be usefull in tapestry-hibernate
and could be mentioned in Grid's docs.
There is already one similar in the wiki, but uses HQL... (I like
Criteria API more)

SOURCE:

import java.util.List;

import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.beaneditor.PropertyModel;
import org.apache.tapestry.grid.GridDataSource;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

/** Grid data source that queries hiberante. It caches count and
results, and is not suitable for reuse.*/
public class  HibernateEntityDataSource<T> implements GridDataSource {

    private final Class<T> persistentClass;
    private final Session _session;
    private final Criteria criteria;

    /** cached data */
    protected List<T> data;
    /** count value cached on first call to [EMAIL PROTECTED] 
#getAvailableRows()}*/
    protected int count=-1;
    /** we will select only part of the table so later when asked for
a row, we use this to offset the index*/
    protected int startIndex=0;
    private ComponentResources _resources;

    /** A datasource for grid. Provide [EMAIL PROTECTED] ComponentResources} and
"sortColumn" event will be triggered
     * from the [EMAIL PROTECTED] #prepare(int, int, PropertyModel, boolean)} 
method.*/
    @SuppressWarnings("unchecked")
    public HibernateEntityDataSource(Session session, Class<T> type,
ComponentResources resources){
        super();
        _session = session;
        persistentClass = type;
        _resources = resources;
        criteria = _session.createCriteria(persistentClass);
    }

    public static final <T> HibernateEntityDataSource<T>
create(Session session,Class<T> type, ComponentResources resources){
        return new HibernateEntityDataSource<T>(session,type, resources);
    }

    /** this value is cached on first access so do not add filters
after it is called*/
    public int getAvailableRows() {
        if(count == -1){
            criteria.setProjection(Projections.rowCount());
            count =((Integer)criteria.list().get(0)).intValue();
            criteria.setProjection(null);
            criteria.setResultTransformer(Criteria.ROOT_ENTITY);
        }
        return count;
    }

    public Class<T> getRowType() {
        return persistentClass;
    }
    /** Add a creterion to filter results. for example: [EMAIL PROTECTED]
Restrictions#eq(String, Object)}*/
    public void addCriterion(Criterion criterion){
        criteria.add(criterion);
    }

    /** The index must be between startIndex and endIndex provided in
[EMAIL PROTECTED] #prepare(int, int, PropertyModel, boolean)} implemented from
[EMAIL PROTECTED] GridDataSource#prepare(int, int, PropertyModel, boolean)}*/
    public Object getRowValue(int index) {
        return data.get(index-startIndex);
    }

    @SuppressWarnings("unchecked")
    public void prepare(int startIndex, int endIndex, PropertyModel
sortModel, boolean ascending) {
        //query is much faster if we take only results we need. it can
be up to 10x faster even for a small table with 1000 records
        criteria.setFirstResult(startIndex);
        criteria.setMaxResults(endIndex-startIndex+1);
        this.startIndex = startIndex;

        String sortColumnName = null;
        if(sortModel != null){
            sortColumnName = sortModel.getPropertyName();
            Order order = ascending ? Order.asc(sortColumnName) :
Order.desc(sortColumnName);
            criteria.addOrder(order);
        }
        if(_resources != null) _resources.triggerEvent("sortColumn",
new Object[]{criteria,sortColumnName, ascending,
persistentClass},null);

        data = criteria.list();
    }
}










On Feb 6, 2008 8:40 AM, Howard Lewis Ship <[EMAIL PROTECTED]> wrote:
> On my list, I think I even added an Issue that outlines the need for a
> full guide to creating your own components.
>
>
> On Feb 5, 2008 11:17 PM, Davor Hrg <[EMAIL PROTECTED]> wrote:
> > It's getting better and better :)
> >
> >
> > looking forward to the tutorial on how to do it for own components :)
> >
> >
> > Davor Hrg
> >
> >
> > On Feb 6, 2008 7:35 AM, Adam Ayres <[EMAIL PROTECTED]> wrote:
> > > I think the component reference guide is great!  In the past I would use
> > > the component reference guide and then would go looking for examples in
> > > the example sites, so for me it is nice that they are now combined.
> > >
> > > The component reference guide is something that we (the company I work
> > > for) would like to use internally for the components we have developed.
> > > However, we are not using maven to build, we use ant/ivy.  Is there any
> > > chance that the component reference guide will be broken away to not
> > > depend on maven?
> > >
> > > Thanks,
> > > Adam
> > >
> > >
> > > -----Original Message-----
> > > From: Howard Lewis Ship [mailto:[EMAIL PROTECTED]
> > > Sent: Tuesday, February 05, 2008 8:42 AM
> > > To: Tapestry users
> > > Subject: Improving Component Reference
> > >
> > > I've extended the component reference to allow images as part of the
> > > documentation ... so far, its all screen shots.
> > >
> > > http://tapestry.formos.com/nightly/tapestry5/tapestry-core/ref/index.htm
> > > l
> > >
> > > Feedback still encouraged!
> > >
> > > --
> > > Howard M. Lewis Ship
> > >
> > > Creator Apache Tapestry and Apache HiveMind
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > For additional commands, e-mail: [EMAIL PROTECTED]
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > For additional commands, e-mail: [EMAIL PROTECTED]
> > >
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
>
>
>
> --
> Howard M. Lewis Ship
>
> Creator Apache Tapestry and Apache HiveMind
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to