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.importexport.service.export.daemon;
35
36 import fr.paris.lutece.plugins.importexport.business.export.AutomaticExportConfig;
37 import fr.paris.lutece.plugins.importexport.business.export.IAutomaticExportConfigDAO;
38 import fr.paris.lutece.plugins.importexport.service.export.ExportManager;
39 import fr.paris.lutece.portal.service.daemon.Daemon;
40 import fr.paris.lutece.portal.service.datastore.DatastoreService;
41 import fr.paris.lutece.portal.service.spring.SpringContextService;
42 import fr.paris.lutece.portal.service.util.AppLogService;
43 import fr.paris.lutece.portal.service.util.AppPropertiesService;
44
45 import java.util.Date;
46 import java.util.List;
47
48 import org.apache.commons.lang3.StringUtils;
49
50
51
52
53 public class ExportDaemon extends Daemon
54 {
55 private static final String PARAMETER_KEY_DAEMON_NEXT_SCHEDULE = "importexport.exportDaemon.daemonNextSchedule";
56 private static final String PARAMETER_KEY_DAEMON_INTERVAL = "importexport.exportDaemon.daemonInterval";
57
58 private static final String PROPERTY_EXPORT_FOLDER = "importexport.exportDaemon.exportFolder";
59
60 private static final String CONSTANT_DEFAULT_DAEMON_INTERVAL = "86400000";
61 private static final String CONSTANT_SLASH = "/";
62
63 private IAutomaticExportConfigDAO _configDAO;
64
65
66
67
68 @Override
69 public void run( )
70 {
71 String strNextSchedule = DatastoreService.getInstanceDataValue( PARAMETER_KEY_DAEMON_NEXT_SCHEDULE, StringUtils.EMPTY );
72 if ( StringUtils.isNotEmpty( strNextSchedule ) && StringUtils.isNumeric( strNextSchedule ) )
73 {
74 long lNextSchedule = Long.parseLong( strNextSchedule );
75
76 if ( lNextSchedule < new Date( ).getTime( ) )
77 {
78 doExportData( );
79
80 lNextSchedule = new Date( ).getTime( ) + Long.parseLong( getDaemonInterval( ) );
81 DatastoreService.setInstanceDataValue( PARAMETER_KEY_DAEMON_NEXT_SCHEDULE, Long.toString( lNextSchedule ) );
82 }
83 }
84 else
85 {
86 String strDaemonInterval = getDaemonInterval( );
87
88 long lNextSchedule = new Date( ).getTime( ) + Long.parseLong( strDaemonInterval );
89 DatastoreService.setInstanceDataValue( PARAMETER_KEY_DAEMON_NEXT_SCHEDULE, Long.toString( lNextSchedule ) );
90 }
91 }
92
93
94
95
96 private void doExportData( )
97 {
98 List<AutomaticExportConfig> listConfig = getAutomaticExportConfigDAO( ).findAll( true );
99 String strExportFolder = AppPropertiesService.getProperty( PROPERTY_EXPORT_FOLDER );
100 if ( strExportFolder != null && !strExportFolder.endsWith( CONSTANT_SLASH ) )
101 {
102 strExportFolder = strExportFolder + CONSTANT_SLASH;
103 }
104 int nExportSuccess = 0;
105 for ( AutomaticExportConfig config : listConfig )
106 {
107 try
108 {
109 boolean bRes = ExportManager.doProcessExportIntoFile( strExportFolder + config.getOutputFileName( ), config.getTableName( ),
110 config.getListColumns( ), config.getXslStylesheetId( ), config.getPlugin( ) );
111 if ( bRes )
112 {
113 nExportSuccess++;
114 }
115 else
116 {
117 AppLogService
118 .error( "The file '" + config.getOutputFileName( ) + "' was NOT filled with data of the table '" + config.getTableName( ) + "'" );
119 }
120 }
121 catch( Exception e )
122 {
123 AppLogService.error( e.getMessage( ), e );
124 }
125 }
126 setLastRunLogs( nExportSuccess + " out of " + listConfig.size( ) + " exports have been performed" );
127 }
128
129
130
131
132
133
134 private IAutomaticExportConfigDAO getAutomaticExportConfigDAO( )
135 {
136 if ( _configDAO == null )
137 {
138 _configDAO = SpringContextService.getBean( ExportManager.BEAN_NAME_AUTOMATIC_EXPORT_CONFIG_DAO );
139 }
140 return _configDAO;
141 }
142
143
144
145
146
147
148 public static String getDaemonNextSchedule( )
149 {
150 return DatastoreService.getInstanceDataValue( PARAMETER_KEY_DAEMON_NEXT_SCHEDULE, StringUtils.EMPTY );
151 }
152
153
154
155
156
157
158
159 public static void setDaemonNextSchedule( Date dateNextSchedule )
160 {
161 if ( dateNextSchedule != null )
162 {
163 setDaemonNextSchedule( dateNextSchedule.getTime( ) );
164 }
165 }
166
167
168
169
170
171
172
173 public static void setDaemonNextSchedule( long lNextSchedule )
174 {
175 DatastoreService.setInstanceDataValue( PARAMETER_KEY_DAEMON_NEXT_SCHEDULE, Long.toString( lNextSchedule ) );
176 }
177
178
179
180
181
182
183 public static String getDaemonInterval( )
184 {
185 String strDaemonInterval = DatastoreService.getInstanceDataValue( PARAMETER_KEY_DAEMON_INTERVAL, CONSTANT_DEFAULT_DAEMON_INTERVAL );
186 if ( strDaemonInterval == null || !StringUtils.isNumeric( strDaemonInterval ) )
187 {
188
189 strDaemonInterval = CONSTANT_DEFAULT_DAEMON_INTERVAL;
190 DatastoreService.setInstanceDataValue( PARAMETER_KEY_DAEMON_INTERVAL, strDaemonInterval );
191 }
192 return strDaemonInterval;
193 }
194
195
196
197
198
199
200
201 public static void setDaemonInterval( long lInterval )
202 {
203 DatastoreService.setInstanceDataValue( PARAMETER_KEY_DAEMON_INTERVAL, Long.toString( lInterval ) );
204 }
205 }