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.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   * class ExportFormatJspBean
76   *
77   */
78  public class ExportFormatJspBean extends PluginAdminPageJspBean
79  {
80      public static final String RIGHT_MANAGE_FORM = "FORM_MANAGEMENT";
81  
82      //	templates
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      //	Markers
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      //	parameters form
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     //	 other constants
101     private static final String EMPTY_STRING = "";
102 
103     //	message
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     //	properties
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     //Jsp Definition
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     //	session fields
123     private int _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_ITEM_PER_PAGE, 15 );
124     private String _strCurrentPageIndexExport;
125     private int _nItemsPerPageForm;
126 
127     /**
128      * Return management export format ( list of export format )
129      * @param request The Http request
130      * @return Html form
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         //ReferenceList refMailingList;
156         //refMailingList=AdminMailingListService.getMailingLists(adminUser);
157         return getAdminPage( templateList.getHtml(  ) );
158     }
159 
160     /**
161     * Gets the export format creation page
162     * @param request The HTTP request
163     * @return The export format creation page
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     * Perform the export format  creation
184     * @param request The HTTP request
185     * @return The URL to go after performing the action
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     * Gets the export format modification page
211     * @param request The HTTP request
212     * @return The export format creation page
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     * Perform the export format modification
254     * @param request The HTTP request
255     * @return The URL to go after performing the action
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      * Gets the confirmation page of delete export format
301      * @param request The HTTP request
302      * @return the confirmation page of delete export format
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     * Perform the export format supression
321     * @param request The HTTP request
322     * @return The URL to go after performing the action
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      * Get the request data and if there is no error insert the data in the exportFormat object specified in parameter.
357      * return null if there is no error or else return the error page url
358      * @param  multipartRequest the request
359      * @param exportFormat the exportFormat Object
360      * @return null if there is no error or else return the error page url
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         //Mandatory fields
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         //Check the XML validity of the XSL stylesheet
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      *  Use parsing for validate the modify xsl file
425      *  @param baXslSource the xsl source
426      *  @return the message exception when the validation is false
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      * return the url of manage export format
449      * @param request the request
450      * @return the url of manage export format
451      */
452     private String getJspManageExportFormat( HttpServletRequest request )
453     {
454         return AppPathService.getBaseUrl( request ) + JSP_MANAGE_EXPORT_FORMAT;
455     }
456 }