View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.calendar.web;
35  
36  import fr.paris.lutece.plugins.calendar.business.category.Category;
37  import fr.paris.lutece.plugins.calendar.business.category.CategoryHome;
38  import fr.paris.lutece.plugins.calendar.service.CalendarResourceIdService;
39  import fr.paris.lutece.plugins.calendar.service.CategoryService;
40  import fr.paris.lutece.plugins.calendar.service.CategoryService.CategoryDisplay;
41  import fr.paris.lutece.portal.business.rbac.RBAC;
42  import fr.paris.lutece.portal.business.user.AdminUser;
43  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
44  import fr.paris.lutece.portal.service.message.AdminMessage;
45  import fr.paris.lutece.portal.service.message.AdminMessageService;
46  import fr.paris.lutece.portal.service.plugin.Plugin;
47  import fr.paris.lutece.portal.service.plugin.PluginService;
48  import fr.paris.lutece.portal.service.rbac.RBACService;
49  import fr.paris.lutece.portal.service.template.AppTemplateService;
50  import fr.paris.lutece.portal.service.util.AppPathService;
51  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
52  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
53  import fr.paris.lutece.portal.web.constants.Messages;
54  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
55  import fr.paris.lutece.util.ReferenceList;
56  import fr.paris.lutece.util.html.HtmlTemplate;
57  import fr.paris.lutece.util.url.UrlItem;
58  
59  import java.util.Collection;
60  import java.util.HashMap;
61  import java.util.Map;
62  
63  import javax.servlet.http.HttpServletRequest;
64  
65  import org.apache.commons.fileupload.FileItem;
66  
67  
68  /**
69   * This class provides the user interface to manage Lutece group features (
70   * manage, create, modify, remove )
71   */
72  public class CalendarCategoryJspBean extends AdminFeaturesPageJspBean
73  {
74      // Right
75      public static final String RIGHT_CATEGORY_MANAGEMENT = "CALENDAR_MANAGEMENT";
76  
77      //Constants
78      private static final String REGEX_ID = "^[\\d]+$";
79      private static final int ERROR_ID_CATEGORY = -1;
80  
81      // JSP
82      private static final String JSP_URL_REMOVE_CATEGORY = "jsp/admin/plugins/calendar/DoRemoveCategory.jsp";
83  
84      //Markers
85      private static final String MARK_CATEGORY_LIST = "category_list";
86      private static final String MARK_CATEGORY_DISPLAY = "categoryDisplay";
87      private static final String MARK_USER_WORKGROUP_LIST = "user_workgroup_list";
88      private static final String MARK_WORKGROUP_SELECTED = "selected_workgroup";
89  
90      // Parameters
91      private static final String PARAMETER_CATEGORY_ID = "category_id";
92      private static final String PARAMETER_CATEGORY_NAME = "category_name";
93      private static final String PARAMETER_CATEGORY_DESCRIPTION = "category_description";
94      private static final String PARAMETER_CATEGORY_UPDATE_ICON = "update_icon";
95      private static final String PARAMETER_IMAGE_CONTENT = "category_icon";
96      private static final String PARAMETER_WORKGROUP_KEY = "workgroup_key";
97  
98      // JSP
99      private static final String JSP_URL_CATEGORY_LIST = "jsp/admin/plugins/calendar/ManageCalendarCategory.jsp";
100 
101     // Templates
102     private static final String TEMPLATE_MANAGE_CATEGORY = "admin/plugins/calendar/category/manage_category.html";
103     private static final String TEMPLATE_CREATE_CATEGORY = "admin/plugins/calendar/category/create_category.html";
104     private static final String TEMPLATE_MODIFY_CATEGORY = "admin/plugins/calendar/category/modify_category.html";
105 
106     // Properties
107     private static final String PROPERTY_PAGE_TITLE_CREATE_CATEGORY = "calendar.create_category.pageTitle";
108     private static final String PROPERTY_PAGE_TITLE_MODIFY_CATEGORY = "calendar.modify_category.pageTitle";
109 
110     // Message
111     private static final String MESSAGE_CATEGORY_EXIST = "calendar.message.categoryExist";
112     private static final String MESSAGE_CATEGORY_IS_LINKED = "calendar.message.categoryIsLinked";
113     private static final String MESSAGE_CATEGORY_ERROR = "calendar.message.categoryError";
114     private static final String MESSAGE_CONFIRM_REMOVE_CATEGORY = "calendar.message.confirmRemoveCategory";
115 
116     /**
117      * Creates a new CategoryJspBean object.
118      */
119     public CalendarCategoryJspBean( )
120     {
121     }
122 
123     /**
124      * Returns Category management form
125      * @param request The Http request
126      * @return Html form
127      * @throws AccessDeniedException If the user is not allowed to access this
128      *             feature
129      */
130     public String getManageCategory( HttpServletRequest request ) throws AccessDeniedException
131     {
132         if ( !RBACService.isAuthorized( CalendarResourceIdService.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
133                 CalendarResourceIdService.PERMISSION_MANAGE, getUser( ) ) )
134         {
135             throw new AccessDeniedException( );
136         }
137         setPageTitleProperty( null );
138 
139         AdminUser user = getUser( );
140 
141         HashMap<String, Collection<CategoryDisplay>> model = new HashMap<String, Collection<CategoryDisplay>>( );
142         model.put( MARK_CATEGORY_LIST, CategoryService.getAllCategoriesDisplay( user ) );
143 
144         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_CATEGORY, getLocale( ), model );
145 
146         return getAdminPage( template.getHtml( ) );
147     }
148 
149     /**
150      * Insert a new Category
151      * @param request The HTTP request
152      * @return String The html code page
153      * @throws AccessDeniedException If the user is not allowed to access this
154      *             feature
155      */
156     public String getCreateCategory( HttpServletRequest request ) throws AccessDeniedException
157     {
158         if ( !RBACService.isAuthorized( CalendarResourceIdService.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
159                 CalendarResourceIdService.PERMISSION_MANAGE, getUser( ) ) )
160         {
161             throw new AccessDeniedException( );
162         }
163         setPageTitleProperty( PROPERTY_PAGE_TITLE_CREATE_CATEGORY );
164 
165         AdminUser user = getUser( );
166         ReferenceList refListWorkGroups = AdminWorkgroupService.getUserWorkgroups( user, getLocale( ) );
167         Map<String, Object> model = new HashMap<String, Object>( );
168         model.put( MARK_USER_WORKGROUP_LIST, refListWorkGroups );
169 
170         //LUTECE-890 : the first workgroup will be selected by default
171         if ( !refListWorkGroups.isEmpty( ) )
172         {
173             model.put( MARK_WORKGROUP_SELECTED, refListWorkGroups.get( 0 ).getCode( ) );
174         }
175 
176         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_CATEGORY, getLocale( ), model );
177 
178         return getAdminPage( template.getHtml( ) );
179     }
180 
181     /**
182      * Create Category
183      * @param request The HTTP request
184      * @return String The url page
185      * @throws AccessDeniedException If the user is not allowed to access this
186      *             feature
187      */
188     public String doCreateCategory( HttpServletRequest request ) throws AccessDeniedException
189     {
190         if ( !RBACService.isAuthorized( CalendarResourceIdService.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
191                 CalendarResourceIdService.PERMISSION_MANAGE, getUser( ) ) )
192         {
193             throw new AccessDeniedException( );
194         }
195         Category category = new Category( );
196         String strCategoryName = request.getParameter( PARAMETER_CATEGORY_NAME );
197         String strCategoryDescription = request.getParameter( PARAMETER_CATEGORY_DESCRIPTION );
198         String strWorkgroup = request.getParameter( PARAMETER_WORKGROUP_KEY );
199 
200         MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
201         FileItem item = mRequest.getFile( PARAMETER_IMAGE_CONTENT );
202 
203         // Mandatory field
204         if ( strCategoryName.length( ) == 0 )
205         {
206             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
207         }
208 
209         Plugin plugin = PluginService.getPlugin( Constants.PLUGIN_NAME );
210 
211         // check if category exist
212         if ( CategoryHome.findByName( strCategoryName, plugin ).size( ) > 0 )
213         {
214             return AdminMessageService.getMessageUrl( request, MESSAGE_CATEGORY_EXIST, AdminMessage.TYPE_STOP );
215         }
216 
217         category.setName( strCategoryName );
218         category.setDescription( strCategoryDescription );
219 
220         byte[] bytes = item.get( );
221 
222         category.setIconContent( bytes );
223         category.setIconMimeType( item.getContentType( ) );
224         category.setWorkgroup( strWorkgroup );
225         CategoryHome.create( category, plugin );
226 
227         return AppPathService.getBaseUrl( request ) + JSP_URL_CATEGORY_LIST;
228     }
229 
230     /**
231      * Returns Category modification form
232      * @param request The HTTP request
233      * @return String The html code page
234      * @throws AccessDeniedException If the user is not allowed to access this
235      *             feature
236      */
237     public String getModifyCategory( HttpServletRequest request ) throws AccessDeniedException
238     {
239         if ( !RBACService.isAuthorized( CalendarResourceIdService.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
240                 CalendarResourceIdService.PERMISSION_MANAGE, getUser( ) ) )
241         {
242             throw new AccessDeniedException( );
243         }
244         setPageTitleProperty( PROPERTY_PAGE_TITLE_MODIFY_CATEGORY );
245 
246         AdminUser user = getUser( );
247         ReferenceList refListWorkGroups = AdminWorkgroupService.getUserWorkgroups( user, getLocale( ) );
248         int nIdCategory = checkCategoryId( request );
249 
250         if ( nIdCategory == ERROR_ID_CATEGORY )
251         {
252             return AdminMessageService.getMessageUrl( request, MESSAGE_CATEGORY_ERROR, AdminMessage.TYPE_ERROR );
253         }
254 
255         Map<String, Object> model = new HashMap<String, Object>( );
256 
257         model.put( MARK_CATEGORY_DISPLAY, CategoryService.getCategoryDisplay( nIdCategory ) );
258         model.put( MARK_USER_WORKGROUP_LIST, refListWorkGroups );
259 
260         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_CATEGORY, getLocale( ), model );
261 
262         return getAdminPage( template.getHtml( ) );
263     }
264 
265     /**
266      * Modify Category
267      * @param request The HTTP request
268      * @return String The url page
269      * @throws AccessDeniedException If the user is not allowed to access this
270      *             feature
271      */
272     public String doModifyCategory( HttpServletRequest request ) throws AccessDeniedException
273     {
274         if ( !RBACService.isAuthorized( CalendarResourceIdService.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
275                 CalendarResourceIdService.PERMISSION_MANAGE, getUser( ) ) )
276         {
277             throw new AccessDeniedException( );
278         }
279         Category category = null;
280         String strCategoryName = request.getParameter( PARAMETER_CATEGORY_NAME );
281         String strCategoryDescription = request.getParameter( PARAMETER_CATEGORY_DESCRIPTION );
282         String strCategoryUpdateIcon = request.getParameter( PARAMETER_CATEGORY_UPDATE_ICON );
283         String strWorkgroup = request.getParameter( PARAMETER_WORKGROUP_KEY );
284 
285         int nIdCategory = checkCategoryId( request );
286 
287         if ( nIdCategory == ERROR_ID_CATEGORY )
288         {
289             return AdminMessageService.getMessageUrl( request, MESSAGE_CATEGORY_ERROR, AdminMessage.TYPE_ERROR );
290         }
291 
292         // Mandatory field
293         if ( strCategoryName.length( ) == 0 )
294         {
295             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
296         }
297 
298         Plugin plugin = PluginService.getPlugin( Constants.PLUGIN_NAME );
299 
300         // check if category exist
301         Collection<Category> categoriesList = CategoryHome.findByName( strCategoryName, plugin );
302 
303         if ( !categoriesList.isEmpty( ) && ( categoriesList.iterator( ).next( ).getId( ) != nIdCategory ) )
304         {
305             return AdminMessageService.getMessageUrl( request, MESSAGE_CATEGORY_EXIST, AdminMessage.TYPE_STOP );
306         }
307 
308         category = CategoryHome.find( nIdCategory, plugin );
309         category.setName( strCategoryName );
310         category.setDescription( strCategoryDescription );
311 
312         if ( strCategoryUpdateIcon != null )
313         {
314             MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
315             FileItem item = mRequest.getFile( PARAMETER_IMAGE_CONTENT );
316 
317             byte[] bytes = item.get( );
318             category.setIconContent( bytes );
319             category.setIconMimeType( item.getContentType( ) );
320         }
321 
322         category.setWorkgroup( strWorkgroup );
323 
324         CategoryHome.update( category, plugin );
325 
326         return AppPathService.getBaseUrl( request ) + JSP_URL_CATEGORY_LIST;
327     }
328 
329     /**
330      * Returns the page of confirmation for deleting a workgroup
331      * 
332      * @param request The Http Request
333      * @return the confirmation url
334      * @throws AccessDeniedException If the user is not allowed to access this
335      *             feature
336      */
337     public String getConfirmRemoveCategory( HttpServletRequest request ) throws AccessDeniedException
338     {
339         if ( !RBACService.isAuthorized( CalendarResourceIdService.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
340                 CalendarResourceIdService.PERMISSION_MANAGE, getUser( ) ) )
341         {
342             throw new AccessDeniedException( );
343         }
344         int nIdCategory = checkCategoryId( request );
345 
346         if ( nIdCategory == ERROR_ID_CATEGORY )
347         {
348             return AdminMessageService.getMessageUrl( request, MESSAGE_CATEGORY_ERROR, AdminMessage.TYPE_ERROR );
349         }
350 
351         Plugin plugin = PluginService.getPlugin( Constants.PLUGIN_NAME );
352 
353         // Test if the category is assigned
354         if ( CategoryHome.findCountIdEvents( nIdCategory, plugin ) > 0 )
355         {
356             return AdminMessageService.getMessageUrl( request, MESSAGE_CATEGORY_IS_LINKED, AdminMessage.TYPE_STOP );
357         }
358 
359         UrlItem url = new UrlItem( JSP_URL_REMOVE_CATEGORY );
360         url.addParameter( PARAMETER_CATEGORY_ID, Integer.toString( nIdCategory ) );
361 
362         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_CATEGORY, url.getUrl( ),
363                 AdminMessage.TYPE_CONFIRMATION );
364     }
365 
366     /**
367      * Perform the deletion
368      * @param request The HTTP request
369      * @return The URL to go after performing the action
370      * @throws AccessDeniedException If the user is not allowed to access this
371      *             feature
372      */
373     public String doRemoveCategory( HttpServletRequest request ) throws AccessDeniedException
374     {
375         if ( !RBACService.isAuthorized( CalendarResourceIdService.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
376                 CalendarResourceIdService.PERMISSION_MANAGE, getUser( ) ) )
377         {
378             throw new AccessDeniedException( );
379         }
380         int nIdCategory = checkCategoryId( request );
381 
382         if ( nIdCategory == ERROR_ID_CATEGORY )
383         {
384             return AdminMessageService.getMessageUrl( request, MESSAGE_CATEGORY_ERROR, AdminMessage.TYPE_ERROR );
385         }
386 
387         Plugin plugin = PluginService.getPlugin( Constants.PLUGIN_NAME );
388 
389         // Test if the category is assigned
390         if ( CategoryHome.findCountIdEvents( nIdCategory, plugin ) > 0 )
391         {
392             return AdminMessageService.getMessageUrl( request, MESSAGE_CATEGORY_IS_LINKED, AdminMessage.TYPE_STOP );
393         }
394 
395         CategoryHome.remove( nIdCategory, plugin );
396 
397         return AppPathService.getBaseUrl( request ) + JSP_URL_CATEGORY_LIST;
398     }
399 
400     /**
401      * 
402      * @param request The http request
403      * @return id of category, ERROR_ID_CATEGORY else
404      */
405     private int checkCategoryId( HttpServletRequest request )
406     {
407         String strCategoryId = request.getParameter( PARAMETER_CATEGORY_ID );
408 
409         if ( ( strCategoryId == null ) || !strCategoryId.matches( REGEX_ID ) )
410         {
411             return ERROR_ID_CATEGORY;
412         }
413 
414         return Integer.parseInt( strCategoryId );
415     }
416 }