View Javadoc
1   /*
2    * Copyright (c) 2002-2018, 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.stock.modules.billetterie.web;
35  
36  import fr.paris.lutece.plugins.stock.modules.billetterie.business.RatingProductDTO;
37  import fr.paris.lutece.plugins.stock.modules.billetterie.service.IRatingProductService;
38  import fr.paris.lutece.plugins.stock.modules.tickets.service.IStatisticService;
39  import fr.paris.lutece.portal.business.user.AdminUser;
40  import fr.paris.lutece.portal.service.dashboard.DashboardComponent;
41  import fr.paris.lutece.portal.service.spring.SpringContextService;
42  import fr.paris.lutece.portal.service.template.AppTemplateService;
43  import fr.paris.lutece.portal.service.util.AppPathService;
44  import fr.paris.lutece.util.html.HtmlTemplate;
45  import fr.paris.lutece.util.html.Paginator;
46  import fr.paris.lutece.util.url.UrlItem;
47  
48  import javax.servlet.http.HttpServletRequest;
49  import java.util.*;
50  
51  import static java.util.Optional.ofNullable;
52  
53  /**
54   *
55   * DatabaseAdminDashboardComponent
56   *
57   */
58  public class BilleterieDashboardComponent extends DashboardComponent
59  {
60      private static final String MARK_NB_PURCHASE_COUNT_OF_MONTH = "nbPurchaseCountDuMois";
61      private static final String MARK_NB_PURCHASE_COUNT_OF_DAY = "nbPurchaseCountDuJour";
62      private static final String MARK_NB_PRODUCT_A_VENIR = "nbProductAVenir";
63      private static final String MARK_NB_PRODUCT_A_L_AFFICHE = "nbProductALAffiche";
64      private static final String MARK_LIST_RATING_FOR_PRODUCT = "list_product_with_rating_note";
65      private static final String MARK_NUMBER_OF_ITEM_PER_PAGE = "nItem_per_page";
66      private static final String MARK_SIZE_LIST_RATING_PRODUCT = "size_list_rp";
67  
68      private static final String BEAN_STOCK_TICKETS_SHOW_SERVICE = "stock-tickets.showService";
69      public static final String BEAN_RATING_PRODUCT_SERVICE = "stock-billetterie.RatingProductService";
70  
71      private static final String PARAMETER_PAGE_INDEX = "page_index";
72      private static final String MARK_PAGINATOR = "paginator";
73  
74      // defaults
75      private static final String DEFAULT_PAGE_INDEX = "1";
76  
77      // TEMPLATES
78      private static final String TEMPLATE_ADMIN_DASHBOARD = "admin/plugins/stock/modules/billetterie/billeterie_dashboard.html";
79  
80      // Variables
81      private IRatingProductService _ratingProductService = SpringContextService.getBean( BEAN_RATING_PRODUCT_SERVICE );
82  
83      /**
84       *
85       * {@inheritDoc}
86       */
87      @Override
88      public String getDashboardData( AdminUser user, HttpServletRequest request )
89      {
90          HtmlTemplate template;
91          IStatisticService statisticService = SpringContextService.getContext( ).getBean( IStatisticService.class );
92  
93          Integer nProductCountALAffiche = statisticService.getCountProductALAffiche( );
94          Integer nProductCountAVenir = statisticService.getCountProductAVenir( );
95  
96          Integer nbPurchaseCountOfDay = statisticService.getCountPurchaseOfDay( );
97          Integer nbPurchaseCountOfMonth = statisticService.getCountPurchaseOfMonth( );
98  
99          List<RatingProductDTO> ratingProductDTOList = new ArrayList<>( );
100         ratingProductDTOList = _ratingProductService.getAllRatingProduct( );
101 
102         Paginator<RatingProductDTO> paginator = null;
103         int nItemPerPage = 5;
104 
105         if ( ratingProductDTOList != null )
106         {
107             ratingProductDTOList.sort( Comparator.comparing( RatingProductDTO::getRating ).reversed( ) );
108 
109             UrlItem urlItem = new UrlItem( AppPathService.getAdminMenuUrl( ) );
110             String strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, DEFAULT_PAGE_INDEX );
111 
112             paginator = new Paginator<RatingProductDTO>( ratingProductDTOList, nItemPerPage, urlItem.getUrl( ), PARAMETER_PAGE_INDEX, strCurrentPageIndex );
113         }
114 
115         // Fill the model
116         Map<String, Object> model = new HashMap<String, Object>( );
117 
118         model.put( MARK_NB_PRODUCT_A_L_AFFICHE, nProductCountALAffiche );
119         model.put( MARK_NB_PRODUCT_A_VENIR, nProductCountAVenir );
120 
121         model.put( MARK_NB_PURCHASE_COUNT_OF_DAY, nbPurchaseCountOfDay );
122         model.put( MARK_NB_PURCHASE_COUNT_OF_MONTH, nbPurchaseCountOfMonth );
123 
124         model.put( MARK_PAGINATOR, paginator );
125 
126         if ( paginator != null )
127         {
128             model.put( MARK_LIST_RATING_FOR_PRODUCT, paginator.getPageItems( ) );
129         }
130 
131         model.put( MARK_NUMBER_OF_ITEM_PER_PAGE, nItemPerPage );
132         model.put( MARK_SIZE_LIST_RATING_PRODUCT, ofNullable( ratingProductDTOList ).map( List::size ).orElse( 0 ) );
133 
134         template = AppTemplateService.getTemplate( TEMPLATE_ADMIN_DASHBOARD, user.getLocale( ), model );
135 
136         return template.getHtml( );
137     }
138 }