View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.plugins.importexport.web;
35  
36  import fr.paris.lutece.plugins.importexport.business.export.AutomaticExportConfig;
37  import fr.paris.lutece.plugins.importexport.business.export.ExportDAO;
38  import fr.paris.lutece.plugins.importexport.business.export.IAutomaticExportConfigDAO;
39  import fr.paris.lutece.plugins.importexport.service.ImportExportPlugin;
40  import fr.paris.lutece.plugins.importexport.service.export.ExportManager;
41  import fr.paris.lutece.plugins.importexport.service.export.daemon.ExportDaemon;
42  import fr.paris.lutece.portal.business.user.AdminUser;
43  import fr.paris.lutece.portal.business.xsl.XslExportHome;
44  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
45  import fr.paris.lutece.portal.service.admin.AdminUserService;
46  import fr.paris.lutece.portal.service.message.AdminMessage;
47  import fr.paris.lutece.portal.service.message.AdminMessageService;
48  import fr.paris.lutece.portal.service.plugin.Plugin;
49  import fr.paris.lutece.portal.service.plugin.PluginService;
50  import fr.paris.lutece.portal.service.spring.SpringContextService;
51  import fr.paris.lutece.portal.service.template.AppTemplateService;
52  import fr.paris.lutece.portal.service.util.AppLogService;
53  import fr.paris.lutece.portal.service.util.AppPathService;
54  import fr.paris.lutece.portal.service.util.AppPropertiesService;
55  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
56  import fr.paris.lutece.util.ReferenceItem;
57  import fr.paris.lutece.util.ReferenceList;
58  import fr.paris.lutece.util.datatable.DataTableManager;
59  import fr.paris.lutece.util.html.HtmlTemplate;
60  import fr.paris.lutece.util.url.UrlItem;
61  
62  import java.io.IOException;
63  import java.text.DateFormat;
64  import java.text.ParseException;
65  import java.text.SimpleDateFormat;
66  import java.util.ArrayList;
67  import java.util.Collection;
68  import java.util.Date;
69  import java.util.HashMap;
70  import java.util.List;
71  import java.util.Map;
72  
73  import javax.servlet.http.HttpServletRequest;
74  import javax.servlet.http.HttpServletResponse;
75  
76  import org.apache.commons.lang3.StringUtils;
77  
78  /**
79   * This class provides the user interface to export data from the database
80   */
81  public class ExportDataJspBean extends AdminFeaturesPageJspBean
82  {
83      public static final String RIGHT_IMPORT_DATA = "IMPORTEXPORT_MANAGEMENT";
84      public static final String PROPERTY_SESSION_AUTOMATIC_EXPORT_TABLE_MANAGER = "importexport.automaticExportTableManager";
85  
86      private static final long serialVersionUID = -8290123143145394692L;
87  
88      private static final String PROPERTY_DATABASE_TABLES = "importexport.database.exportableTableNames";
89      private static final String PROPERTY_MESSAGE_EXPORT_DATA_PAGE_TITLE = "importexport.export_data.pageTitle";
90      private static final String PROPERTY_MESSAGE_EXPORT_DATA_COLUMNS_SELECTION_PAGE_TITLE = "importexport.export_data.columns_selection.pageTitle";
91      private static final String PROPERTY_MESSAGE_AUTOMATIC_EXPORT_CONFIGURATION_PAGE_TITLE = "importexport.export_data.automaticExportConfig.pageTitle";
92      private static final String PROPERTY_MESSAGE_ADD_EXPORT_CONFIG = "importexport.export_data.addExportConfig.pageTitle";
93      private static final String PROPERTY_MESSAGE_MODIFY_EXPORT_CONFIG = "importexport.export_data.modifyExportConfig.pageTitle";
94  
95      private static final String PROPERTY_AUTOMATIC_EXPORT_ID = "importexport.export_data.automaticExportConfig.labelId";
96      private static final String PROPERTY_AUTOMATIC_EXPORT_TABLE_NAME = "importexport.export_data.automaticExportConfig.labelTableName";
97      private static final String PROPERTY_AUTOMATIC_EXPORT_OUTPUT_FILE_NAME = "importexport.export_data.automaticExportConfig.labelOutputFileName";
98      private static final String PROPERTY_AUTOMATIC_EXPORT_ACTION = "importexport.export_data.automaticExportConfig.lableActions";
99  
100     private static final String MARK_DATABASE_TABLES = "databaseTables";
101     private static final String MARK_LIST_PLUGIN = "listPlugin";
102     private static final String MARK_COLUMNS = "cols";
103     private static final String MARK_LIST_XSL_EXPORT = "listXslExport";
104     private static final String MARK_TABLE_MANAGER = "tableManager";
105     private static final String MARK_DAEMON_NEXT_SCHEDULE = "daemon_next_schedule";
106     private static final String MARK_DAEMON_INTERVAL = "daemon_interval";
107     private static final String MARK_ID_CONFIG = "idConfig";
108     private static final String MARK_CONF = "conf";
109 
110     private static final String PARAMETER_TABLE_NAME = "databaseTable";
111     private static final String PARAMETER_PLUGIN_NAME = "plugin";
112     private static final String PARAMETER_COLUMNS = "columns";
113     private static final String PARAMETER_XSL_EXPORT_ID = "xslExport";
114     private static final String PARAMETER_OUTPUT_FILE_NAME = "output_file_name";
115     private static final String PARAMETER_PAGE_RELOAD = "pageReload";
116     private static final String PARAMETER_SAVE_EXPORT_CONFIG = "saveExportConfig";
117     private static final String PARAMETER_CANCEL = "cancel";
118 
119     private static final String TEMPLATE_EXPORT_DATA = "admin/plugins/importexport/export_data.html";
120     private static final String TEMPLATE_EXPORT_DATA_SELECT_COLUMNS = "admin/plugins/importexport/export_data_select_columns.html";
121     private static final String TEMPLATE_EXPORT_WAITING = "admin/plugins/importexport/export_waiting.html";
122     private static final String TEMPLATE_AUTOMATIC_EXPORT_CONFIGURATION = "admin/plugins/importexport/automatic_export_config.html";
123     private static final String TEMPLATE_CREATE_MODIFY_EXPORT_CONFIG = "admin/plugins/importexport/create_modify_export_config.html";
124 
125     private static final String MESSAGE_ERROR_NO_COLUMN_SELECTED = "importexport.export_data.error.noColumnSelected";
126     private static final String MESSAGE_ERROR_UPDATE_COLUMNS = "importexport.export_data.automaticExportConfig.error.updateColumns";
127     private static final String MESSAGE_ERROR_MANDATORY_FIELDS = "portal.util.message.mandatoryFields";
128     private static final String MESSAGE_ERROR_DATE_FORMAT = "importexport.export_data.automaticExportConfig.error.dateFormat";
129     private static final String MESSAGE_CONFIRM_DELETE_EXPORT_CONFIG = "importexport.export_data.automaticExportConfig.confirmDeleteExportConfig";
130 
131     private static final String JSP_URL_EXPORT_WAITING_PAGE = "jsp/admin/plugins/importexport/GetExportProcessing.jsp";
132     private static final String JSP_URL_AUTOMATIC_EXPORT_CONFIGURATION = "jsp/admin/plugins/importexport/GetAutomaticExportConfiguration.jsp";
133     private static final String JSP_URL_DO_DELETE_EXPORT_CONFIG = "jsp/admin/plugins/importexport/DoDeleteExportConfig.jsp";
134     private static final String JSP_URL_EXPORT_DATA = "jsp/admin/plugins/importexport/GetExportData.jsp";
135 
136     private static final String CONSTANT_SEMICOLON = ";";
137 
138     private IAutomaticExportConfigDAO _automaticExportConfigDAO = SpringContextService.getBean( ExportManager.BEAN_NAME_AUTOMATIC_EXPORT_CONFIG_DAO );
139 
140     /**
141      * Creates a new ExportDataJspBean object.
142      */
143     public ExportDataJspBean( )
144     {
145     }
146 
147     /**
148      * Get the export data page
149      * 
150      * @param request
151      *            The request
152      * @return The HTML content to display
153      */
154     public String getExportData( HttpServletRequest request )
155     {
156         String strDatabaseTables = AppPropertiesService.getProperty( PROPERTY_DATABASE_TABLES );
157         ReferenceList refList = new ReferenceList( );
158         if ( StringUtils.isNotBlank( strDatabaseTables ) )
159         {
160             for ( String strDatabaseTable : strDatabaseTables.split( CONSTANT_SEMICOLON ) )
161             {
162                 ReferenceItem refItem = new ReferenceItem( );
163                 refItem.setCode( strDatabaseTable );
164                 refItem.setName( strDatabaseTable );
165                 refList.add( refItem );
166             }
167         }
168         Map<String, Object> model = new HashMap<String, Object>( );
169 
170         model.put( MARK_DATABASE_TABLES, refList );
171         Collection<Plugin> listPlugins = PluginService.getPluginList( );
172         ReferenceList refListPlugins = new ReferenceList( );
173         ReferenceItem refItemPlugin = new ReferenceItem( );
174         Plugin core = PluginService.getCore( );
175         refItemPlugin.setName( core.getName( ) );
176         refItemPlugin.setCode( core.getName( ) );
177         refListPlugins.add( refItemPlugin );
178         for ( Plugin plugin : listPlugins )
179         {
180             if ( plugin.isDbPoolRequired( ) )
181             {
182                 refItemPlugin = new ReferenceItem( );
183                 refItemPlugin.setName( plugin.getName( ) );
184                 refItemPlugin.setCode( plugin.getName( ) );
185                 refListPlugins.add( refItemPlugin );
186             }
187         }
188         model.put( MARK_LIST_PLUGIN, refListPlugins );
189         ReferenceList refListXslExport = XslExportHome.getRefListByPlugin( ImportExportPlugin.getPlugin( ) );
190         model.put( MARK_LIST_XSL_EXPORT, refListXslExport );
191 
192         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_EXPORT_DATA, AdminUserService.getLocale( request ), model );
193 
194         setPageTitleProperty( PROPERTY_MESSAGE_EXPORT_DATA_PAGE_TITLE );
195 
196         return getAdminPage( template.getHtml( ) );
197     }
198 
199     /**
200      * Get the page to choose which columns of the table to export
201      * 
202      * @param request
203      *            The request
204      * @return The HTML content to display
205      * @throws AccessDeniedException
206      *             If the database table has not been declared as an exportable table
207      */
208     public String getExportColumns( HttpServletRequest request ) throws AccessDeniedException
209     {
210         String strTableName = request.getParameter( PARAMETER_TABLE_NAME );
211         String strPluginName = request.getParameter( PARAMETER_PLUGIN_NAME );
212         String strXslExportId = request.getParameter( PARAMETER_XSL_EXPORT_ID );
213         Plugin plugin = PluginService.getPlugin( strPluginName );
214         boolean bAuthorizedTable = false;
215         String strDatabaseTables = AppPropertiesService.getProperty( PROPERTY_DATABASE_TABLES );
216         for ( String strDatabaseTable : strDatabaseTables.split( CONSTANT_SEMICOLON ) )
217         {
218             if ( StringUtils.equals( strDatabaseTable, strTableName ) )
219             {
220                 bAuthorizedTable = true;
221                 break;
222             }
223         }
224         if ( !bAuthorizedTable )
225         {
226             throw new AccessDeniedException( "The database table '" + strTableName + "' has NOT been decalred as an exportable table" );
227         }
228         Map<String, Object> model = new HashMap<String, Object>( );
229 
230         model.put( PARAMETER_TABLE_NAME, strTableName );
231         model.put( PARAMETER_PLUGIN_NAME, strPluginName );
232         model.put( PARAMETER_XSL_EXPORT_ID, strXslExportId );
233         List<String> listColumns = ExportDAO.getTableColumnsNames( strTableName, plugin );
234         model.put( MARK_COLUMNS, listColumns );
235 
236         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_EXPORT_DATA_SELECT_COLUMNS, AdminUserService.getLocale( request ), model );
237 
238         setPageTitleProperty( PROPERTY_MESSAGE_EXPORT_DATA_COLUMNS_SELECTION_PAGE_TITLE );
239 
240         return getAdminPage( template.getHtml( ) );
241     }
242 
243     /**
244      * Do export data from a database table and start a download of the export
245      * 
246      * @param request
247      *            The request
248      * @return The next URL to redirect to
249      * @throws AccessDeniedException
250      *             If the database table has not been declared as an exportable table
251      * @throws IOException
252      *             If an IOException occurs
253      */
254     public String doExportData( HttpServletRequest request ) throws AccessDeniedException, IOException
255     {
256         String strTableName = request.getParameter( PARAMETER_TABLE_NAME );
257         String strPluginName = request.getParameter( PARAMETER_PLUGIN_NAME );
258         String strXslExportId = request.getParameter( PARAMETER_XSL_EXPORT_ID );
259         Plugin plugin = PluginService.getPlugin( strPluginName );
260         boolean bAuthorizedTable = false;
261         String strDatabaseTables = AppPropertiesService.getProperty( PROPERTY_DATABASE_TABLES );
262         for ( String strDatabaseTable : strDatabaseTables.split( CONSTANT_SEMICOLON ) )
263         {
264             if ( StringUtils.equals( strDatabaseTable, strTableName ) )
265             {
266                 bAuthorizedTable = true;
267                 break;
268             }
269         }
270         if ( !bAuthorizedTable )
271         {
272             throw new AccessDeniedException( "The database table '" + strTableName + "' has NOT been decalred as an exportable table" );
273         }
274 
275         String [ ] strColumns = request.getParameterValues( PARAMETER_COLUMNS );
276         if ( strColumns == null || strColumns.length == 0 )
277         {
278             return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_NO_COLUMN_SELECTED, AdminMessage.TYPE_STOP );
279         }
280         List<String> listColumns = new ArrayList<String>( strColumns.length );
281         for ( String strColumn : strColumns )
282         {
283             listColumns.add( strColumn );
284         }
285 
286         int nXslExportId = 0;
287         if ( StringUtils.isNumeric( strXslExportId ) )
288         {
289             nXslExportId = Integer.parseInt( strXslExportId );
290         }
291         ExportManager.registerAsynchronousExport( strTableName, listColumns, nXslExportId, plugin, AdminUserService.getAdminUser( request ) );
292         return AppPathService.getBaseUrl( request ) + JSP_URL_EXPORT_WAITING_PAGE;
293     }
294 
295     /**
296      * Get the waiting page that indicates that an export is processing, or the result page if the export has ended.
297      * 
298      * @param request
299      *            The request
300      * @param response
301      *            The response
302      * @return The HTML content to display, or null if a download has be initialized
303      */
304     public String getExportProcessing( HttpServletRequest request, HttpServletResponse response )
305     {
306         AdminUser admin = AdminUserService.getAdminUser( request );
307         if ( ExportManager.hasExportInProcess( admin.getUserId( ) ) )
308         {
309             HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_EXPORT_WAITING, AdminUserService.getLocale( request ) );
310             return getAdminPage( template.getHtml( ) );
311         }
312 
313         return getExportResult( request, response );
314     }
315 
316     /**
317      * Get the page that display the result of an import, or the export data main page if no export result is available for the given user
318      * 
319      * @param request
320      *            The request
321      * @param response
322      *            The response
323      * @return The HTML content to display, or null if a download has be initialized
324      */
325     public String getExportResult( HttpServletRequest request, HttpServletResponse response )
326     {
327         String strFileURl = ExportManager.getExportResult( AdminUserService.getAdminUser( request ) );
328         if ( StringUtils.isNotBlank( strFileURl ) )
329         {
330             try
331             {
332                 response.sendRedirect( AppPathService.getBaseUrl( request ) + strFileURl );
333                 return null;
334             }
335             catch( IOException e )
336             {
337                 AppLogService.error( e.getMessage( ), e );
338             }
339         }
340         return getExportData( request );
341     }
342 
343     /**
344      * Get the page to manage automatic exports
345      * 
346      * @param request
347      *            The request
348      * @return The HTML content to display
349      */
350     public String getAutomaticExportConfiguration( HttpServletRequest request )
351     {
352         List<AutomaticExportConfig> listConfig = _automaticExportConfigDAO.findAll( false );
353         String strDaemonNextSchedule = ExportDaemon.getDaemonNextSchedule( );
354         String strInterval = ExportDaemon.getDaemonInterval( );
355         long lInterval = Long.parseLong( strInterval );
356 
357         Date dateNextSchedule;
358         if ( !StringUtils.isEmpty( strDaemonNextSchedule ) && StringUtils.isNumeric( strDaemonNextSchedule ) )
359         {
360             dateNextSchedule = new Date( Long.parseLong( strDaemonNextSchedule ) );
361         }
362         else
363         {
364             dateNextSchedule = new Date( );
365         }
366 
367         DataTableManager<AutomaticExportConfig> tableManager = (DataTableManager<AutomaticExportConfig>) request.getSession( )
368                 .getAttribute( PROPERTY_SESSION_AUTOMATIC_EXPORT_TABLE_MANAGER );
369         if ( tableManager == null )
370         {
371             tableManager = new DataTableManager<AutomaticExportConfig>( JSP_URL_AUTOMATIC_EXPORT_CONFIGURATION, JSP_URL_AUTOMATIC_EXPORT_CONFIGURATION, 50,
372                     true );
373             tableManager.addColumn( PROPERTY_AUTOMATIC_EXPORT_ID, "id", true );
374             tableManager.addColumn( PROPERTY_AUTOMATIC_EXPORT_TABLE_NAME, "tableName", true );
375             tableManager.addColumn( PROPERTY_AUTOMATIC_EXPORT_OUTPUT_FILE_NAME, "outputFileName", true );
376             tableManager.addActionColumn( PROPERTY_AUTOMATIC_EXPORT_ACTION );
377             request.getSession( ).setAttribute( PROPERTY_SESSION_AUTOMATIC_EXPORT_TABLE_MANAGER, tableManager );
378         }
379         tableManager.filterSortAndPaginate( request, listConfig );
380 
381         Map<String, Object> model = new HashMap<String, Object>( );
382         model.put( MARK_TABLE_MANAGER, tableManager );
383         DateFormat df = new SimpleDateFormat( "dd/MM/yyyy HH:mm:ss" );
384         model.put( MARK_DAEMON_NEXT_SCHEDULE, df.format( dateNextSchedule ) );
385         model.put( MARK_DAEMON_INTERVAL, lInterval / 1000 );
386 
387         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_AUTOMATIC_EXPORT_CONFIGURATION, AdminUserService.getLocale( request ), model );
388 
389         setPageTitleProperty( PROPERTY_MESSAGE_AUTOMATIC_EXPORT_CONFIGURATION_PAGE_TITLE );
390         String strContent = template.getHtml( );
391         tableManager.clearItems( );
392         return getAdminPage( strContent );
393     }
394 
395     /**
396      * Do modify the configuration of automatic export
397      * 
398      * @param request
399      *            The request
400      * @return The next URL to redirect to
401      */
402     public String doModifyAutomaticExport( HttpServletRequest request )
403     {
404         if ( StringUtils.isEmpty( request.getParameter( PARAMETER_CANCEL ) ) )
405         {
406             String strDaemonNextSchedule = request.getParameter( MARK_DAEMON_NEXT_SCHEDULE );
407             String strInterval = request.getParameter( MARK_DAEMON_INTERVAL );
408             if ( StringUtils.isEmpty( strDaemonNextSchedule ) || StringUtils.isEmpty( strInterval ) || !StringUtils.isNumeric( strInterval ) )
409             {
410                 return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_MANDATORY_FIELDS, AdminMessage.TYPE_ERROR );
411             }
412             long lInterval = Long.parseLong( strInterval );
413             try
414             {
415                 DateFormat df = new SimpleDateFormat( "dd/MM/yyyy HH:mm:ss" );
416                 Date dateNextSchedule = df.parse( strDaemonNextSchedule );
417                 ExportDaemon.setDaemonNextSchedule( dateNextSchedule );
418                 ExportDaemon.setDaemonInterval( lInterval * 1000 );
419 
420             }
421             catch( ParseException e )
422             {
423                 return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_DATE_FORMAT, AdminMessage.TYPE_ERROR );
424             }
425         }
426         return AppPathService.getBaseUrl( request ) + JSP_URL_EXPORT_DATA;
427     }
428 
429     /**
430      * Get the page to create or modify an export config
431      * 
432      * @param request
433      *            The request
434      * @param response
435      *            The response
436      * @return The HTML content to display, or null if a redirection has already been send
437      */
438     public String getCreateModifyExportConfig( HttpServletRequest request, HttpServletResponse response )
439     {
440 
441         if ( StringUtils.isNotEmpty( request.getParameter( PARAMETER_CANCEL ) ) )
442         {
443             try
444             {
445                 response.sendRedirect( AppPathService.getBaseUrl( request ) + JSP_URL_AUTOMATIC_EXPORT_CONFIGURATION );
446             }
447             catch( IOException e )
448             {
449                 AppLogService.error( e.getMessage( ), e );
450             }
451             return null;
452         }
453         if ( StringUtils.isNotEmpty( request.getParameter( PARAMETER_SAVE_EXPORT_CONFIG ) ) )
454         {
455             try
456             {
457                 response.sendRedirect( doCreateModifyExportConfig( request ) );
458             }
459             catch( IOException e )
460             {
461                 AppLogService.error( e.getMessage( ), e );
462             }
463             return null;
464         }
465 
466         String strIdConfig = request.getParameter( MARK_ID_CONFIG );
467         boolean bPageReload = StringUtils.isNotEmpty( request.getParameter( PARAMETER_PAGE_RELOAD ) );
468         Map<String, Object> model = new HashMap<String, Object>( );
469         if ( StringUtils.isNotEmpty( strIdConfig ) && StringUtils.isNumeric( strIdConfig ) )
470         {
471             int nIdConfig = Integer.parseInt( strIdConfig );
472             AutomaticExportConfig config;
473             if ( bPageReload )
474             {
475                 config = getConfigFromRequest( request );
476             }
477             else
478             {
479                 config = _automaticExportConfigDAO.findById( nIdConfig );
480             }
481             if ( StringUtils.isNotEmpty( config.getTableName( ) ) )
482             {
483                 List<String> listColumns = ExportDAO.getTableColumnsNames( config.getTableName( ), config.getPlugin( ) );
484                 model.put( MARK_COLUMNS, listColumns );
485             }
486             model.put( MARK_CONF, config );
487             model.put( MARK_ID_CONFIG, strIdConfig );
488             setPageTitleProperty( PROPERTY_MESSAGE_MODIFY_EXPORT_CONFIG );
489         }
490         else
491         {
492             setPageTitleProperty( PROPERTY_MESSAGE_ADD_EXPORT_CONFIG );
493             if ( bPageReload )
494             {
495                 AutomaticExportConfig config = getConfigFromRequest( request );
496                 model.put( MARK_CONF, config );
497                 if ( StringUtils.isNotEmpty( config.getTableName( ) ) )
498                 {
499                     List<String> listColumns = ExportDAO.getTableColumnsNames( config.getTableName( ), config.getPlugin( ) );
500                     model.put( MARK_COLUMNS, listColumns );
501                 }
502             }
503         }
504 
505         String strDatabaseTables = AppPropertiesService.getProperty( PROPERTY_DATABASE_TABLES );
506         ReferenceList refList = new ReferenceList( );
507         if ( StringUtils.isNotBlank( strDatabaseTables ) )
508         {
509             for ( String strDatabaseTable : strDatabaseTables.split( CONSTANT_SEMICOLON ) )
510             {
511                 ReferenceItem refItem = new ReferenceItem( );
512                 refItem.setCode( strDatabaseTable );
513                 refItem.setName( strDatabaseTable );
514                 refList.add( refItem );
515             }
516         }
517         model.put( MARK_DATABASE_TABLES, refList );
518 
519         Collection<Plugin> listPlugins = PluginService.getPluginList( );
520         ReferenceList refListPlugins = new ReferenceList( );
521         ReferenceItem refItemPlugin = new ReferenceItem( );
522         Plugin core = PluginService.getCore( );
523         refItemPlugin.setName( core.getName( ) );
524         refItemPlugin.setCode( core.getName( ) );
525         refListPlugins.add( refItemPlugin );
526         for ( Plugin plugin : listPlugins )
527         {
528             if ( plugin.isDbPoolRequired( ) )
529             {
530                 refItemPlugin = new ReferenceItem( );
531                 refItemPlugin.setName( plugin.getName( ) );
532                 refItemPlugin.setCode( plugin.getName( ) );
533                 refListPlugins.add( refItemPlugin );
534             }
535         }
536         model.put( MARK_LIST_PLUGIN, refListPlugins );
537         ReferenceList refListXslExport = XslExportHome.getRefListByPlugin( ImportExportPlugin.getPlugin( ) );
538         model.put( MARK_LIST_XSL_EXPORT, refListXslExport );
539 
540         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_MODIFY_EXPORT_CONFIG, AdminUserService.getLocale( request ), model );
541         return getAdminPage( template.getHtml( ) );
542     }
543 
544     /**
545      * Do create or modify an export config
546      * 
547      * @param request
548      *            The request
549      * @return The next URL to redirect to
550      */
551     public String doCreateModifyExportConfig( HttpServletRequest request )
552     {
553         if ( StringUtils.isEmpty( request.getParameter( PARAMETER_CANCEL ) ) )
554         {
555             AutomaticExportConfig config = getConfigFromRequest( request );
556 
557             if ( StringUtils.isEmpty( config.getTableName( ) ) || StringUtils.isEmpty( config.getOutputFileName( ) ) )
558             {
559                 return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_MANDATORY_FIELDS, AdminMessage.TYPE_ERROR );
560             }
561             if ( config.getListColumns( ) == null || config.getListColumns( ).size( ) == 0 )
562             {
563                 return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_NO_COLUMN_SELECTED, AdminMessage.TYPE_ERROR );
564             }
565             List<String> listColumns = ExportDAO.getTableColumnsNames( config.getTableName( ), config.getPlugin( ) );
566             if ( !listColumns.containsAll( config.getListColumns( ) ) )
567             {
568                 return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_UPDATE_COLUMNS, AdminMessage.TYPE_ERROR );
569             }
570 
571             if ( config.getId( ) > 0 )
572             {
573                 _automaticExportConfigDAO.update( config );
574             }
575             else
576             {
577                 _automaticExportConfigDAO.insert( config );
578             }
579         }
580         return AppPathService.getBaseUrl( request ) + JSP_URL_AUTOMATIC_EXPORT_CONFIGURATION;
581     }
582 
583     /**
584      * Get an export config from an HTTP request
585      * 
586      * @param request
587      *            The request
588      * @return The export config containing data in the request
589      */
590     private AutomaticExportConfig getConfigFromRequest( HttpServletRequest request )
591     {
592         String strIdConfig = request.getParameter( MARK_ID_CONFIG );
593         String strTableName = request.getParameter( PARAMETER_TABLE_NAME );
594         String strOutputFileName = request.getParameter( PARAMETER_OUTPUT_FILE_NAME );
595         String strPluginName = request.getParameter( PARAMETER_PLUGIN_NAME );
596         String strXslExport = request.getParameter( PARAMETER_XSL_EXPORT_ID );
597         String [ ] strListColumns = request.getParameterValues( PARAMETER_COLUMNS );
598         AutomaticExportConfigbusiness/export/AutomaticExportConfig.html#AutomaticExportConfig">AutomaticExportConfig config = new AutomaticExportConfig( );
599         if ( StringUtils.isNotEmpty( strIdConfig ) && StringUtils.isNumeric( strIdConfig ) )
600         {
601             int nId = Integer.parseInt( strIdConfig );
602             config.setId( nId );
603         }
604         config.setTableName( strTableName );
605         config.setOutputFileName( strOutputFileName );
606         config.setPlugin( PluginService.getPlugin( strPluginName ) );
607         if ( StringUtils.isNotEmpty( strXslExport ) && StringUtils.isNumeric( strXslExport ) )
608         {
609             config.setXslStylesheetId( Integer.parseInt( strXslExport ) );
610         }
611         if ( strListColumns != null && strListColumns.length > 0 )
612         {
613             List<String> listColumns = new ArrayList<String>( strListColumns.length );
614             for ( String strColumn : strListColumns )
615             {
616                 listColumns.add( strColumn );
617             }
618             config.setListColumns( listColumns );
619         }
620 
621         return config;
622     }
623 
624     /**
625      * Confirm the removal of an automatic export configuration
626      * 
627      * @param request
628      *            The request
629      * @return The next URL to redirect to
630      */
631     public String confirmDeleteExportConfig( HttpServletRequest request )
632     {
633         UrlItem urlItem = new UrlItem( JSP_URL_DO_DELETE_EXPORT_CONFIG );
634         urlItem.addParameter( MARK_ID_CONFIG, request.getParameter( MARK_ID_CONFIG ) );
635         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_DELETE_EXPORT_CONFIG, urlItem.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
636     }
637 
638     /**
639      * Do remove an automatic export configuration
640      * 
641      * @param request
642      *            The request
643      * @return The next URL to redirect to
644      */
645     public String doDeleteExportConfig( HttpServletRequest request )
646     {
647         String strId = request.getParameter( MARK_ID_CONFIG );
648         if ( StringUtils.isNotEmpty( strId ) && StringUtils.isNumeric( strId ) )
649         {
650             int nId = Integer.parseInt( strId );
651             _automaticExportConfigDAO.delete( nId );
652         }
653         return AppPathService.getBaseUrl( request ) + JSP_URL_AUTOMATIC_EXPORT_CONFIGURATION;
654     }
655 }