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.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   * Export manager
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       * Private constructor
75       */
76      private ExportManager( )
77      {
78      }
79  
80      /**
81       * Do process the export of a table of the database
82       * 
83       * @param strTableName
84       *            The name of the database table to export
85       * @param listColumns
86       *            The list of columns to export
87       * @param nXSLStylesheetId
88       *            The id of the XSL export style sheet to use to format data retrieved from the database
89       * @param plugin
90       *            The plugin to get the pool of
91       * @return The string containing the formatted values of the table of the database
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      * Register an import to be generated asynchronously
133      * 
134      * @param strTableName
135      *            The name of the database table to export
136      * @param listColumns
137      *            The list of columns to export
138      * @param nXSLStylesheetId
139      *            The id of the XSL export style sheet to use to format data retrieved from the database
140      * @param plugin
141      *            The plugin to get the pool of
142      * @param admin
143      *            The admin user that started the export, or null if it has been started by a daemon
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      * Check if an admin user has an export processing
155      * 
156      * @param nAdminId
157      *            The id of the admin user
158      * @return True if the admin user has an export processing, false otherwise
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      * Get the result of an export
175      * 
176      * @param admin
177      *            The admin user that started the export
178      * @return The URL of the generated file relative from the root of the webapp, or null if no export was found
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      * Do process an export into a file of the file system. If the file can not be written into, then the export is not processed.
196      * 
197      * @param strOutputFilePath
198      *            The path of the file to save the export into
199      * @param strTableName
200      *            The name of the database table to export
201      * @param listColumns
202      *            The list of columns to export
203      * @param nXSLStylesheetId
204      *            The id of the XSL export style sheet to use to format data retrieved from the database
205      * @param plugin
206      *            The plugin to get the pool of
207      * @return True if the export succeeded and the file was written, false otherwise
208      * @see #doProcessExport(String, List, int, Plugin)
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                 // If we could not remove the file, then we ignore it
223                 if ( !bRemoved )
224                 {
225                     return false;
226                 }
227             }
228             // If the file does not exist, we check that its containing folder exists
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                 // This close both the file writer and the buffered writer
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             // We close both writers if they are still open
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 }