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