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.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   * This class provides the user interface to manage StyleSheet features
88   */
89  public class CalendarStyleSheetJspBean extends AdminFeaturesPageJspBean
90  {
91      ////////////////////////////////////////////////////////////////////////////
92      // Constants
93  
94      // Right
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     // Markers
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     // Templates files path
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     // JSP
120     private static final String JSP_URL_STYLESHEET_LIST = "jsp/admin/plugins/calendar/style/ManageExportStyleSheets.jsp";
121 
122     // Properties
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     //Messages
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      * Displays the stylesheets list
139      * @return the html code for displaying the stylesheets list
140      * @param request The request
141      * @throws AccessDeniedException If the user is not allowed to access this
142      *             feature
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         // Parameters processing
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      * Returns the create form of a new stylesheet with the upload field
205      * @param request the http request
206      * @return the html code for the create form of a new stylesheet
207      * @throws AccessDeniedException If the user is not allowed to access this
208      *             feature
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         //model.put( MARK_MODE_LIST, ModeHome.getModes(  ) );
221         //model.put( MARK_MODE_ID, strModeId );
222         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_STYLESHEET, getLocale( ), model );
223 
224         return getAdminPage( template.getHtml( ) );
225     }
226 
227     /**
228      * Processes the creation form of a new stylesheet by recovering the
229      * parameters
230      * in the http request
231      * @param request the http request
232      * @return The Jsp URL of the process result
233      * @throws AccessDeniedException If the user is not allowed to access this
234      *             feature
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         // Mandatory fields
255         if ( strFileExtension.equals( "" ) )
256         {
257             return AdminMessageService.getMessageUrl( multipartRequest, Messages.MANDATORY_FIELDS,
258                     AdminMessage.TYPE_STOP );
259         }
260 
261         //Test if there aren't special characters in extension
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         //insert in the table stylesheet of the database
274         CalendarStyleSheetHome.create( stylesheet, strFileExtension, plugin );
275 
276         //create a local file
277         //localStyleSheetFile( stylesheet );
278 
279         //Displays the list of the stylesheet files
280         return AppPathService.getBaseUrl( request ) + JSP_URL_STYLESHEET_LIST;
281     }
282 
283     /**
284      * Reads stylesheet's data
285      * @param multipartRequest The request
286      * @param stylesheet The style sheet
287      * @return An error message URL or null if no error
288      * @throws AccessDeniedException If the user is not allowed to access this
289      *             feature
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         //String strStyleId = multipartRequest.getParameter( Parameters.STYLES );
303         //String strModeId = multipartRequest.getParameter( Parameters.MODE_STYLESHEET );
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         // Mandatory fields
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         // Check the XML validity of the XSL stylesheet
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         //stylesheet.setStyleId( Integer.parseInt( strStyleId ) );
329         //stylesheet.setModeId( Integer.parseInt( strModeId ) );
330         stylesheet.setSource( baXslSource );
331         stylesheet.setFile( strFilename );
332 
333         return strErrorUrl;
334     }
335 
336     /**
337      * Returns the form to update a stylesheet whose identifer is stored in the
338      * http request
339      * @param request The http request
340      * @return The html code
341      * @throws AccessDeniedException If the user is not allowed to access this
342      *             feature
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         //model.put( MARK_MODE_LIST, ModeHome.getModes(  ) );
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      * Return a ReferenceList with id style for code and a concatenation of
369      * portal name + portlet type name + style description for name.
370      * @return The {@link ReferenceList}
371      * @throws AccessDeniedException If the user is not allowed to access this
372      *             feature
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      * Processes the updating form of a stylesheet whose new parameters are
407      * stored in the
408      * http request
409      * @param request The http request
410      * @return The Jsp URL of the process result
411      * @throws AccessDeniedException If the user is not allowed to access this
412      *             feature
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         // Mandatory fields
437         if ( strFileExtension.equals( StringUtils.EMPTY ) )
438         {
439             return AdminMessageService.getMessageUrl( multipartRequest, Messages.MANDATORY_FIELDS,
440                     AdminMessage.TYPE_STOP );
441         }
442 
443         //Test if there aren't special characters in extension
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         // Remove the old local file
454         removeOldLocalStyleSheet( nId );
455 
456         // Update the stylesheet in database
457         CalendarStyleSheetHome.update( stylesheet, plugin );
458 
459         // Update the export file extension in database
460         CalendarStyleSheetHome.updateExtension( stylesheet.getId( ), strFileExtension, plugin );
461 
462         // Recreate the local file
463         //localStyleSheetFile( stylesheet );
464 
465         //Displays the list of the stylesheet files
466         return AppPathService.getBaseUrl( request ) + JSP_URL_STYLESHEET_LIST;
467     }
468 
469     /**
470      * Returns the confirm of removing the style whose identifier is in
471      * the http request
472      * 
473      * @param request The Http request
474      * @return the html code for the remove confirmation page
475      * @throws AccessDeniedException If the user is not allowed to access this
476      *             feature
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      * Processes the deletion of a stylesheet
500      * @param request the http request
501      * @return The Jsp URL of the process result
502      * @throws AccessDeniedException If the user is not allowed to access this
503      *             feature
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         //int nIdStyle = Integer.parseInt( request.getParameter( Parameters.STYLE_ID ) );
517         StyleSheet stylesheet = CalendarStyleSheetHome.findByPrimaryKey( nId, plugin );
518         String strFile = stylesheet.getFile( );
519         CalendarStyleSheetHome.remove( nId, plugin );
520 
521         //removal of the XSL file
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         // return getHomeUrl( request );
533         //return JSP_REMOVE_STYLE + "?" + Parameters.STYLE_ID + "=" + nIdStyle;
534         return JSP_REMOVE_STYLE;
535     }
536 
537     //////////////////////////////////////////////////////////////////////////////////
538     // Private implementation
539 
540     /**
541      * Use parsing for validate the modify xsl file
542      * 
543      * @param baXslSource The XSL source
544      * @return the message exception when the validation is false
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      * Create and Update the local download file
567      * 
568      * @param stylesheet The style sheet
569      */
570 
571     /**
572      * @param stylesheet
573      */
574 
575     /*
576      * private void localStyleSheetFile( StyleSheet stylesheet )
577      * {
578      * int nModeId = stylesheet.getModeId( );
579      * Mode mode = ModeHome.findByPrimaryKey( nModeId );
580      * String strPathStyleSheet = AppPathService.getPath( PROPERTY_PATH_XSL ) +
581      * mode.getPath( );
582      * String strFileName = stylesheet.getFile( );
583      * String strFilePath = strPathStyleSheet + strFileName;
584      * 
585      * try
586      * {
587      * File file = new File( strFilePath );
588      * 
589      * if ( file.exists( ) )
590      * {
591      * file.delete( );
592      * }
593      * 
594      * FileOutputStream fos = new FileOutputStream( file );
595      * fos.write( stylesheet.getSource( ) );
596      * fos.flush( );
597      * fos.close( );
598      * }
599      * catch ( IOException e )
600      * {
601      * AppLogService.error( e.getMessage( ), e );
602      * }
603      * }
604      */
605 
606     /**
607      * remove the xsl file from the tmp directory
608      * @param nId the identifier of the file
609      */
610     private void removeOldLocalStyleSheet( int nId )
611     {
612         Plugin plugin = PluginService.getPlugin( Constants.PLUGIN_NAME );
613 
614         //Remove the file which been modify
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 }