View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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   * Abstract Paginator
45   * 
46   * @param <E>
47   *            the type
48   */
49  public abstract class AbstractPaginator<E> implements IPaginator<E>
50  {
51      /** Default value for Page Index Parameter */
52      public static final String PARAMETER_PAGE_INDEX = "page_index";
53  
54      /** Default value for Items Per Page Parameter */
55      public static final String PARAMETER_ITEMS_PER_PAGE = "items_per_page";
56      public static final String LABEL_FIRST = "|&lt;";
57      public static final String LABEL_PREVIOUS = "&lt;";
58      public static final String LABEL_NEXT = "&gt;";
59      public static final String LABEL_LAST = "&gt;|";
60      private static final long serialVersionUID = -2795417742672443863L;
61  
62      // Variables declarations
63      /** page index parameter name */
64      protected String _strPageIndexParameterName;
65  
66      /** items per page */
67      protected int _nItemPerPage;
68  
69      /** base url */
70      protected String _strBaseUrl;
71  
72      /** the actual list */
73      protected List<E> _list;
74  
75      /** the current page */
76      protected int _nPageCurrent;
77  
78      /** items per page parameter name */
79      protected String _strItemsPerPageParameterName;
80  
81      /** element count */
82      protected int _nItemsCount;
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public int getPageCurrent( )
89      {
90          return _nPageCurrent;
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      @Override
97      public String getFirstPageLink( )
98      {
99          UrlIteml/UrlItem.html#UrlItem">UrlItem url = new UrlItem( _strBaseUrl );
100         url.addParameter( _strPageIndexParameterName, "" + 1 );
101 
102         return url.getUrl( );
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public String getPreviousPageLink( )
110     {
111         int nPreviousIndex = _nPageCurrent - 1;
112         UrlIteml/UrlItem.html#UrlItem">UrlItem url = new UrlItem( _strBaseUrl );
113         url.addParameter( _strPageIndexParameterName, "" + nPreviousIndex );
114 
115         return url.getUrl( );
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     @Override
122     public String getLastPageLink( )
123     {
124         UrlIteml/UrlItem.html#UrlItem">UrlItem url = new UrlItem( _strBaseUrl );
125         url.addParameter( _strPageIndexParameterName, "" + getPagesCount( ) );
126 
127         return url.getUrl( );
128     }
129 
130     /**
131      *
132      * {@inheritDoc}
133      */
134     @Override
135     public int getItemsPerPage( )
136     {
137         return _nItemPerPage;
138     }
139 
140     /**
141      * {@inheritDoc}
142      */
143     @Override
144     public String getNextPageLink( )
145     {
146         int nNextIndex = _nPageCurrent + 1;
147         UrlIteml/UrlItem.html#UrlItem">UrlItem url = new UrlItem( _strBaseUrl );
148         url.addParameter( _strPageIndexParameterName, "" + nNextIndex );
149 
150         return url.getUrl( );
151     }
152 
153     /**
154      * {@inheritDoc}
155      */
156     @Override
157     public List<PaginatorPage> getPagesLinks( )
158     {
159         List<PaginatorPage> list = new ArrayList<>( );
160 
161         // Number of link pages in total
162         int nNbLinkPages = getPagesCount( );
163 
164         // Max number of link pages displayed
165         int nNbLinkPagesToDisplay = 10;
166 
167         // Number of previous link pages to display
168         int nOffsetPrev = nNbLinkPagesToDisplay / 2;
169 
170         // Number of next link pages to display
171         int nOffsetNext = nNbLinkPagesToDisplay / 2;
172 
173         // Set the offsets
174         if ( _nPageCurrent <= ( nNbLinkPagesToDisplay - nOffsetPrev ) )
175         {
176             // If the current page is within the first 5 pages
177             nOffsetPrev = _nPageCurrent - 1;
178             nOffsetNext = nNbLinkPagesToDisplay - nOffsetPrev;
179         }
180         else
181             if ( ( _nPageCurrent + nOffsetNext ) > nNbLinkPages )
182             {
183                 // If the current page is within the last 5 pages
184                 nOffsetNext = nNbLinkPages - _nPageCurrent;
185                 nOffsetPrev = nNbLinkPagesToDisplay - nOffsetNext;
186             }
187 
188         // Index of the last number of the link page to display
189         int nMax = nNbLinkPages;
190 
191         // Index of the first number of the link page to display
192         int nMin = 1;
193 
194         int nTmpMin = _nPageCurrent - nOffsetPrev;
195         int nTmpMax = _nPageCurrent + nOffsetNext;
196 
197         if ( nTmpMax < nMax )
198         {
199             nMax = nTmpMax;
200         }
201 
202         if ( nTmpMin > 0 )
203         {
204             nMin = nTmpMin;
205         }
206 
207         for ( int i = nMin; i <= nMax; i++ )
208         {
209             PaginatorPagenatorPage.html#PaginatorPage">PaginatorPage page = new PaginatorPage( );
210             String strIndex = "" + i;
211             UrlIteml/UrlItem.html#UrlItem">UrlItem url = new UrlItem( _strBaseUrl );
212             url.addParameter( _strPageIndexParameterName, strIndex );
213             page.setUrl( url.getUrl( ) );
214             page.setName( strIndex );
215             page.setIndex( i );
216             list.add( page );
217         }
218 
219         return list;
220     }
221 
222     /**
223      * {@inheritDoc}
224      */
225     @Override
226     public String getLabelFirst( )
227     {
228         return LABEL_FIRST;
229     }
230 
231     /**
232      * {@inheritDoc}
233      */
234     @Override
235     public String getLabelPrevious( )
236     {
237         return LABEL_PREVIOUS;
238     }
239 
240     /**
241      * {@inheritDoc}
242      */
243     @Override
244     public String getLabelNext( )
245     {
246         return LABEL_NEXT;
247     }
248 
249     /**
250      * {@inheritDoc}
251      */
252     @Override
253     public String getLabelLast( )
254     {
255         return LABEL_LAST;
256     }
257 
258     /**
259      * {@inheritDoc}
260      */
261     @Override
262     public String getLabelItemCount( )
263     {
264         return "";
265     }
266 
267     /**
268      * {@inheritDoc}
269      */
270     @Override
271     public String getLabelItemCountPerPage( )
272     {
273         return "";
274     }
275 
276     /**
277      * {@inheritDoc}
278      */
279     @Override
280     public String getItemsPerPageParameterName( )
281     {
282         return _strItemsPerPageParameterName;
283     }
284 
285     /**
286      * {@inheritDoc}
287      */
288     @Override
289     public void setItemsPerPageParameterName( String strItemsPerPageParameterName )
290     {
291         _strItemsPerPageParameterName = strItemsPerPageParameterName;
292     }
293 
294     /**
295      * {@inheritDoc}
296      */
297     @Override
298     public int getPagesCount( )
299     {
300         return ( ( _nItemsCount - 1 ) / _nItemPerPage ) + 1;
301     }
302 
303     /**
304      * {@inheritDoc}
305      */
306     @Override
307     public int getRangeMin( )
308     {
309         return ( !_list.isEmpty( ) ) ? ( ( _nItemPerPage * ( _nPageCurrent - 1 ) ) + 1 ) : 0;
310     }
311 
312     /**
313      * {@inheritDoc}
314      */
315     @Override
316     public int getRangeMax( )
317     {
318         return ( _nItemsCount < ( ( _nItemPerPage * _nPageCurrent ) - 1 ) ) ? _nItemsCount : ( _nItemPerPage * _nPageCurrent );
319     }
320 
321     /**
322      * {@inheritDoc}
323      */
324     @Override
325     public int getItemsCount( )
326     {
327         return _nItemsCount;
328     }
329 
330     /**
331      * Gets the number of items per page from a request parameter
332      * 
333      * @param request
334      *            The HTTP request
335      * @param strParameter
336      *            The request parameter name
337      * @param nCurrent
338      *            The current number of items
339      * @param nDefault
340      *            The default number of items
341      * @return The new number of items
342      */
343     public static int getItemsPerPage( HttpServletRequest request, String strParameter, int nCurrent, int nDefault )
344     {
345         int nItemsPerPage;
346         String strItemsPerPage = request.getParameter( strParameter );
347 
348         if ( strItemsPerPage != null )
349         {
350             nItemsPerPage = Integer.parseInt( strItemsPerPage );
351         }
352         else
353         {
354             if ( nCurrent != 0 )
355             {
356                 nItemsPerPage = nCurrent;
357             }
358             else
359             {
360                 nItemsPerPage = nDefault;
361             }
362         }
363 
364         return nItemsPerPage;
365     }
366 
367     /**
368      * Gets the new page index from a request parameter
369      * 
370      * @param request
371      *            The HTTP request
372      * @param strParameter
373      *            The request parameter name
374      * @param strCurrentPageIndex
375      *            The current page index
376      * @return The new page index
377      */
378     public static String getPageIndex( HttpServletRequest request, String strParameter, String strCurrentPageIndex )
379     {
380         String strPageIndex = request.getParameter( strParameter );
381         strPageIndex = ( strPageIndex != null ) ? strPageIndex : strCurrentPageIndex;
382 
383         return strPageIndex;
384     }
385 }