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