View Javadoc
1   /*
2    * Copyright (c) 2002-2025, 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   
35  package fr.paris.lutece.plugins.formresponsxpage.web;
36  
37  import fr.paris.lutece.portal.service.util.AppPropertiesService;
38  import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
39  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
40  import fr.paris.lutece.util.html.AbstractPaginator;
41  import fr.paris.lutece.util.url.UrlItem;
42  
43  import java.util.Enumeration;
44  import java.util.HashMap;
45  import java.util.List;
46  import java.util.Map;
47  import javax.servlet.http.HttpServletRequest;
48  
49  import org.apache.commons.lang3.StringUtils;
50  
51  public abstract class AbstractJspBean <S, T> extends MVCAdminJspBean
52  {
53      
54      // Properties
55      protected static final String PROPERTY_DEFAULT_LIST_ITEM_PER_PAGE = "formresponsxpage.listItems.itemsPerPage";
56      private static final int PROPERTY_DEFAULT_ITEM_PER_PAGE = 50;
57      
58      // Parameters
59      private static final String PARAMETER_PAGE_INDEX = "page_index";
60      protected static final String PARAMETER_SEARCH_ORDER_BY = "orderBy";
61      private static final String  PARAMETER_MAP_FILTER_CRITERIA = "mapFilterCriteria";  
62      
63      // Markers
64      private static final String MARK_PAGINATOR = "paginator";
65      private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
66  
67      //Search
68      private static final String FILTER_ATTRIBUTES_PREFIX = "filter_";
69      private static final String SORT_ATTRIBUTES_ASC = " ASC ";
70      private static final String SORT_ATTRIBUTES_DESC = " DESC ";
71      
72      //Variables
73      private String _strCurrentPageIndex;
74      private int _nItemsPerPage;
75      private String _strSortMode="";
76  
77      /**
78       * Return a model that contains the list and paginator infos
79       * @param request The HTTP request
80       * @param strBookmark The bookmark
81       * @param list The list of item
82       * @param strManageJsp The JSP
83       * @return The model
84       */
85      protected <T> Map<String, Object> getPaginatedListModel( HttpServletRequest request, String strBookmark, List<S> list,
86          String strManageJsp )
87      {
88          int nDefaultItemsPerPage = getPluginDefaultNumberOfItemPerPage( );
89          _strCurrentPageIndex = AbstractPaginator.getPageIndex( request, AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
90          _nItemsPerPage = AbstractPaginator.getItemsPerPage( request, AbstractPaginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, nDefaultItemsPerPage );
91  
92          UrlItem url = new UrlItem( strManageJsp );
93          String strUrl = url.getUrl(  );
94  
95          // PAGINATOR
96          LocalizedPaginator<S> paginator = new LocalizedPaginator<>( list, _nItemsPerPage, strUrl, PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale(  ) );
97  
98          Map<String, Object> model = getModel(  );
99  
100         model.put( MARK_NB_ITEMS_PER_PAGE, String.valueOf( _nItemsPerPage ) );
101         model.put( MARK_PAGINATOR, paginator );
102         model.put( strBookmark, getItemsFromIds ( paginator.getPageItems( ) ) );
103 
104         return model;
105     }
106     
107     /**
108      * Get Items from Ids list
109      * @param <T>
110      *
111      * @param <S> the generic type of the Ids
112      * @param <T> the generic type of the items
113      * @param <S>
114      * @param listIds
115      * @return the populated list of items corresponding to the id List
116      */
117      abstract  List<T> getItemsFromIds ( List<S> listIds ) ;
118      
119      int getPluginDefaultNumberOfItemPerPage( ) { return PROPERTY_DEFAULT_ITEM_PER_PAGE; } ;
120      
121       /**
122       * set _strCurrentPageIndex to null
123       */
124      protected void resetCurrentPageIndexOfPaginator() {
125     	 _strCurrentPageIndex=null;
126      }
127      
128      /**
129       * the name of the filter criteria sent in the request must start with "filter_"
130       * @param request 
131       * @return mapFilterCriteria : contains all names/values filter criteria
132       */
133      protected Map<String,String> getFilterCriteriaFromRequest( HttpServletRequest request ) 
134      {	
135     	   	
136      	Enumeration enumeration = request.getParameterNames( );
137      	Map<String, String> mapFilterCriteria = new HashMap<>( );
138          
139      	while ( enumeration.hasMoreElements( ) )
140 		{
141 			String strParameterName = (String) enumeration.nextElement();
142 			
143 			//All parameters from search bar start with the same prefix "filter_" 
144 			if ( strParameterName.startsWith( FILTER_ATTRIBUTES_PREFIX ) && !StringUtils.isBlank( request.getParameter( strParameterName ) ) )
145 			{
146 				mapFilterCriteria.put( strParameterName.substring(FILTER_ATTRIBUTES_PREFIX.length()), request.getParameter( strParameterName ) );
147 			}
148 		}
149         return mapFilterCriteria;            	
150      }  
151      
152      /**
153       *  add persistent values of the search inputs in model
154       *  @param model : map containing name parameters of http request with values associated.
155       *  @param mapFilterCriteria : contains search bar names/values inputs 
156       */
157      protected void addSearchParameters( Map<String, Object>  model, Map<String,String> mapFilterCriteria) {
158      	
159     	 //Persistent values of search inputs
160     	 model.put(PARAMETER_MAP_FILTER_CRITERIA, mapFilterCriteria); 
161  	}
162 
163      /**
164       *  get _strSortMode
165       *  At each sort request, the sort mode switches (ASC to DESC and vice versa)
166       */
167      protected String getSortMode() {
168     	 	 
169     	 _strSortMode = (_strSortMode==SORT_ATTRIBUTES_ASC)?SORT_ATTRIBUTES_DESC:SORT_ATTRIBUTES_ASC;
170 
171     	 return _strSortMode;
172     	 
173      }
174 }