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.commons.ResultList;
38  import fr.paris.lutece.plugins.stock.commons.dao.PaginationProperties;
39  import fr.paris.lutece.plugins.stock.commons.exception.FunctionnalException;
40  import fr.paris.lutece.plugins.stock.modules.billetterie.utils.constants.BilletterieConstants;
41  import fr.paris.lutece.plugins.stock.modules.tickets.business.ShowCategoryDTO;
42  import fr.paris.lutece.plugins.stock.modules.tickets.business.ShowDTO;
43  import fr.paris.lutece.plugins.stock.modules.tickets.business.ShowFilter;
44  import fr.paris.lutece.plugins.stock.modules.tickets.service.IShowCategoryService;
45  import fr.paris.lutece.plugins.stock.modules.tickets.service.IShowService;
46  import fr.paris.lutece.plugins.stock.modules.tickets.utils.constants.TicketsConstants;
47  import fr.paris.lutece.plugins.stock.utils.constants.StockConstants;
48  import fr.paris.lutece.portal.service.i18n.I18nService;
49  import fr.paris.lutece.portal.service.message.AdminMessage;
50  import fr.paris.lutece.portal.service.message.AdminMessageService;
51  import fr.paris.lutece.portal.service.spring.SpringContextService;
52  import fr.paris.lutece.portal.service.template.AppTemplateService;
53  import fr.paris.lutece.portal.service.util.AppLogService;
54  import fr.paris.lutece.portal.service.util.AppPathService;
55  import fr.paris.lutece.portal.web.constants.Parameters;
56  import fr.paris.lutece.util.datatable.DataTableManager;
57  import fr.paris.lutece.util.html.HtmlTemplate;
58  
59  import org.apache.commons.lang.StringUtils;
60  
61  import org.apache.log4j.Logger;
62  
63  import java.lang.reflect.Method;
64  
65  import java.util.ArrayList;
66  import java.util.HashMap;
67  import java.util.List;
68  import java.util.Locale;
69  import java.util.Map;
70  
71  import javax.servlet.http.HttpServletRequest;
72  
73  /**
74   * The Class CategoryJspBean.
75   */
76  public class CategoryJspBean extends AbstractJspBean
77  {
78      public static final Logger LOGGER = Logger.getLogger( CategoryJspBean.class );
79  
80      /** The Constant PARAMETER_CATEGORY_ID. */
81      public static final String PARAMETER_CATEGORY_ID = "category_id";
82  
83      /** The Constant RIGHT_MANAGE_CATEGORIES. */
84      public static final String RIGHT_MANAGE_CATEGORIES = "CATEGORIES_MANAGEMENT";
85  
86      /** The Constant MARK_CATEGORY. */
87      public static final String MARK_CATEGORY = "category";
88  
89      /** The constants for DataTableManager */
90      public static final String MARK_DATA_TABLE_CATEGORY = "dataTableCategory";
91      public static final String MARK_FILTER_CATEGORY = "filterCategory";
92      public static final String MACRO_COLUMN_ACTIONS_CATEGORY = "columnActionsCategory";
93      public static final String MACRO_COLUMN_NAME_CATEGORY = "columnNameCategory";
94  
95      /** The Constant MARK_TITLE. */
96      public static final String MARK_TITLE = "title";
97  
98      /** The Constant PARAMETER_CATEGORY_TYPE_LIST. */
99      public static final String PARAMETER_CATEGORY_TYPE_LIST = "category_type_list";
100 
101     /** The Constant PARAMETER_CATEGORY_TYPE_LIST_DEFAULT. */
102     public static final String PARAMETER_CATEGORY_TYPE_LIST_DEFAULT = "category_type_list_default";
103 
104     /** The Constant PARAMETER_FILTER_NAME. */
105     private static final String PARAMETER_FILTER_NAME = "filter_name";
106 
107     /** The Constant BEAN_STOCK_TICKETS_SHOW_SERVICE. */
108     private static final String BEAN_STOCK_TICKETS_SHOW_SERVICE = "stock-tickets.showService";
109 
110     // I18N
111     /** The Constant PAGE_TITLE_MANAGE_CATEGORY. */
112     private static final String PAGE_TITLE_MANAGE_CATEGORY = "module.stock.billetterie.manage_category.title";
113 
114     /** The Constant PAGE_TITLE_CREATE_CATEGORY. */
115     private static final String PAGE_TITLE_CREATE_CATEGORY = "module.stock.billetterie.create_category.title";
116 
117     /** The Constant PAGE_TITLE_MODIFY_CATEGORY. */
118     private static final String PAGE_TITLE_MODIFY_CATEGORY = "module.stock.billetterie.save_category.title";
119 
120     // JSP
121     /** The Constant JSP_MANAGE_CATEGORYS. */
122     private static final String JSP_MANAGE_CATEGORYS = "jsp/admin/plugins/stock/modules/billetterie/ManageCategories.jsp";
123 
124     /** The Constant CATEGORY_DO_DELETE_JSP. */
125     private static final String CATEGORY_DO_DELETE_JSP = "jsp/admin/plugins/stock/modules/billetterie/DoDeleteCategory.jsp";
126 
127     /** The Constant JSP_SAVE_CATEGORY. */
128     private static final String JSP_SAVE_CATEGORY = "SaveCategory.jsp";
129 
130     // Templates
131     /** The Constant TEMPLATE_MANAGE_CATEGORIES. */
132     private static final String TEMPLATE_MANAGE_CATEGORIES = "admin/plugins/stock/modules/billetterie/manage_categories.html";
133 
134     /** The Constant TEMPLATE_SAVE_CATEGORY. */
135     private static final String TEMPLATE_SAVE_CATEGORY = "admin/plugins/stock/modules/billetterie/save_category.html";
136 
137     // MESSAGES
138     /** The Constant MESSAGE_CONFIRMATION_DELETE_CATEGORY. */
139     private static final String MESSAGE_CONFIRMATION_DELETE_CATEGORY = "module.stock.billetterie.message.deleteCategory.confirmation";
140 
141     /** The Constant MESSAGE_DELETE_CATEGORY_WITH_SHOW. */
142     private static final String MESSAGE_DELETE_CATEGORY_WITH_SHOW = "module.stock.billetterie.message.deleteCategory.with.show";
143 
144     // MEMBERS VARIABLES
145     /** The _service category. */
146     // @Inject
147     private IShowCategoryService _serviceCategory;
148 
149     /** The _category filter. */
150     private CategoryFilter _categoryFilter;
151 
152     /** The _service show. */
153     // @Inject
154     // @Named( "stock-tickets.showService" )
155     private IShowService _serviceShow;
156 
157     /**
158      * Instantiates a new category jsp bean.
159      */
160     public CategoryJspBean( )
161     {
162         // super( );
163         _categoryFilter = new CategoryFilter( );
164         _serviceCategory = SpringContextService.getContext( ).getBean( IShowCategoryService.class );
165         _serviceShow = (IShowService) SpringContextService.getBean( BEAN_STOCK_TICKETS_SHOW_SERVICE );
166     }
167 
168     /**
169      * Builds the filter.
170      *
171      * @param filter
172      *            the filter
173      * @param request
174      *            the request
175      */
176     protected void buildFilter( CategoryFilter filter, HttpServletRequest request )
177     {
178         filter.setName( request.getParameter( PARAMETER_FILTER_NAME ) );
179     }
180 
181     /**
182      * Generates a HTML form that displays all categorys.
183      *
184      * @param request
185      *            the Http request
186      * @return HTML
187      */
188     public String getManageCategories( HttpServletRequest request )
189     {
190         setPageTitleProperty( PAGE_TITLE_MANAGE_CATEGORY );
191 
192         CategoryFilter filter = getCategoryFilter( request );
193         List<String> orderList = new ArrayList<String>( );
194         orderList.add( BilletterieConstants.NAME );
195         filter.setOrders( orderList );
196         filter.setOrderAsc( true );
197 
198         // Fill the model
199         Map<String, Object> model = new HashMap<String, Object>( );
200 
201         // Obtention des objets sauvegardés en session
202         DataTableManager<ShowCategoryDTO> dataTableToUse = getDataTable( request, filter );
203         model.put( MARK_DATA_TABLE_CATEGORY, dataTableToUse );
204 
205         model.put( TicketsConstants.MARK_NB_ITEMS_PER_PAGE, String.valueOf( _nItemsPerPage ) );
206         model.put( TicketsConstants.MARK_FILTER, filter );
207 
208         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_CATEGORIES, getLocale( ), model );
209 
210         // opération nécessaire pour eviter les fuites de mémoires
211         dataTableToUse.clearItems( );
212 
213         return getAdminPage( template.getHtml( ) );
214     }
215 
216     /**
217      * Get the DataTableManager object for the ShowDTO bean
218      * 
219      * @param request
220      *            the http request
221      * @param filter
222      *            the filter
223      * @return the data table to use
224      */
225     private DataTableManager<ShowCategoryDTO> getDataTable( HttpServletRequest request, CategoryFilter filter )
226     {
227         // si un objet est déjà présent en session, on l'utilise
228         Method findMethod = null;
229 
230         try
231         {
232             findMethod = _serviceCategory.getClass( ).getMethod( PARAMETER_FIND_BY_FILTER_NAME_METHOD, CategoryFilter.class, PaginationProperties.class );
233         }
234         catch( Exception e )
235         {
236             LOGGER.error( "Erreur lors de l'obtention du data table : ", e );
237         }
238 
239         DataTableManager<ShowCategoryDTO> dataTableToUse = getAbstractDataTableManager( request, filter, MARK_DATA_TABLE_CATEGORY, JSP_MANAGE_CATEGORYS,
240                 _serviceCategory, findMethod );
241 
242         // si pas d'objet en session, il faut ajouter les colonnes à afficher
243         dataTableToUse.getListColumn( ).clear( );
244 
245         if ( dataTableToUse.getListColumn( ).isEmpty( ) )
246         {
247             dataTableToUse.addColumn( "module.stock.billetterie.manage_category.filter.name", "name", true );
248             dataTableToUse.addFreeColumn( "module.stock.billetterie.manage_category.actionsLabel", MACRO_COLUMN_ACTIONS_CATEGORY );
249         }
250 
251         saveDataTableInSession( request, dataTableToUse, MARK_DATA_TABLE_CATEGORY );
252 
253         return dataTableToUse;
254     }
255 
256     /**
257      * Returns the form to modify a provider.
258      *
259      * @param request
260      *            The Http request
261      * @param strCategoryClassName
262      *            The class name of the provider entity to modify
263      * @return the html code of the provider form
264      */
265     public String getSaveCategory( HttpServletRequest request, String strCategoryClassName )
266     {
267         ShowCategoryDTO category = null;
268         Map<String, Object> model = new HashMap<String, Object>( );
269 
270         FunctionnalException fe = getErrorOnce( request );
271 
272         if ( fe != null )
273         {
274             category = (ShowCategoryDTO) fe.getBean( );
275             model.put( BilletterieConstants.ERROR, getHtmlError( fe ) );
276         }
277         else
278         {
279             String strCategoryId = request.getParameter( PARAMETER_CATEGORY_ID );
280 
281             if ( strCategoryId != null )
282             {
283                 setPageTitleProperty( PAGE_TITLE_MODIFY_CATEGORY );
284 
285                 int nIdCategory = Integer.parseInt( strCategoryId );
286                 category = _serviceCategory.findById( nIdCategory );
287             }
288             else
289             {
290                 setPageTitleProperty( PAGE_TITLE_CREATE_CATEGORY );
291                 category = new ShowCategoryDTO( );
292             }
293         }
294 
295         model.put( StockConstants.MARK_JSP_BACK, JSP_MANAGE_CATEGORYS );
296         model.put( MARK_CATEGORY, category );
297 
298         if ( ( category.getId( ) != null ) && ( category.getId( ) != 0 ) )
299         {
300             model.put( MARK_TITLE, I18nService.getLocalizedString( PAGE_TITLE_MODIFY_CATEGORY, Locale.getDefault( ) ) );
301         }
302         else
303         {
304             model.put( MARK_TITLE, I18nService.getLocalizedString( PAGE_TITLE_CREATE_CATEGORY, Locale.getDefault( ) ) );
305         }
306 
307         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SAVE_CATEGORY, getLocale( ), model );
308 
309         return getAdminPage( template.getHtml( ) );
310     }
311 
312     /**
313      * Save a category.
314      *
315      * @param request
316      *            The HTTP request
317      * @return redirection url
318      */
319     public String doSaveCategory( HttpServletRequest request )
320     {
321         if ( null != request.getParameter( StockConstants.PARAMETER_BUTTON_CANCEL ) )
322         {
323             return doGoBack( request );
324         }
325 
326         ShowCategoryDTO category = new ShowCategoryDTO( );
327         populate( category, request );
328 
329         try
330         {
331             // Controls mandatory fields
332             validateBilletterie( category );
333             _serviceCategory.doSaveCategory( category );
334         }
335         catch( FunctionnalException e )
336         {
337             return manageFunctionnalException( request, e, JSP_SAVE_CATEGORY );
338         }
339 
340         return doGoBack( request );
341     }
342 
343     /**
344      * Return the url of the JSP which called the last action.
345      *
346      * @param request
347      *            The Http request
348      * @return The url of the last JSP
349      */
350     private String doGoBack( HttpServletRequest request )
351     {
352         String strJspBack = request.getParameter( StockConstants.MARK_JSP_BACK );
353 
354         return StringUtils.isNotBlank( strJspBack ) ? ( AppPathService.getBaseUrl( request ) + strJspBack )
355                 : ( AppPathService.getBaseUrl( request ) + JSP_MANAGE_CATEGORYS );
356     }
357 
358     /**
359      * Gets the category filter.
360      *
361      * @param request
362      *            the request
363      * @return the category filter
364      */
365     private CategoryFilter getCategoryFilter( HttpServletRequest request )
366     {
367         // SORT
368         String strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
369 
370         CategoryFilter filter = new CategoryFilter( );
371         buildFilter( filter, request );
372         _categoryFilter = filter;
373 
374         if ( strSortedAttributeName != null )
375         {
376             _categoryFilter.getOrders( ).add( strSortedAttributeName );
377 
378             String strAscSort = request.getParameter( Parameters.SORTED_ASC );
379             boolean bIsAscSort = Boolean.parseBoolean( strAscSort );
380             _categoryFilter.setOrderAsc( bIsAscSort );
381         }
382 
383         return _categoryFilter;
384     }
385 
386     /**
387      * Returns the confirmation message to delete a category.
388      *
389      * @param request
390      *            The Http request
391      * @return the html code message
392      */
393     public String getDeleteCategory( HttpServletRequest request )
394     {
395         String strCategoryId = request.getParameter( PARAMETER_CATEGORY_ID );
396 
397         int nIdCategory;
398 
399         try
400         {
401             nIdCategory = Integer.parseInt( strCategoryId );
402         }
403         catch( NumberFormatException e )
404         {
405             AppLogService.debug( e );
406 
407             return AdminMessageService.getMessageUrl( request, StockConstants.MESSAGE_ERROR_OCCUR, AdminMessage.TYPE_STOP );
408         }
409 
410         Map<String, Object> urlParam = new HashMap<String, Object>( );
411         urlParam.put( PARAMETER_CATEGORY_ID, nIdCategory );
412 
413         String strJspBack = request.getParameter( StockConstants.MARK_JSP_BACK );
414 
415         if ( StringUtils.isNotBlank( strJspBack ) )
416         {
417             urlParam.put( StockConstants.MARK_JSP_BACK, strJspBack );
418         }
419 
420         // BO-CU02-E02-RGE01 : Aucun spectacle ne doit être rattaché à la
421         // catégorie sélectionnée
422         ShowFilter filter = new ShowFilter( );
423         filter.setIdCategory( nIdCategory );
424 
425         ResultList<ShowDTO> bookingList = this._serviceShow.findByFilter( filter, null );
426 
427         if ( ( bookingList != null ) && !bookingList.isEmpty( ) )
428         {
429             return AdminMessageService.getMessageUrl( request, MESSAGE_DELETE_CATEGORY_WITH_SHOW, AdminMessage.TYPE_STOP );
430         }
431 
432         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRMATION_DELETE_CATEGORY, CATEGORY_DO_DELETE_JSP, AdminMessage.TYPE_CONFIRMATION,
433                 urlParam );
434     }
435 
436     /**
437      * Delete a category.
438      *
439      * @param request
440      *            The Http request
441      * @return the html code message
442      */
443     public String doDeleteCategory( HttpServletRequest request )
444     {
445         String strCategoryId = request.getParameter( PARAMETER_CATEGORY_ID );
446 
447         int nIdCategory;
448 
449         try
450         {
451             nIdCategory = Integer.parseInt( strCategoryId );
452         }
453         catch( NumberFormatException e )
454         {
455             AppLogService.debug( e );
456 
457             return AdminMessageService.getMessageUrl( request, StockConstants.MESSAGE_ERROR_OCCUR, AdminMessage.TYPE_STOP );
458         }
459 
460         _serviceCategory.doDeleteCategory( nIdCategory );
461 
462         return doGoBack( request );
463     }
464 }