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