View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.util.html;
35  
36  import fr.paris.lutece.util.url.UrlItem;
37  
38  import java.util.ArrayList;
39  import java.util.List;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  
44  /**
45   * Abstract Paginator
46   * @param <E> the type
47   */
48  public abstract class AbstractPaginator<E> implements IPaginator<E>
49  {
50      /** Default value for Page Index Parameter */
51      public static final String PARAMETER_PAGE_INDEX = "page_index";
52  
53      /** Default value for Items Per Page Parameter */
54      public static final String PARAMETER_ITEMS_PER_PAGE = "items_per_page";
55      public static final String LABEL_FIRST = "|&lt;";
56      public static final String LABEL_PREVIOUS = "&lt;";
57      public static final String LABEL_NEXT = "&gt;";
58      public static final String LABEL_LAST = "&gt;|";
59      private static final long serialVersionUID = -2795417742672443863L;
60  
61      // Variables declarations
62      /** page index parameter name */
63      protected String _strPageIndexParameterName;
64  
65      /** items per page */
66      protected int _nItemPerPage;
67  
68      /** base url */
69      protected String _strBaseUrl;
70  
71      /** the actual list */
72      protected List<E> _list;
73  
74      /** the current page */
75      protected int _nPageCurrent;
76  
77      /** items per page parameter name */
78      protected String _strItemsPerPageParameterName;
79  
80      /** element count */
81      protected int _nItemsCount;
82  
83      /**
84       *{@inheritDoc}
85       */
86      @Override
87      public int getPageCurrent(  )
88      {
89          return _nPageCurrent;
90      }
91  
92      /**
93       *{@inheritDoc}
94       */
95      @Override
96      public String getFirstPageLink(  )
97      {
98          UrlItem url = new UrlItem( _strBaseUrl );
99          url.addParameter( _strPageIndexParameterName, "" + 1 );
100 
101         return url.getUrl(  );
102     }
103 
104     /**
105      *{@inheritDoc}
106      */
107     @Override
108     public String getPreviousPageLink(  )
109     {
110         int nPreviousIndex = _nPageCurrent - 1;
111         UrlItem url = new UrlItem( _strBaseUrl );
112         url.addParameter( _strPageIndexParameterName, "" + nPreviousIndex );
113 
114         return url.getUrl(  );
115     }
116 
117     /**
118      *{@inheritDoc}
119      */
120     @Override
121     public String getLastPageLink(  )
122     {
123         UrlItem url = new UrlItem( _strBaseUrl );
124         url.addParameter( _strPageIndexParameterName, "" + getPagesCount(  ) );
125 
126         return url.getUrl(  );
127     }
128 
129     /**
130      *
131      * {@inheritDoc}
132      */
133     @Override
134     public int getItemsPerPage(  )
135     {
136         return _nItemPerPage;
137     }
138 
139     /**
140      *{@inheritDoc}
141      */
142     @Override
143     public String getNextPageLink(  )
144     {
145         int nNextIndex = _nPageCurrent + 1;
146         UrlItem url = new UrlItem( _strBaseUrl );
147         url.addParameter( _strPageIndexParameterName, "" + nNextIndex );
148 
149         return url.getUrl(  );
150     }
151 
152     /**
153      *{@inheritDoc}
154      */
155     @Override
156     public List<PaginatorPage> getPagesLinks(  )
157     {
158         List<PaginatorPage> list = new ArrayList<PaginatorPage>(  );
159 
160         // Number of link pages in total
161         int nNbLinkPages = getPagesCount(  );
162 
163         // Max number of link pages displayed
164         int nNbLinkPagesToDisplay = 10;
165 
166         // Number of previous link pages to display
167         int nOffsetPrev = nNbLinkPagesToDisplay / 2;
168 
169         // Number of next link pages to display
170         int nOffsetNext = nNbLinkPagesToDisplay / 2;
171 
172         // Set the offsets
173         if ( _nPageCurrent <= ( nNbLinkPagesToDisplay - nOffsetPrev ) )
174         {
175             // If the current page is within the first 5 pages
176             nOffsetPrev = _nPageCurrent - 1;
177             nOffsetNext = nNbLinkPagesToDisplay - nOffsetPrev;
178         }
179         else if ( ( _nPageCurrent + nOffsetNext ) > nNbLinkPages )
180         {
181             // If the current page is within the last 5 pages
182             nOffsetNext = nNbLinkPages - _nPageCurrent;
183             nOffsetPrev = nNbLinkPagesToDisplay - nOffsetNext;
184         }
185 
186         // Index of the last number of the link page to display
187         int nMax = nNbLinkPages;
188 
189         // Index of the first number of the link page to display
190         int nMin = 1;
191 
192         int nTmpMin = _nPageCurrent - nOffsetPrev;
193         int nTmpMax = _nPageCurrent + nOffsetNext;
194 
195         if ( nTmpMax < nMax )
196         {
197             nMax = nTmpMax;
198         }
199 
200         if ( nTmpMin > 0 )
201         {
202             nMin = nTmpMin;
203         }
204 
205         for ( int i = nMin; i <= nMax; i++ )
206         {
207             PaginatorPage page = new PaginatorPage(  );
208             String strIndex = "" + i;
209             UrlItem url = new UrlItem( _strBaseUrl );
210             url.addParameter( _strPageIndexParameterName, strIndex );
211             page.setUrl( url.getUrl(  ) );
212             page.setName( strIndex );
213             page.setIndex( i );
214             list.add( page );
215         }
216 
217         return list;
218     }
219 
220     /**
221      *{@inheritDoc}
222      */
223     @Override
224     public String getLabelFirst(  )
225     {
226         return LABEL_FIRST;
227     }
228 
229     /**
230      *{@inheritDoc}
231      */
232     @Override
233     public String getLabelPrevious(  )
234     {
235         return LABEL_PREVIOUS;
236     }
237 
238     /**
239      *{@inheritDoc}
240      */
241     @Override
242     public String getLabelNext(  )
243     {
244         return LABEL_NEXT;
245     }
246 
247     /**
248      *{@inheritDoc}
249      */
250     @Override
251     public String getLabelLast(  )
252     {
253         return LABEL_LAST;
254     }
255 
256     /**
257      *{@inheritDoc}
258      */
259     @Override
260     public String getLabelItemCount(  )
261     {
262         return "";
263     }
264 
265     /**
266      *{@inheritDoc}
267      */
268     @Override
269     public String getLabelItemCountPerPage(  )
270     {
271         return "";
272     }
273 
274     /**
275      *{@inheritDoc}
276      */
277     @Override
278     public String getItemsPerPageParameterName(  )
279     {
280         return _strItemsPerPageParameterName;
281     }
282 
283     /**
284      *{@inheritDoc}
285      */
286     @Override
287     public void setItemsPerPageParameterName( String strItemsPerPageParameterName )
288     {
289         _strItemsPerPageParameterName = strItemsPerPageParameterName;
290     }
291 
292     /**
293      *{@inheritDoc}
294      */
295     @Override
296     public int getPagesCount(  )
297     {
298         return ( ( _nItemsCount - 1 ) / _nItemPerPage ) + 1;
299     }
300 
301     /**
302      *{@inheritDoc}
303      */
304     @Override
305     public int getRangeMin(  )
306     {
307         return ( !_list.isEmpty(  ) ) ? ( ( _nItemPerPage * ( _nPageCurrent - 1 ) ) + 1 ) : 0;
308     }
309 
310     /**
311      *{@inheritDoc}
312      */
313     @Override
314     public int getRangeMax(  )
315     {
316         return ( _nItemsCount < ( ( _nItemPerPage * _nPageCurrent ) - 1 ) ) ? _nItemsCount : ( _nItemPerPage * _nPageCurrent );
317     }
318 
319     /**
320      *{@inheritDoc}
321      */
322     @Override
323     public int getItemsCount(  )
324     {
325         return _nItemsCount;
326     }
327 
328     /**
329      * Gets the number of items per page from a request parameter
330      * @param request The HTTP request
331      * @param strParameter The request parameter name
332      * @param nCurrent The current number of items
333      * @param nDefault The default number of items
334      * @return The new number of items
335      */
336     public static int getItemsPerPage( HttpServletRequest request, String strParameter, int nCurrent, int nDefault )
337     {
338         int nItemsPerPage;
339         String strItemsPerPage = request.getParameter( strParameter );
340 
341         if ( strItemsPerPage != null )
342         {
343             nItemsPerPage = Integer.parseInt( strItemsPerPage );
344         }
345         else
346         {
347             if ( nCurrent != 0 )
348             {
349                 nItemsPerPage = nCurrent;
350             }
351             else
352             {
353                 nItemsPerPage = nDefault;
354             }
355         }
356 
357         return nItemsPerPage;
358     }
359 
360     /**
361      * Gets the new page index from a request parameter
362      * @param request The HTTP request
363      * @param strParameter The request parameter name
364      * @param strCurrentPageIndex The current page index
365      * @return The new page index
366      */
367     public static String getPageIndex( HttpServletRequest request, String strParameter, String strCurrentPageIndex )
368     {
369         String strPageIndex = request.getParameter( strParameter );
370         strPageIndex = ( strPageIndex != null ) ? strPageIndex : strCurrentPageIndex;
371 
372         return strPageIndex;
373     }
374 }