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.form.web;
35
36 import fr.paris.lutece.plugins.form.business.ExportFormat;
37 import fr.paris.lutece.plugins.form.business.ExportFormatHome;
38 import fr.paris.lutece.plugins.form.service.ExportFormatResourceIdService;
39 import fr.paris.lutece.portal.business.rbac.RBAC;
40 import fr.paris.lutece.portal.service.fileupload.FileUploadService;
41 import fr.paris.lutece.portal.service.i18n.I18nService;
42 import fr.paris.lutece.portal.service.message.AdminMessage;
43 import fr.paris.lutece.portal.service.message.AdminMessageService;
44 import fr.paris.lutece.portal.service.plugin.Plugin;
45 import fr.paris.lutece.portal.service.rbac.RBACService;
46 import fr.paris.lutece.portal.service.template.AppTemplateService;
47 import fr.paris.lutece.portal.service.util.AppLogService;
48 import fr.paris.lutece.portal.service.util.AppPathService;
49 import fr.paris.lutece.portal.service.util.AppPropertiesService;
50 import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
51 import fr.paris.lutece.portal.web.constants.Parameters;
52 import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
53 import fr.paris.lutece.util.html.HtmlTemplate;
54 import fr.paris.lutece.util.html.Paginator;
55 import fr.paris.lutece.util.url.UrlItem;
56
57 import org.apache.commons.fileupload.FileItem;
58
59 import org.xml.sax.InputSource;
60
61 import java.io.ByteArrayInputStream;
62
63 import java.util.HashMap;
64 import java.util.List;
65 import java.util.Locale;
66
67 import javax.servlet.http.HttpServletRequest;
68
69 import javax.xml.parsers.SAXParser;
70 import javax.xml.parsers.SAXParserFactory;
71
72
73
74
75
76
77
78 public class ExportFormatJspBean extends PluginAdminPageJspBean
79 {
80 public static final String RIGHT_MANAGE_FORM = "FORM_MANAGEMENT";
81
82
83 private static final String TEMPLATE_MANAGE_EXPORT = "admin/plugins/form/manage_export_format.html";
84 private static final String TEMPLATE_CREATE_EXPORT = "admin/plugins/form/create_export_format.html";
85 private static final String TEMPLATE_MODIFY_EXPORT = "admin/plugins/form/modify_export_format.html";
86
87
88 private static final String MARK_EXPORT_LIST = "export_list";
89 private static final String MARK_EXPORT = "export";
90 private static final String MARK_PAGINATOR = "paginator";
91 private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
92
93
94 private static final String PARAMETER_ID_EXPORT = "id_export";
95 private static final String PARAMETER_TITLE = "title";
96 private static final String PARAMETER_DESCRIPTION = "description";
97 private static final String PARAMETER_EXTENSION = "extension";
98 private static final String PARAMETER_PAGE_INDEX = "page_index";
99
100
101 private static final String EMPTY_STRING = "";
102
103
104 private static final String MESSAGE_CONFIRM_REMOVE_EXPORT = "form.message.confirmRemoveExportFormat";
105 private static final String MESSAGE_MANDATORY_FIELD = "form.message.mandatory.field";
106 private static final String FIELD_TITLE = "form.createExportFormat.labelTitle";
107 private static final String FIELD_DESCRIPTION = "form.createExportFormat.labelDescription";
108 private static final String FIELD_EXTENSION = "form.createExportFormat.labelExtension";
109 private static final String FIELD_XSL = "form.createExportFormat.labelXsl";
110 private static final String MESSAGE_STYLESHEET_NOT_VALID = "form.message.stylesheetNotValid";
111
112
113 private static final String PROPERTY_ITEM_PER_PAGE = "form.itemsPerPage";
114 private static final String PROPERTY_MANAGE_EXPORT_FORMAT_TITLE = "form.manageExportFormat.pageTitle";
115 private static final String PROPERTY_MODIFY_EXPORT_FORMAT_TITLE = "form.modifyExportFormat.title";
116 private static final String PROPERTY_CREATE_EXPORT_FORMAT_TITLE = "form.createExportFormat.title";
117
118
119 private static final String JSP_MANAGE_EXPORT_FORMAT = "jsp/admin/plugins/form/ManageExportFormat.jsp";
120 private static final String JSP_DO_REMOVE_EXPORT_FORMAT = "jsp/admin/plugins/form/DoRemoveExportFormat.jsp";
121
122
123 private int _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_ITEM_PER_PAGE, 15 );
124 private String _strCurrentPageIndexExport;
125 private int _nItemsPerPageForm;
126
127
128
129
130
131
132 public String getManageExportFormat( HttpServletRequest request )
133 {
134 Plugin plugin = getPlugin( );
135 Locale locale = getLocale( );
136 HashMap model = new HashMap( );
137 List<ExportFormat> listExportFormat = ExportFormatHome.getList( plugin );
138 listExportFormat = (List) RBACService.getAuthorizedCollection( listExportFormat,
139 ExportFormatResourceIdService.PERMISSION_MANAGE, getUser( ) );
140 _strCurrentPageIndexExport = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX,
141 _strCurrentPageIndexExport );
142 _nItemsPerPageForm = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE,
143 _nItemsPerPageForm, _nDefaultItemsPerPage );
144
145 Paginator paginator = new Paginator( listExportFormat, _nItemsPerPageForm, getJspManageExportFormat( request ),
146 PARAMETER_PAGE_INDEX, _strCurrentPageIndexExport );
147
148 model.put( MARK_PAGINATOR, paginator );
149 model.put( MARK_NB_ITEMS_PER_PAGE, EMPTY_STRING + _nItemsPerPageForm );
150 model.put( MARK_EXPORT_LIST, paginator.getPageItems( ) );
151 setPageTitleProperty( PROPERTY_MANAGE_EXPORT_FORMAT_TITLE );
152
153 HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_EXPORT, locale, model );
154
155
156
157 return getAdminPage( templateList.getHtml( ) );
158 }
159
160
161
162
163
164
165 public String getCreateExportFormat( HttpServletRequest request )
166 {
167 if ( !RBACService.isAuthorized( ExportFormat.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
168 ExportFormatResourceIdService.PERMISSION_MANAGE, getUser( ) ) )
169 {
170 return getManageExportFormat( request );
171 }
172
173 Locale locale = getLocale( );
174 HashMap model = new HashMap( );
175 setPageTitleProperty( PROPERTY_CREATE_EXPORT_FORMAT_TITLE );
176
177 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_EXPORT, locale, model );
178
179 return getAdminPage( template.getHtml( ) );
180 }
181
182
183
184
185
186
187 public String doCreateExportFormat( HttpServletRequest request )
188 {
189 if ( !RBACService.isAuthorized( ExportFormat.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
190 ExportFormatResourceIdService.PERMISSION_MANAGE, getUser( ) ) )
191 {
192 return getJspManageExportFormat( request );
193 }
194
195 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
196 ExportFormatess/ExportFormat.html#ExportFormat">ExportFormat exportFormat = new ExportFormat( );
197 String strError = getExportFormatData( multipartRequest, exportFormat );
198
199 if ( strError != null )
200 {
201 return strError;
202 }
203
204 ExportFormatHome.create( exportFormat, getPlugin( ) );
205
206 return getJspManageExportFormat( request );
207 }
208
209
210
211
212
213
214 public String getModifyExportFormat( HttpServletRequest request )
215 {
216 Plugin plugin = getPlugin( );
217 Locale locale = getLocale( );
218 ExportFormat exportFormat;
219 String strIdExport = request.getParameter( PARAMETER_ID_EXPORT );
220 HashMap model = new HashMap( );
221 int nIdExport = -1;
222
223 if ( ( strIdExport != null ) && !strIdExport.equals( EMPTY_STRING ) &&
224 RBACService.isAuthorized( ExportFormat.RESOURCE_TYPE, strIdExport,
225 ExportFormatResourceIdService.PERMISSION_MANAGE, getUser( ) ) )
226 {
227 try
228 {
229 nIdExport = Integer.parseInt( strIdExport );
230 }
231 catch ( NumberFormatException ne )
232 {
233 AppLogService.error( ne );
234
235 return getManageExportFormat( request );
236 }
237 }
238 else
239 {
240 return getManageExportFormat( request );
241 }
242
243 exportFormat = ExportFormatHome.findByPrimaryKey( nIdExport, plugin );
244 model.put( MARK_EXPORT, exportFormat );
245 setPageTitleProperty( PROPERTY_MODIFY_EXPORT_FORMAT_TITLE );
246
247 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_EXPORT, locale, model );
248
249 return getAdminPage( template.getHtml( ) );
250 }
251
252
253
254
255
256
257 public String doModifyExportFormat( HttpServletRequest request )
258 {
259 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
260 Plugin plugin = getPlugin( );
261 ExportFormat exportFormat;
262 String strIdExport = multipartRequest.getParameter( PARAMETER_ID_EXPORT );
263 int nIdExport = -1;
264
265 if ( ( strIdExport != null ) && !strIdExport.equals( EMPTY_STRING ) &&
266 RBACService.isAuthorized( ExportFormat.RESOURCE_TYPE, strIdExport,
267 ExportFormatResourceIdService.PERMISSION_MANAGE, getUser( ) ) )
268 {
269 try
270 {
271 nIdExport = Integer.parseInt( strIdExport );
272 }
273 catch ( NumberFormatException ne )
274 {
275 AppLogService.error( ne );
276
277 return getHomeUrl( request );
278 }
279 }
280 else
281 {
282 return getHomeUrl( request );
283 }
284
285 exportFormat = ExportFormatHome.findByPrimaryKey( nIdExport, plugin );
286
287 String strError = getExportFormatData( multipartRequest, exportFormat );
288
289 if ( strError != null )
290 {
291 return strError;
292 }
293
294 ExportFormatHome.update( exportFormat, getPlugin( ) );
295
296 return getJspManageExportFormat( request );
297 }
298
299
300
301
302
303
304 public String getConfirmRemoveExportFormat( HttpServletRequest request )
305 {
306 if ( request.getParameter( PARAMETER_ID_EXPORT ) == null )
307 {
308 return getHomeUrl( request );
309 }
310
311 String strIdExport = request.getParameter( PARAMETER_ID_EXPORT );
312 UrlItem url = new UrlItem( JSP_DO_REMOVE_EXPORT_FORMAT );
313 url.addParameter( PARAMETER_ID_EXPORT, strIdExport );
314
315 return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_EXPORT, url.getUrl( ),
316 AdminMessage.TYPE_CONFIRMATION );
317 }
318
319
320
321
322
323
324 public String doRemoveExportFormat( HttpServletRequest request )
325 {
326 String strIdExport = request.getParameter( PARAMETER_ID_EXPORT );
327 int nIdExport = -1;
328
329 if ( ( request.getParameter( PARAMETER_ID_EXPORT ) == null ) ||
330 !RBACService.isAuthorized( ExportFormat.RESOURCE_TYPE, strIdExport,
331 ExportFormatResourceIdService.PERMISSION_MANAGE, getUser( ) ) )
332 {
333 return getJspManageExportFormat( request );
334 }
335
336 try
337 {
338 nIdExport = Integer.parseInt( strIdExport );
339 }
340 catch ( NumberFormatException ne )
341 {
342 AppLogService.error( ne );
343
344 return getHomeUrl( request );
345 }
346
347 if ( nIdExport != -1 )
348 {
349 ExportFormatHome.remove( nIdExport, getPlugin( ) );
350 }
351
352 return getJspManageExportFormat( request );
353 }
354
355
356
357
358
359
360
361
362 private String getExportFormatData( MultipartHttpServletRequest multipartRequest, ExportFormat exportFormat )
363 {
364 String strTitle = multipartRequest.getParameter( PARAMETER_TITLE );
365 String strDescription = multipartRequest.getParameter( PARAMETER_DESCRIPTION );
366 String strExtension = multipartRequest.getParameter( PARAMETER_EXTENSION );
367
368 String strFieldError = EMPTY_STRING;
369 FileItem fileSource = multipartRequest.getFile( Parameters.STYLESHEET_SOURCE );
370 String strFilename = FileUploadService.getFileNameOnly( fileSource );
371
372 if ( ( strTitle == null ) || strTitle.trim( ).equals( EMPTY_STRING ) )
373 {
374 strFieldError = FIELD_TITLE;
375 }
376
377 else if ( ( strDescription == null ) || strDescription.trim( ).equals( EMPTY_STRING ) )
378 {
379 strFieldError = FIELD_DESCRIPTION;
380 }
381 else if ( ( strExtension == null ) || strExtension.trim( ).equals( EMPTY_STRING ) )
382 {
383 strFieldError = FIELD_EXTENSION;
384 }
385
386 else if ( ( strFilename == null ) || ( strFilename.equals( "" ) && ( exportFormat.getXsl( ) == null ) ) )
387 {
388 strFieldError = FIELD_XSL;
389 }
390
391
392 if ( !strFieldError.equals( EMPTY_STRING ) )
393 {
394 Object[] tabRequiredFields = { I18nService.getLocalizedString( strFieldError, getLocale( ) ) };
395
396 return AdminMessageService.getMessageUrl( multipartRequest, MESSAGE_MANDATORY_FIELD, tabRequiredFields,
397 AdminMessage.TYPE_STOP );
398 }
399
400 byte[] baXslSource = fileSource.get( );
401
402
403 if ( ( strFilename != null ) && !strFilename.equals( "" ) && ( isValid( baXslSource ) != null ) )
404 {
405 Object[] args = { isValid( baXslSource ) };
406
407 return AdminMessageService.getMessageUrl( multipartRequest, MESSAGE_STYLESHEET_NOT_VALID, args,
408 AdminMessage.TYPE_STOP );
409 }
410
411 exportFormat.setTitle( strTitle );
412 exportFormat.setDescription( strDescription );
413 exportFormat.setExtension( strExtension );
414
415 if ( ( strFilename != null ) && !strFilename.equals( "" ) )
416 {
417 exportFormat.setXsl( baXslSource );
418 }
419
420 return null;
421 }
422
423
424
425
426
427
428 private String isValid( byte[] baXslSource )
429 {
430 String strError = null;
431
432 try
433 {
434 SAXParserFactory factory = SAXParserFactory.newInstance( );
435 SAXParser analyzer = factory.newSAXParser( );
436 InputSource is = new InputSource( new ByteArrayInputStream( baXslSource ) );
437 analyzer.getXMLReader( ).parse( is );
438 }
439 catch ( Exception e )
440 {
441 strError = e.getMessage( );
442 }
443
444 return strError;
445 }
446
447
448
449
450
451
452 private String getJspManageExportFormat( HttpServletRequest request )
453 {
454 return AppPathService.getBaseUrl( request ) + JSP_MANAGE_EXPORT_FORMAT;
455 }
456 }