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.business.category.CategoryFilter;
37  import fr.paris.lutece.plugins.stock.business.product.ProductFilter;
38  import fr.paris.lutece.plugins.stock.business.provider.ProviderFilter;
39  import fr.paris.lutece.plugins.stock.commons.ResultList;
40  import fr.paris.lutece.plugins.stock.commons.dao.PaginationProperties;
41  import fr.paris.lutece.plugins.stock.commons.exception.BusinessException;
42  import fr.paris.lutece.plugins.stock.commons.exception.FunctionnalException;
43  import fr.paris.lutece.plugins.stock.modules.billetterie.utils.constants.BilletterieConstants;
44  import fr.paris.lutece.plugins.stock.modules.tickets.business.PartnerDTO;
45  import fr.paris.lutece.plugins.stock.modules.tickets.business.SeanceDTO;
46  import fr.paris.lutece.plugins.stock.modules.tickets.business.SeanceFilter;
47  import fr.paris.lutece.plugins.stock.modules.tickets.business.ShowDTO;
48  import fr.paris.lutece.plugins.stock.modules.tickets.business.ShowFilter;
49  import fr.paris.lutece.plugins.stock.modules.tickets.service.ICategoryService;
50  import fr.paris.lutece.plugins.stock.modules.tickets.service.IProviderService;
51  import fr.paris.lutece.plugins.stock.modules.tickets.service.ISeanceService;
52  import fr.paris.lutece.plugins.stock.modules.tickets.service.IShowService;
53  import fr.paris.lutece.plugins.stock.modules.tickets.service.IStatisticService;
54  import fr.paris.lutece.plugins.stock.modules.tickets.utils.constants.TicketsConstants;
55  import fr.paris.lutece.plugins.stock.service.ISubscriptionProductService;
56  import fr.paris.lutece.plugins.stock.utils.ImageUtils;
57  import fr.paris.lutece.plugins.stock.utils.ListUtils;
58  import fr.paris.lutece.plugins.stock.utils.constants.StockConstants;
59  import fr.paris.lutece.portal.service.i18n.I18nService;
60  import fr.paris.lutece.portal.service.message.AdminMessage;
61  import fr.paris.lutece.portal.service.message.AdminMessageService;
62  import fr.paris.lutece.portal.service.resource.ExtendableResourceRemovalListenerService;
63  import fr.paris.lutece.portal.service.spring.SpringContextService;
64  import fr.paris.lutece.portal.service.template.AppTemplateService;
65  import fr.paris.lutece.portal.service.util.AppLogService;
66  import fr.paris.lutece.portal.service.util.AppPathService;
67  import fr.paris.lutece.portal.service.util.AppPropertiesService;
68  import fr.paris.lutece.portal.web.constants.Parameters;
69  import fr.paris.lutece.portal.web.resource.ExtendableResourcePluginActionManager;
70  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
71  import fr.paris.lutece.util.ReferenceList;
72  import fr.paris.lutece.util.datatable.DataTableManager;
73  import fr.paris.lutece.util.html.HtmlTemplate;
74  
75  import org.apache.commons.fileupload.FileItem;
76  import org.apache.commons.io.FilenameUtils;
77  import org.apache.commons.lang.StringUtils;
78  import org.apache.log4j.Logger;
79  import org.springframework.transaction.annotation.Transactional;
80  
81  import java.io.File;
82  import java.io.IOException;
83  import java.io.InputStream;
84  import java.lang.reflect.Method;
85  import java.text.SimpleDateFormat;
86  import java.util.ArrayList;
87  import java.util.Date;
88  import java.util.HashMap;
89  import java.util.List;
90  import java.util.Locale;
91  import java.util.Map;
92  
93  import javax.servlet.http.HttpServletRequest;
94  
95  /**
96   * The Class ShowJspBean.
97   */
98  public class ShowJspBean extends AbstractJspBean
99  {
100     /** The Constant LOGGER. */
101     public static final Logger LOGGER = Logger.getLogger( ShowJspBean.class );
102 
103     /** The Constant PARAMETER_CATEGORY_ID. */
104     public static final String PARAMETER_CATEGORY_ID = "product_id";
105 
106     /** The Constant PARAMETER_PRODUCT_PARTNER_ID. */
107     public static final String PARAMETER_PRODUCT_PARTNER_ID = "partnerId";
108 
109     /** The Constant PARAMETER_PRODUCT_CATEGORY_ID. */
110     public static final String PARAMETER_PRODUCT_CATEGORY_ID = "categoryId";
111 
112     /** The Constant RIGHT_MANAGE_PRODUCTS. */
113     public static final String RIGHT_MANAGE_PRODUCTS = "PRODUCTS_MANAGEMENT";
114 
115     /** The Constant MARK_PRODUCT. */
116     public static final String MARK_PRODUCT = "product";
117 
118     /** The Constant MARK_TITLE. */
119     public static final String MARK_TITLE = "title";
120 
121     /** The constants for DataTableManager */
122     public static final String MARK_DATA_TABLE_PRODUCT = "dataTableProduct";
123     public static final String MARK_FILTER_PRODUCT = "filterProduct";
124     public static final String MACRO_COLUMN_ACTIONS_PRODUCT = "columnActionsProduct";
125     public static final String MACRO_COLUMN_NAME_PRODUCT = "columnNameProduct";
126     public static final String MACRO_COLUMN_DATES_PRODUCT = "columnDatesProduct";
127 
128     /** The Constant PARAMETER_PRODUCT_TYPE_LIST. */
129     public static final String PARAMETER_PRODUCT_TYPE_LIST = "product_type_list";
130     private static final long serialVersionUID = 5782544563181019800L;
131 
132     /** The Constant PARAMETER_PRODUCT_TYPE_LIST_DEFAULT. */
133     public static final String PARAMETER_PRODUCT_TYPE_LIST_DEFAULT = "product_type_list_default";
134 
135     /** The Constant BEAN_STOCK_TICKETS_SEANCE_SERVICE. */
136     private static final String BEAN_STOCK_TICKETS_SEANCE_SERVICE = "stock-tickets.seanceService";
137 
138     /** The Constant BEAN_STOCK_TICKETS_SHOW_SERVICE. */
139     private static final String BEAN_STOCK_TICKETS_SHOW_SERVICE = "stock-tickets.showService";
140 
141     /** The Constant MARK_LIST_CATEGORIES. */
142     private static final String MARK_LIST_CATEGORIES = "category_list";
143 
144     /** The Constant MARK_LIST_PROVIDERS. */
145     private static final String MARK_LIST_PROVIDERS = "provider_list";
146     private static final String MARK_CONTACT_LIST = "contact_list";
147 
148     private static final String PARAMETER_PRODUCT_ID_PROVIDER = "idProvider";
149     public static final String PARAMETER_REFRESH_CONTACT = "refresh_contact";
150 
151     private static final String MARK_URL_POSTER = "url_poster";
152 
153     /** The Constant MARK_PUBLIC_LIST. */
154     private static final String MARK_PUBLIC_LIST = "public_list";
155 
156     /** The Constant PARAMETER_POSTER. */
157     private static final String PARAMETER_POSTER = "posterFile";
158 
159     /** The Constant PARAMETER_A_LAFFICHE. */
160     private static final String PARAMETER_A_LAFFICHE = "aLaffiche";
161 
162     /** The Constant PROPERTY_POSTER_WIDTH. */
163     private static final String PROPERTY_POSTER_THUMB_WIDTH = "stock-billetterie.poster.width";
164 
165     /** The Constant PROPERTY_POSTER_HEIGHT. */
166     private static final String PROPERTY_POSTER_THUMB_HEIGHT = "stock-billetterie.poster.height";
167     private static final String PROPERTY_POSTER_MAX_HEIGHT = "stock-billetterie.poster.max-height";
168     private static final String PROPERTY_POSTER_MAX_WIDTH = "stock-billetterie.poster.max-width";
169     private static final int PROPERTY_POSTER_MAX_HEIGHT_DEFAULT = 400;
170     private static final int PROPERTY_POSTER_MAX_WIDTH_DEFAULT = 400;
171     private static final String PROPERTY_POSTER_PATH = "stock-billetterie.poster.path";
172     private static final String PROPERTY_RIMG_POSTER_PATH = "stock-billetterie.poster.rimg.path";
173 
174     /** The Constant PROPERTY_STOCK_BILLETTERIE_SHOW_PUBLIC. */
175     private static final String PROPERTY_STOCK_BILLETTERIE_SHOW_PUBLIC = "stock-billetterie.show.public";
176 
177     /** The Constant MARK_WEBAPP_URL. */
178     private static final String MARK_WEBAPP_URL = "webapp_url";
179 
180     /** The Constant MARK_LOCALE. */
181     private static final String MARK_LOCALE = "locale";
182 
183     // I18N
184     /** The Constant PAGE_TITLE_MANAGE_PRODUCT. */
185     private static final String PAGE_TITLE_MANAGE_PRODUCT = "module.stock.billetterie.manage_product.title";
186 
187     /** The Constant PAGE_TITLE_CREATE_PRODUCT. */
188     private static final String PAGE_TITLE_CREATE_PRODUCT = "module.stock.billetterie.save_product.create.title";
189 
190     /** The Constant PAGE_TITLE_MODIFY_PRODUCT. */
191     private static final String PAGE_TITLE_MODIFY_PRODUCT = "module.stock.billetterie.save_product.title";
192 
193     // Properties
194 
195     // JSP
196     /** The Constant JSP_MANAGE_PRODUCTS. */
197     private static final String JSP_MANAGE_PRODUCTS = "jsp/admin/plugins/stock/modules/billetterie/ManageProducts.jsp";
198 
199     /** The Constant JSP_SAVE_PRODUCT. */
200     private static final String JSP_SAVE_PRODUCT = "SaveProduct.jsp";
201 
202     /** The Constant CATEGORY_DO_DELETE_JSP. */
203     private static final String CATEGORY_DO_DELETE_JSP = "jsp/admin/plugins/stock/modules/billetterie/DoDeleteProduct.jsp";
204 
205     // Templates
206     /** The Constant TEMPLATE_MANAGE_PRODUCTS. */
207     private static final String TEMPLATE_MANAGE_PRODUCTS = "admin/plugins/stock/modules/billetterie/manage_products.html";
208 
209     /** The Constant TEMPLATE_SAVE_PRODUCT. */
210     private static final String TEMPLATE_SAVE_PRODUCT = "admin/plugins/stock/modules/billetterie/save_product.html";
211 
212     //
213     // // Mark
214 
215     // MESSAGES
216     /** The Constant MESSAGE_CONFIRMATION_DELETE_PRODUCT. */
217     private static final String MESSAGE_CONFIRMATION_DELETE_PRODUCT = "module.stock.billetterie.message.deleteProduct.confirmation";
218 
219     /** The Constant MESSAGE_DELETE_SHOW_WITH_SEANCE. */
220     private static final String MESSAGE_DELETE_SHOW_WITH_SEANCE = "module.stock.billetterie.message.deleteProduct.with.seance";
221 
222     /** The Constant MESSAGE_ERROR_MANDATORY_POSTER. */
223     private static final String MESSAGE_ERROR_MANDATORY_POSTER = "module.stock.billetterie.message.error.mandatory_poster";
224 
225     /** The Constant ERROR_MESSAGE_GET_AFFICHE. */
226     private static final String MESSAGE_ERROR_GET_AFFICHE = "module.stock.billetterie.message.error.get.affiche";
227 
228     // MEMBERS VARIABLES
229     /** The _service product. */
230     // @Inject
231     // @Named( "stock-tickets.showService" )
232     private IShowService _serviceProduct;
233 
234     /** The _service offer. */
235     // @Inject
236     // @Named( "stock-tickets.seanceService" )
237     private ISeanceService _serviceOffer;
238 
239     /** The _service provider. */
240     // @Inject
241     private IProviderService _serviceProvider;
242 
243     /** The _service category. */
244     // @Inject
245     private ICategoryService _serviceCategory;
246 
247     /** The _service statistic. */
248     // @Inject
249     private IStatisticService _serviceStatistic;
250     private ISubscriptionProductService _subscriptionProductService;
251 
252     /** The _product filter. */
253     private transient ProductFilter _productFilter;
254 
255     /**
256      * Instantiates a new show jsp bean.
257      */
258     public ShowJspBean( )
259     {
260         _productFilter = new ShowFilter( );
261         _serviceProduct = (IShowService) SpringContextService.getBean( BEAN_STOCK_TICKETS_SHOW_SERVICE );
262         _serviceOffer = (ISeanceService) SpringContextService.getBean( BEAN_STOCK_TICKETS_SEANCE_SERVICE );
263         _serviceProvider = SpringContextService.getContext( ).getBean( IProviderService.class );
264         _serviceCategory = SpringContextService.getContext( ).getBean( ICategoryService.class );
265         _serviceStatistic = SpringContextService.getContext( ).getBean( IStatisticService.class );
266         _subscriptionProductService = SpringContextService.getContext( ).getBean( ISubscriptionProductService.class );
267     }
268 
269     /**
270      * Generates a HTML form that displays all products.
271      *
272      * @param request
273      *            the Http request
274      * @return HTML
275      */
276     public String getManageProducts( HttpServletRequest request )
277     {
278         // String strToDate = getToday( );
279 
280         setPageTitleProperty( PAGE_TITLE_MANAGE_PRODUCT );
281 
282         ProductFilter filter = getProductFilter( request );
283         List<String> orderList = new ArrayList<String>( );
284         orderList.add( BilletterieConstants.NAME );
285         filter.setOrderAsc( true );
286         if ( filter.getOrders( ) != null && filter.getOrders( ).size( ) > 0 )
287         {
288             filter.setOrders( filter.getOrders( ) );
289         }
290         else
291         {
292             filter.setOrders( orderList );
293         }
294 
295         /*
296          * if ( filter.getDateFrom( ) == null ) { filter.setDateFrom( strToDate ); }
297          */
298 
299         DataTableManager<ShowDTO> dataTableToUse = getDataTable( request, filter );
300 
301         Map<String, Object> model = new HashMap<String, Object>( );
302 
303         model.put( MARK_DATA_TABLE_PRODUCT, dataTableToUse );
304 
305         // Fill the model
306         model.put( TicketsConstants.MARK_NB_ITEMS_PER_PAGE, String.valueOf( _nItemsPerPage ) );
307 
308         // Combo
309         ProviderFilter providerFilter = new ProviderFilter( );
310         providerFilter.setOrderAsc( true );
311         providerFilter.setOrders( orderList );
312 
313         ReferenceList providerComboList = ListUtils.toReferenceList( _serviceProvider.findByFilter( providerFilter, null ), BilletterieConstants.ID,
314                 BilletterieConstants.NAME, StockConstants.EMPTY_STRING );
315         CategoryFilter categoryFilter = new CategoryFilter( );
316         categoryFilter.setOrderAsc( true );
317         categoryFilter.setOrders( orderList );
318 
319         ReferenceList categoryComboList = ListUtils.toReferenceList( _serviceCategory.findByFilter( categoryFilter, null ), BilletterieConstants.ID,
320                 BilletterieConstants.NAME, StockConstants.EMPTY_STRING );
321         model.put( MARK_LIST_PROVIDERS, providerComboList );
322         model.put( MARK_LIST_CATEGORIES, categoryComboList );
323         // the filter
324         model.put( TicketsConstants.MARK_FILTER, filter );
325         model.put( MARK_LOCALE, getLocale( ) );
326         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_PRODUCTS, getLocale( ), model );
327 
328         // opération nécessaire pour eviter les fuites de mémoires
329         dataTableToUse.clearItems( );
330 
331         return getAdminPage( template.getHtml( ) );
332     }
333 
334     /**
335      * Get the DataTableManager object for the ShowDTO bean
336      * 
337      * @param request
338      *            the http request
339      * @param filter
340      *            the filter
341      * @return the data table to use
342      */
343     private DataTableManager<ShowDTO> getDataTable( HttpServletRequest request, ProductFilter filter )
344     {
345         // si un objet est déjà présent en session, on l'utilise
346         Method findMethod = null;
347 
348         try
349         {
350             findMethod = _serviceProduct.getClass( ).getMethod( PARAMETER_FIND_BY_FILTER_NAME_METHOD, ProductFilter.class, PaginationProperties.class );
351         }
352         catch( Exception e )
353         {
354             LOGGER.error( "Erreur lors de l'obtention du data table : ", e );
355         }
356 
357         DataTableManager<ShowDTO> dataTableToUse = getAbstractDataTableManager( request, filter, MARK_DATA_TABLE_PRODUCT, JSP_MANAGE_PRODUCTS, _serviceProduct,
358                 findMethod );
359 
360         // si pas d'objet en session, il faut ajouter les colonnes à afficher
361         if ( dataTableToUse.getListColumn( ).isEmpty( ) )
362         {
363             dataTableToUse.addColumn( "module.stock.billetterie.manage_product.filter.name", "name", true );
364             dataTableToUse.addColumn( "module.stock.billetterie.manage_product.filter.provider", "providerName", true );
365             dataTableToUse.addColumn( "module.stock.billetterie.manage_product.filter.category", "categoryName", true );
366             // dataTableToUse.addColumn("module.stock.billetterie.manage_product.filter.date_from", "startDate", true);
367             // dataTableToUse.addColumn("module.stock.billetterie.manage_product.filter.date_to", "endDate", true);
368             dataTableToUse.addFreeColumn( "module.stock.billetterie.manage_product.dates", MACRO_COLUMN_DATES_PRODUCT );
369             dataTableToUse.addFreeColumn( "module.stock.billetterie.manage_product.actionsLabel", MACRO_COLUMN_ACTIONS_PRODUCT );
370         }
371 
372         saveDataTableInSession( request, dataTableToUse, MARK_DATA_TABLE_PRODUCT );
373 
374         return dataTableToUse;
375     }
376 
377     private ReferenceList getContactComboList( int idProvider )
378     {
379         PartnerDTO findById = _serviceProvider.findById( idProvider );
380         ReferenceList contactComboList = ListUtils.toReferenceList( findById.getContactList( ), BilletterieConstants.ID, BilletterieConstants.NAME,
381                 StockConstants.EMPTY_STRING );
382         return contactComboList;
383     }
384 
385     /**
386      * Returns the form to modify a provider.
387      *
388      * @param request
389      *            The Http request
390      * @param strProductClassName
391      *            The class name of the provider entity to modify
392      * @return the html code of the provider form
393      */
394     @Transactional( readOnly = true )
395     public String getSaveProduct( HttpServletRequest request, String strProductClassName )
396     {
397         ShowDTO product = null;
398         Map<String, Object> model = new HashMap<String, Object>( );
399 
400         FunctionnalException fe = getErrorOnce( request );
401 
402         if ( fe != null )
403         {
404             product = (ShowDTO) fe.getBean( );
405             model.put( BilletterieConstants.ERROR, getHtmlError( fe ) );
406         }
407         else
408         {
409             String strProductId = request.getParameter( PARAMETER_CATEGORY_ID );
410             String strPartnerId = request.getParameter( PARAMETER_PRODUCT_PARTNER_ID );
411             String strCategoryId = request.getParameter( PARAMETER_PRODUCT_CATEGORY_ID );
412 
413             if ( strProductId != null )
414             {
415                 setPageTitleProperty( PAGE_TITLE_MODIFY_PRODUCT );
416 
417                 int nIdProduct = Integer.parseInt( strProductId );
418                 product = _serviceProduct.findById( nIdProduct );
419 
420                 int idProvider = product.getIdProvider( );
421 
422                 if ( request.getParameter( PARAMETER_REFRESH_CONTACT ) != null )
423                 {
424                     // case of wanting to get the contact which can be link to
425                     // the product
426                     populate( product, request );
427                     String strIdProvider = request.getParameter( PARAMETER_PRODUCT_ID_PROVIDER );
428                     int nIdSelectedProvider = Integer.valueOf( strIdProvider );
429 
430                     if ( nIdSelectedProvider >= 0 )
431                     {
432                         idProvider = nIdSelectedProvider;
433                     }
434 
435                 }
436 
437                 model.put( MARK_CONTACT_LIST, getContactComboList( idProvider ) );
438 
439                 _serviceProduct.correctProduct( product );
440             }
441             else
442             {
443                 setPageTitleProperty( PAGE_TITLE_CREATE_PRODUCT );
444                 product = new ShowDTO( );
445 
446                 // If creation and filter "Salle" exist, pre-select the partner
447                 if ( StringUtils.isNotBlank( strPartnerId ) )
448                 {
449                     product.setIdProvider( Integer.parseInt( strPartnerId ) );
450                 }
451                 else
452                 {
453                     populate( product, request );
454                 }
455 
456                 // If creation and filter "Categorie" exist, pre-select the
457                 // category
458                 if ( StringUtils.isNotBlank( strCategoryId ) )
459                 {
460                     product.setIdCategory( Integer.parseInt( strCategoryId ) );
461                 }
462 
463                 if ( ( request.getParameter( PARAMETER_PRODUCT_ID_PROVIDER ) != null ) && !request.getParameter( PARAMETER_PRODUCT_ID_PROVIDER ).equals( "-1" ) )
464                 {
465                     Integer idProvider = Integer.valueOf( request.getParameter( PARAMETER_PRODUCT_ID_PROVIDER ) );
466 
467                     model.put( MARK_CONTACT_LIST, getContactComboList( idProvider ) );
468                 }
469             }
470         }
471 
472         if ( request instanceof MultipartHttpServletRequest )
473         {
474             MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
475             FileItem fileItem = multipartRequest.getFile( PARAMETER_POSTER );
476 
477             if ( ( fileItem != null ) && ( fileItem.getSize( ) > 0 ) )
478             {
479                 // writePoster( request, product );
480                 product.setPosterName( fileItem.getName( ) );
481                 populate( product, request );
482             }
483         }
484 
485         // Combo
486         List<String> orderList = new ArrayList<String>( );
487         orderList.add( BilletterieConstants.NAME );
488 
489         ProviderFilter providerFilter = new ProviderFilter( );
490         providerFilter.setOrderAsc( true );
491         providerFilter.setOrders( orderList );
492 
493         ReferenceList providerComboList = ListUtils.toReferenceList( _serviceProvider.findByFilter( providerFilter, null ), BilletterieConstants.ID,
494                 BilletterieConstants.NAME, StockConstants.EMPTY_STRING );
495         CategoryFilter categoryFilter = new CategoryFilter( );
496         categoryFilter.setOrderAsc( true );
497         categoryFilter.setOrders( orderList );
498 
499         ReferenceList categoryComboList = ListUtils.toReferenceList( _serviceCategory.findByFilter( categoryFilter, null ), BilletterieConstants.ID,
500                 BilletterieConstants.NAME, StockConstants.EMPTY_STRING );
501         model.put( MARK_LIST_PROVIDERS, providerComboList );
502         model.put( MARK_LIST_CATEGORIES, categoryComboList );
503         model.put( MARK_PUBLIC_LIST, ListUtils.getPropertyList( PROPERTY_STOCK_BILLETTERIE_SHOW_PUBLIC ) );
504 
505         model.put( StockConstants.MARK_JSP_BACK, request.getParameter( StockConstants.MARK_JSP_BACK ) );
506         model.put( MARK_PRODUCT, product );
507         model.put( MARK_URL_POSTER, AppPropertiesService.getProperty( PROPERTY_RIMG_POSTER_PATH ) );
508 
509         // Richtext editor parameters
510         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
511         model.put( MARK_LOCALE, getLocale( ) );
512 
513         if ( ( product.getId( ) != null ) && ( product.getId( ) != 0 ) )
514         {
515             model.put( MARK_TITLE, I18nService.getLocalizedString( PAGE_TITLE_MODIFY_PRODUCT, Locale.getDefault( ) ) );
516         }
517         else
518         {
519             model.put( MARK_TITLE, I18nService.getLocalizedString( PAGE_TITLE_CREATE_PRODUCT, Locale.getDefault( ) ) );
520         }
521 
522         ExtendableResourcePluginActionManager.fillModel( request, getUser( ), model, String.valueOf( product.getId( ) ), ShowDTO.PROPERTY_RESOURCE_TYPE );
523 
524         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SAVE_PRODUCT, getLocale( ), model );
525 
526         return getAdminPage( template.getHtml( ) );
527     }
528 
529     /**
530      * Save a product.
531      *
532      * @param request
533      *            The HTTP request
534      * @return redirection url
535      */
536     public String doSaveProduct( HttpServletRequest request )
537     {
538         if ( null != request.getParameter( StockConstants.PARAMETER_BUTTON_CANCEL ) )
539         {
540             return doGoBack( request );
541         }
542 
543         String strUpdateDate = getToday( );
544 
545         ShowDTO product = new ShowDTO( );
546         populate( product, request );
547 
548         product.setUpdateDate( strUpdateDate );
549 
550         if ( StringUtils.isNotEmpty( request.getParameter( PARAMETER_A_LAFFICHE ) ) )
551         {
552             product.setAlaffiche( true );
553         }
554 
555         try
556         {
557             // Get and save the poster into temp files
558             File [ ] filePosterArray = writePoster( request, product );
559 
560             // Controls mandatory fields
561             validateBilletterie( product );
562 
563             // Persist entity
564             ShowDTO saveProduct = _serviceProduct.doSaveProduct( product, filePosterArray );
565 
566             // Statistic management
567             _serviceStatistic.doManageProductSaving( saveProduct );
568         }
569         catch( FunctionnalException e )
570         {
571             return manageFunctionnalException( request, e, JSP_SAVE_PRODUCT );
572         }
573 
574         return doGoBack( request );
575     }
576 
577     /**
578      * Get the poster in the request and write it on the disk.
579      *
580      * @param request
581      *            http request (multipart if contains poster)
582      * @param product
583      *            product entity
584      * @return the poster image file (0 : thumbnail, 1 : full size)
585      * @throws BusinessException
586      *             the business exception
587      */
588     private File [ ] writePoster( HttpServletRequest request, ShowDTO product ) throws BusinessException
589     {
590         File [ ] filePosterArray = null;
591         boolean fileGotten = false;
592 
593         // File uploaded
594         if ( request instanceof MultipartHttpServletRequest )
595         {
596             MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
597             FileItem fileItem = multipartRequest.getFile( PARAMETER_POSTER );
598 
599             if ( ( fileItem != null ) && ( fileItem.getSize( ) > 0 ) )
600             {
601                 InputStream fisPoster = null;
602                 InputStream realFisPoster = null;
603 
604                 try
605                 {
606                     fisPoster = fileItem.getInputStream( );
607                     realFisPoster = fileItem.getInputStream( );
608 
609                     // File fPoster = new File( new File( posterFolderPath ),
610                     // fileItem.getName( ) );
611                     File fPoster = File.createTempFile( FilenameUtils.getBaseName( fileItem.getName( ) ), null );
612                     File realPoster = File.createTempFile( FilenameUtils.getBaseName( fileItem.getName( ) ), null );
613 
614                     // Generate unique name
615                     fPoster = fr.paris.lutece.plugins.stock.utils.FileUtils.getUniqueFile( fPoster );
616                     realPoster = fr.paris.lutece.plugins.stock.utils.FileUtils.getUniqueFile( realPoster );
617 
618                     // Store poster picture
619                     fr.paris.lutece.plugins.stock.utils.FileUtils.writeInputStreamToFile( fisPoster, fPoster );
620                     fr.paris.lutece.plugins.stock.utils.FileUtils.writeInputStreamToFile( realFisPoster, realPoster );
621 
622                     File fPosterResize = ImageUtils.resizeImage( fPoster,
623                             AppPropertiesService.getPropertyInt( PROPERTY_POSTER_MAX_WIDTH, PROPERTY_POSTER_MAX_WIDTH_DEFAULT ),
624                             AppPropertiesService.getPropertyInt( PROPERTY_POSTER_MAX_HEIGHT, PROPERTY_POSTER_MAX_HEIGHT_DEFAULT ), null );
625 
626                     // Create a thumbnail image
627                     File fTbPoster = ImageUtils.createThumbnail( fPoster, AppPropertiesService.getPropertyInt( PROPERTY_POSTER_THUMB_WIDTH, 120 ),
628                             AppPropertiesService.getPropertyInt( PROPERTY_POSTER_THUMB_HEIGHT, 200 ) );
629 
630                     // Save file name into entity
631                     product.setPosterName( fPoster.getName( ) );
632                     fileGotten = true;
633 
634                     filePosterArray = new File [ ] {
635                             fTbPoster, fPosterResize, realPoster
636                     };
637                 }
638                 catch( IOException e )
639                 {
640                     throw new BusinessException( product, MESSAGE_ERROR_GET_AFFICHE );
641                 }
642                 finally
643                 {
644                     if ( fisPoster != null )
645                     {
646                         try
647                         {
648                             fisPoster.close( );
649                         }
650                         catch( IOException e )
651                         {
652                             AppLogService.error( e.getMessage( ), e );
653                         }
654                     }
655                 }
656             }
657         }
658 
659         if ( !fileGotten )
660         {
661             // Error mandatory poster
662             if ( StringUtils.isEmpty( product.getPosterName( ) ) )
663             {
664                 throw new BusinessException( product, MESSAGE_ERROR_MANDATORY_POSTER );
665             }
666         }
667 
668         return filePosterArray;
669     }
670 
671     /**
672      * Return the url of the JSP which called the last action.
673      *
674      * @param request
675      *            The Http request
676      * @return The url of the last JSP
677      */
678     private String doGoBack( HttpServletRequest request )
679     {
680         String strJspBack = request.getParameter( StockConstants.MARK_JSP_BACK );
681 
682         return StringUtils.isNotBlank( strJspBack ) ? ( AppPathService.getBaseUrl( request ) + strJspBack )
683                 : ( AppPathService.getBaseUrl( request ) + JSP_MANAGE_PRODUCTS );
684     }
685 
686     /**
687      * Gets the product filter.
688      *
689      * @param request
690      *            the request
691      * @return the product filter
692      */
693     private ProductFilter getProductFilter( HttpServletRequest request )
694     {
695         // SORT
696         String strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
697 
698         // "filter" in request ==> new filter. use old one otherwise
699         // if ( request.getParameter( TicketsConstants.PARAMETER_FILTER ) !=
700         // null )
701         // {
702         ProductFilter filter = new ShowFilter( );
703         populate( filter, request );
704 
705         if ( StringUtils.isNotEmpty( request.getParameter( PARAMETER_A_LAFFICHE ) ) )
706         {
707             filter.setAlaffiche( true );
708         }
709 
710         _productFilter = filter;
711 
712         // }
713         if ( strSortedAttributeName != null )
714         {
715             _productFilter.getOrders( ).add( strSortedAttributeName );
716 
717             String strAscSort = request.getParameter( Parameters.SORTED_ASC );
718             boolean bIsAscSort = Boolean.parseBoolean( strAscSort );
719             _productFilter.setOrderAsc( bIsAscSort );
720         }
721 
722         return _productFilter;
723     }
724 
725     /**
726      * Returns the confirmation message to delete a product.
727      *
728      * @param request
729      *            The Http request
730      * @return the html code message
731      */
732     public String getDeleteProduct( HttpServletRequest request )
733     {
734         String strProductId = request.getParameter( PARAMETER_CATEGORY_ID );
735 
736         int nIdProduct;
737 
738         try
739         {
740             nIdProduct = Integer.parseInt( strProductId );
741         }
742         catch( NumberFormatException e )
743         {
744             LOGGER.debug( e );
745 
746             return AdminMessageService.getMessageUrl( request, StockConstants.MESSAGE_ERROR_OCCUR, AdminMessage.TYPE_STOP );
747         }
748 
749         Map<String, Object> urlParam = new HashMap<String, Object>( );
750         urlParam.put( PARAMETER_CATEGORY_ID, nIdProduct );
751 
752         String strJspBack = request.getParameter( StockConstants.MARK_JSP_BACK );
753 
754         if ( StringUtils.isNotBlank( strJspBack ) )
755         {
756             urlParam.put( StockConstants.MARK_JSP_BACK, strJspBack );
757         }
758 
759         // Only product without seance can be delete
760         SeanceFilter filter = new SeanceFilter( );
761         filter.setProductId( nIdProduct );
762 
763         ResultList<SeanceDTO> bookingList = _serviceOffer.findByFilter( filter, null );
764 
765         if ( ( bookingList != null ) && !bookingList.isEmpty( ) )
766         {
767             return AdminMessageService.getMessageUrl( request, MESSAGE_DELETE_SHOW_WITH_SEANCE, AdminMessage.TYPE_STOP );
768         }
769 
770         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRMATION_DELETE_PRODUCT, CATEGORY_DO_DELETE_JSP, AdminMessage.TYPE_CONFIRMATION,
771                 urlParam );
772     }
773 
774     /**
775      * Delete a product.
776      *
777      * @param request
778      *            The Http request
779      * @return the html code message
780      */
781     public String doDeleteProduct( HttpServletRequest request )
782     {
783         String strProductId = request.getParameter( PARAMETER_CATEGORY_ID );
784 
785         int nIdProduct;
786 
787         try
788         {
789             nIdProduct = Integer.parseInt( strProductId );
790         }
791         catch( NumberFormatException e )
792         {
793             LOGGER.debug( e );
794 
795             return AdminMessageService.getMessageUrl( request, StockConstants.MESSAGE_ERROR_OCCUR, AdminMessage.TYPE_STOP );
796         }
797 
798         // On supprime les statistiques du spectacle
799         _serviceStatistic.doRemoveProductStatisticByIdProduct( nIdProduct );
800 
801         // delete the subscription link to the product
802         _subscriptionProductService.doDeleteByIdProduct( strProductId );
803 
804         _serviceProduct.doDeleteProduct( nIdProduct );
805 
806         // on supprime toutes référence à ce spectacle dans le plugin extends
807         ExtendableResourceRemovalListenerService.doRemoveResourceExtentions( ShowDTO.PROPERTY_RESOURCE_TYPE, Integer.toString( nIdProduct ) );
808 
809         return doGoBack( request );
810     }
811 
812     /**
813      * Return the today date.
814      * 
815      * @return the today date
816      */
817     public String getToday( )
818     {
819         String format = "dd/MM/yyyy";
820         SimpleDateFormat formater = new java.text.SimpleDateFormat( format );
821         Date date = new java.util.Date( );
822         return formater.format( date );
823     }
824 }