View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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  package fr.paris.lutece.plugins.announce.web;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Map;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  import org.apache.commons.lang.StringUtils;
44  
45  import fr.paris.lutece.plugins.announce.business.Category;
46  import fr.paris.lutece.plugins.announce.business.CategoryHome;
47  import fr.paris.lutece.plugins.announce.business.SectorHome;
48  import fr.paris.lutece.plugins.announce.service.AnnounceService;
49  import fr.paris.lutece.plugins.announce.service.CategoryResourceIdService;
50  import fr.paris.lutece.plugins.announce.service.EntryTypeService;
51  import fr.paris.lutece.plugins.announce.utils.AnnounceUtils;
52  import fr.paris.lutece.plugins.genericattributes.business.Entry;
53  import fr.paris.lutece.plugins.genericattributes.business.EntryFilter;
54  import fr.paris.lutece.plugins.genericattributes.business.EntryHome;
55  import fr.paris.lutece.portal.business.rbac.RBAC;
56  import fr.paris.lutece.portal.business.user.AdminUser;
57  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
58  import fr.paris.lutece.portal.service.captcha.CaptchaSecurityService;
59  import fr.paris.lutece.portal.service.i18n.I18nService;
60  import fr.paris.lutece.portal.service.mailinglist.AdminMailingListService;
61  import fr.paris.lutece.portal.service.message.AdminMessage;
62  import fr.paris.lutece.portal.service.message.AdminMessageService;
63  import fr.paris.lutece.portal.service.plugin.Plugin;
64  import fr.paris.lutece.portal.service.rbac.RBACService;
65  import fr.paris.lutece.portal.service.spring.SpringContextService;
66  import fr.paris.lutece.portal.service.template.AppTemplateService;
67  import fr.paris.lutece.portal.service.util.AppPathService;
68  import fr.paris.lutece.portal.service.util.AppPropertiesService;
69  import fr.paris.lutece.portal.service.workflow.WorkflowService;
70  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
71  import fr.paris.lutece.portal.web.constants.Messages;
72  import fr.paris.lutece.util.ReferenceList;
73  import fr.paris.lutece.util.html.AbstractPaginator;
74  import fr.paris.lutece.util.html.HtmlTemplate;
75  import fr.paris.lutece.util.html.Paginator;
76  import fr.paris.lutece.util.url.UrlItem;
77  
78  /**
79   * This class provides the user interface to manage category features ( manage, create, modify, remove )
80   */
81  public class CategoryJspBean extends PluginAdminPageJspBean
82  {
83      /**
84       * Right to manage this feature
85       */
86      public static final String RIGHT_MANAGE_ANNOUNCE = "ANNOUNCE_MANAGEMENT";
87      private static final long serialVersionUID = 5116073608003926398L;
88  
89      /* Misc */
90      private static final String REGEX_ID = "^[\\d]+$";
91      private static final String ANCHOR_NAME = "entry_list";
92      private static final String UNAUTHORIZED = "Unauthorized";
93  
94      // Parameters
95      private static final String PARAMETER_PAGE_INDEX = "page_index";
96      private static final String PARAMETER_CATEGORY_ID = "category_id";
97      private static final String PARAMETER_CATEGORY_LABEL = "category_label";
98      private static final String PARAMETER_CATEGORY_SECTOR_ID = "category_sector_id";
99      private static final String PARAMETER_CATEGORY_ANNOUNCES_VALIDATION = "category_announces_validation";
100     private static final String PARAMETER_DISPLAY_PRICE = "display_price";
101     private static final String PARAMETER_MAILING_LIST_ID = "mailing_list_id";
102     private static final String PARAMETER_ID_WORKFLOW = "id_workflow";
103     private static final String PARAMETER_DISPLAY_CAPTCHA = "display_captcha";
104     private static final String PARAMETER_PRICE_MANDATORY = "price_mandatory";
105     /* properties */
106     private static final String PROPERTY_PAGE_TITLE_MANAGE_CATEGORIES = "announce.manage_categories.pageTitle";
107     private static final String PROPERTY_PAGE_TITLE_CREATE_CATEGORY = "announce.create_category.pageTitle";
108     private static final String PROPERTY_PAGE_TITLE_MODIFY_CATEGORY = "announce.modify_category.pageTitle";
109     private static final String PROPERTY_DEFAULT_LIST_CATEGORY_PER_PAGE = "announce.category.itemsPerPage";
110     private static final String PROPERTY_GLOBAL_PARAMETER = "announce.globalParameter";
111     private static final String PROPERTY_CREATE_CATEGORY_YES = "announce.create_category_yes";
112     private static final String PROPERTY_CREATE_CATEGORY_NO = "announce.create_category_no";
113     private static final String PROPERTY_MODIFY_CATEGORY_YES = "announce.modify_category_yes";
114     private static final String PROPERTY_MODIFY_CATEGORY_NO = "announce.modify_category_no";
115     private static final String PROPERTY_PAGE_TITLE_VIEW_FORM_EXAMPLE = "announce.view_form_example.pageTitle";
116     private static final String PROPERTY_NOTHING = "announce.mailing_list.nothing";
117 
118     /* templates */
119     private static final String TEMPLATE_MANAGE_CATEGORIES = "admin/plugins/announce/manage_categories.html";
120     private static final String TEMPLATE_CREATE_CATEGORY = "admin/plugins/announce/create_category.html";
121     private static final String TEMPLATE_MODIFY_CATEGORY = "admin/plugins/announce/modify_category.html";
122 
123     /* Jsp Definition */
124     private static final String JSP_DO_REMOVE_CATEGORY = "jsp/admin/plugins/announce/DoRemoveCategory.jsp";
125     private static final String JSP_MANAGE_CATEGORIES = "jsp/admin/plugins/announce/ManageCategories.jsp";
126     private static final String JSP_URL_MODIFY = "ModifyCategory.jsp";
127 
128     /* Jsp Redirect */
129     private static final String JSP_REDIRECT_TO_MANAGE_CATEGORIES = "ManageCategories.jsp";
130     private static final String JSP_MODIFY_CATEGORY = "jsp/admin/plugins/announce/ModifyCategory.jsp";
131 
132     /* Messages */
133     private static final String MESSAGE_CONFIRM_REMOVE_CATEGORY = "announce.message.confirmRemoveCategory";
134     private static final String MESSAGE_PLEASE_REMOVE_ANNOUCES = "announce.message.pleaseRemoveAnnounces";
135     private static final String MESSAGE_PLEASE_REMOVE_ENTRIES = "announce.message.pleaseRemoveEntries";
136     private static final String MESSAGE_COPY_TITLE = "announce.message.copy_title";
137 
138     /* Markers */
139     private static final String MARK_CATEGORY = "category";
140     private static final String MARK_LIST_CATEGORIES = "list_categories";
141     private static final String MARK_PAGINATOR = "paginator";
142     private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
143     private static final String MARK_LIST_FIELDS = "list_sectors";
144     private static final String MARK_LIST_CAT_DUP = "list_categories_dup";
145     private static final String MARK_LIST_ANNOUNCES_VALIDATION = "list_announces_validation";
146     private static final String MARK_ENTRY_LIST = "entry_list";
147     private static final String MARK_ENTRY_TYPE_LIST = "entry_type_list";
148     private static final String MARK_PLUGIN = "plugin";
149     private static final String MARK_MAILING_LIST_LIST = "mailing_list_list";
150     private static final String MARK_GROUP_ENTRY_LIST = "entry_group_list";
151     private static final String MARK_LIST_ORDER_FIRST_LEVEL = "listOrderFirstLevel";
152     private static final String MARK_LIST_WORKFLOWS = "listWorkflows";
153     private static final String MARK_IS_CAPTCHA_ENABLED = "isCaptchaEnabled";
154     private static final CaptchaSecurityService _captchaSecurityService = new CaptchaSecurityService( );
155 
156     /* Variables */
157     private AnnounceService _announceService = SpringContextService.getBean( AnnounceService.BEAN_NAME );
158     private String _strCurrentPageIndex;
159     private int _nItemsPerPage;
160 
161     /**
162      * {@inheritDoc}
163      */
164     @Override
165     public Plugin getPlugin( )
166     {
167         Plugin plugin = super.getPlugin( );
168 
169         if ( plugin == null )
170         {
171             plugin = AnnounceUtils.getPlugin( );
172         }
173 
174         return plugin;
175     }
176 
177     /**
178      * Returns the list of category
179      * 
180      * @param request
181      *            The HTTP request
182      * @return the categories list
183      */
184     public String getManageCategories( HttpServletRequest request )
185     {
186         setPageTitleProperty( PROPERTY_PAGE_TITLE_MANAGE_CATEGORIES );
187 
188         _strCurrentPageIndex = AbstractPaginator.getPageIndex( request, AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
189         int defaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_CATEGORY_PER_PAGE, 50 );
190         _nItemsPerPage = AbstractPaginator.getItemsPerPage( request, AbstractPaginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, defaultItemsPerPage );
191 
192         List<Category> listCategories = CategoryHome.findAll( );
193 
194         Paginator<Category> paginator = new Paginator<>( listCategories, _nItemsPerPage, getUrlPage( ), PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
195 
196         Map<String, Object> model = new HashMap<>( );
197 
198         model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
199         model.put( MARK_PAGINATOR, paginator );
200         model.put( MARK_LIST_CATEGORIES, paginator.getPageItems( ) );
201 
202         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_CATEGORIES, getLocale( ), model );
203 
204         return getAdminPage( templateList.getHtml( ) );
205     }
206 
207     /**
208      * Returns the form to create a category
209      * 
210      * @return the HTML code of the category form
211      * @param request
212      *            The HTTP request
213      * @throws fr.paris.lutece.portal.service.admin.AccessDeniedException
214      *             access denied exception
215      */
216     public String getCreateCategory( HttpServletRequest request ) throws AccessDeniedException
217     {
218         if ( !RBACService.isAuthorized( Category.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, CategoryResourceIdService.PERMISSION_CREATE, getUser( ) ) )
219         {
220             throw new AccessDeniedException( UNAUTHORIZED );
221         }
222 
223         setPageTitleProperty( PROPERTY_PAGE_TITLE_CREATE_CATEGORY );
224 
225         ReferenceList refMailingList = new ReferenceList( );
226         AdminUser adminUser = getUser( );
227         String strNothing = I18nService.getLocalizedString( PROPERTY_NOTHING, request.getLocale( ) );
228         refMailingList.addItem( -1, strNothing );
229         refMailingList.addAll( AdminMailingListService.getMailingLists( adminUser ) );
230 
231         ReferenceList listSectors = SectorHome.findLocaleReferenceList( request.getLocale( ) );
232 
233         ReferenceList listCatDup = CategoryHome.findCategoriesReferenceList( );
234 
235         ReferenceList listAnnouncesValidation = new ReferenceList( );
236         listAnnouncesValidation.addItem( 0, I18nService.getLocalizedString( PROPERTY_GLOBAL_PARAMETER, request.getLocale( ) ) );
237         listAnnouncesValidation.addItem( 1, I18nService.getLocalizedString( PROPERTY_CREATE_CATEGORY_YES, request.getLocale( ) ) );
238         listAnnouncesValidation.addItem( 2, I18nService.getLocalizedString( PROPERTY_CREATE_CATEGORY_NO, request.getLocale( ) ) );
239 
240         HashMap<String, Object> model = new HashMap<>( );
241         model.put( MARK_LIST_FIELDS, listSectors );
242 
243         model.put( MARK_LIST_CAT_DUP, listCatDup );
244 
245         model.put( MARK_MAILING_LIST_LIST, refMailingList );
246         model.put( MARK_LIST_ANNOUNCES_VALIDATION, listAnnouncesValidation );
247         model.put( MARK_LIST_WORKFLOWS, WorkflowService.getInstance( ).getWorkflowsEnabled( getUser( ), getLocale( ) ) );
248         model.put( MARK_IS_CAPTCHA_ENABLED, _captchaSecurityService.isAvailable( ) );
249 
250         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_CATEGORY, getLocale( ), model );
251 
252         return getAdminPage( template.getHtml( ) );
253     }
254 
255     /**
256      * Process the data capture form of a new category
257      * 
258      * @return The JSP URL of the process result
259      * @param request
260      *            The HTTP Request
261      * @throws fr.paris.lutece.portal.service.admin.AccessDeniedException
262      *             access denied exception
263      */
264     public String doCreateCategory( HttpServletRequest request ) throws AccessDeniedException
265     {
266         if ( !RBACService.isAuthorized( Category.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, CategoryResourceIdService.PERMISSION_CREATE, getUser( ) ) )
267         {
268             throw new AccessDeniedException( UNAUTHORIZED );
269         }
270 
271         String strCategoryLabel = request.getParameter( PARAMETER_CATEGORY_LABEL );
272         int nIdSector = Integer.parseInt( request.getParameter( PARAMETER_CATEGORY_SECTOR_ID ) );
273         int nAnnouncesValidation = Integer.parseInt( request.getParameter( PARAMETER_CATEGORY_ANNOUNCES_VALIDATION ) );
274         String strDisplayPrice = request.getParameter( PARAMETER_DISPLAY_PRICE );
275         int nIdMailingList = Integer.parseInt( request.getParameter( PARAMETER_MAILING_LIST_ID ) );
276         int nIdWorkflow = Integer.parseInt( request.getParameter( PARAMETER_ID_WORKFLOW ) != null ? request.getParameter( PARAMETER_ID_WORKFLOW ) : "-1" );
277         boolean bDisplayCaptcha = Boolean.parseBoolean( request.getParameter( PARAMETER_DISPLAY_CAPTCHA ) );
278         boolean bPriceMandatory = Boolean.parseBoolean( request.getParameter( PARAMETER_PRICE_MANDATORY ) );
279 
280         // Mandatory sectors
281         if ( ( nIdSector == 0 ) || StringUtils.isEmpty( strCategoryLabel ) )
282         {
283             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
284         }
285 
286         Categorynounce/business/Category.html#Category">Category category = new Category( );
287         category.setLabel( strCategoryLabel );
288         category.setIdSector( nIdSector );
289         category.setIdMailingList( nIdMailingList );
290         category.setAnnouncesValidation( nAnnouncesValidation );
291         category.setIdWorkflow( nIdWorkflow );
292         category.setDisplayPrice( strDisplayPrice != null );
293 
294         category.setPriceMandatory( category.getDisplayPrice( ) && bPriceMandatory );
295         category.setDisplayCaptcha( bDisplayCaptcha );
296 
297         CategoryHome.create( category );
298 
299         // if the operation occurred well, redirects towards the list
300         return JSP_REDIRECT_TO_MANAGE_CATEGORIES;
301     }
302 
303     /**
304      * Returns the form to update info about a category
305      * 
306      * @return The HTML form to update info
307      * @param request
308      *            The HTTP request
309      * @throws fr.paris.lutece.portal.service.admin.AccessDeniedException
310      *             access denied exception
311      */
312     public String getModifyCategory( HttpServletRequest request ) throws AccessDeniedException
313     {
314         setPageTitleProperty( PROPERTY_PAGE_TITLE_MODIFY_CATEGORY );
315 
316         Category category = getAuthorizedCategory( request, CategoryResourceIdService.PERMISSION_MODIFY );
317 
318         ReferenceList listSectors = SectorHome.findLocaleReferenceList( request.getLocale( ) );
319 
320         ReferenceList refMailingList = new ReferenceList( );
321         AdminUser adminUser = getUser( );
322         String strNothing = I18nService.getLocalizedString( PROPERTY_NOTHING, request.getLocale( ) );
323         refMailingList.addItem( -1, strNothing );
324         refMailingList.addAll( AdminMailingListService.getMailingLists( adminUser ) );
325 
326         ReferenceList listAnnouncesValidation = new ReferenceList( );
327         listAnnouncesValidation.addItem( 0, I18nService.getLocalizedString( PROPERTY_GLOBAL_PARAMETER, request.getLocale( ) ) );
328         listAnnouncesValidation.addItem( 1, I18nService.getLocalizedString( PROPERTY_MODIFY_CATEGORY_YES, request.getLocale( ) ) );
329         listAnnouncesValidation.addItem( 2, I18nService.getLocalizedString( PROPERTY_MODIFY_CATEGORY_NO, request.getLocale( ) ) );
330 
331         EntryFilter entryFilter = new EntryFilter( );
332         entryFilter.setIdResource( category.getId( ) );
333         entryFilter.setResourceType( Category.RESOURCE_TYPE );
334         entryFilter.setEntryParentNull( EntryFilter.FILTER_TRUE );
335         entryFilter.setFieldDependNull( EntryFilter.FILTER_TRUE );
336 
337         List<Entry> listEntryFirstLevel = EntryHome.getEntryList( entryFilter );
338         List<Entry> listEntry = new ArrayList<>( listEntryFirstLevel.size( ) );
339 
340         List<Integer> listOrderFirstLevel = new ArrayList<>( listEntryFirstLevel.size( ) );
341 
342         entryFilter = new EntryFilter( );
343         entryFilter.setIdResource( category.getId( ) );
344         entryFilter.setResourceType( Category.RESOURCE_TYPE );
345         entryFilter.setFieldDependNull( EntryFilter.FILTER_TRUE );
346 
347         for ( Entry entry : listEntryFirstLevel )
348         {
349             listEntry.add( entry );
350             // If the entry is a group, we add entries associated with this group
351             listOrderFirstLevel.add( listEntry.size( ) );
352 
353             if ( Boolean.TRUE.equals( entry.getEntryType( ).getGroup( ) ) )
354             {
355                 entryFilter.setIdEntryParent( entry.getIdEntry( ) );
356 
357                 List<Entry> listEntryGroup = EntryHome.getEntryList( entryFilter );
358                 entry.setChildren( listEntryGroup );
359                 listEntry.addAll( listEntryGroup );
360             }
361         }
362 
363         Map<String, Object> model = new HashMap<>( );
364         model.put( MARK_GROUP_ENTRY_LIST, getRefListGroups( category.getId( ) ) );
365         model.put( MARK_ENTRY_TYPE_LIST, EntryTypeService.getInstance( ).getEntryTypeReferenceList( ) );
366         model.put( MARK_ENTRY_LIST, listEntry );
367         model.put( MARK_LIST_ORDER_FIRST_LEVEL, listOrderFirstLevel );
368         model.put( MARK_LIST_WORKFLOWS, WorkflowService.getInstance( ).getWorkflowsEnabled( getUser( ), getLocale( ) ) );
369         model.put( MARK_IS_CAPTCHA_ENABLED, _captchaSecurityService.isAvailable( ) );
370 
371         UrlItem url = new UrlItem( JSP_URL_MODIFY );
372         url.addParameter( PARAMETER_CATEGORY_ID, category.getId( ) );
373 
374         model.put( MARK_CATEGORY, category );
375         model.put( MARK_NB_ITEMS_PER_PAGE, Integer.toString( _nItemsPerPage ) );
376         model.put( MARK_LIST_FIELDS, listSectors );
377         model.put( MARK_MAILING_LIST_LIST, refMailingList );
378         model.put( MARK_LIST_ANNOUNCES_VALIDATION, listAnnouncesValidation );
379         model.put( MARK_PLUGIN, getPlugin( ) );
380 
381         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_CATEGORY, getLocale( ), model );
382 
383         return getAdminPage( template.getHtml( ) );
384     }
385 
386     /**
387      * Process the change form of a category
388      * 
389      * @return The JSP URL of the process result
390      * @param request
391      *            The HTTP request
392      * @throws fr.paris.lutece.portal.service.admin.AccessDeniedException
393      *             access denied exception
394      */
395     public String doModifyCategory( HttpServletRequest request ) throws AccessDeniedException
396     {
397         String strCategoryLabel = request.getParameter( PARAMETER_CATEGORY_LABEL );
398         int nIdSector = Integer.parseInt( request.getParameter( PARAMETER_CATEGORY_SECTOR_ID ) );
399         int nAnnouncesValidation = Integer.parseInt( request.getParameter( PARAMETER_CATEGORY_ANNOUNCES_VALIDATION ) );
400         String strDisplayPrice = request.getParameter( PARAMETER_DISPLAY_PRICE );
401         int nIdMailingList = Integer.parseInt( request.getParameter( PARAMETER_MAILING_LIST_ID ) );
402         int nIdWorkflow = Integer.parseInt( request.getParameter( PARAMETER_ID_WORKFLOW ) != null ? request.getParameter( PARAMETER_ID_WORKFLOW ) : "-1" );
403         boolean bDisplayCaptcha = Boolean.parseBoolean( request.getParameter( PARAMETER_DISPLAY_CAPTCHA ) );
404         boolean bPriceMandatory = Boolean.parseBoolean( request.getParameter( PARAMETER_PRICE_MANDATORY ) );
405 
406         // Mandatory categories
407         if ( StringUtils.isEmpty( strCategoryLabel ) || ( nIdSector == 0 ) )
408         {
409             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
410         }
411 
412         Category category = getAuthorizedCategory( request, CategoryResourceIdService.PERMISSION_MODIFY );
413         category.setLabel( strCategoryLabel );
414         category.setIdSector( nIdSector );
415         category.setAnnouncesValidation( nAnnouncesValidation );
416         category.setIdMailingList( nIdMailingList );
417         category.setIdWorkflow( nIdWorkflow );
418         category.setDisplayCaptcha( bDisplayCaptcha );
419         category.setDisplayPrice( strDisplayPrice != null );
420 
421         category.setPriceMandatory( category.getDisplayPrice( ) && bPriceMandatory );
422 
423         CategoryHome.update( category );
424 
425         // if the operation occurred well, redirects towards the list
426         return JSP_REDIRECT_TO_MANAGE_CATEGORIES;
427     }
428 
429     /**
430      * Manages the removal form of a category whose identifier is in the http request
431      * 
432      * @return the HTML code to confirm
433      * @param request
434      *            The HTTP request
435      * @throws fr.paris.lutece.portal.service.admin.AccessDeniedException
436      *             access denied exception
437      */
438     public String getConfirmRemoveCategory( HttpServletRequest request ) throws AccessDeniedException
439     {
440         int nIdCategory = Integer.parseInt( request.getParameter( PARAMETER_CATEGORY_ID ) );
441         Category category = getAuthorizedCategory( request, CategoryResourceIdService.PERMISSION_DELETE );
442 
443         if ( ( category.getNumberAnnounces( ) == 0 ) && ( CategoryHome.countEntriesForCategory( category ) == 0 ) )
444         {
445             UrlItem url = new UrlItem( JSP_DO_REMOVE_CATEGORY );
446             url.addParameter( PARAMETER_CATEGORY_ID, nIdCategory );
447 
448             return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_CATEGORY, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
449         }
450 
451         if ( category.getNumberAnnounces( ) != 0 )
452         {
453             return AdminMessageService.getMessageUrl( request, MESSAGE_PLEASE_REMOVE_ANNOUCES, AdminMessage.TYPE_STOP );
454         }
455 
456         if ( CategoryHome.countEntriesForCategory( category ) != 0 )
457         {
458             return AdminMessageService.getMessageUrl( request, MESSAGE_PLEASE_REMOVE_ENTRIES, AdminMessage.TYPE_STOP );
459         }
460 
461         return null;
462     }
463 
464     /**
465      * Treats the removal form of a category
466      * 
467      * @return the JSP URL to display the form to manage categories
468      * @param request
469      *            The HTTP request
470      * @throws fr.paris.lutece.portal.service.admin.AccessDeniedException
471      *             access denied exception
472      */
473     public String doRemoveCategory( HttpServletRequest request ) throws AccessDeniedException
474     {
475         Category category = getAuthorizedCategory( request, CategoryResourceIdService.PERMISSION_DELETE );
476         CategoryHome.remove( category );
477 
478         // TODO : remove entries, responses, fields, etc...
479 
480         // if the operation occurred well, redirects towards the list
481         return JSP_REDIRECT_TO_MANAGE_CATEGORIES;
482     }
483 
484     /**
485      * gets the category form example
486      * 
487      * @param request
488      *            the httpRequest
489      * @return String of template for form example
490      */
491     public String getExampleForm( HttpServletRequest request )
492     {
493         setPageTitleProperty( PROPERTY_PAGE_TITLE_VIEW_FORM_EXAMPLE );
494 
495         int nIdCategory = Integer.parseInt( request.getParameter( PARAMETER_CATEGORY_ID ) );
496         Category category = CategoryHome.findByPrimaryKey( nIdCategory );
497 
498         return getAdminPage( _announceService.getHtmlAnnounceForm( null, category, getLocale( ), false, request ) );
499     }
500 
501     /**
502      * Return UrlPage URL
503      * 
504      * @return URL
505      */
506     private String getUrlPage( )
507     {
508         UrlItem url = new UrlItem( JSP_MANAGE_CATEGORIES );
509 
510         return url.getUrl( );
511     }
512 
513     /**
514      * Get the authorized category
515      *
516      * @param request
517      *            The {@link HttpServletRequest}
518      * @param strPermissionType
519      *            The type of permission (see {@link CategoryResourceIdService} class)
520      * @return The category or null if user have no access
521      * @throws AccessDeniedException
522      *             If the user is not authorized to access this feature
523      */
524     private Category getAuthorizedCategory( HttpServletRequest request, String strPermissionType ) throws AccessDeniedException
525     {
526         String strIdCategory = request.getParameter( PARAMETER_CATEGORY_ID );
527 
528         if ( ( strIdCategory == null ) || !strIdCategory.matches( REGEX_ID ) )
529         {
530             throw new AccessDeniedException( UNAUTHORIZED );
531         }
532 
533         int nIdCategory = Integer.parseInt( strIdCategory );
534         Category category = CategoryHome.findByPrimaryKey( nIdCategory );
535 
536         if ( ( category == null ) || !RBACService.isAuthorized( Category.RESOURCE_TYPE, String.valueOf( category.getId( ) ), strPermissionType, getUser( ) ) )
537         {
538             throw new AccessDeniedException( UNAUTHORIZED );
539         }
540 
541         return category;
542     }
543 
544     /**
545      * Get the URL to manage categories
546      * 
547      * @param request
548      *            The request
549      * @return The URL to manage categories
550      */
551     public static String getUrlManageCategories( HttpServletRequest request )
552     {
553         return AppPathService.getBaseUrl( request ) + JSP_MANAGE_CATEGORIES;
554     }
555 
556     /**
557      * Get the URL to modify a category
558      * 
559      * @param request
560      *            The request
561      * @param nIdCategory
562      *            The id of the category to modify
563      * @return The URL to modify a category
564      */
565     public static String getUrlModifyCategory( HttpServletRequest request, int nIdCategory )
566     {
567         UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_MODIFY_CATEGORY );
568         url.addParameter( PARAMETER_CATEGORY_ID, nIdCategory );
569         url.setAnchor( ANCHOR_NAME );
570 
571         return url.getUrl( );
572     }
573 
574     /**
575      * Get the reference list of groups
576      * 
577      * @param nIdCategory
578      *            the id of the category
579      * @return The reference list of groups of the given category
580      */
581     private static ReferenceList getRefListGroups( int nIdCategory )
582     {
583         EntryFilter entryFilter = new EntryFilter( );
584         entryFilter.setIdResource( nIdCategory );
585         entryFilter.setResourceType( Category.RESOURCE_TYPE );
586         entryFilter.setIdIsGroup( 1 );
587 
588         List<Entry> listEntry = EntryHome.getEntryList( entryFilter );
589 
590         ReferenceList refListGroups = new ReferenceList( );
591 
592         for ( Entry entry : listEntry )
593         {
594             refListGroups.addItem( entry.getIdEntry( ), entry.getTitle( ) );
595         }
596 
597         return refListGroups;
598     }
599 
600     /**
601      * copy the form whose key is specified in the Http request
602      * 
603      * @param request
604      *            The HTTP request
605      * @return The URL to go after performing the action
606      */
607     public String doCopyCategory( HttpServletRequest request )
608     {
609         String strIdCategory = request.getParameter( PARAMETER_CATEGORY_ID );
610         int nIdCategory = Integer.parseInt( strIdCategory );
611         Category category = CategoryHome.findByPrimaryKey( nIdCategory );
612         Object [ ] strTitleCategory = {
613                 category.getLabel( )
614         };
615         String strTitleCopyCategory = I18nService.getLocalizedString( MESSAGE_COPY_TITLE, strTitleCategory, getLocale( ) );
616         category.setLabel( strTitleCopyCategory );
617         CategoryHome.copy( category );
618 
619         return JSP_REDIRECT_TO_MANAGE_CATEGORIES;
620     }
621 
622 }