1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
70
71
72 public class CalendarCategoryJspBean extends AdminFeaturesPageJspBean
73 {
74
75 public static final String RIGHT_CATEGORY_MANAGEMENT = "CALENDAR_MANAGEMENT";
76
77
78 private static final String REGEX_ID = "^[\\d]+$";
79 private static final int ERROR_ID_CATEGORY = -1;
80
81
82 private static final String JSP_URL_REMOVE_CATEGORY = "jsp/admin/plugins/calendar/DoRemoveCategory.jsp";
83
84
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
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
99 private static final String JSP_URL_CATEGORY_LIST = "jsp/admin/plugins/calendar/ManageCalendarCategory.jsp";
100
101
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
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
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
118
119 public CalendarCategoryJspBean( )
120 {
121 }
122
123
124
125
126
127
128
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
151
152
153
154
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
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
183
184
185
186
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
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
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
232
233
234
235
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
267
268
269
270
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
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
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
331
332
333
334
335
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
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
368
369
370
371
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
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
403
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 }