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.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
67
68
69 public class PageTemplatesJspBean extends AdminFeaturesPageJspBean
70 {
71
72
73
74
75 public static final String RIGHT_MANAGE_PAGE_TEMPLATES = "CORE_PAGE_TEMPLATE_MANAGEMENT";
76
77
78
79
80 private static final long serialVersionUID = -142214702397662732L;
81
82
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
88 private static final String MARK_PAGE_TEMPLATES_LIST = "page_templates_list";
89 private static final String MARK_PAGE_TEMPLATE = "page_template";
90
91
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
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
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
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
118 private static final String JSP_DO_REMOVE_PAGE_TEMPLATE = "jsp/admin/style/DoRemovePageTemplate.jsp";
119
120
121
122
123
124
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
140
141
142
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
155
156
157
158
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
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
206 return getHomeUrl( request );
207 }
208
209
210
211
212
213
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
231
232
233
234
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
311 return getHomeUrl( request );
312 }
313
314
315
316
317
318
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
347
348
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
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
380
381
382
383
384
385
386
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 }