Collections of web application techniques

Monday, May 18, 2009

Custom Pagination With JBoss Seam

Seam has its own pagination and you can get paginations for practically free. All you need to do is to extend your component from org.jboss.seam.framework.EntityQuery. In fact if you use seam-gen to generate the UI, you'll get all the search form, sorting and paging without knowing much. But this is not much fun for a software developer trapped in a cubicle, so I had to create something.

My custom pagination described here has these additional features:
- Selectable page sizes
- Total counts are displayed
- Navigations are not limited to just first, previous, next and last result sets. Mine shows the current and a fixed number of pages before or after the current similarly to how Google does it.

Below is an example of the output:



import com.inventasoft.ui.Pagination;
import com.inventasoft.ui.PageControls;
import com.inventasoft.ui.QueryHelper;

public class YourBean implements QueryHelper {
private Pagination pagination;
private PageControls pageControls = new PageControls();

public void setUp() {
pagination = new Pagination
}

public List anActionListener() {
pagination.execute(pageControls.getPage(), pageControls.getPageSize());
return pagination.getResults();
}

public Query getCountQuery() {
Object o2 = Component.getInstance("dataEM");
EntityManager em = (EntityManager) o2;

queryEntity.setEjbql("from AResultClass");
queryEntity.setRestrictions(Arrays.asList(RESTRICTIONS));
queryEntity.setOrder(selection.getOrder());

String sqlStr = "select count(*) " + queryEntity.getCountQuery();
Query query = em.createQuery(sqlStr);
return query;
}

public Query getSelectQuery(int start, int pageSize) {
Object o2 = Component.getInstance("dataEM");
EntityManager em = (EntityManager) o2;

String selectQuery = queryEntity.getQuery();
Query query = em.createQuery(selectQuery);
query.setFirstResult(start);
query.setMaxResults(pageSize);
return query;
}
}

3 comments:

  1. Very good. Interested to see the entire utility if you dont mind ?

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. See my September's blog for a simpler way to do this.

    ReplyDelete