1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.crm.business.demand;
35
36 import fr.paris.lutece.plugins.crm.service.CRMPlugin;
37 import fr.paris.lutece.plugins.crm.service.demand.DemandStatusCRMService;
38 import fr.paris.lutece.plugins.crm.util.constants.CRMConstants;
39 import fr.paris.lutece.portal.service.content.XPageAppService;
40 import fr.paris.lutece.portal.service.util.AppPathService;
41 import fr.paris.lutece.portal.service.util.AppPropertiesService;
42 import fr.paris.lutece.portal.web.util.LocalizedDelegatePaginator;
43 import fr.paris.lutece.util.html.IPaginator;
44 import fr.paris.lutece.util.html.Paginator;
45 import fr.paris.lutece.util.url.UrlItem;
46
47 import org.apache.commons.lang3.StringUtils;
48
49 import java.util.Date;
50 import java.util.List;
51
52 import javax.servlet.http.HttpServletRequest;
53 import javax.servlet.http.HttpSession;
54
55
56
57
58
59 public class PaginationFilterSortManager
60 {
61
62 public static final String SESSION_PAGINATION_CURRENT_PAGE_INDEX = "currentPageIndex";
63 public static final String SESSION_PAGINATION_ITEMS_PER_PAGE = "itemsPerPage";
64 public static final String SESSION_PAGINATION_PAGINATOR = "paginator";
65
66
67 public static final String SESSION_SORT_ATTRIBUTE = "sortAttribute";
68 public static final String SESSION_SORT_ASC = "sortIsAsc";
69
70
71 public static final String SESSION_FILTER_MODIFICATION_DATE = "modificationDate";
72 public static final String SESSION_FILTER_STR_MODIFICATION_DATE = "strModificationDate";
73 public static final String SESSION_FILTER_DEMAND_TYPE = "demandType";
74 public static final String SESSION_FILTER_NOTIFICATION = "notification";
75 private HttpServletRequest _request;
76 private String _strXpageApp;
77
78
79
80
81
82
83
84 public PaginationFilterSortManager( HttpServletRequest request )
85 {
86 this( request, CRMPlugin.PLUGIN_NAME );
87
88 }
89
90
91
92
93
94
95
96
97
98
99 public PaginationFilterSortManager( HttpServletRequest request, String strXpageApp )
100 {
101 _request = request;
102 _strXpageApp = strXpageApp;
103 }
104
105
106
107
108 public void cleanSession( )
109 {
110 HttpSession session = _request.getSession( );
111
112 int nIdStatus;
113
114 for ( DemandStatusCRM statusCRM : DemandStatusCRMService.getService( ).getAllStatusCRM( _request.getLocale( ) ) )
115 {
116 nIdStatus = statusCRM.getIdStatusCRM( );
117
118
119 session.removeAttribute( SESSION_PAGINATION_CURRENT_PAGE_INDEX + nIdStatus );
120 session.removeAttribute( SESSION_PAGINATION_ITEMS_PER_PAGE + nIdStatus );
121 session.removeAttribute( SESSION_PAGINATION_PAGINATOR + nIdStatus );
122
123
124 session.removeAttribute( SESSION_SORT_ATTRIBUTE + nIdStatus );
125 session.removeAttribute( SESSION_SORT_ASC + nIdStatus );
126 }
127
128
129 this.cleanSessionFilter( );
130 }
131
132
133
134
135 public void cleanSessionFilter( )
136 {
137 HttpSession session = _request.getSession( );
138 session.removeAttribute( SESSION_FILTER_DEMAND_TYPE );
139 session.removeAttribute( SESSION_FILTER_MODIFICATION_DATE );
140 session.removeAttribute( SESSION_FILTER_NOTIFICATION );
141 }
142
143
144
145
146
147
148
149
150
151
152
153 public void storeSort( int nIdStatus, String strSortAttribute, boolean bSortIsAsc )
154 {
155 HttpSession session = _request.getSession( );
156 session.setAttribute( SESSION_SORT_ATTRIBUTE + nIdStatus, strSortAttribute );
157 session.setAttribute( SESSION_SORT_ASC + nIdStatus, bSortIsAsc );
158 }
159
160
161
162
163
164
165
166
167 public DemandSort retrieveSort( int nIdStatus )
168 {
169 HttpSession session = _request.getSession( );
170 String strSortAttribute = (String) ( session.getAttribute( SESSION_SORT_ATTRIBUTE + nIdStatus ) );
171 Boolean bSortIsAsc = (Boolean) ( session.getAttribute( SESSION_SORT_ASC + nIdStatus ) );
172
173 DemandSort demandSort = null;
174
175 if ( StringUtils.isNotBlank( strSortAttribute ) )
176 {
177 demandSort = new DemandSort( strSortAttribute, bSortIsAsc );
178 }
179
180 return demandSort;
181 }
182
183
184
185
186
187
188 public String retrieveFilterNotification( )
189 {
190 HttpSession session = _request.getSession( );
191
192 return (String) ( session.getAttribute( SESSION_FILTER_NOTIFICATION ) );
193 }
194
195
196
197
198
199
200
201 public void storeFilterNotification( String strNotification )
202 {
203 HttpSession session = _request.getSession( );
204 session.setAttribute( SESSION_FILTER_NOTIFICATION, strNotification );
205 }
206
207
208
209
210
211
212 public Integer retrieveFilterDemandType( )
213 {
214 HttpSession session = _request.getSession( );
215
216 return (Integer) ( session.getAttribute( SESSION_FILTER_DEMAND_TYPE ) );
217 }
218
219
220
221
222
223
224
225 public void storeFilterDemandType( int nIdDemandType )
226 {
227 HttpSession session = _request.getSession( );
228 session.setAttribute( SESSION_FILTER_DEMAND_TYPE, nIdDemandType );
229 }
230
231
232
233
234
235
236 public Date retrieveFilterModificationDate( )
237 {
238 HttpSession session = _request.getSession( );
239
240 return (Date) ( session.getAttribute( SESSION_FILTER_MODIFICATION_DATE ) );
241 }
242
243
244
245
246
247
248 public String retrieveFilterStringModificationDate( )
249 {
250 HttpSession session = _request.getSession( );
251
252 return (String) ( session.getAttribute( SESSION_FILTER_STR_MODIFICATION_DATE ) );
253 }
254
255
256
257
258
259
260
261 public void storeFilterModificationDate( Date modificationDate )
262 {
263 HttpSession session = _request.getSession( );
264 session.setAttribute( SESSION_FILTER_MODIFICATION_DATE, modificationDate );
265 }
266
267
268
269
270
271
272
273 public void storeFilterStringModificationDate( String strModificationDate )
274 {
275 HttpSession session = _request.getSession( );
276 session.setAttribute( SESSION_FILTER_STR_MODIFICATION_DATE, strModificationDate );
277 }
278
279
280
281
282
283
284
285
286
287
288
289
290 public IPaginator<Demand> createAndStorePaginator( int nIdStatus, List<Demand> listDemand, int nTotalResult )
291 {
292 HttpSession session = _request.getSession( );
293 UrlItem url = new UrlItem( AppPathService.getBaseUrl( _request ) + AppPathService.getPortalUrl( ) );
294 url.addParameter( XPageAppService.PARAM_XPAGE_APP, _strXpageApp );
295 url.addParameter( CRMConstants.PARAMETER_SESSION, Boolean.TRUE.toString( ) );
296
297 int nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( CRMConstants.PROPERTY_DEMANDS_PER_PAGE, 50 );
298
299 String strCurrentPageIndex = this.retrieveCurrentPageIndex( nIdStatus );
300
301 if ( StringUtils.isBlank( strCurrentPageIndex ) )
302 {
303 strCurrentPageIndex = "1";
304 }
305
306 Integer nItemsPerPage = this.retrieveItemsPerPage( nIdStatus );
307
308 if ( nItemsPerPage == null )
309 {
310 nItemsPerPage = nDefaultItemsPerPage;
311 }
312
313 strCurrentPageIndex = Paginator.getPageIndex( _request, Paginator.PARAMETER_PAGE_INDEX + nIdStatus, strCurrentPageIndex );
314
315 nItemsPerPage = Paginator.getItemsPerPage( _request, Paginator.PARAMETER_ITEMS_PER_PAGE + nIdStatus, nItemsPerPage, nDefaultItemsPerPage );
316
317 IPaginator<Demand> paginator = new LocalizedDelegatePaginator<Demand>( listDemand, nItemsPerPage, url.getUrl( ), Paginator.PARAMETER_PAGE_INDEX
318 + nIdStatus, strCurrentPageIndex, nTotalResult, _request.getLocale( ) );
319
320 paginator.setItemsPerPageParameterName( Paginator.PARAMETER_ITEMS_PER_PAGE + nIdStatus );
321
322 session.setAttribute( SESSION_PAGINATION_CURRENT_PAGE_INDEX + nIdStatus, strCurrentPageIndex );
323 session.setAttribute( SESSION_PAGINATION_ITEMS_PER_PAGE + nIdStatus, nItemsPerPage );
324 session.setAttribute( SESSION_PAGINATION_PAGINATOR + nIdStatus, paginator );
325
326 return paginator;
327 }
328
329
330
331
332
333
334
335
336 public IPaginator<Demand> retrievePaginator( int nIdStatus )
337 {
338 HttpSession session = _request.getSession( );
339
340 return (IPaginator<Demand>) ( session.getAttribute( SESSION_PAGINATION_PAGINATOR + nIdStatus ) );
341 }
342
343
344
345
346
347
348
349
350 public String retrieveCurrentPageIndex( int nIdStatus )
351 {
352 HttpSession session = _request.getSession( );
353
354 return (String) ( session.getAttribute( SESSION_PAGINATION_CURRENT_PAGE_INDEX + nIdStatus ) );
355 }
356
357
358
359
360
361
362
363
364 public Integer retrieveItemsPerPage( int nIdStatus )
365 {
366 HttpSession session = _request.getSession( );
367
368 return (Integer) ( session.getAttribute( SESSION_PAGINATION_ITEMS_PER_PAGE + nIdStatus ) );
369 }
370
371
372
373
374
375
376
377
378
379
380 public IPaginationProperties retrievePaginationProperties( int nIdStatus, Integer nTotalResult )
381 {
382 int nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( CRMConstants.PROPERTY_DEMANDS_PER_PAGE, 50 );
383
384 String strCurrentPageIndex = this.retrieveCurrentPageIndex( nIdStatus );
385
386 if ( StringUtils.isBlank( strCurrentPageIndex ) )
387 {
388 strCurrentPageIndex = "1";
389 }
390
391 Integer nItemsPerPage = this.retrieveItemsPerPage( nIdStatus );
392
393 if ( nItemsPerPage == null )
394 {
395 nItemsPerPage = nDefaultItemsPerPage;
396 }
397
398 strCurrentPageIndex = Paginator.getPageIndex( _request, Paginator.PARAMETER_PAGE_INDEX + nIdStatus, strCurrentPageIndex );
399
400 int nCurrentPageIndex = 1;
401
402 if ( StringUtils.isNotBlank( strCurrentPageIndex ) )
403 {
404 nCurrentPageIndex = Integer.valueOf( strCurrentPageIndex );
405 }
406
407 nItemsPerPage = Paginator.getItemsPerPage( _request, Paginator.PARAMETER_ITEMS_PER_PAGE + nIdStatus, nItemsPerPage, nDefaultItemsPerPage );
408
409 while ( ( ( nCurrentPageIndex - 1 ) * nItemsPerPage ) > nTotalResult )
410 {
411 nCurrentPageIndex = nCurrentPageIndex - 1;
412 strCurrentPageIndex = Integer.toString( nCurrentPageIndex );
413 }
414
415 return new PaginationPropertiesImpl( ( nCurrentPageIndex - 1 ) * nItemsPerPage, nItemsPerPage, nItemsPerPage, nCurrentPageIndex );
416 }
417 }