KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > pagination > Paginator


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
// Paginator
14
//
15
//
16

17 package org.jahia.utils.pagination;
18
19 import java.util.List JavaDoc;
20
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23 import javax.servlet.http.HttpSession JavaDoc;
24
25 import org.jahia.utils.JahiaConsole;
26
27 /**
28  * Paginates a list for partial display (i.e. showing only a window of the
29  * list elements at a time).
30  *
31  * @author MJ
32  */

33 public class Paginator
34 {
35
36     /**
37      * The context key used to add the total numbner of items in the list.
38      */

39     public static final String JavaDoc TOTAL_ITEMS = "totalItems";
40
41     /**
42      * The context key used to retrieve the current page number.
43      */

44     public static final String JavaDoc PAGE_KEY = "paginated_page";
45
46     /**
47      * The context key used to add the previous page number.
48      */

49     public static final String JavaDoc PREV_PAGE = "prevPage";
50
51     /**
52      * The context key used to add the current page number.
53      */

54     public static final String JavaDoc CUR_PAGE = "currentPage";
55
56     /**
57      * The context key used to add the next page number.
58      */

59     public static final String JavaDoc NEXT_PAGE = "nextPage";
60
61     /**
62      * The context key used to add the list of page numbers.
63      */

64     public static final String JavaDoc PAGES = "pages";
65
66     /**
67      * Handles paging of the list in cases where the list represents all
68      * items avaliable.
69      */

70     public static List JavaDoc paginate (List JavaDoc items,
71                                  int nbrItemsPerPage,
72                                  HttpServletRequest JavaDoc request,
73                                  HttpServletResponse JavaDoc response,
74                                  HttpSession JavaDoc session )
75     {
76         return paginate(items, items.size(), nbrItemsPerPage, request, response, session);
77     }
78
79     /**
80      * Handles paging of the list in cases where there may be more items
81      * total than are in the list (eg. if some items were filtered out
82      * due to a query).
83      */

84     public static List JavaDoc paginate ( List JavaDoc items,
85                                   int totalNbrItems,
86                                   int nbrItemsPerPage,
87                                   HttpServletRequest JavaDoc request,
88                                   HttpServletResponse JavaDoc response,
89                                   HttpSession JavaDoc session )
90     {
91         session.setAttribute(TOTAL_ITEMS, new Integer JavaDoc(totalNbrItems));
92         JahiaConsole.println("Paginator", "totalNbrItems=" + totalNbrItems);
93
94         if (totalNbrItems > 0) {
95             int nbrPages = getNbrPages(nbrItemsPerPage, totalNbrItems);
96             JahiaConsole.println("Paginator", "nbrPages=" + nbrPages);
97
98             if (nbrPages > 1) {
99                 int curPageNbr = getCurrentPageNbr(request, response, session, nbrPages);
100                 session.setAttribute(CUR_PAGE, new Integer JavaDoc(curPageNbr));
101
102                 // Create the page links.
103
Integer JavaDoc[] pages = new Integer JavaDoc[nbrPages];
104                 for (int i = 0; i < nbrPages; i++) {
105                     pages[i] = new Integer JavaDoc(i + 1);
106                 }
107                 session.setAttribute(PAGES, pages);
108                 session.setAttribute(PREV_PAGE,
109                             (curPageNbr > 1 ?
110                              new Integer JavaDoc(curPageNbr - 1) : null));
111                 session.setAttribute(NEXT_PAGE,
112                             (curPageNbr < nbrPages ?
113                              new Integer JavaDoc(curPageNbr + 1) : null));
114
115                 // Get the subset of items to display.
116
items = getPage(items, curPageNbr, nbrItemsPerPage);
117             }
118         }
119         else
120         {
121             items = null;
122         }
123
124         return items;
125     }
126
127     /**
128      * Returns a valid page number for the page to display.
129      *
130      * @param session The session to pull the page number from.
131      * @param nbrPages The highest possible page number.
132      */

133     private static final int getCurrentPageNbr ( HttpServletRequest JavaDoc request,
134                                                  HttpServletResponse JavaDoc response,
135                                                  HttpSession JavaDoc session,
136                                                  int nbrPages )
137     {
138         String JavaDoc page = request.getParameter(PAGE_KEY);
139         int pgNbr = (page != null ? Integer.parseInt(page) : 1);
140         if (pgNbr < 1) {
141             pgNbr = 1;
142         } else if (pgNbr > nbrPages) {
143             pgNbr = nbrPages;
144         }
145         return pgNbr;
146     }
147
148     /**
149      * Calculates the number of pages based on the number of items.
150      *
151      * @param nbrItemsPerPage The number of items to display per page.
152      * @param nbrItems The total number of items.
153      * @return The number of pages to display items on.
154      */

155     protected static final int getNbrPages (int nbrItemsPerPage, int nbrItems)
156     {
157         // The cast is to assure the division returns a float.
158
return (int)Math.ceil((float)nbrItems / nbrItemsPerPage);
159     }
160
161     /**
162      * Gets the sub list of items to display for the current page.
163      *
164      * @param items The complete list of items.
165      * @param pgNbr The current page number.
166      * @param nbrItemsPerPage The number of items to display per page.
167      */

168     protected static final List JavaDoc getPage (List JavaDoc items, int pgNbr,
169                                        int nbrItemsPerPage)
170     {
171         return items.subList
172             ((pgNbr - 1) * nbrItemsPerPage,
173              Math.min(pgNbr * nbrItemsPerPage, items.size()));
174     }
175
176     /**
177      * Quick and dirty function to take a type of thing being
178      * searched for and turning it into an SQL regular expression.
179      */

180     protected static String JavaDoc makeSQLRegex(String JavaDoc type, String JavaDoc searchFor)
181     {
182         if (type == null) type = "Contains";
183         if (type.equals("Contains") || type.equals("StartsWith"))
184             searchFor = searchFor + "%";
185         if (type.equals("Contains") || type.equals("EndsWith"))
186             searchFor = "%" + searchFor;
187         return searchFor;
188     }
189 }
190
Popular Tags