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.util.datatable;
35
36 import fr.paris.lutece.portal.service.util.AppLogService;
37 import fr.paris.lutece.portal.web.constants.Parameters;
38 import fr.paris.lutece.portal.web.l10n.LocaleService;
39 import fr.paris.lutece.portal.web.util.LocalizedDelegatePaginator;
40 import fr.paris.lutece.portal.web.util.LocalizedPaginator;
41 import fr.paris.lutece.util.ReferenceList;
42 import fr.paris.lutece.util.UniqueIDGenerator;
43 import fr.paris.lutece.util.html.IPaginator;
44 import fr.paris.lutece.util.html.Paginator;
45 import fr.paris.lutece.util.sort.AttributeComparator;
46 import fr.paris.lutece.util.url.UrlItem;
47
48 import org.apache.commons.beanutils.BeanUtilsBean;
49 import org.apache.commons.lang.StringUtils;
50
51 import java.io.Serializable;
52
53 import java.lang.reflect.InvocationTargetException;
54 import java.lang.reflect.Method;
55
56 import java.util.ArrayList;
57 import java.util.Collection;
58 import java.util.Collections;
59 import java.util.HashMap;
60 import java.util.List;
61 import java.util.Locale;
62 import java.util.Map;
63
64 import javax.servlet.http.HttpServletRequest;
65
66
67
68
69
70
71 public class DataTableManager<T> implements Serializable
72 {
73
74
75
76 private static final long serialVersionUID = -3906455886374172029L;
77 private static final String CONSTANT_GET = "get";
78 private static final String CONSTANT_IS = "is";
79 private static final String CONSTANT_DATA_TABLE_MANAGER_ID_PREFIX = "dataTableManager";
80 private String _strSortUrl;
81 private List<DataTableColumn> _listColumn = new ArrayList<DataTableColumn>( );
82 private FilterPanel _filterPanel;
83 private IPaginator<T> _paginator;
84 private String _strCurrentPageIndex = StringUtils.EMPTY;
85 private int _nItemsPerPage;
86 private int _nDefautlItemsPerPage;
87 private boolean _bEnablePaginator;
88 private Locale _locale;
89 private String _strSortedAttributeName;
90 private boolean _bIsAscSort;
91 private String _strUid;
92
93
94
95
96 protected DataTableManager( )
97 {
98 generateDataTableId( );
99 }
100
101
102
103
104
105
106
107
108 public DataTableManager( String strSortUrl, String strFilterUrl, int nDefautlItemsPerPage, boolean bEnablePaginator )
109 {
110 _filterPanel = new FilterPanel( strFilterUrl );
111 _nDefautlItemsPerPage = nDefautlItemsPerPage;
112 _nItemsPerPage = _nDefautlItemsPerPage;
113 _strCurrentPageIndex = "1";
114 _bEnablePaginator = bEnablePaginator;
115 generateDataTableId( );
116 setSortUrl( strSortUrl );
117 }
118
119
120
121
122
123
124
125
126
127
128
129 public void addColumn( String strColumnTitle, String strObjectName, boolean bSortable )
130 {
131 _listColumn.add( new DataTableColumn( strColumnTitle, strObjectName, bSortable, DataTableColumnType.STRING ) );
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145 public void addLabelColumn( String strColumnTitle, String strObjectName, boolean bSortable )
146 {
147 _listColumn.add( new DataTableColumn( strColumnTitle, strObjectName, bSortable, DataTableColumnType.LABEL ) );
148 }
149
150
151
152
153
154
155
156
157
158 public void addActionColumn( String strColumnTitle )
159 {
160 _listColumn.add( new DataTableColumn( strColumnTitle, null, false, DataTableColumnType.ACTION ) );
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176 public void addBooleanColumn( String strColumnTitle, String strObjectName, String strLabelTrue, String strLabelFalse )
177 {
178 _listColumn.add( new DataTableColumn( strColumnTitle, strObjectName, false, DataTableColumnType.BOOLEAN,
179 strLabelTrue, strLabelFalse ) );
180 }
181
182
183
184
185
186
187
188
189
190
191
192
193 public void addFreeColumn( String strColumnTitle, String strFreemarkerMacroName )
194 {
195 _listColumn.add( new DataTableColumn( strColumnTitle, strFreemarkerMacroName, false, DataTableColumnType.ACTION ) );
196 }
197
198
199
200
201
202
203
204
205
206
207
208
209 public void addEmailColumn( String strColumnTitle, String strObjectName, boolean bSortable )
210 {
211 _listColumn.add( new DataTableColumn( strColumnTitle, strObjectName, bSortable, DataTableColumnType.EMAIL ) );
212 }
213
214
215
216
217
218
219
220
221
222
223
224
225 public void addFilter( DataTableFilterType filterType, String strParameterName, String strFilterLabel )
226 {
227 _filterPanel.addFilter( filterType, strParameterName, strFilterLabel );
228 }
229
230
231
232
233
234
235
236
237
238
239 public void addDropDownListFilter( String strParameterName, String strFilterLabel, ReferenceList refList )
240 {
241 _filterPanel.addDropDownListFilter( strParameterName, strFilterLabel, refList );
242 }
243
244
245
246
247
248
249 public void filterSortAndPaginate( HttpServletRequest request, List<T> items )
250 {
251 List<T> filteredSortedPaginatedItems = new ArrayList<T>( items );
252
253 boolean bSubmitedDataTable = hasDataTableFormBeenSubmited( request );
254
255
256 Collection<DataTableFilter> listFilters = _filterPanel.getListFilter( );
257 boolean bUpdateFilter = false;
258 boolean bResetFilter = false;
259
260
261 if ( bSubmitedDataTable )
262 {
263 bResetFilter = StringUtils.equals( request.getParameter( FilterPanel.PARAM_FILTER_PANEL_PREFIX +
264 FilterPanel.PARAM_RESET_FILTERS ), Boolean.TRUE.toString( ) );
265 bUpdateFilter = true;
266
267 if ( !bResetFilter )
268 {
269 bUpdateFilter = StringUtils.equals( request.getParameter( FilterPanel.PARAM_FILTER_PANEL_PREFIX +
270 FilterPanel.PARAM_UPDATE_FILTERS ), Boolean.TRUE.toString( ) );
271 }
272 }
273
274 for ( DataTableFilter filter : listFilters )
275 {
276 String strFilterValue;
277
278 if ( bSubmitedDataTable && bUpdateFilter )
279 {
280
281 strFilterValue = request.getParameter( FilterPanel.PARAM_FILTER_PANEL_PREFIX +
282 filter.getParameterName( ) );
283
284 if ( !bResetFilter && ( filter.getFilterType( ) == DataTableFilterType.BOOLEAN ) &&
285 ( strFilterValue == null ) )
286 {
287 strFilterValue = Boolean.FALSE.toString( );
288 }
289
290 filter.setValue( strFilterValue );
291 }
292 else
293 {
294 strFilterValue = filter.getValue( );
295 }
296
297 if ( StringUtils.isNotBlank( strFilterValue ) )
298 {
299 List<T> bufferList = new ArrayList<T>( );
300
301 for ( T item : filteredSortedPaginatedItems )
302 {
303 Method method = getMethod( item, filter.getParameterName( ), CONSTANT_GET );
304
305 if ( ( method == null ) && ( filter.getFilterType( ) == DataTableFilterType.BOOLEAN ) )
306 {
307 method = getMethod( item, filter.getParameterName( ), CONSTANT_IS );
308 }
309
310 if ( method != null )
311 {
312 try
313 {
314 Object value = method.invoke( item );
315
316 if ( ( value != null ) && strFilterValue.equals( value.toString( ) ) )
317 {
318 bufferList.add( item );
319 }
320 }
321 catch ( Exception e )
322 {
323 AppLogService.error( e.getMessage( ), e );
324 }
325 }
326 }
327
328 filteredSortedPaginatedItems.retainAll( bufferList );
329 }
330 }
331
332
333 if ( bSubmitedDataTable )
334 {
335
336 String strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
337
338 if ( strSortedAttributeName != null )
339 {
340
341 _strSortedAttributeName = strSortedAttributeName;
342 _bIsAscSort = Boolean.parseBoolean( request.getParameter( Parameters.SORTED_ASC ) );
343 }
344 }
345
346
347 if ( _strSortedAttributeName != null )
348 {
349 Collections.sort( filteredSortedPaginatedItems,
350 new AttributeComparator( _strSortedAttributeName, _bIsAscSort ) );
351 }
352
353
354 if ( bSubmitedDataTable )
355 {
356
357 if ( _bEnablePaginator )
358 {
359 int nOldItemsPerPage = _nItemsPerPage;
360 _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE,
361 _nItemsPerPage, _nDefautlItemsPerPage );
362
363
364 if ( _nItemsPerPage != nOldItemsPerPage )
365 {
366 _strCurrentPageIndex = Integer.toString( 1 );
367 }
368 else
369 {
370 _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX,
371 _strCurrentPageIndex );
372 }
373 }
374 else
375 {
376 _strCurrentPageIndex = Integer.toString( 1 );
377 _nItemsPerPage = filteredSortedPaginatedItems.size( );
378 }
379 }
380
381
382 _paginator = new LocalizedPaginator<T>( filteredSortedPaginatedItems, _nItemsPerPage, getSortUrl( ),
383 Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex, request.getLocale( ) );
384 }
385
386
387
388
389
390 public FilterPanel getFilterPanel( )
391 {
392 return _filterPanel;
393 }
394
395
396
397
398
399 public void setFilterPanel( FilterPanel filterPanel )
400 {
401 _filterPanel = filterPanel;
402 }
403
404
405
406
407
408 public List<DataTableColumn> getListColumn( )
409 {
410 return _listColumn;
411 }
412
413
414
415
416
417 public void setListColumn( List<DataTableColumn> listColumn )
418 {
419 _listColumn = listColumn;
420 }
421
422
423
424
425
426 public String getSortUrl( )
427 {
428 return _strSortUrl;
429 }
430
431
432
433
434
435 public void setSortUrl( String strSortUrl )
436 {
437 _strSortUrl = strSortUrl;
438
439 if ( ( _strSortUrl != null ) && StringUtils.isNotEmpty( _strSortUrl ) &&
440 !StringUtils.contains( _strSortUrl, getId( ) ) )
441 {
442
443 UrlItem urlItem = new UrlItem( _strSortUrl );
444 urlItem.addParameter( getId( ), getId( ) );
445 _strSortUrl = urlItem.getUrl( );
446 }
447 }
448
449
450
451
452
453
454
455 public List<T> getItems( )
456 {
457 return _paginator.getPageItems( );
458 }
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473 public void setItems( List<T> items, int nTotalItemsNumber )
474 {
475 _paginator = new LocalizedDelegatePaginator<T>( items, _nItemsPerPage, getSortUrl( ),
476 Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex, nTotalItemsNumber, _locale );
477 }
478
479
480
481
482
483 public void clearItems( )
484 {
485 _paginator = null;
486 _locale = null;
487 }
488
489
490
491
492
493
494
495
496 public IPaginator<T> getPaginator( )
497 {
498 return _paginator;
499 }
500
501
502
503
504
505 public boolean getEnablePaginator( )
506 {
507 return _bEnablePaginator;
508 }
509
510
511
512
513
514 public Locale getLocale( )
515 {
516 return _locale;
517 }
518
519
520
521
522
523 public void setLocale( Locale locale )
524 {
525 _locale = locale;
526 }
527
528
529
530
531
532 public String getId( )
533 {
534 return _strUid;
535 }
536
537
538
539
540
541
542 public DataTablePaginationProperties getAndUpdatePaginator( HttpServletRequest request )
543 {
544 DataTablePaginationProperties paginationProperties = null;
545
546 if ( _bEnablePaginator )
547 {
548 paginationProperties = new DataTablePaginationProperties( );
549
550 if ( hasDataTableFormBeenSubmited( request ) )
551 {
552 _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX,
553 _strCurrentPageIndex );
554 _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE,
555 _nItemsPerPage, _nDefautlItemsPerPage );
556 }
557
558 paginationProperties.setItemsPerPage( _nItemsPerPage );
559
560 int nCurrentPageIndex = 1;
561
562 if ( !StringUtils.isEmpty( _strCurrentPageIndex ) )
563 {
564 nCurrentPageIndex = Integer.parseInt( _strCurrentPageIndex );
565 }
566
567 paginationProperties.setCurrentPageIndex( nCurrentPageIndex );
568 }
569
570 _locale = ( request != null ) ? request.getLocale( ) : LocaleService.getDefault( );
571
572 return paginationProperties;
573 }
574
575
576
577
578
579
580 public DataTableSort getAndUpdateSort( HttpServletRequest request )
581 {
582 if ( hasDataTableFormBeenSubmited( request ) )
583 {
584 String strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
585
586 if ( strSortedAttributeName != null )
587 {
588
589 _strSortedAttributeName = strSortedAttributeName;
590 _bIsAscSort = Boolean.parseBoolean( request.getParameter( Parameters.SORTED_ASC ) );
591 }
592 }
593
594 DataTableSort sort = new DataTableSort( _strSortedAttributeName, _bIsAscSort );
595
596 return sort;
597 }
598
599
600
601
602
603
604
605
606
607 public <K> K getAndUpdateFilter( HttpServletRequest request, K filterObject )
608 {
609 List<DataTableFilter> listFilters = _filterPanel.getListFilter( );
610
611 boolean bSubmitedDataTable = hasDataTableFormBeenSubmited( request );
612
613 boolean bResetFilter = false;
614 boolean bUpdateFilter = false;
615
616 Map<String, Object> mapFilter = new HashMap<String, Object>( );
617
618 if ( bSubmitedDataTable )
619 {
620 bUpdateFilter = true;
621 StringUtils.equals( request.getParameter( FilterPanel.PARAM_FILTER_PANEL_PREFIX +
622 FilterPanel.PARAM_RESET_FILTERS ), Boolean.TRUE.toString( ) );
623
624 if ( !bResetFilter )
625 {
626 bUpdateFilter = StringUtils.equals( request.getParameter( FilterPanel.PARAM_FILTER_PANEL_PREFIX +
627 FilterPanel.PARAM_UPDATE_FILTERS ), Boolean.TRUE.toString( ) );
628 }
629 }
630
631 for ( DataTableFilter filter : listFilters )
632 {
633 if ( bSubmitedDataTable )
634 {
635 String strFilterValue = request.getParameter( FilterPanel.PARAM_FILTER_PANEL_PREFIX +
636 filter.getParameterName( ) );
637
638 if ( bUpdateFilter )
639 {
640 filter.setValue( strFilterValue );
641 }
642 }
643
644 if ( StringUtils.isNotBlank( filter.getValue( ) ) )
645 {
646 mapFilter.put( filter.getParameterName( ), filter.getValue( ) );
647 }
648 }
649
650 try
651 {
652 BeanUtilsBean.getInstance( ).populate( filterObject, mapFilter );
653 }
654 catch ( IllegalAccessException e )
655 {
656 AppLogService.error( e.getMessage( ), e );
657
658 return null;
659 }
660 catch ( InvocationTargetException e )
661 {
662 AppLogService.error( e.getMessage( ), e );
663
664 return null;
665 }
666
667 return filterObject;
668 }
669
670
671
672
673
674 public String getFilterPanelPrefix( )
675 {
676 return FilterPanel.PARAM_FILTER_PANEL_PREFIX;
677 }
678
679
680
681
682
683
684
685
686
687
688 private Method getMethod( Object obj, String strAttributName, String strMethodPrefix )
689 {
690 Method method = null;
691 String strFirstLetter = strAttributName.substring( 0, 1 ).toUpperCase( );
692
693 String strMethodName = strMethodPrefix + strFirstLetter +
694 strAttributName.substring( 1, strAttributName.length( ) );
695
696 try
697 {
698 method = obj.getClass( ).getMethod( strMethodName );
699 }
700 catch ( Exception e )
701 {
702 AppLogService.debug( e );
703 }
704
705 return method;
706 }
707
708
709
710
711 private void generateDataTableId( )
712 {
713 this._strUid = CONSTANT_DATA_TABLE_MANAGER_ID_PREFIX + UniqueIDGenerator.getNewId( );
714 }
715
716
717
718
719
720
721
722 private boolean hasDataTableFormBeenSubmited( HttpServletRequest request )
723 {
724 return ( request != null ) ? StringUtils.equals( request.getParameter( getId( ) ), getId( ) ) : false;
725 }
726 }