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.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
56
57
58 public class ExportResponsesDaemon extends Daemon
59 {
60
61
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
110
111
112
113
114
115
116
117
118
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 }