View Javadoc
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.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   * Manages pagination, filters and sort for demands
57   *
58   */
59  public class PaginationFilterSortManager
60  {
61      // PAGINATION
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      // SORT
67      public static final String SESSION_SORT_ATTRIBUTE = "sortAttribute";
68      public static final String SESSION_SORT_ASC = "sortIsAsc";
69  
70      // FILTER
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       * Constructor
80       * 
81       * @param request
82       *            the request
83       */
84      public PaginationFilterSortManager( HttpServletRequest request )
85      {
86          this( request, CRMPlugin.PLUGIN_NAME );
87  
88      }
89  
90      /**
91       * Constructor
92       * 
93       * @param request
94       *            the request
95       * @param strXpageApp
96       *            the XpageApp
97       * 
98       */
99      public PaginationFilterSortManager( HttpServletRequest request, String strXpageApp )
100     {
101         _request = request;
102         _strXpageApp = strXpageApp;
103     }
104 
105     /**
106      * Delete stored values for pagination, filter and sort
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             // PAGINATION
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             // SORT
124             session.removeAttribute( SESSION_SORT_ATTRIBUTE + nIdStatus );
125             session.removeAttribute( SESSION_SORT_ASC + nIdStatus );
126         }
127 
128         // FILTER
129         this.cleanSessionFilter( );
130     }
131 
132     /**
133      * Delete stored values for filter
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      * Store a sort into session
145      * 
146      * @param nIdStatus
147      *            the id status
148      * @param strSortAttribute
149      *            the sort attribute
150      * @param bSortIsAsc
151      *            the order
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      * Retrieve a stored sort from session
162      * 
163      * @param nIdStatus
164      *            the id status
165      * @return a DemandSort
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      * Retrieve the stored notification filter
185      * 
186      * @return the notification filter
187      */
188     public String retrieveFilterNotification( )
189     {
190         HttpSession session = _request.getSession( );
191 
192         return (String) ( session.getAttribute( SESSION_FILTER_NOTIFICATION ) );
193     }
194 
195     /**
196      * Store a notification filter into session
197      * 
198      * @param strNotification
199      *            the notification filter
200      */
201     public void storeFilterNotification( String strNotification )
202     {
203         HttpSession session = _request.getSession( );
204         session.setAttribute( SESSION_FILTER_NOTIFICATION, strNotification );
205     }
206 
207     /**
208      * Retrieve the stored demand type filter
209      * 
210      * @return the demand type filter
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      * Store the demand type filter into session
221      * 
222      * @param nIdDemandType
223      *            the demand type id
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      * Retrieve the stored modification date filter
233      * 
234      * @return the modification date filter
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      * Retrieve the stored modification date filter as string
245      * 
246      * @return the modification date filter as string
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      * Store the modification date filter into session
257      * 
258      * @param modificationDate
259      *            the modification date
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      * Store the modification date filter as string into session
269      * 
270      * @param strModificationDate
271      *            the modification date as string
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      * Create a paginator and store it into session
281      * 
282      * @param nIdStatus
283      *            the id status
284      * @param listDemand
285      *            the result list
286      * @param nTotalResult
287      *            the total number of results
288      * @return a paginator
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      * Retrieve a stored paginator
331      * 
332      * @param nIdStatus
333      *            the id status
334      * @return the paginator
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      * Retrieve a stored current page index
345      * 
346      * @param nIdStatus
347      *            the id status
348      * @return the current page index
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      * Retrieve a stored number of items per page
359      * 
360      * @param nIdStatus
361      *            the id status
362      * @return the number of items per page
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      * Return a bean for pagination in service/dao using parameter in http request
373      * 
374      * @param nIdStatus
375      *            the id status
376      * @param nTotalResult
377      *            the total number of results
378      * @return pagination properties
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 }