1 17 package org.jahia.utils.pagination; 18 19 import java.util.List ; 20 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.http.HttpServletResponse ; 23 import javax.servlet.http.HttpSession ; 24 25 import org.jahia.utils.JahiaConsole; 26 27 33 public class Paginator 34 { 35 36 39 public static final String TOTAL_ITEMS = "totalItems"; 40 41 44 public static final String PAGE_KEY = "paginated_page"; 45 46 49 public static final String PREV_PAGE = "prevPage"; 50 51 54 public static final String CUR_PAGE = "currentPage"; 55 56 59 public static final String NEXT_PAGE = "nextPage"; 60 61 64 public static final String PAGES = "pages"; 65 66 70 public static List paginate (List items, 71 int nbrItemsPerPage, 72 HttpServletRequest request, 73 HttpServletResponse response, 74 HttpSession session ) 75 { 76 return paginate(items, items.size(), nbrItemsPerPage, request, response, session); 77 } 78 79 84 public static List paginate ( List items, 85 int totalNbrItems, 86 int nbrItemsPerPage, 87 HttpServletRequest request, 88 HttpServletResponse response, 89 HttpSession session ) 90 { 91 session.setAttribute(TOTAL_ITEMS, new Integer (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 (curPageNbr)); 101 102 Integer [] pages = new Integer [nbrPages]; 104 for (int i = 0; i < nbrPages; i++) { 105 pages[i] = new Integer (i + 1); 106 } 107 session.setAttribute(PAGES, pages); 108 session.setAttribute(PREV_PAGE, 109 (curPageNbr > 1 ? 110 new Integer (curPageNbr - 1) : null)); 111 session.setAttribute(NEXT_PAGE, 112 (curPageNbr < nbrPages ? 113 new Integer (curPageNbr + 1) : null)); 114 115 items = getPage(items, curPageNbr, nbrItemsPerPage); 117 } 118 } 119 else 120 { 121 items = null; 122 } 123 124 return items; 125 } 126 127 133 private static final int getCurrentPageNbr ( HttpServletRequest request, 134 HttpServletResponse response, 135 HttpSession session, 136 int nbrPages ) 137 { 138 String 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 155 protected static final int getNbrPages (int nbrItemsPerPage, int nbrItems) 156 { 157 return (int)Math.ceil((float)nbrItems / nbrItemsPerPage); 159 } 160 161 168 protected static final List getPage (List 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 180 protected static String makeSQLRegex(String type, String 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 |