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;
35
36 import fr.paris.lutece.plugins.importexport.business.ImportExportElement;
37 import fr.paris.lutece.plugins.importexport.business.export.ExportDAO;
38 import fr.paris.lutece.plugins.importexport.business.export.RowExportData;
39 import fr.paris.lutece.portal.business.user.AdminUser;
40 import fr.paris.lutece.portal.service.daemon.ThreadLauncherDaemon;
41 import fr.paris.lutece.portal.service.plugin.Plugin;
42 import fr.paris.lutece.portal.service.util.AppLogService;
43 import fr.paris.lutece.portal.service.util.AppPropertiesService;
44 import fr.paris.lutece.portal.service.xsl.XslExportService;
45 import fr.paris.lutece.util.xml.XmlUtil;
46
47 import java.io.BufferedWriter;
48 import java.io.File;
49 import java.io.FileWriter;
50 import java.io.IOException;
51 import java.util.HashMap;
52 import java.util.List;
53 import java.util.Map;
54
55
56
57
58 public final class ExportManager
59 {
60 public static final String BEAN_NAME_AUTOMATIC_EXPORT_CONFIG_DAO = "importexport.automaticExportConfigDAO";
61
62 private static final String XML_TAG_EXPORT = "export";
63 private static final String XML_TAG_ROW = "row";
64 private static final String XML_PARAMETER_COLUMNS = "columns";
65
66 private static final String PROPERTY_EXPORT_COLUMN_NAME_SEPARATOR = "importexport.exportdata.xml.columnNameSeparator";
67
68 private static final String CONSTANT_SEMICOLON = ";";
69
70 private static ExportDAOportexport/business/export/ExportDAO.html#ExportDAO">ExportDAO _exportDAO = new ExportDAO( );
71 private static Map<Integer, RunnableExportService> _mapRunningImports = new HashMap<Integer, RunnableExportService>( );
72
73
74
75
76 private ExportManager( )
77 {
78 }
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 public static String doProcessExport( String strTableName, List<String> listColumns, int nXSLStylesheetId, Plugin plugin )
94 {
95 String strSeparator = AppPropertiesService.getProperty( PROPERTY_EXPORT_COLUMN_NAME_SEPARATOR, CONSTANT_SEMICOLON );
96 StringBuffer sbXml = new StringBuffer( XmlUtil.getXmlHeader( ) );
97 Map<String, String> mapAttributes = new HashMap<String, String>( );
98 StringBuilder sbColumns = new StringBuilder( );
99 boolean bIsFirst = true;
100 for ( String strColumnName : listColumns )
101 {
102 if ( !bIsFirst )
103 {
104 sbColumns.append( strSeparator );
105 }
106 else
107 {
108 bIsFirst = false;
109 }
110 sbColumns.append( strColumnName );
111 }
112
113 mapAttributes.put( XML_PARAMETER_COLUMNS, sbColumns.toString( ) );
114 XmlUtil.beginElement( sbXml, XML_TAG_EXPORT, mapAttributes );
115
116 List<RowExportData> listRowData = _exportDAO.getDataFromTable( strTableName, listColumns, plugin );
117
118 for ( RowExportData rowExportData : listRowData )
119 {
120 XmlUtil.beginElement( sbXml, XML_TAG_ROW );
121 for ( ImportExportElement element : rowExportData.getListExportElements( ) )
122 {
123 XmlUtil.addElementHtml( sbXml, element.getColumnName( ), element.getValue( ) );
124 }
125 XmlUtil.endElement( sbXml, XML_TAG_ROW );
126 }
127 XmlUtil.endElement( sbXml, XML_TAG_EXPORT );
128 return XslExportService.exportXMLWithXSL( nXSLStylesheetId, sbXml.toString( ) );
129 }
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145 public static void registerAsynchronousExport( String strTableName, List<String> listColumns, int nXSLStylesheetId, Plugin plugin, AdminUser admin )
146 {
147 RunnableExportServiceice/export/RunnableExportService.html#RunnableExportService">RunnableExportService exportService = new RunnableExportService( strTableName, listColumns, nXSLStylesheetId, plugin,
148 Integer.toString( admin.getUserId( ) ) );
149 _mapRunningImports.put( admin.getUserId( ), exportService );
150 ThreadLauncherDaemon.addItemToQueue( exportService, strTableName, plugin );
151 }
152
153
154
155
156
157
158
159
160 public static boolean hasExportInProcess( int nAdminId )
161 {
162 if ( nAdminId > 0 )
163 {
164 RunnableExportService runnableImportService = _mapRunningImports.get( nAdminId );
165 if ( runnableImportService != null )
166 {
167 return runnableImportService.getServiceStatus( ) != RunnableExportService.STATUS_FINISHED;
168 }
169 }
170 return false;
171 }
172
173
174
175
176
177
178
179
180 public static String getExportResult( AdminUser admin )
181 {
182 RunnableExportService exportService = _mapRunningImports.get( admin.getUserId( ) );
183 if ( exportService != null )
184 {
185 if ( exportService.getServiceStatus( ) == RunnableExportService.STATUS_FINISHED )
186 {
187 _mapRunningImports.remove( admin.getUserId( ) );
188 return exportService.getExportedFileRelativeUrl( );
189 }
190 }
191 return null;
192 }
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 public static boolean doProcessExportIntoFile( String strOutputFilePath, String strTableName, List<String> listColumns, int nXSLStylesheetId,
211 Plugin plugin )
212 {
213 FileWriter fileWriter = null;
214 BufferedWriter bufferedWriter = null;
215 boolean bResult = false;
216 try
217 {
218 File file = new File( strOutputFilePath );
219 if ( file.exists( ) )
220 {
221 boolean bRemoved = file.delete( );
222
223 if ( !bRemoved )
224 {
225 return false;
226 }
227 }
228
229 else
230 {
231 File containingFolder = file.getParentFile( );
232 if ( !containingFolder.exists( ) )
233 {
234 containingFolder.mkdirs( );
235 }
236 }
237
238 if ( file.createNewFile( ) && file.canWrite( ) )
239 {
240 String strExport = doProcessExport( strTableName, listColumns, nXSLStylesheetId, plugin );
241 fileWriter = new FileWriter( file );
242 bufferedWriter = new BufferedWriter( fileWriter );
243 bufferedWriter.write( strExport );
244 bufferedWriter.flush( );
245
246 bufferedWriter.close( );
247 bufferedWriter = null;
248 fileWriter = null;
249 bResult = true;
250 }
251 }
252 catch( Exception e )
253 {
254 AppLogService.error( e.getMessage( ), e );
255 }
256 finally
257 {
258
259 if ( bufferedWriter != null )
260 {
261 try
262 {
263 bufferedWriter.close( );
264 }
265 catch( IOException e )
266 {
267 AppLogService.error( e.getMessage( ), e );
268 }
269 }
270 if ( fileWriter != null )
271 {
272 try
273 {
274 fileWriter.close( );
275 }
276 catch( IOException e )
277 {
278 AppLogService.error( e.getMessage( ), e );
279 }
280 }
281 }
282 return bResult;
283 }
284 }