ItemNavigator.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 org.apache.commons.lang3.StringUtils;

  37. import java.io.Serializable;

  38. import java.util.List;

  39. /**
  40.  *
  41.  * ItemNavigation provides a way to navigate between items
  42.  *
  43.  */
  44. public class ItemNavigator implements Serializable
  45. {
  46.     private static final long serialVersionUID = -6240496590042563143L;
  47.     private List<String> _listItems;
  48.     private int _nCurrentItemId;
  49.     private String _strBaseUrl;
  50.     private String _strParameterName;

  51.     /**
  52.      * Create a new instance of ItemNavigator
  53.      *
  54.      * @param listItems
  55.      *            the list of item
  56.      * @param nCurrentItemId
  57.      *            The current map key
  58.      * @param strBaseUrl
  59.      *            The base url
  60.      * @param strParameterName
  61.      *            The parameter name
  62.      */
  63.     public ItemNavigator( List<String> listItems, int nCurrentItemId, String strBaseUrl, String strParameterName )
  64.     {
  65.         _listItems = listItems;
  66.         _nCurrentItemId = nCurrentItemId;
  67.         _strBaseUrl = strBaseUrl;
  68.         _strParameterName = strParameterName;
  69.     }

  70.     /**
  71.      * Set the current item id given the content of the item
  72.      *
  73.      * @param strItem
  74.      *            the item
  75.      */
  76.     public void setCurrentItemId( String strItem )
  77.     {
  78.         int nCurrentItemId = _nCurrentItemId;

  79.         if ( ( strItem != null ) && ( _listItems != null ) && !_listItems.isEmpty( ) && !strItem.equals( _listItems.get( _nCurrentItemId ) ) )
  80.         {
  81.             int nPreviousItemId = _nCurrentItemId - 1;
  82.             int nNextItemId = _nCurrentItemId + 1;

  83.             if ( ( nPreviousItemId >= 0 ) && strItem.equals( _listItems.get( nPreviousItemId ) ) )
  84.             {
  85.                 // Check if it is the previous item
  86.                 nCurrentItemId = nPreviousItemId;
  87.             }
  88.             else
  89.                 if ( ( nNextItemId < _listItems.size( ) ) && strItem.equals( _listItems.get( nNextItemId ) ) )
  90.                 {
  91.                     // Check if it is the next item
  92.                     nCurrentItemId = nNextItemId;
  93.                 }
  94.                 else
  95.                 {
  96.                     // No choice, have to check every items
  97.                     for ( int nIndex = 0; nIndex < _listItems.size( ); nIndex++ )
  98.                     {
  99.                         if ( strItem.equals( _listItems.get( nIndex ) ) )
  100.                         {
  101.                             nCurrentItemId = nIndex;

  102.                             break;
  103.                         }
  104.                     }
  105.                 }
  106.         }

  107.         _nCurrentItemId = nCurrentItemId;
  108.     }

  109.     /**
  110.      * Get the previous page link of the item
  111.      *
  112.      * @return the previous page link of the item
  113.      */
  114.     public String getPreviousPageLink( )
  115.     {
  116.         int nPreviousItemId = _nCurrentItemId - 1;

  117.         if ( ( _listItems != null ) && !_listItems.isEmpty( ) && ( nPreviousItemId >= 0 ) )
  118.         {
  119.             UrlItem url = new UrlItem( _strBaseUrl );
  120.             url.addParameter( _strParameterName, _listItems.get( nPreviousItemId ) );

  121.             return url.getUrl( );
  122.         }

  123.         return StringUtils.EMPTY;
  124.     }

  125.     /**
  126.      * Get the next page link of the item
  127.      *
  128.      * @return the next page link of the item
  129.      */
  130.     public String getNextPageLink( )
  131.     {
  132.         int nNextItemId = _nCurrentItemId + 1;

  133.         if ( ( _listItems != null ) && !_listItems.isEmpty( ) && ( nNextItemId < _listItems.size( ) ) )
  134.         {
  135.             UrlItem url = new UrlItem( _strBaseUrl );
  136.             url.addParameter( _strParameterName, _listItems.get( nNextItemId ) );

  137.             return url.getUrl( );
  138.         }

  139.         return StringUtils.EMPTY;
  140.     }

  141.     /**
  142.      * Get the Url of he navigator
  143.      *
  144.      * @return The Url of the navigator
  145.      */
  146.     public String getBaseUrl( )
  147.     {
  148.         return _strBaseUrl;
  149.     }

  150.     /**
  151.      * Set the Url of he navigator
  152.      *
  153.      * @param strBaseUrl
  154.      *            The new Url of the navigator
  155.      */
  156.     public void setBaseUrl( String strBaseUrl )
  157.     {
  158.         _strBaseUrl = strBaseUrl;
  159.     }

  160.     /**
  161.      * Get the size of the item list
  162.      *
  163.      * @return the size of the item list
  164.      */
  165.     public int getListItemSize( )
  166.     {
  167.         if ( _listItems != null )
  168.         {
  169.             return _listItems.size( );
  170.         }

  171.         return 0;
  172.     }

  173.     /**
  174.      * Get the current item map key
  175.      *
  176.      * @return the current item map key
  177.      */
  178.     public int getCurrentItemId( )
  179.     {
  180.         return _nCurrentItemId;
  181.     }
  182. }