Friday, November 17, 2006

Sorting and Paging with EJB3

In this article we shall use NetBeans Visual Web Pack 5.5 to create and deploy an ejb3 application along with a web application. The application has two parts. A) An Enterprise application using ejb3 B) A Web application which displays a data grid with sorting and paging. Netbeans has wizards which can manage sorting and paging data bound to a database table. Here we shall see how to achive this features using ejb.
At the end you build a travel web application as shown in the figure

Contents
Prerequisites
Creating Travel EJB application
Creating Entity Classes from Database
Writing StateLess Session Bean
Creating Travel Web Application

Creating DataProvider
Adding Table Component
Binding DataProvider
Creating EjbTableSorter
Binding EjbTableSorter
Adding Paging Buttons


Prerequisites

Before you use this tutorial, you must have the NetBeans 5.5 IDE and Visual Web Pack 5.5 installed on your system. Familiarize yourself with the basic parts of the IDE and read the Getting Started With NetBeans Visual Web Pack 5.5 for an introduction to the Visual Web Pack development environment. All steps in this tutorial are based on the default Visual Web Pack project, which uses the JSF 1.2 and Java EE 5 technologies.

Note: To access the TRAVEL database used in this tutorial, you must configure the IDE to use Sun Java System Application Server 9.0 (also known as GlassFish v1 UR1). For information about installing and configuring the Visual Web Pack and the Sun Java System Application Server, see the NetBeans Visual Web Pack 5.5 Installation Instructions.


Creating Travel EJB application
  1. Choose File > New Project (Ctrl-Shift-N) from the main menu.
  2. Select Enterprise Application from the Enterprise category and click Next.
  3. Name the project NewsApp and set the server to Sun Java System Application Server.
  4. Set the J2EE Version to Java EE 5, and select Create EJB Module and Create Web Application Module, if unselected.
  5. Project Name = Travel
  6. Select a Project Location
  7. Uncheck create web-application module and Create Client module
  8. Click Finish.
Creating Entity Classes from Database

  1. Right click Travel-ejb project
  2. Select New Entity Classes form Database
  3. Create a new DataSource
  4. Add a datasource
  5. Select Person Database Table (next)
  6. Click Create Persistance Unit
  7. Click Create
  8. Click Finish



































Writing StateLess Session Bean

  1. Right Click Travel-Ejb Project Select New Session Bean
  2. Type EJB Name : Person, Package com.travel.service
  3. Check Remote and Local
  4. Add Two Libraries to Travel-ejb . ToplinkEssentials is already available in netbeans. Dataprovider.jar is in following path C:/Sunnetbeans-5.5/rave2.0/modules/ext
  5. Open PersonBean.java. Right Click anywhere in
  6. the code . Add Business Method . Add following methods
  7. getPersons,getcount
  8. Build Travel

Here is the code of PersonBean.java
/* Returns sorted list of persons, Nomber of objects returned can be controlled by specifying
maxresukts and startposition */
public List getPersons(int startPosition, int maxResult, SortCriteria[] sc) {
String query = "select object(o) from Person as o";
if(sc != null){
query = query + " " + "order by " + " " + "o."+sc[0].getCriteriaKey();
if(!sc[0].isAscending()){
query = query + " desc";
}
}

Query q = em.createQuery(query);
q.setMaxResults(maxResult);
q.setFirstResult(startPosition);
return q.getResultList();
}
/* Count */
public int getCount() {
Query q = em.createQuery("select count(o) from Person as o ");
Long count = (Long)q.getSingleResult();
if(count != null){
return count.intValue();
}
return 0;
}


Creating Travel Web Application
  1. Create a new Visual Web application
  2. Add Travel-ejb in the Libraries of TravelWeb project

Creating DataProvider

  1. Create PersonDP.java
  2. Right Click any where in the code select Enterprise Resources | call Enterprise Bean
  3. Select Person Bean
  4. Add following methods to PersonDP refreshDP,getRowCount,next,previous
  5. Add two properties startPosition and maxresults


/*
* PersonDP.java
* Created on November 16, 2006, 6:10 PM
*/
package dataproviders;

public class PersonDP extends ObjectListDataProvider{
/** Creates a new instance of PersonDP */
public PersonDP() {
/* needed by netbeans editor */

setObjectType(Person.class);
}
/* fetch a fresh list of persons from ejb layer*/
public void refreshDP(){
clearObjectList();
setList(lookupPersonBean().getPersons(getStartPosition(),getMaxresults(),null));
}
/* Fetch sorted list of persons from ejb layer*/
public void refreshDP(SortCriteria[] sc){
clearObjectList();

setList(lookupPersonBean().getPersons(getStartPosition(),getMaxresults(),sc));
}
/* find count */
public int getRowCount() throws DataProviderException {
if(Beans.isDesignTime()){

return 5;
}

return lookupPersonBean().getCount();}
/* controls page size*/
private int maxresults =5;
/* starting row*/
private int startPosition;
public int getMaxresults() {
return maxresults;
}
public void setMaxresults(int maxresults) {
this.maxresults = maxresults;
}
public int getStartPosition() {
return startPosition;

}
public void setStartPosition(int startPosition) {
this.startPosition = startPosition;

refreshDP();
}
public void setLastPosition() {

startPosition = (lookupPersonBean().getCount()/maxresults) * maxresults;
refreshDP();
}
/*move to next page*/
public void next(){
if(startPosition + maxresults <= lookupPersonBean().getCount()){
startPosition = startPosition + maxresults;
}
refreshDP();
}
public void previous(){
if(startPosition < lookupPersonBean().getCount()){
startPosition = startPosition - maxresults;
}
if (startPosition < 0){
startPosition =0;
}
refreshDP();
}
/* Helper for calling ejb service . Created by netbeans*/
private PersonRemote lookupPersonBean() {
try {
Context c = new InitialContext();
return (PersonRemote) c.lookup("java:comp/env/ejb/PersonBean");
}
catch(NamingException ne) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught" ,ne);
throw new RuntimeException(ne);
}
}

Adding Table Component

  1. Open Page1 in TraveWeb project
  2. Drag and drop a Table from Pallet window'
  3. Add a Property to SessionBean1
  4. Name of the property is persondp type PersonDP
  5. Inittialize private PersonDP persondp = new PersonDP();
  6. Clean Build TravelWeb
  7. Clean Build Travel-ejb (dont touch travle project)
  8. Restart Netbeans
  9. Open Page 1
  10. Right Click table component on the page
  11. Select Table Layout

Select persondp in getDataFrom dialog

Set Table Layout Options

Creating EjbTableSorter

  1. Create EjbTableSorter.java
  2. Create a Property ejbTableSorter in SessionBean1
  3. Initialize ejbTableSorter

/*
* EjbTableSorter.java
* Created on November 16, 2006, 6:42 PM

*/
package dataproviders;

import com.sun.data.provider.DataProviderException;
import com.sun.data.provider.RowKey;
import com.sun.data.provider.SortCriteria;
import com.sun.data.provider.TableDataProvider;
import com.sun.data.provider.TableDataSorter;
import java.beans.Beans;
import java.io.Serializable;

import java.util.Locale;

public class EjbTableSorter implements TableDataSorter , Serializable {
/** Creates a new instance of EjbTableSorter */
public EjbTableSorter() {
}

protected SortCriteria sortCriteria[];
protected Locale sortLocale;
/** Creates a new instance of EjbSorter */

public EjbTableSorter(SortCriteria sortCriteria[], Locale sortLoc
ale) {
this.sortCriteria = sortCriteria;
this.sortLocale = sortLocale;
}
public void setSortCriteria(SortCriteria[] sortCriteria) {
this.sortCriteria = sortCriteria;

}

public SortCriteria[] getSortCriteria() {
return sortCriteria;
}

public void setSortLocale(Locale locale) {
this.sortLocale= locale;

}

public Locale getSortLocale() {

return this.sortLocale;
}
/* returns sorted list of keys. Gets frersh sorted list of objects from ejb layer*/
public RowKey[] sort(TableDataProvider provider, RowKey[] rows) throws DataProviderException {
if(!Beans.isDesignTime()){

PersonDP persondp = (PersonDP)provider;
persondp.refreshDP(sortCriteria);
System.err.println(" Executed sort EjbSorter.java");
return persondp.getRowKeys(rows.length,null);

}
return provider.getRowKeys(rows.length,null);
}
}


Binding EjbTableSorter
  1. Open Page1 in Visual editor
  2. Select TableRowGroup1 in the Outline window
  3. Rigth Click TableRowGroup1 | Property Bindings
  4. Select all radio button in Property Bindings
  5. Select TableDataSorter in left pane
  6. Select SessionBean1 | ejbSorter in right pane
  7. close
    Adding Paging Buttons
  8. Open Page1 in visual editor
  9. Drag and drop GropPanel from pallet windows
  10. Drag and drop Button from pallet window on to the GroupPanel 4 of them
  11. Change the text property of each button using property editor
  12. Double click on the <<| Button
  13. Add following line getSessionBean1().getPersondp().setStartPosition(0);
  14. Add following code in each button
  15. getSessionBean1().getPersondp().previous(); getSessionBean1().getPersondp().next(); getSessionBean1().getPersondp().setLastPosition();
  16. Deploy Travel Project
  17. Run TravelWeb
  18. Voila your your webrowser will look like the screen shot we started with . You can sort the table, navigate the pages. Also the sorting can be removed. Also sorting order is retained even while moving to another page.

2 comments:

JAJI said...

Hello,

I try to execute the example, there is a problem with the EJB remote call in glassfish. can you please post the source code this tutorial ? or explain me if there is a configuration do add in glassfish

kiran kumar said...

jaji I used Sun Application Server. Theoratically glass fish should not be a problem .There is no addtional configuration needed. Please post the error message you recieved.