View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.service.daemon;
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.business.Form;
39  import fr.paris.lutece.plugins.form.business.FormFilter;
40  import fr.paris.lutece.plugins.form.business.FormHome;
41  import fr.paris.lutece.plugins.form.service.FormPlugin;
42  import fr.paris.lutece.plugins.form.service.export.IExportService;
43  import fr.paris.lutece.plugins.form.service.parameter.FormParameterService;
44  import fr.paris.lutece.portal.service.daemon.Daemon;
45  import fr.paris.lutece.portal.service.plugin.Plugin;
46  import fr.paris.lutece.portal.service.plugin.PluginService;
47  import fr.paris.lutece.portal.service.util.AppLogService;
48  
49  import org.apache.commons.lang.StringUtils;
50  
51  import java.util.List;
52  
53  /**
54   *
55   * ExportResponsesDaemon
56   *
57   */
58  public class ExportResponsesDaemon extends Daemon
59  {
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      public void run( )
65      {
66          StringBuilder sbLog = new StringBuilder( );
67          FormParameterService formParamService = FormParameterService.getService( );
68          int nIdExport = formParamService.getIdExportResponsesDaemon( );
69          Plugin plugin = PluginService.getPlugin( FormPlugin.PLUGIN_NAME );
70          ExportFormat exportFormat = ExportFormatHome.findByPrimaryKey( nIdExport, plugin );
71  
72          if ( exportFormat != null )
73          {
74              FormFilter filter = new FormFilter( );
75              filter.setIdState( 1 );
76  
77              List<Form> listForms = FormHome.getFormList( filter, plugin );
78  
79              if ( ( listForms != null ) && !listForms.isEmpty( ) )
80              {
81                  for ( Form form : listForms )
82                  {
83                      if ( form != null )
84                      {
85                          exportFormResponses( form, sbLog, exportFormat, plugin );
86                      }
87                  }
88              }
89              else
90              {
91                  sbLog.append( "\nThere are no forms in the application. No export done." );
92              }
93          }
94          else
95          {
96              sbLog.append( "\nInvalid export format. The daemon is not well configured. \nPlease configure the daemon export format in the "
97                      + "advanced parameters of the plugin-form." );
98          }
99  
100         if ( StringUtils.isBlank( sbLog.toString( ) ) )
101         {
102             sbLog.append( "\nNo responses to export" );
103         }
104 
105         setLastRunLogs( sbLog.toString( ) );
106     }
107 
108     /**
109      * Export the form responses
110      * 
111      * @param form
112      *            the form
113      * @param sbLog
114      *            the log
115      * @param exportFormat
116      *            the export format
117      * @param plugin
118      *            the plugin
119      */
120     private void exportFormResponses( Form form, StringBuilder sbLog, ExportFormat exportFormat, Plugin plugin )
121     {
122         sbLog.append( "\nExporting responses for form ID " + form.getIdForm( ) );
123 
124         IExportService exportService = FormParameterService.getService( ).getFileExportDaemonType( );
125 
126         if ( exportService != null )
127         {
128             exportService.doExport( form, sbLog, exportFormat, plugin );
129         }
130         else
131         {
132             AppLogService.error( "ExportResponsesDaemon - No file export service found." );
133             sbLog.append( "\nNo file export service found." );
134         }
135     }
136 }