Skip to content

Instantly share code, notes, and snippets.

@xfyre
Created January 2, 2015 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xfyre/ecb36a9173aed6a37f14 to your computer and use it in GitHub Desktop.
Save xfyre/ecb36a9173aed6a37f14 to your computer and use it in GitHub Desktop.
package com.xdance.misc;
import java.io.*;
import java.util.List;
import org.apache.tapestry5.grid.GridDataSource;
import org.apache.tapestry5.grid.SortConstraint;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.*;
public class PagingHibernateGridDataSource implements GridDataSource {
private int startIndex;
private List<?> preparedResults;
private Class<?> entityType;
private Session session;
private byte[] storedCriteria;
private Order defaultOrder;
public PagingHibernateGridDataSource ( Class<?> entityType, DetachedCriteria criteria, Session session, Order defaultOder ) {
try {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream ();
ObjectOutputStream objectStream = new ObjectOutputStream ( byteStream );
objectStream.writeObject ( criteria );
this.storedCriteria = byteStream.toByteArray ();
this.entityType = entityType;
this.session = session;
this.defaultOrder = defaultOder;
} catch ( IOException e ) {
throw new RuntimeException ( e );
}
}
private DetachedCriteria getCriteria () {
try {
ObjectInputStream objectStream = new ObjectInputStream ( new ByteArrayInputStream ( storedCriteria ) );
return (DetachedCriteria) objectStream.readObject ();
} catch ( IOException e ) {
throw new RuntimeException ( e );
} catch ( ClassNotFoundException e ) {
throw new RuntimeException ( e );
}
}
@Override
public int getAvailableRows () {
Criteria criteria = getCriteria ().getExecutableCriteria ( session ).setProjection ( Projections.rowCount () );
Number count = (Number) criteria.uniqueResult ();
return count.intValue ();
}
@Override
public void prepare ( int startIndex, int endIndex, List<SortConstraint> sortConstraints ) {
Criteria criteria = getCriteria ().getExecutableCriteria ( session );
criteria.setFirstResult ( startIndex ).setMaxResults ( endIndex - startIndex + 1 );
if ( defaultOrder != null && Tools.isEmpty ( sortConstraints ) )
criteria.addOrder ( defaultOrder );
else
for ( SortConstraint constraint : sortConstraints ) {
String propertyName = constraint.getPropertyModel ().getPropertyName ();
switch ( constraint.getColumnSort () ) {
case ASCENDING:
criteria.addOrder ( Order.asc ( propertyName ) );
break;
case DESCENDING:
criteria.addOrder ( Order.desc ( propertyName ) );
break;
default:
break;
}
}
this.startIndex = startIndex;
preparedResults = criteria.list ();
}
@Override
public Object getRowValue ( int index ) {
return entityType.cast ( preparedResults.get ( index - startIndex ) );
}
@Override
public Class<?> getRowType () {
return entityType;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment