Friday, April 18, 2008

Google Type Paging :

Need to change as per requirement and Use the loop at your JSP page with starting from startPage to endPage and if pageIndex will be your current Page


 

import java.util.List;

import javax.persistence.Query;


 

public class Page {

    private int startPage;

    private int endPage;

    private double count;

    private static int vellySize = 10;

    private List<?> results;

private int pageIndex;

    private int pageSize;

    private int lastPageIndex;


 

    public Page() {

    }


 

    public Page(Query selectQuery, Query countQuery, int index, int size) {

        if (index <= 0)

            index = 1;

        // If page index is not zero based

        pageIndex = index;

        // Make sure that the page size is within its limits

        pageSize = size;

        // Get the total number of items in the database.

        // If count was of type Long you would lose the precision when

        // calling Math.ceil() --> count is declared as double

        count = (Long) countQuery.getSingleResult();

        if (count <= 0) {

            // You need to handle this situation. One option is to throw an

            // exception or display an error message using FacesMessages.

            return;

        }


 

        // calculate the number of pages and set last page index

        lastPageIndex = (int) Math.ceil(count / pageSize);


 

        // make sure that the page index in not out of scope

        if (pageIndex > lastPageIndex)

            pageIndex = 1;

        results = selectQuery.setMaxResults(pageSize).setFirstResult(

                pageSize * (pageIndex - 1)).getResultList();

        setEndPage();

        setStartPage();

    }


 

    public int getStartPage() {

        return startPage;

    }

    public int getEndPage() {

        return endPage;

    }

    public void setStartPage() {

        int fst = (vellySize / 2);


 

        if (pageIndex - fst <= 0)

            this.startPage = 1;

        else

            startPage = pageIndex - fst;

    }

    public void setEndPage() {

        int lst = (vellySize / 2);


 

        if (pageIndex + lst > (int) Math.ceil(count / pageSize))

            endPage = (int) Math.ceil(count / pageSize);

        else

            endPage = pageIndex + lst;

    }


 

    public List getResults() {

        return results;

    }


 

    public int getPageIndex() {

        return pageIndex;

    }


 

    public int getLastPageIndex() {

        return lastPageIndex;

    }


 

    public int getPageSize() {

        return pageSize;

    }


 

    /**

     * This method used to set the page index after the delete operation. If the

     * current page having only single record than after the delete operation

     * the user will forwarded to previous page.

     *

     * i.e. if currentPage Index is 2 than user will forwarded to 1 page if and

     * only if the page having no records after delete operation. If pageIndex

     * less than 0 it will set to 1 by default.

     *

     */

public void setPageAfterDelete() {

        // Set the page Index by checking the result size. If it is Zero means

        // user deleted the last record than this will set to previous page.

        if (getResults().size() == 0)

            pageIndex--;

        // If pageIndex < 0 than need to set it to 1st page.

        if (pageIndex < 0)

            pageIndex = 1;

    }

}

No comments:

Contributors