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