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.portal.business.xsl.XslExport;
37  import fr.paris.lutece.portal.business.xsl.XslExportHome;
38  import fr.paris.lutece.portal.service.plugin.Plugin;
39  import fr.paris.lutece.portal.service.util.AppLogService;
40  import fr.paris.lutece.portal.service.util.AppPathService;
41  
42  import java.util.List;
43  
44  /**
45   * RunnableExportService
46   */
47  public class RunnableExportService implements Runnable
48  {
49  
50      public static final int STATUS_QUEUED = 0;
51      public static final int STATUS_WORKING = 1;
52      public static final int STATUS_FINISHED = 2;
53  
54      private static final String PLUGIN_IMPORTEXPORT_FOLDER = "plugins/importexport/";
55      private static final String CONSTANT_SLASH = "/";
56      private static final String CONSTANT_POINT = ".";
57  
58      private int _nStatus = STATUS_QUEUED;
59      private String _strTableName;
60      private List<String> _listColumns;
61      private int _nXSLStylesheetId;
62      private Plugin _plugin;
63      private String _strExportKey;
64      private String _strFileExtention;
65  
66      /**
67       * Creates a new export to run in a dedicated thread
68       * 
69       * @param strTableName
70       *            The name of table to export
71       * @param listColumns
72       *            The list of columns to export
73       * @param nXSLStylesheetId
74       *            The id of the stylesheet to apply to the data retrieved from the database. If the id is 0, then the row XML is returned
75       * @param plugin
76       *            The plugin to get the pool of
77       * @param strExportKey
78       *            The key of the export. Only one export can be run at the same time for a given key
79       */
80      public RunnableExportService( String strTableName, List<String> listColumns, int nXSLStylesheetId, Plugin plugin, String strExportKey )
81      {
82          this._strTableName = strTableName;
83          this._listColumns = listColumns;
84          this._plugin = plugin;
85          this._nXSLStylesheetId = nXSLStylesheetId;
86          this._strExportKey = strExportKey;
87          XslExport xslExport = XslExportHome.findByPrimaryKey( _nXSLStylesheetId );
88          _strFileExtention = xslExport.getExtension( );
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public void run( )
96      {
97          _nStatus = STATUS_WORKING;
98          try
99          {
100             ExportManager.doProcessExportIntoFile( getExportedFileName( ), _strTableName, _listColumns, _nXSLStylesheetId, _plugin );
101         }
102         catch( Exception e )
103         {
104             AppLogService.error( e.getMessage( ), e );
105         }
106         finally
107         {
108             _nStatus = STATUS_FINISHED;
109         }
110     }
111 
112     /**
113      * Get the status of the service.
114      * 
115      * @return {@link #STATUS_QUEUED} if the serviced has not been started, {@link #STATUS_WORKING} if it is executing, or {@link #STATUS_FINISHED} if its
116      *         execution has ended.
117      */
118     public int getServiceStatus( )
119     {
120         return _nStatus;
121     }
122 
123     /**
124      * Get the name of the file generated by this export service
125      * 
126      * @return The name of the file generated by this export service
127      */
128     public String getExportedFileName( )
129     {
130         return AppPathService.getWebAppPath( ) + CONSTANT_SLASH + getExportedFileRelativeUrl( );
131     }
132 
133     /**
134      * Get the URL of the file generated by this export service. The URL is relative from the root folder of the webapp
135      * 
136      * @return The URL of the file generated by this export service
137      */
138     public String getExportedFileRelativeUrl( )
139     {
140         return PLUGIN_IMPORTEXPORT_FOLDER + _strExportKey + CONSTANT_SLASH + _strTableName + CONSTANT_POINT + _strFileExtention;
141     }
142 }