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.stylesheet.CalendarStyleSheetHome;
37 import fr.paris.lutece.plugins.calendar.service.CalendarResourceIdService;
38 import fr.paris.lutece.portal.business.portalcomponent.PortalComponentHome;
39 import fr.paris.lutece.portal.business.portlet.PortletType;
40 import fr.paris.lutece.portal.business.portlet.PortletTypeHome;
41 import fr.paris.lutece.portal.business.rbac.RBAC;
42 import fr.paris.lutece.portal.business.style.Mode;
43 import fr.paris.lutece.portal.business.style.ModeHome;
44 import fr.paris.lutece.portal.business.style.Style;
45 import fr.paris.lutece.portal.business.style.StyleHome;
46 import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
47 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
48 import fr.paris.lutece.portal.service.fileupload.FileUploadService;
49 import fr.paris.lutece.portal.service.i18n.I18nService;
50 import fr.paris.lutece.portal.service.message.AdminMessage;
51 import fr.paris.lutece.portal.service.message.AdminMessageService;
52 import fr.paris.lutece.portal.service.plugin.Plugin;
53 import fr.paris.lutece.portal.service.plugin.PluginService;
54 import fr.paris.lutece.portal.service.rbac.RBACService;
55 import fr.paris.lutece.portal.service.template.AppTemplateService;
56 import fr.paris.lutece.portal.service.util.AppPathService;
57 import fr.paris.lutece.portal.service.util.AppPropertiesService;
58 import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
59 import fr.paris.lutece.portal.web.constants.Messages;
60 import fr.paris.lutece.portal.web.constants.Parameters;
61 import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
62 import fr.paris.lutece.portal.web.util.LocalizedPaginator;
63 import fr.paris.lutece.util.ReferenceList;
64 import fr.paris.lutece.util.html.HtmlTemplate;
65 import fr.paris.lutece.util.html.Paginator;
66 import fr.paris.lutece.util.sort.AttributeComparator;
67 import fr.paris.lutece.util.url.UrlItem;
68
69 import java.io.ByteArrayInputStream;
70 import java.io.File;
71 import java.util.Collection;
72 import java.util.Collections;
73 import java.util.HashMap;
74 import java.util.List;
75 import java.util.Map;
76
77 import javax.servlet.http.HttpServletRequest;
78 import javax.xml.parsers.SAXParser;
79 import javax.xml.parsers.SAXParserFactory;
80
81 import org.apache.commons.fileupload.FileItem;
82 import org.apache.commons.lang.StringUtils;
83 import org.xml.sax.InputSource;
84
85
86
87
88
89 public class CalendarStyleSheetJspBean extends AdminFeaturesPageJspBean
90 {
91
92
93
94
95 public static final String RIGHT_MANAGE_STYLESHEET = "CALENDAR_MANAGEMENT";
96 public static final String PROPERTY_EXTENSION_REGEXP = ".?[a-zA-Z0-9\\s]*";
97
98 private static final long serialVersionUID = -2844865034209122837L;
99
100
101 private static final String MARK_MODE_ID = "mode_id";
102 private static final String MARK_MODE_LIST = "mode_list";
103 private static final String MARK_STYLESHEET_LIST = "stylesheet_list";
104 private static final String MARK_STYLE_LIST = "style_list";
105 private static final String MARK_STYLESHEET = "stylesheet";
106 private static final String MARK_PAGINATOR = "paginator";
107 private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
108 private static final String MARK_PORTAL_COMPONENT_NAME = "portal_component_name";
109 private static final String MARK_PORTLET_TYPE_NAME = "portlet_type_name";
110 private static final String MARK_STYLE_DESCRIPTION = "style_description";
111 private static final String MARK_EXPORT_EXTENSION = "export_extension";
112
113
114 private static final String TEMPLATE_MANAGE_STYLESHEETS = "admin/plugins/calendar/manage_stylesheets.html";
115 private static final String TEMPLATE_CREATE_STYLESHEET = "admin/plugins/calendar/create_stylesheet.html";
116 private static final String TEMPLATE_MODIFY_STYLESHEET = "admin/plugins/calendar/modify_stylesheet.html";
117 private static final String TEMPLATE_STYLE_SELECT_OPTION = "admin/plugins/calendar/style_select_option.html";
118
119
120 private static final String JSP_URL_STYLESHEET_LIST = "jsp/admin/plugins/calendar/style/ManageExportStyleSheets.jsp";
121
122
123 private static final String PROPERTY_PATH_XSL = "path.stylesheet";
124 private static final String PROPERTY_STYLESHEETS_PER_PAGE = "paginator.stylesheet.itemsPerPage";
125 private static final String MESSAGE_STYLESHEET_NOT_VALID = "portal.style.message.stylesheetNotValid";
126 private static final String MESSAGE_CONFIRM_DELETE_STYLESHEET = "portal.style.message.stylesheetConfirmDelete";
127 private static final String LABEL_ALL = "portal.util.labelAll";
128 private static final String JSP_DO_REMOVE_STYLESHEET = "jsp/admin/style/DoRemoveStyleSheet.jsp";
129 private static final String JSP_REMOVE_STYLE = "RemoveStyle.jsp";
130
131
132 private static final String MESSAGE_INVALID_EXTENSION = "calendar.message.invalidFileExtension";
133 private int _nItemsPerPage;
134 private int _nDefaultItemsPerPage;
135 private String _strCurrentPageIndex;
136
137
138
139
140
141
142
143
144 public String getManageStyleSheet( HttpServletRequest request ) throws AccessDeniedException
145 {
146 if ( !RBACService.isAuthorized( CalendarResourceIdService.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
147 CalendarResourceIdService.PERMISSION_MANAGE, getUser( ) ) )
148 {
149 throw new AccessDeniedException( );
150 }
151
152 ReferenceList listModes = ModeHome.getModes( );
153 String strComboItem = I18nService.getLocalizedString( LABEL_ALL, getLocale( ) );
154 listModes.addItem( -1, strComboItem );
155
156 Plugin plugin = PluginService.getPlugin( Constants.PLUGIN_NAME );
157 List<StyleSheet> listStyleSheets = (List<StyleSheet>) CalendarStyleSheetHome.getStyleSheetList( plugin );
158
159 String strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
160 String strAscSort = null;
161
162 if ( strSortedAttributeName != null )
163 {
164 strAscSort = request.getParameter( Parameters.SORTED_ASC );
165
166 boolean bIsAscSort = Boolean.parseBoolean( strAscSort );
167
168 Collections.sort( listStyleSheets, new AttributeComparator( strSortedAttributeName, bIsAscSort ) );
169 }
170
171 _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_STYLESHEETS_PER_PAGE, 50 );
172 _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
173 _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage,
174 _nDefaultItemsPerPage );
175
176 String strURL = getHomeUrl( request );
177
178 if ( strSortedAttributeName != null )
179 {
180 strURL += ( "?" + Parameters.SORTED_ATTRIBUTE_NAME + "=" + strSortedAttributeName );
181 }
182
183 if ( strAscSort != null )
184 {
185 strURL += ( "&" + Parameters.SORTED_ASC + "=" + strAscSort );
186 }
187
188 LocalizedPaginator<StyleSheet> paginator = new LocalizedPaginator<StyleSheet>( listStyleSheets, _nItemsPerPage,
189 strURL, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale( ) );
190
191 Map<String, Object> model = new HashMap<String, Object>( );
192 model.put( MARK_MODE_ID, "" );
193 model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
194 model.put( MARK_PAGINATOR, paginator );
195 model.put( MARK_STYLESHEET_LIST, paginator.getPageItems( ) );
196 model.put( MARK_MODE_LIST, listModes );
197
198 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_STYLESHEETS, getLocale( ), model );
199
200 return getAdminPage( template.getHtml( ) );
201 }
202
203
204
205
206
207
208
209
210 public String getCreateStyleSheet( HttpServletRequest request ) throws AccessDeniedException
211 {
212 if ( !RBACService.isAuthorized( CalendarResourceIdService.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
213 CalendarResourceIdService.PERMISSION_MANAGE, getUser( ) ) )
214 {
215 throw new AccessDeniedException( );
216 }
217 Map<String, Object> model = new HashMap<String, Object>( );
218 model.put( MARK_STYLE_LIST, getStyleList( ) );
219
220
221
222 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_STYLESHEET, getLocale( ), model );
223
224 return getAdminPage( template.getHtml( ) );
225 }
226
227
228
229
230
231
232
233
234
235
236 public String doCreateStyleSheet( HttpServletRequest request ) throws AccessDeniedException
237 {
238 if ( !RBACService.isAuthorized( CalendarResourceIdService.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
239 CalendarResourceIdService.PERMISSION_MANAGE, getUser( ) ) )
240 {
241 throw new AccessDeniedException( );
242 }
243 StyleSheet stylesheet = new StyleSheet( );
244 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
245 String strErrorUrl = getData( multipartRequest, stylesheet );
246
247 if ( strErrorUrl != null )
248 {
249 return strErrorUrl;
250 }
251
252 String strFileExtension = request.getParameter( Constants.PARAMETER_EXPORT_EXTENSION );
253
254
255 if ( strFileExtension.equals( "" ) )
256 {
257 return AdminMessageService.getMessageUrl( multipartRequest, Messages.MANDATORY_FIELDS,
258 AdminMessage.TYPE_STOP );
259 }
260
261
262 boolean isAllowedExp = strFileExtension.matches( PROPERTY_EXTENSION_REGEXP );
263
264 if ( !isAllowedExp )
265 {
266 return AdminMessageService.getMessageUrl( request, MESSAGE_INVALID_EXTENSION, AdminMessage.TYPE_STOP );
267 }
268
269 strFileExtension = ( strFileExtension.startsWith( "." ) ) ? strFileExtension.substring( 1 ) : strFileExtension;
270
271 Plugin plugin = PluginService.getPlugin( Constants.PLUGIN_NAME );
272
273
274 CalendarStyleSheetHome.create( stylesheet, strFileExtension, plugin );
275
276
277
278
279
280 return AppPathService.getBaseUrl( request ) + JSP_URL_STYLESHEET_LIST;
281 }
282
283
284
285
286
287
288
289
290
291 private String getData( MultipartHttpServletRequest multipartRequest, StyleSheet stylesheet )
292 throws AccessDeniedException
293 {
294 if ( !RBACService.isAuthorized( CalendarResourceIdService.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
295 CalendarResourceIdService.PERMISSION_MANAGE, getUser( ) ) )
296 {
297 throw new AccessDeniedException( );
298 }
299 String strErrorUrl = null;
300 String strDescription = multipartRequest.getParameter( Parameters.STYLESHEET_NAME );
301
302
303
304 FileItem fileSource = multipartRequest.getFile( Parameters.STYLESHEET_SOURCE );
305 byte[] baXslSource = fileSource.get( );
306 String strFilename = FileUploadService.getFileNameOnly( fileSource );
307
308 String strFileExtension = multipartRequest.getParameter( Constants.PARAMETER_EXPORT_EXTENSION );
309
310
311 if ( strDescription.equals( "" ) || ( strFileExtension.equals( "" ) ) || ( strFilename == null )
312 || strFilename.equals( "" ) )
313 {
314 return AdminMessageService.getMessageUrl( multipartRequest, Messages.MANDATORY_FIELDS,
315 AdminMessage.TYPE_STOP );
316 }
317
318
319 if ( isValid( baXslSource ) != null )
320 {
321 Object[] args = { isValid( baXslSource ) };
322
323 return AdminMessageService.getMessageUrl( multipartRequest, MESSAGE_STYLESHEET_NOT_VALID, args,
324 AdminMessage.TYPE_STOP );
325 }
326
327 stylesheet.setDescription( strDescription );
328
329
330 stylesheet.setSource( baXslSource );
331 stylesheet.setFile( strFilename );
332
333 return strErrorUrl;
334 }
335
336
337
338
339
340
341
342
343
344 public String getModifyStyleSheet( HttpServletRequest request ) throws AccessDeniedException
345 {
346 if ( !RBACService.isAuthorized( CalendarResourceIdService.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
347 CalendarResourceIdService.PERMISSION_MANAGE, getUser( ) ) )
348 {
349 throw new AccessDeniedException( );
350 }
351 String strStyleSheetId = request.getParameter( Parameters.STYLESHEET_ID );
352 int nId = Integer.parseInt( strStyleSheetId );
353
354 Plugin plugin = PluginService.getPlugin( Constants.PLUGIN_NAME );
355
356 Map<String, Object> model = new HashMap<String, Object>( );
357 model.put( MARK_STYLE_LIST, getStyleList( ) );
358
359 model.put( MARK_STYLESHEET, CalendarStyleSheetHome.findByPrimaryKey( nId, plugin ) );
360 model.put( MARK_EXPORT_EXTENSION, CalendarStyleSheetHome.getExtension( nId, plugin ) );
361
362 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_STYLESHEET, getLocale( ), model );
363
364 return getAdminPage( template.getHtml( ) );
365 }
366
367
368
369
370
371
372
373
374 public ReferenceList getStyleList( ) throws AccessDeniedException
375 {
376 if ( !RBACService.isAuthorized( CalendarResourceIdService.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
377 CalendarResourceIdService.PERMISSION_MANAGE, getUser( ) ) )
378 {
379 throw new AccessDeniedException( );
380 }
381 Collection<Style> stylesList = StyleHome.getStylesList( );
382 ReferenceList stylesListWithLabels = new ReferenceList( );
383
384 for ( Style style : stylesList )
385 {
386 HashMap<String, Object> model = new HashMap<String, Object>( );
387 model.put( MARK_PORTAL_COMPONENT_NAME, PortalComponentHome.findByPrimaryKey( style.getPortalComponentId( ) )
388 .getName( ) );
389
390 PortletType portletType = PortletTypeHome.findByPrimaryKey( style.getPortletTypeId( ) );
391
392 model.put(
393 MARK_PORTLET_TYPE_NAME,
394 ( ( portletType != null ) ? ( I18nService.getLocalizedString( portletType.getNameKey( ),
395 getLocale( ) ) ) : "" ) );
396 model.put( MARK_STYLE_DESCRIPTION, style.getDescription( ) );
397
398 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_STYLE_SELECT_OPTION, getLocale( ), model );
399 stylesListWithLabels.addItem( style.getId( ), template.getHtml( ) );
400 }
401
402 return stylesListWithLabels;
403 }
404
405
406
407
408
409
410
411
412
413
414 public String doModifyStyleSheet( HttpServletRequest request ) throws AccessDeniedException
415 {
416 if ( !RBACService.isAuthorized( CalendarResourceIdService.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
417 CalendarResourceIdService.PERMISSION_MANAGE, getUser( ) ) )
418 {
419 throw new AccessDeniedException( );
420 }
421 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
422 int nId = Integer.parseInt( multipartRequest.getParameter( Parameters.STYLESHEET_ID ) );
423
424 Plugin plugin = PluginService.getPlugin( Constants.PLUGIN_NAME );
425
426 StyleSheet stylesheet = CalendarStyleSheetHome.findByPrimaryKey( nId, plugin );
427 String strErrorUrl = getData( multipartRequest, stylesheet );
428
429 if ( strErrorUrl != null )
430 {
431 return strErrorUrl;
432 }
433
434 String strFileExtension = request.getParameter( Constants.PARAMETER_EXPORT_EXTENSION );
435
436
437 if ( strFileExtension.equals( StringUtils.EMPTY ) )
438 {
439 return AdminMessageService.getMessageUrl( multipartRequest, Messages.MANDATORY_FIELDS,
440 AdminMessage.TYPE_STOP );
441 }
442
443
444 boolean isAllowedExp = strFileExtension.matches( PROPERTY_EXTENSION_REGEXP );
445
446 if ( !isAllowedExp )
447 {
448 return AdminMessageService.getMessageUrl( request, MESSAGE_INVALID_EXTENSION, AdminMessage.TYPE_STOP );
449 }
450
451 strFileExtension = ( strFileExtension.startsWith( "." ) ) ? strFileExtension.substring( 1 ) : strFileExtension;
452
453
454 removeOldLocalStyleSheet( nId );
455
456
457 CalendarStyleSheetHome.update( stylesheet, plugin );
458
459
460 CalendarStyleSheetHome.updateExtension( stylesheet.getId( ), strFileExtension, plugin );
461
462
463
464
465
466 return AppPathService.getBaseUrl( request ) + JSP_URL_STYLESHEET_LIST;
467 }
468
469
470
471
472
473
474
475
476
477
478 public String getRemoveStyleSheet( HttpServletRequest request ) throws AccessDeniedException
479 {
480 if ( !RBACService.isAuthorized( CalendarResourceIdService.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
481 CalendarResourceIdService.PERMISSION_MANAGE, getUser( ) ) )
482 {
483 throw new AccessDeniedException( );
484 }
485 String strId = request.getParameter( Parameters.STYLESHEET_ID );
486 UrlItem url = new UrlItem( JSP_DO_REMOVE_STYLESHEET );
487 url.addParameter( Parameters.STYLESHEET_ID, strId );
488
489 Plugin plugin = PluginService.getPlugin( Constants.PLUGIN_NAME );
490
491 StyleSheet stylesheet = CalendarStyleSheetHome.findByPrimaryKey( Integer.parseInt( strId ), plugin );
492 Object[] args = { stylesheet.getDescription( ) };
493
494 return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_DELETE_STYLESHEET, args, url.getUrl( ),
495 AdminMessage.TYPE_CONFIRMATION );
496 }
497
498
499
500
501
502
503
504
505 public String doRemoveStyleSheet( HttpServletRequest request ) throws AccessDeniedException
506 {
507 if ( !RBACService.isAuthorized( CalendarResourceIdService.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
508 CalendarResourceIdService.PERMISSION_MANAGE, getUser( ) ) )
509 {
510 throw new AccessDeniedException( );
511 }
512 int nId = Integer.parseInt( request.getParameter( Parameters.STYLESHEET_ID ) );
513
514 Plugin plugin = PluginService.getPlugin( Constants.PLUGIN_NAME );
515
516
517 StyleSheet stylesheet = CalendarStyleSheetHome.findByPrimaryKey( nId, plugin );
518 String strFile = stylesheet.getFile( );
519 CalendarStyleSheetHome.remove( nId, plugin );
520
521
522 int nModeId = stylesheet.getModeId( );
523 Mode mode = ModeHome.findByPrimaryKey( nModeId );
524 String strPathStyleSheet = AppPathService.getPath( PROPERTY_PATH_XSL ) + mode.getPath( );
525 File fileToDelete = new File( strPathStyleSheet, strFile );
526
527 if ( fileToDelete.exists( ) )
528 {
529 fileToDelete.delete( );
530 }
531
532
533
534 return JSP_REMOVE_STYLE;
535 }
536
537
538
539
540
541
542
543
544
545
546 private String isValid( byte[] baXslSource )
547 {
548 String strError = null;
549
550 try
551 {
552 SAXParserFactory factory = SAXParserFactory.newInstance( );
553 SAXParser analyzer = factory.newSAXParser( );
554 InputSource is = new InputSource( new ByteArrayInputStream( baXslSource ) );
555 analyzer.getXMLReader( ).parse( is );
556 }
557 catch ( Exception e )
558 {
559 strError = e.getMessage( );
560 }
561
562 return strError;
563 }
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610 private void removeOldLocalStyleSheet( int nId )
611 {
612 Plugin plugin = PluginService.getPlugin( Constants.PLUGIN_NAME );
613
614
615 StyleSheet stylesheet = CalendarStyleSheetHome.findByPrimaryKey( nId, plugin );
616 int nMode = stylesheet.getModeId( );
617 Mode mode = ModeHome.findByPrimaryKey( nMode );
618 String strPathStyleSheet = AppPathService.getPath( PROPERTY_PATH_XSL ) + mode.getPath( );
619 String strOldFileName = stylesheet.getFile( );
620 String strOldFilePath = strPathStyleSheet + strOldFileName;
621 File oldFile = new File( strOldFilePath );
622
623 if ( oldFile.exists( ) )
624 {
625 oldFile.delete( );
626 }
627 }
628 }