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 org.apache.commons.lang3.StringUtils;
39  
40  import java.io.Serializable;
41  
42  import java.util.List;
43  
44  /**
45   *
46   * ItemNavigation provides a way to navigate between items
47   *
48   */
49  public class ItemNavigator implements Serializable
50  {
51      private static final long serialVersionUID = -6240496590042563143L;
52      private List<String> _listItems;
53      private int _nCurrentItemId;
54      private String _strBaseUrl;
55      private String _strParameterName;
56  
57      /**
58       * Create a new instance of ItemNavigator
59       * 
60       * @param listItems
61       *            the list of item
62       * @param nCurrentItemId
63       *            The current map key
64       * @param strBaseUrl
65       *            The base url
66       * @param strParameterName
67       *            The parameter name
68       */
69      public ItemNavigator( List<String> listItems, int nCurrentItemId, String strBaseUrl, String strParameterName )
70      {
71          _listItems = listItems;
72          _nCurrentItemId = nCurrentItemId;
73          _strBaseUrl = strBaseUrl;
74          _strParameterName = strParameterName;
75      }
76  
77      /**
78       * Set the current item id given the content of the item
79       * 
80       * @param strItem
81       *            the item
82       */
83      public void setCurrentItemId( String strItem )
84      {
85          int nCurrentItemId = _nCurrentItemId;
86  
87          if ( ( strItem != null ) && ( _listItems != null ) && !_listItems.isEmpty( ) && !strItem.equals( _listItems.get( _nCurrentItemId ) ) )
88          {
89              int nPreviousItemId = _nCurrentItemId - 1;
90              int nNextItemId = _nCurrentItemId + 1;
91  
92              if ( ( nPreviousItemId >= 0 ) && strItem.equals( _listItems.get( nPreviousItemId ) ) )
93              {
94                  // Check if it is the previous item
95                  nCurrentItemId = nPreviousItemId;
96              }
97              else
98                  if ( ( nNextItemId < _listItems.size( ) ) && strItem.equals( _listItems.get( nNextItemId ) ) )
99                  {
100                     // Check if it is the next item
101                     nCurrentItemId = nNextItemId;
102                 }
103                 else
104                 {
105                     // No choice, have to check every items
106                     for ( int nIndex = 0; nIndex < _listItems.size( ); nIndex++ )
107                     {
108                         if ( strItem.equals( _listItems.get( nIndex ) ) )
109                         {
110                             nCurrentItemId = nIndex;
111 
112                             break;
113                         }
114                     }
115                 }
116         }
117 
118         _nCurrentItemId = nCurrentItemId;
119     }
120 
121     /**
122      * Get the previous page link of the item
123      * 
124      * @return the previous page link of the item
125      */
126     public String getPreviousPageLink( )
127     {
128         int nPreviousItemId = _nCurrentItemId - 1;
129 
130         if ( ( _listItems != null ) && !_listItems.isEmpty( ) && ( nPreviousItemId >= 0 ) )
131         {
132             UrlIteml/UrlItem.html#UrlItem">UrlItem url = new UrlItem( _strBaseUrl );
133             url.addParameter( _strParameterName, _listItems.get( nPreviousItemId ) );
134 
135             return url.getUrl( );
136         }
137 
138         return StringUtils.EMPTY;
139     }
140 
141     /**
142      * Get the next page link of the item
143      * 
144      * @return the next page link of the item
145      */
146     public String getNextPageLink( )
147     {
148         int nNextItemId = _nCurrentItemId + 1;
149 
150         if ( ( _listItems != null ) && !_listItems.isEmpty( ) && ( nNextItemId < _listItems.size( ) ) )
151         {
152             UrlIteml/UrlItem.html#UrlItem">UrlItem url = new UrlItem( _strBaseUrl );
153             url.addParameter( _strParameterName, _listItems.get( nNextItemId ) );
154 
155             return url.getUrl( );
156         }
157 
158         return StringUtils.EMPTY;
159     }
160 
161     /**
162      * Get the Url of he navigator
163      * 
164      * @return The Url of the navigator
165      */
166     public String getBaseUrl( )
167     {
168         return _strBaseUrl;
169     }
170 
171     /**
172      * Set the Url of he navigator
173      * 
174      * @param strBaseUrl
175      *            The new Url of the navigator
176      */
177     public void setBaseUrl( String strBaseUrl )
178     {
179         _strBaseUrl = strBaseUrl;
180     }
181 
182     /**
183      * Get the size of the item list
184      * 
185      * @return the size of the item list
186      */
187     public int getListItemSize( )
188     {
189         if ( _listItems != null )
190         {
191             return _listItems.size( );
192         }
193 
194         return 0;
195     }
196 
197     /**
198      * Get the current item map key
199      * 
200      * @return the current item map key
201      */
202     public int getCurrentItemId( )
203     {
204         return _nCurrentItemId;
205     }
206 }