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.portal.web.style;
35  
36  import fr.paris.lutece.portal.business.style.PageTemplate;
37  import fr.paris.lutece.portal.business.style.PageTemplateHome;
38  import fr.paris.lutece.portal.service.fileupload.FileUploadService;
39  import fr.paris.lutece.portal.service.message.AdminMessage;
40  import fr.paris.lutece.portal.service.message.AdminMessageService;
41  import fr.paris.lutece.portal.service.template.AppTemplateService;
42  import fr.paris.lutece.portal.service.util.AppLogService;
43  import fr.paris.lutece.portal.service.util.AppPathService;
44  import fr.paris.lutece.portal.service.util.AppPropertiesService;
45  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
46  import fr.paris.lutece.portal.web.constants.Messages;
47  import fr.paris.lutece.portal.web.constants.Parameters;
48  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
49  import fr.paris.lutece.util.file.FileUtil;
50  import fr.paris.lutece.util.html.HtmlTemplate;
51  import fr.paris.lutece.util.url.UrlItem;
52  
53  import org.apache.commons.fileupload.FileItem;
54  import org.apache.commons.lang.StringUtils;
55  
56  import java.io.File;
57  import java.io.FileOutputStream;
58  import java.io.IOException;
59  
60  import java.util.HashMap;
61  
62  import javax.servlet.http.HttpServletRequest;
63  
64  
65  /**
66   * This class provides the user interface to manage page templates features (
67   * manage, create, modify, remove)
68   */
69  public class PageTemplatesJspBean extends AdminFeaturesPageJspBean
70  {
71      // Right
72      /**
73       * Right to manage page templates
74       */
75      public static final String RIGHT_MANAGE_PAGE_TEMPLATES = "CORE_PAGE_TEMPLATE_MANAGEMENT";
76  
77      /**
78       * Serial version UID
79       */
80      private static final long serialVersionUID = -142214702397662732L;
81  
82      // Properties for page titles
83      private static final String PROPERTY_PAGE_TITLE_PAGE_TEMPLATE_LIST = "portal.style.manage_page_templates.pageTitle";
84      private static final String PROPERTY_PAGE_TITLE_CREATE_PAGE_TEMPLATE = "portal.style.create_page_template.pageTitle";
85      private static final String PROPERTY_PAGE_TITLE_MODIFY_PAGE_TEMPLATE = "portal.style.modify_page_template.pageTitle";
86  
87      // Markers
88      private static final String MARK_PAGE_TEMPLATES_LIST = "page_templates_list";
89      private static final String MARK_PAGE_TEMPLATE = "page_template";
90  
91      // Templates files path
92      private static final String TEMPLATE_PAGE_TEMPLATES = "admin/style/manage_page_templates.html";
93      private static final String TEMPLATE_CREATE_PAGE_TEMPLATE = "admin/style/create_page_template.html";
94      private static final String TEMPLATE_MODIFY_PAGE_TEMPLATE = "admin/style/modify_page_template.html";
95  
96      // Properties
97      private static final String PROPERTY_PATH_TEMPLATE = "path.templates";
98      private static final String PROPERTY_PATH_FILE_PAGE_TEMPLATE = "path.file.page.template";
99      private static final String PROPERTY_PATH_IMAGE_PAGE_TEMPLATE = "path.image.page.template";
100 
101     // Messages
102     private static final String MESSAGE_CONFIRM_DELETE_PAGE_TEMPLATE = "portal.style.message.pageTemplateConfirmDelete";
103     private static final String MESSAGE_PAGE_TEMPLATE_IS_USED = "portal.style.message.pageTemplateIsUsed";
104     private static final String MESSAGE_WRONG_IMAGE_EXTENSION = "portal.util.message.wrongImageExtention";
105     private static final String MESSAGE_WRONG_HTML_EXTENSION = "portal.util.message.wrongHtmlExtention";
106 
107     // Parameters
108     private static final String PARAMETER_PAGE_TEMPLATE_FILE = "page_template_file";
109     private static final String PARAMETER_PAGE_TEMPLATE_PICTURE = "page_template_picture";
110     private static final String PARAMETER_PAGE_TEMPLATE_UPDATE_IMAGE = "update_image";
111     private static final String PARAMETER_PAGE_TEMPLATE_UPDATE_FILE = "update_file";
112     private static final String strPathImagePageTemplate = AppPathService.getPath( PROPERTY_PATH_IMAGE_PAGE_TEMPLATE ) +
113         File.separator;
114     private static final String strPathFilePageTemplate = AppPathService.getPath( PROPERTY_PATH_TEMPLATE ) +
115         File.separator + AppPropertiesService.getProperty( PROPERTY_PATH_FILE_PAGE_TEMPLATE );
116 
117     //JSP
118     private static final String JSP_DO_REMOVE_PAGE_TEMPLATE = "jsp/admin/style/DoRemovePageTemplate.jsp";
119 
120     /**
121      * Returns the list of page templates
122      *
123      * @param request The Http request
124      * @return the html code for display the page templates list
125      */
126     public String getManagePageTemplate( HttpServletRequest request )
127     {
128         setPageTitleProperty( PROPERTY_PAGE_TITLE_PAGE_TEMPLATE_LIST );
129 
130         HashMap<String, Object> model = new HashMap<String, Object>(  );
131         model.put( MARK_PAGE_TEMPLATES_LIST, PageTemplateHome.getPageTemplatesList(  ) );
132 
133         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_PAGE_TEMPLATES, getLocale(  ), model );
134 
135         return getAdminPage( template.getHtml(  ) );
136     }
137 
138     /**
139      * Returns the page template form of creation
140      *
141      * @param request The Http request
142      * @return the html code of the page template
143      */
144     public String getCreatePageTemplate( HttpServletRequest request )
145     {
146         setPageTitleProperty( PROPERTY_PAGE_TITLE_CREATE_PAGE_TEMPLATE );
147 
148         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_PAGE_TEMPLATE, getLocale(  ) );
149 
150         return getAdminPage( template.getHtml(  ) );
151     }
152 
153     /**
154      * Processes the creation form of a new page template by recovering the
155      * parameters in the http request
156      *
157      * @param request the http request
158      * @return The Jsp URL of the process result
159      */
160     public String doCreatePageTemplate( HttpServletRequest request )
161     {
162         PageTemplate pageTemplate = new PageTemplate(  );
163 
164         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
165 
166         String strDescription = multipartRequest.getParameter( Parameters.PAGE_TEMPLATE_DESCRIPTION );
167 
168         //Mandatory fields
169         if ( StringUtils.isEmpty( strDescription ) )
170         {
171             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
172         }
173 
174         FileItem fileTemplate = multipartRequest.getFile( PARAMETER_PAGE_TEMPLATE_FILE );
175         FileItem filePicture = multipartRequest.getFile( PARAMETER_PAGE_TEMPLATE_PICTURE );
176 
177         String strFileName = FileUploadService.getFileNameOnly( fileTemplate );
178         String strPictureName = FileUploadService.getFileNameOnly( filePicture );
179 
180         if ( StringUtils.isEmpty( strFileName ) || StringUtils.isEmpty( strPictureName ) )
181         {
182             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FILE, AdminMessage.TYPE_STOP );
183         }
184 
185         if ( !FileUtil.hasHtmlExtension( strFileName ) )
186         {
187             return AdminMessageService.getMessageUrl( request, MESSAGE_WRONG_HTML_EXTENSION, AdminMessage.TYPE_STOP );
188         }
189 
190         if ( !FileUtil.hasImageExtension( strPictureName ) )
191         {
192             return AdminMessageService.getMessageUrl( request, MESSAGE_WRONG_IMAGE_EXTENSION, AdminMessage.TYPE_STOP );
193         }
194 
195         pageTemplate.setFile( AppPropertiesService.getProperty( PROPERTY_PATH_FILE_PAGE_TEMPLATE ) + strFileName );
196         writeTemplateFile( strFileName, strPathFilePageTemplate, fileTemplate );
197 
198         pageTemplate.setPicture( strPictureName );
199 
200         writeTemplateFile( strPictureName, strPathImagePageTemplate, filePicture );
201 
202         pageTemplate.setDescription( strDescription );
203         PageTemplateHome.create( pageTemplate );
204 
205         // If the process is successful, redirects towards the theme view
206         return getHomeUrl( request );
207     }
208 
209     /**
210      * Returns the page template form of update
211      *
212      * @param request The Http request
213      * @return the html code of the page template form
214      */
215     public String getModifyPageTemplate( HttpServletRequest request )
216     {
217         setPageTitleProperty( PROPERTY_PAGE_TITLE_MODIFY_PAGE_TEMPLATE );
218 
219         String strId = request.getParameter( Parameters.PAGE_TEMPLATE_ID );
220 
221         HashMap<String, Object> model = new HashMap<String, Object>(  );
222         model.put( MARK_PAGE_TEMPLATE, PageTemplateHome.findByPrimaryKey( Integer.parseInt( strId ) ) );
223 
224         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_PAGE_TEMPLATE, getLocale(  ), model );
225 
226         return getAdminPage( template.getHtml(  ) );
227     }
228 
229     /**
230      * Processes the updating form of a page template whose new parameters are
231      * stored in the http request
232      *
233      * @param request The http request
234      * @return The Jsp URL of the process result
235      */
236     public String doModifyPageTemplate( HttpServletRequest request )
237     {
238         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
239 
240         String strId = multipartRequest.getParameter( Parameters.PAGE_TEMPLATE_ID );
241         String strDescription = multipartRequest.getParameter( Parameters.PAGE_TEMPLATE_DESCRIPTION );
242         String strUpdatePicture = multipartRequest.getParameter( PARAMETER_PAGE_TEMPLATE_UPDATE_IMAGE );
243         String strUpdateFile = multipartRequest.getParameter( PARAMETER_PAGE_TEMPLATE_UPDATE_FILE );
244 
245         PageTemplate pageTemplate = PageTemplateHome.findByPrimaryKey( Integer.parseInt( strId ) );
246 
247         boolean bUpdateFile = false;
248         boolean bUpdatePicture = false;
249         FileItem fileTemplate = multipartRequest.getFile( PARAMETER_PAGE_TEMPLATE_FILE );
250         String strFileName = FileUploadService.getFileNameOnly( fileTemplate );
251         FileItem filePicture = multipartRequest.getFile( PARAMETER_PAGE_TEMPLATE_PICTURE );
252         String strPictureName = FileUploadService.getFileNameOnly( filePicture );
253 
254         boolean bHasError = false;
255 
256         if ( strUpdateFile != null )
257         {
258             if ( StringUtils.isEmpty( strFileName ) )
259             {
260                 bHasError = true;
261             }
262 
263             bUpdateFile = true;
264         }
265 
266         if ( strUpdatePicture != null )
267         {
268             if ( StringUtils.isEmpty( strPictureName ) )
269             {
270                 bHasError = true;
271             }
272 
273             bUpdatePicture = true;
274         }
275 
276         if ( bHasError || StringUtils.isEmpty( strDescription ) )
277         {
278             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FILE, AdminMessage.TYPE_STOP );
279         }
280 
281         if ( bUpdateFile )
282         {
283             new File( AppPathService.getPath( PROPERTY_PATH_TEMPLATE ) + File.separator + pageTemplate.getFile(  ) ).delete(  );
284             pageTemplate.setFile( AppPropertiesService.getProperty( PROPERTY_PATH_FILE_PAGE_TEMPLATE ) + strFileName );
285 
286             if ( !FileUtil.hasHtmlExtension( strFileName ) )
287             {
288                 return AdminMessageService.getMessageUrl( request, MESSAGE_WRONG_HTML_EXTENSION, AdminMessage.TYPE_STOP );
289             }
290 
291             writeTemplateFile( strFileName, strPathFilePageTemplate, fileTemplate );
292         }
293 
294         if ( bUpdatePicture )
295         {
296             new File( strPathImagePageTemplate, pageTemplate.getPicture(  ) ).delete(  );
297             pageTemplate.setPicture( strPictureName );
298 
299             if ( !FileUtil.hasImageExtension( strPictureName ) )
300             {
301                 return AdminMessageService.getMessageUrl( request, MESSAGE_WRONG_IMAGE_EXTENSION, AdminMessage.TYPE_STOP );
302             }
303 
304             writeTemplateFile( strPictureName, strPathImagePageTemplate, filePicture );
305         }
306 
307         pageTemplate.setDescription( strDescription );
308         PageTemplateHome.update( pageTemplate );
309 
310         // If the process is successful, redirects towards the page template management page
311         return getHomeUrl( request );
312     }
313 
314     /**
315      * Returns the confirm of removing the page_template whose identifier is in
316      * the http request
317      * @param request The Http request
318      * @return the html code for the remove confirmation page
319      */
320     public String getConfirmRemovePageTemplate( HttpServletRequest request )
321     {
322         String strId = request.getParameter( Parameters.PAGE_TEMPLATE_ID );
323         int nId = Integer.parseInt( strId );
324 
325         boolean bIsUsed = PageTemplateHome.checkStylePageTemplateIsUsed( nId );
326 
327         if ( !bIsUsed )
328         {
329             return AdminMessageService.getMessageUrl( request, MESSAGE_PAGE_TEMPLATE_IS_USED, AdminMessage.TYPE_STOP );
330         }
331 
332         PageTemplate pageTemplate = PageTemplateHome.findByPrimaryKey( Integer.parseInt( strId ) );
333         String strPathPageTemplateFile = AppPathService.getPath( PROPERTY_PATH_TEMPLATE ) + File.separator +
334             pageTemplate.getFile(  );
335         String strPathPictureFile = strPathImagePageTemplate + pageTemplate.getPicture(  );
336         Object[] args = { strPathPageTemplateFile, strPathPictureFile };
337 
338         UrlItem url = new UrlItem( JSP_DO_REMOVE_PAGE_TEMPLATE );
339         url.addParameter( Parameters.PAGE_TEMPLATE_ID, nId );
340 
341         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_DELETE_PAGE_TEMPLATE, args, url.getUrl(  ),
342             AdminMessage.TYPE_CONFIRMATION );
343     }
344 
345     /**
346      * Processes the deletion of a page template
347      * @param request the http request
348      * @return The Jsp URL of the process result
349      */
350     public String doRemovePageTemplate( HttpServletRequest request )
351     {
352         String strId = request.getParameter( Parameters.PAGE_TEMPLATE_ID );
353         int nId = Integer.parseInt( strId );
354 
355         // Delete files associated
356         PageTemplate pageTemplate = PageTemplateHome.findByPrimaryKey( Integer.parseInt( strId ) );
357 
358         File filePageTemplateToDelete = new File( AppPathService.getPath( PROPERTY_PATH_TEMPLATE ),
359                 pageTemplate.getFile(  ) );
360 
361         if ( filePageTemplateToDelete.exists(  ) )
362         {
363             filePageTemplateToDelete.delete(  );
364         }
365 
366         File filePictureToDelete = new File( strPathImagePageTemplate, pageTemplate.getPicture(  ) );
367 
368         if ( filePictureToDelete.exists(  ) )
369         {
370             filePictureToDelete.delete(  );
371         }
372 
373         PageTemplateHome.remove( nId );
374 
375         return getHomeUrl( request );
376     }
377 
378     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
379     // private methods
380 
381     /**
382      * Write the templates files (html and image)
383      *
384      * @param strFileName The name of the file
385      * @param strPath The path of the file
386      * @param fileItem The fileItem object which contains the new file
387      */
388     private void writeTemplateFile( String strFileName, String strPath, FileItem fileItem )
389     {
390         FileOutputStream fosFile = null;
391 
392         try
393         {
394             File file = new File( strPath + strFileName );
395 
396             if ( file.exists(  ) )
397             {
398                 file.delete(  );
399             }
400 
401             fosFile = new FileOutputStream( file );
402             fosFile.flush(  );
403             fosFile.write( fileItem.get(  ) );
404             fosFile.close(  );
405         }
406         catch ( IOException e )
407         {
408             AppLogService.error( e.getMessage(  ), e );
409         }
410         finally
411         {
412             if ( fosFile != null )
413             {
414                 try
415                 {
416                     fosFile.close(  );
417                 }
418                 catch ( IOException e )
419                 {
420                     AppLogService.error( e.getMessage(  ), e );
421                 }
422             }
423         }
424     }
425 }