StyleSheetJspBean.java

  1. /*
  2.  * Copyright (c) 2002-2022, 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.portal.web.stylesheet;

  35. import java.io.ByteArrayInputStream;
  36. import java.io.File;
  37. import java.io.FileOutputStream;
  38. import java.io.IOException;
  39. import java.util.Collection;
  40. import java.util.Collections;
  41. import java.util.HashMap;
  42. import java.util.List;
  43. import java.util.Map;

  44. import javax.servlet.http.HttpServletRequest;
  45. import javax.xml.parsers.SAXParser;
  46. import javax.xml.parsers.SAXParserFactory;

  47. import org.apache.commons.fileupload.FileItem;
  48. import org.xml.sax.InputSource;

  49. import fr.paris.lutece.portal.business.portalcomponent.PortalComponentHome;
  50. import fr.paris.lutece.portal.business.portlet.PortletType;
  51. import fr.paris.lutece.portal.business.portlet.PortletTypeHome;
  52. import fr.paris.lutece.portal.business.style.Mode;
  53. import fr.paris.lutece.portal.business.style.ModeHome;
  54. import fr.paris.lutece.portal.business.style.Style;
  55. import fr.paris.lutece.portal.business.style.StyleHome;
  56. import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
  57. import fr.paris.lutece.portal.business.stylesheet.StyleSheetHome;
  58. import fr.paris.lutece.portal.service.admin.AccessDeniedException;
  59. import fr.paris.lutece.portal.service.fileupload.FileUploadService;
  60. import fr.paris.lutece.portal.service.i18n.I18nService;
  61. import fr.paris.lutece.portal.service.message.AdminMessage;
  62. import fr.paris.lutece.portal.service.message.AdminMessageService;
  63. import fr.paris.lutece.portal.service.security.SecurityTokenService;
  64. import fr.paris.lutece.portal.service.template.AppTemplateService;
  65. import fr.paris.lutece.portal.service.util.AppLogService;
  66. import fr.paris.lutece.portal.service.util.AppPathService;
  67. import fr.paris.lutece.portal.service.util.AppPropertiesService;
  68. import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
  69. import fr.paris.lutece.portal.web.constants.Messages;
  70. import fr.paris.lutece.portal.web.constants.Parameters;
  71. import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
  72. import fr.paris.lutece.portal.web.util.LocalizedPaginator;
  73. import fr.paris.lutece.util.ReferenceList;
  74. import fr.paris.lutece.util.file.FileUtil;
  75. import fr.paris.lutece.util.html.AbstractPaginator;
  76. import fr.paris.lutece.util.html.HtmlTemplate;
  77. import fr.paris.lutece.util.sort.AttributeComparator;

  78. /**
  79.  * This class provides the user interface to manage StyleSheet features
  80.  */
  81. public class StyleSheetJspBean extends AdminFeaturesPageJspBean
  82. {
  83.     // //////////////////////////////////////////////////////////////////////////
  84.     // Constants

  85.     // Right
  86.     /**
  87.      * Right to manage stylesheets
  88.      */
  89.     public static final String RIGHT_MANAGE_STYLESHEET = "CORE_STYLESHEET_MANAGEMENT";

  90.     /**
  91.      * Serial version UID
  92.      */
  93.     private static final long serialVersionUID = 8176263113722225633L;

  94.     // Markers
  95.     private static final String MARK_MODE_ID = "mode_id";
  96.     private static final String MARK_MODE_LIST = "mode_list";
  97.     private static final String MARK_STYLESHEET_LIST = "stylesheet_list";
  98.     private static final String MARK_STYLE_LIST = "style_list";
  99.     private static final String MARK_STYLESHEET = "stylesheet";
  100.     private static final String MARK_PAGINATOR = "paginator";
  101.     private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
  102.     private static final String MARK_PORTAL_COMPONENT_NAME = "portal_component_name";
  103.     private static final String MARK_PORTLET_TYPE_NAME = "portlet_type_name";
  104.     private static final String MARK_STYLE_DESCRIPTION = "style_description";

  105.     // Templates files path
  106.     private static final String TEMPLATE_MANAGE_STYLESHEETS = "admin/stylesheet/manage_stylesheets.html";
  107.     private static final String TEMPLATE_CREATE_STYLESHEET = "admin/stylesheet/create_stylesheet.html";
  108.     private static final String TEMPLATE_MODIFY_STYLESHEET = "admin/stylesheet/modify_stylesheet.html";
  109.     private static final String TEMPLATE_STYLE_SELECT_OPTION = "admin/stylesheet/style_select_option.html";

  110.     // Properties
  111.     private static final String PROPERTY_PATH_XSL = "path.stylesheet";
  112.     private static final String PROPERTY_STYLESHEETS_PER_PAGE = "paginator.stylesheet.itemsPerPage";
  113.     private static final String MESSAGE_STYLESHEET_ALREADY_EXISTS = "portal.style.message.stylesheetAlreadyExists";
  114.     private static final String MESSAGE_STYLESHEET_NOT_VALID = "portal.style.message.stylesheetNotValid";
  115.     private static final String MESSAGE_CONFIRM_DELETE_STYLESHEET = "portal.style.message.stylesheetConfirmDelete";
  116.     private static final String LABEL_ALL = "portal.util.labelAll";
  117.     private static final String JSP_DO_REMOVE_STYLESHEET = "jsp/admin/style/DoRemoveStyleSheet.jsp";
  118.     private static final String JSP_REMOVE_STYLE = "RemoveStyle.jsp";
  119.     private int _nItemsPerPage;
  120.     private String _strCurrentPageIndex;

  121.     /**
  122.      * Displays the stylesheets list
  123.      *
  124.      * @return the html code for displaying the stylesheets list
  125.      * @param request
  126.      *            The request
  127.      */
  128.     public String getManageStyleSheet( HttpServletRequest request )
  129.     {
  130.         // Parameters processing
  131.         String strModeId = request.getParameter( Parameters.MODE_ID );
  132.         strModeId = ( strModeId != null ) ? strModeId : "-1";

  133.         int nModeId = Integer.parseInt( strModeId );

  134.         ReferenceList listModes = ModeHome.getModes( );
  135.         String strComboItem = I18nService.getLocalizedString( LABEL_ALL, getLocale( ) );
  136.         listModes.addItem( -1, strComboItem );

  137.         List<StyleSheet> listStyleSheets = (List<StyleSheet>) StyleSheetHome.getStyleSheetList( nModeId );

  138.         String strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
  139.         String strAscSort = null;

  140.         if ( strSortedAttributeName != null )
  141.         {
  142.             strAscSort = request.getParameter( Parameters.SORTED_ASC );

  143.             boolean bIsAscSort = Boolean.parseBoolean( strAscSort );

  144.             Collections.sort( listStyleSheets, new AttributeComparator( strSortedAttributeName, bIsAscSort ) );
  145.         }

  146.         int defaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_STYLESHEETS_PER_PAGE, 50 );
  147.         _strCurrentPageIndex = AbstractPaginator.getPageIndex( request, AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
  148.         _nItemsPerPage = AbstractPaginator.getItemsPerPage( request, AbstractPaginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, defaultItemsPerPage );

  149.         String strURL = getHomeUrl( request );

  150.         if ( strSortedAttributeName != null )
  151.         {
  152.             strURL += ( "?" + Parameters.SORTED_ATTRIBUTE_NAME + "=" + strSortedAttributeName );
  153.         }

  154.         if ( strAscSort != null )
  155.         {
  156.             strURL += ( "&" + Parameters.SORTED_ASC + "=" + strAscSort );
  157.         }

  158.         LocalizedPaginator<StyleSheet> paginator = new LocalizedPaginator<>( listStyleSheets, _nItemsPerPage, strURL, AbstractPaginator.PARAMETER_PAGE_INDEX,
  159.                 _strCurrentPageIndex, getLocale( ) );

  160.         Map<String, Object> model = new HashMap<>( );
  161.         model.put( MARK_MODE_ID, strModeId );
  162.         model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
  163.         model.put( MARK_PAGINATOR, paginator );
  164.         model.put( MARK_STYLESHEET_LIST, paginator.getPageItems( ) );
  165.         model.put( MARK_MODE_LIST, listModes );

  166.         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_STYLESHEETS, getLocale( ), model );

  167.         return getAdminPage( template.getHtml( ) );
  168.     }

  169.     /**
  170.      * Returns the create form of a new stylesheet with the upload field
  171.      *
  172.      * @param request
  173.      *            the http request
  174.      * @return the html code for the create form of a new stylesheet
  175.      */
  176.     public String getCreateStyleSheet( HttpServletRequest request )
  177.     {
  178.         String strModeId = request.getParameter( Parameters.MODE_ID );

  179.         Map<String, Object> model = new HashMap<>( );
  180.         model.put( MARK_STYLE_LIST, getStyleList( ) );
  181.         model.put( MARK_MODE_LIST, ModeHome.getModes( ) );
  182.         model.put( MARK_MODE_ID, strModeId );
  183.         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_CREATE_STYLESHEET ) );

  184.         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_STYLESHEET, getLocale( ), model );

  185.         return getAdminPage( template.getHtml( ) );
  186.     }

  187.     /**
  188.      * Processes the creation form of a new stylesheet by recovering the parameters in the http request
  189.      *
  190.      * @param request
  191.      *            the http request
  192.      * @return The Jsp URL of the process result
  193.      * @throws AccessDeniedException
  194.      *             if the security token is invalid
  195.      */
  196.     public String doCreateStyleSheet( HttpServletRequest request ) throws AccessDeniedException
  197.     {
  198.         StyleSheet stylesheet = new StyleSheet( );
  199.         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  200.         String strErrorUrl = getData( multipartRequest, stylesheet );

  201.         if ( strErrorUrl != null )
  202.         {
  203.             return strErrorUrl;
  204.         }
  205.         if ( !SecurityTokenService.getInstance( ).validate( multipartRequest, TEMPLATE_CREATE_STYLESHEET ) )
  206.         {
  207.             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
  208.         }

  209.         // insert in the table stylesheet of the database
  210.         StyleSheetHome.create( stylesheet );

  211.         // create a local file
  212.         localStyleSheetFile( stylesheet );

  213.         // Displays the list of the stylesheet files
  214.         return getHomeUrl( request );
  215.     }

  216.     /**
  217.      * Reads stylesheet's data
  218.      *
  219.      * @param multipartRequest
  220.      *            The request
  221.      * @param stylesheet
  222.      *            The style sheet
  223.      * @return An error message URL or null if no error
  224.      */
  225.     private String getData( MultipartHttpServletRequest multipartRequest, StyleSheet stylesheet )
  226.     {
  227.         String strErrorUrl = null;
  228.         String strDescription = multipartRequest.getParameter( Parameters.STYLESHEET_NAME );
  229.         String strStyleId = multipartRequest.getParameter( Parameters.STYLES );
  230.         String strModeId = multipartRequest.getParameter( Parameters.MODE_STYLESHEET );

  231.         FileItem fileSource = multipartRequest.getFile( Parameters.STYLESHEET_SOURCE );
  232.         byte [ ] baXslSource = fileSource.get( );
  233.         String strFilename = FileUploadService.getFileNameOnly( fileSource );

  234.         // Mandatory fields
  235.         if ( strDescription.equals( "" ) || ( strFilename == null ) || strFilename.equals( "" ) )
  236.         {
  237.             return AdminMessageService.getMessageUrl( multipartRequest, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
  238.         }

  239.         // test the existence of style or mode already associate with this stylesheet
  240.         int nStyleId = Integer.parseInt( strStyleId );
  241.         int nModeId = Integer.parseInt( strModeId );
  242.         int nCount = StyleSheetHome.getStyleSheetNbPerStyleMode( nStyleId, nModeId );

  243.         // Do not create a stylesheet of there is already one
  244.         if ( ( nCount >= 1 ) && ( stylesheet.getId( ) == 0 /* creation */ ) )
  245.         {
  246.             return AdminMessageService.getMessageUrl( multipartRequest, MESSAGE_STYLESHEET_ALREADY_EXISTS, AdminMessage.TYPE_STOP );
  247.         }

  248.         // Check the XML validity of the XSL stylesheet
  249.         if ( isValid( baXslSource ) != null )
  250.         {
  251.             Object [ ] args = {
  252.                     isValid( baXslSource )
  253.             };

  254.             return AdminMessageService.getMessageUrl( multipartRequest, MESSAGE_STYLESHEET_NOT_VALID, args, AdminMessage.TYPE_STOP );
  255.         }

  256.         stylesheet.setDescription( strDescription );
  257.         stylesheet.setStyleId( Integer.parseInt( strStyleId ) );
  258.         stylesheet.setModeId( Integer.parseInt( strModeId ) );
  259.         stylesheet.setSource( baXslSource );
  260.         stylesheet.setFile( strFilename );

  261.         return strErrorUrl;
  262.     }

  263.     /**
  264.      * Returns the form to update a stylesheet whose identifer is stored in the http request
  265.      *
  266.      * @param request
  267.      *            The http request
  268.      * @return The html code
  269.      */
  270.     public String getModifyStyleSheet( HttpServletRequest request )
  271.     {
  272.         String strStyleSheetId = request.getParameter( Parameters.STYLESHEET_ID );
  273.         int nId = Integer.parseInt( strStyleSheetId );

  274.         Map<String, Object> model = new HashMap<>( );
  275.         model.put( MARK_STYLE_LIST, getStyleList( ) );
  276.         model.put( MARK_MODE_LIST, ModeHome.getModes( ) );
  277.         model.put( MARK_STYLESHEET, StyleSheetHome.findByPrimaryKey( nId ) );
  278.         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MODIFY_STYLESHEET ) );

  279.         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_STYLESHEET, getLocale( ), model );

  280.         return getAdminPage( template.getHtml( ) );
  281.     }

  282.     /**
  283.      * Return a ReferenceList with id style for code and a concatenation of portal name + portlet type name + style description for name.
  284.      *
  285.      * @return The {@link ReferenceList}
  286.      */
  287.     public ReferenceList getStyleList( )
  288.     {
  289.         Collection<Style> stylesList = StyleHome.getStylesList( );
  290.         ReferenceList stylesListWithLabels = new ReferenceList( );

  291.         for ( Style style : stylesList )
  292.         {
  293.             HashMap<String, Object> model = new HashMap<>( );
  294.             model.put( MARK_PORTAL_COMPONENT_NAME, PortalComponentHome.findByPrimaryKey( style.getPortalComponentId( ) ).getName( ) );

  295.             PortletType portletType = PortletTypeHome.findByPrimaryKey( style.getPortletTypeId( ) );

  296.             model.put( MARK_PORTLET_TYPE_NAME,
  297.                     ( ( portletType != null ) ? ( I18nService.getLocalizedString( portletType.getNameKey( ), getLocale( ) ) ) : "" ) );
  298.             model.put( MARK_STYLE_DESCRIPTION, style.getDescription( ) );

  299.             HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_STYLE_SELECT_OPTION, getLocale( ), model );
  300.             stylesListWithLabels.addItem( style.getId( ), template.getHtml( ) );
  301.         }

  302.         return stylesListWithLabels;
  303.     }

  304.     /**
  305.      * Processes the updating form of a stylesheet whose new parameters are stored in the http request
  306.      *
  307.      * @param request
  308.      *            The http request
  309.      * @return The Jsp URL of the process result
  310.      * @throws AccessDeniedException
  311.      *             if the security token is invalid
  312.      */
  313.     public String doModifyStyleSheet( HttpServletRequest request ) throws AccessDeniedException
  314.     {
  315.         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  316.         int nId = Integer.parseInt( multipartRequest.getParameter( Parameters.STYLESHEET_ID ) );
  317.         StyleSheet stylesheet = StyleSheetHome.findByPrimaryKey( nId );
  318.         String strErrorUrl = getData( multipartRequest, stylesheet );

  319.         if ( strErrorUrl != null )
  320.         {
  321.             return strErrorUrl;
  322.         }
  323.         if ( !SecurityTokenService.getInstance( ).validate( multipartRequest, TEMPLATE_MODIFY_STYLESHEET ) )
  324.         {
  325.             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
  326.         }

  327.         // Remove the old local file
  328.         removeOldLocalStyleSheet( nId );

  329.         // Update the stylesheet in database
  330.         StyleSheetHome.update( stylesheet );

  331.         // Recreate the local file
  332.         localStyleSheetFile( stylesheet );

  333.         // Displays the management stylesheet page
  334.         return getHomeUrl( request );
  335.     }

  336.     /**
  337.      * Returns the confirm of removing the style whose identifier is in the http request
  338.      *
  339.      * @param request
  340.      *            The Http request
  341.      * @return the html code for the remove confirmation page
  342.      */
  343.     public String getRemoveStyleSheet( HttpServletRequest request )
  344.     {
  345.         String strId = request.getParameter( Parameters.STYLESHEET_ID );

  346.         StyleSheet stylesheet = StyleSheetHome.findByPrimaryKey( Integer.parseInt( strId ) );
  347.         Object [ ] args = {
  348.                 stylesheet.getDescription( )
  349.         };

  350.         Map<String, Object> parameters = new HashMap<>( );
  351.         parameters.put( Parameters.STYLESHEET_ID, strId );
  352.         parameters.put( Parameters.STYLE_ID, stylesheet.getStyleId( ) );
  353.         parameters.put( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, JSP_DO_REMOVE_STYLESHEET ) );
  354.         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_DELETE_STYLESHEET, args, null, JSP_DO_REMOVE_STYLESHEET, null,
  355.                 AdminMessage.TYPE_CONFIRMATION, parameters );
  356.     }

  357.     /**
  358.      * Processes the deletion of a stylesheet
  359.      *
  360.      * @param request
  361.      *            the http request
  362.      * @return The Jsp URL of the process result
  363.      * @throws AccessDeniedException
  364.      *             if the security token is invalid
  365.      */
  366.     public String doRemoveStyleSheet( HttpServletRequest request ) throws AccessDeniedException
  367.     {
  368.         if ( !SecurityTokenService.getInstance( ).validate( request, JSP_DO_REMOVE_STYLESHEET ) )
  369.         {
  370.             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
  371.         }
  372.         int nId = Integer.parseInt( request.getParameter( Parameters.STYLESHEET_ID ) );
  373.         int nIdStyle = Integer.parseInt( request.getParameter( Parameters.STYLE_ID ) );
  374.         StyleSheet stylesheet = StyleSheetHome.findByPrimaryKey( nId );
  375.         String strFile = stylesheet.getFile( );
  376.         StyleSheetHome.remove( nId );

  377.         // removal of the XSL file
  378.         int nModeId = stylesheet.getModeId( );
  379.         Mode mode = ModeHome.findByPrimaryKey( nModeId );
  380.         String strPathStyleSheet = AppPathService.getPath( PROPERTY_PATH_XSL ) + mode.getPath( );
  381.         File fileToDelete = new File( strPathStyleSheet, strFile );
  382.         FileUtil.deleteFile( fileToDelete );

  383.         return JSP_REMOVE_STYLE + "?" + Parameters.STYLE_ID + "=" + nIdStyle;
  384.     }

  385.     // ////////////////////////////////////////////////////////////////////////////////
  386.     // Private implementation

  387.     /**
  388.      * Use parsing for validate the modify xsl file
  389.      *
  390.      * @param baXslSource
  391.      *            The XSL source
  392.      * @return the message exception when the validation is false
  393.      */
  394.     private String isValid( byte [ ] baXslSource )
  395.     {
  396.         String strError = null;

  397.         try
  398.         {
  399.             SAXParserFactory factory = SAXParserFactory.newInstance( );
  400.             factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
  401.             factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
  402.             SAXParser analyzer = factory.newSAXParser( );
  403.             InputSource is = new InputSource( new ByteArrayInputStream( baXslSource ) );
  404.             analyzer.getXMLReader( ).parse( is );
  405.         }
  406.         catch( Exception e )
  407.         {
  408.             strError = e.getMessage( );
  409.         }

  410.         return strError;
  411.     }

  412.     /**
  413.      * Create and Update the local download file
  414.      *
  415.      * @param stylesheet
  416.      *            The style sheet
  417.      */
  418.     private void localStyleSheetFile( StyleSheet stylesheet )
  419.     {
  420.         int nModeId = stylesheet.getModeId( );
  421.         Mode mode = ModeHome.findByPrimaryKey( nModeId );
  422.         String strPathStyleSheet = AppPathService.getPath( PROPERTY_PATH_XSL ) + mode.getPath( );
  423.         String strFileName = stylesheet.getFile( );
  424.         String strFilePath = strPathStyleSheet + strFileName;

  425.         File file = new File( strFilePath );
  426.         FileUtil.deleteFile( file );
  427.         try ( FileOutputStream fos = new FileOutputStream( file ) )
  428.         {
  429.             fos.write( stylesheet.getSource( ) );
  430.         }
  431.         catch( IOException e )
  432.         {
  433.             AppLogService.error( e.getMessage( ), e );
  434.         }
  435.     }

  436.     /**
  437.      * remove the xsl file from the tmp directory
  438.      *
  439.      * @param nId
  440.      *            the identifier of the file
  441.      */
  442.     private void removeOldLocalStyleSheet( int nId )
  443.     {
  444.         // Remove the file which been modify
  445.         StyleSheet stylesheet = StyleSheetHome.findByPrimaryKey( nId );
  446.         int nMode = stylesheet.getModeId( );
  447.         Mode mode = ModeHome.findByPrimaryKey( nMode );
  448.         String strPathStyleSheet = AppPathService.getPath( PROPERTY_PATH_XSL ) + mode.getPath( );
  449.         String strOldFileName = stylesheet.getFile( );
  450.         String strOldFilePath = strPathStyleSheet + strOldFileName;
  451.         File oldFile = new File( strOldFilePath );
  452.         FileUtil.deleteFile( oldFile );
  453.     }
  454. }