AbstractPaginator.java

  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. import fr.paris.lutece.util.url.UrlItem;

  36. import java.util.ArrayList;
  37. import java.util.List;

  38. import javax.servlet.http.HttpServletRequest;

  39. /**
  40.  * Abstract Paginator
  41.  *
  42.  * @param <E>
  43.  *            the type
  44.  */
  45. public abstract class AbstractPaginator<E> implements IPaginator<E>
  46. {
  47.     /** Default value for Page Index Parameter */
  48.     public static final String PARAMETER_PAGE_INDEX = "page_index";

  49.     /** Default value for Items Per Page Parameter */
  50.     public static final String PARAMETER_ITEMS_PER_PAGE = "items_per_page";
  51.     public static final String LABEL_FIRST = "|&lt;";
  52.     public static final String LABEL_PREVIOUS = "&lt;";
  53.     public static final String LABEL_NEXT = "&gt;";
  54.     public static final String LABEL_LAST = "&gt;|";
  55.     private static final long serialVersionUID = -2795417742672443863L;

  56.     // Variables declarations
  57.     /** page index parameter name */
  58.     protected String _strPageIndexParameterName;

  59.     /** items per page */
  60.     protected int _nItemPerPage;

  61.     /** base url */
  62.     protected String _strBaseUrl;

  63.     /** the actual list */
  64.     protected List<E> _list;

  65.     /** the current page */
  66.     protected int _nPageCurrent;

  67.     /** items per page parameter name */
  68.     protected String _strItemsPerPageParameterName;

  69.     /** element count */
  70.     protected int _nItemsCount;

  71.     /**
  72.      * {@inheritDoc}
  73.      */
  74.     @Override
  75.     public int getPageCurrent( )
  76.     {
  77.         return _nPageCurrent;
  78.     }

  79.     /**
  80.      * {@inheritDoc}
  81.      */
  82.     @Override
  83.     public String getFirstPageLink( )
  84.     {
  85.         UrlItem url = new UrlItem( _strBaseUrl );
  86.         url.addParameter( _strPageIndexParameterName, "" + 1 );

  87.         return url.getUrl( );
  88.     }

  89.     /**
  90.      * {@inheritDoc}
  91.      */
  92.     @Override
  93.     public String getPreviousPageLink( )
  94.     {
  95.         int nPreviousIndex = _nPageCurrent - 1;
  96.         UrlItem url = new UrlItem( _strBaseUrl );
  97.         url.addParameter( _strPageIndexParameterName, "" + nPreviousIndex );

  98.         return url.getUrl( );
  99.     }

  100.     /**
  101.      * {@inheritDoc}
  102.      */
  103.     @Override
  104.     public String getLastPageLink( )
  105.     {
  106.         UrlItem url = new UrlItem( _strBaseUrl );
  107.         url.addParameter( _strPageIndexParameterName, "" + getPagesCount( ) );

  108.         return url.getUrl( );
  109.     }

  110.     /**
  111.      *
  112.      * {@inheritDoc}
  113.      */
  114.     @Override
  115.     public int getItemsPerPage( )
  116.     {
  117.         return _nItemPerPage;
  118.     }

  119.     /**
  120.      * {@inheritDoc}
  121.      */
  122.     @Override
  123.     public String getNextPageLink( )
  124.     {
  125.         int nNextIndex = _nPageCurrent + 1;
  126.         UrlItem url = new UrlItem( _strBaseUrl );
  127.         url.addParameter( _strPageIndexParameterName, "" + nNextIndex );

  128.         return url.getUrl( );
  129.     }

  130.     /**
  131.      * {@inheritDoc}
  132.      */
  133.     @Override
  134.     public List<PaginatorPage> getPagesLinks( )
  135.     {
  136.         List<PaginatorPage> list = new ArrayList<>( );

  137.         // Number of link pages in total
  138.         int nNbLinkPages = getPagesCount( );

  139.         // Max number of link pages displayed
  140.         int nNbLinkPagesToDisplay = 10;

  141.         // Number of previous link pages to display
  142.         int nOffsetPrev = nNbLinkPagesToDisplay / 2;

  143.         // Number of next link pages to display
  144.         int nOffsetNext = nNbLinkPagesToDisplay / 2;

  145.         // Set the offsets
  146.         if ( _nPageCurrent <= ( nNbLinkPagesToDisplay - nOffsetPrev ) )
  147.         {
  148.             // If the current page is within the first 5 pages
  149.             nOffsetPrev = _nPageCurrent - 1;
  150.             nOffsetNext = nNbLinkPagesToDisplay - nOffsetPrev;
  151.         }
  152.         else
  153.             if ( ( _nPageCurrent + nOffsetNext ) > nNbLinkPages )
  154.             {
  155.                 // If the current page is within the last 5 pages
  156.                 nOffsetNext = nNbLinkPages - _nPageCurrent;
  157.                 nOffsetPrev = nNbLinkPagesToDisplay - nOffsetNext;
  158.             }

  159.         // Index of the last number of the link page to display
  160.         int nMax = nNbLinkPages;

  161.         // Index of the first number of the link page to display
  162.         int nMin = 1;

  163.         int nTmpMin = _nPageCurrent - nOffsetPrev;
  164.         int nTmpMax = _nPageCurrent + nOffsetNext;

  165.         if ( nTmpMax < nMax )
  166.         {
  167.             nMax = nTmpMax;
  168.         }

  169.         if ( nTmpMin > 0 )
  170.         {
  171.             nMin = nTmpMin;
  172.         }

  173.         for ( int i = nMin; i <= nMax; i++ )
  174.         {
  175.             PaginatorPage page = new PaginatorPage( );
  176.             String strIndex = "" + i;
  177.             UrlItem url = new UrlItem( _strBaseUrl );
  178.             url.addParameter( _strPageIndexParameterName, strIndex );
  179.             page.setUrl( url.getUrl( ) );
  180.             page.setName( strIndex );
  181.             page.setIndex( i );
  182.             list.add( page );
  183.         }

  184.         return list;
  185.     }

  186.     /**
  187.      * {@inheritDoc}
  188.      */
  189.     @Override
  190.     public String getLabelFirst( )
  191.     {
  192.         return LABEL_FIRST;
  193.     }

  194.     /**
  195.      * {@inheritDoc}
  196.      */
  197.     @Override
  198.     public String getLabelPrevious( )
  199.     {
  200.         return LABEL_PREVIOUS;
  201.     }

  202.     /**
  203.      * {@inheritDoc}
  204.      */
  205.     @Override
  206.     public String getLabelNext( )
  207.     {
  208.         return LABEL_NEXT;
  209.     }

  210.     /**
  211.      * {@inheritDoc}
  212.      */
  213.     @Override
  214.     public String getLabelLast( )
  215.     {
  216.         return LABEL_LAST;
  217.     }

  218.     /**
  219.      * {@inheritDoc}
  220.      */
  221.     @Override
  222.     public String getLabelItemCount( )
  223.     {
  224.         return "";
  225.     }

  226.     /**
  227.      * {@inheritDoc}
  228.      */
  229.     @Override
  230.     public String getLabelItemCountPerPage( )
  231.     {
  232.         return "";
  233.     }

  234.     /**
  235.      * {@inheritDoc}
  236.      */
  237.     @Override
  238.     public String getItemsPerPageParameterName( )
  239.     {
  240.         return _strItemsPerPageParameterName;
  241.     }

  242.     /**
  243.      * {@inheritDoc}
  244.      */
  245.     @Override
  246.     public void setItemsPerPageParameterName( String strItemsPerPageParameterName )
  247.     {
  248.         _strItemsPerPageParameterName = strItemsPerPageParameterName;
  249.     }

  250.     /**
  251.      * {@inheritDoc}
  252.      */
  253.     @Override
  254.     public int getPagesCount( )
  255.     {
  256.         return ( ( _nItemsCount - 1 ) / _nItemPerPage ) + 1;
  257.     }

  258.     /**
  259.      * {@inheritDoc}
  260.      */
  261.     @Override
  262.     public int getRangeMin( )
  263.     {
  264.         return ( !_list.isEmpty( ) ) ? ( ( _nItemPerPage * ( _nPageCurrent - 1 ) ) + 1 ) : 0;
  265.     }

  266.     /**
  267.      * {@inheritDoc}
  268.      */
  269.     @Override
  270.     public int getRangeMax( )
  271.     {
  272.         return ( _nItemsCount < ( ( _nItemPerPage * _nPageCurrent ) - 1 ) ) ? _nItemsCount : ( _nItemPerPage * _nPageCurrent );
  273.     }

  274.     /**
  275.      * {@inheritDoc}
  276.      */
  277.     @Override
  278.     public int getItemsCount( )
  279.     {
  280.         return _nItemsCount;
  281.     }

  282.     /**
  283.      * Gets the number of items per page from a request parameter
  284.      *
  285.      * @param request
  286.      *            The HTTP request
  287.      * @param strParameter
  288.      *            The request parameter name
  289.      * @param nCurrent
  290.      *            The current number of items
  291.      * @param nDefault
  292.      *            The default number of items
  293.      * @return The new number of items
  294.      */
  295.     public static int getItemsPerPage( HttpServletRequest request, String strParameter, int nCurrent, int nDefault )
  296.     {
  297.         int nItemsPerPage;
  298.         String strItemsPerPage = request.getParameter( strParameter );

  299.         if ( strItemsPerPage != null )
  300.         {
  301.             nItemsPerPage = Integer.parseInt( strItemsPerPage );
  302.         }
  303.         else
  304.         {
  305.             if ( nCurrent != 0 )
  306.             {
  307.                 nItemsPerPage = nCurrent;
  308.             }
  309.             else
  310.             {
  311.                 nItemsPerPage = nDefault;
  312.             }
  313.         }

  314.         return nItemsPerPage;
  315.     }

  316.     /**
  317.      * Gets the new page index from a request parameter
  318.      *
  319.      * @param request
  320.      *            The HTTP request
  321.      * @param strParameter
  322.      *            The request parameter name
  323.      * @param strCurrentPageIndex
  324.      *            The current page index
  325.      * @return The new page index
  326.      */
  327.     public static String getPageIndex( HttpServletRequest request, String strParameter, String strCurrentPageIndex )
  328.     {
  329.         String strPageIndex = request.getParameter( strParameter );
  330.         strPageIndex = ( strPageIndex != null ) ? strPageIndex : strCurrentPageIndex;

  331.         return strPageIndex;
  332.     }
  333. }