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.business.export;
35  
36  import fr.paris.lutece.plugins.importexport.service.ImportExportPlugin;
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.portal.service.plugin.PluginService;
39  import fr.paris.lutece.util.sql.DAOUtil;
40  
41  import java.util.ArrayList;
42  import java.util.List;
43  
44  import org.apache.commons.lang3.StringUtils;
45  
46  /**
47   * Implementation of the IAutomaticExportConfigDAO interface
48   */
49  public class AutomaticExportConfigDAO implements IAutomaticExportConfigDAO
50  {
51      private static final String SQL_QUERY_NEW_PRIMARY_KEY = " SELECT MAX(id) FROM importexport_export_config ";
52      private static final String SQL_QUERY_FIND_CONFIG_BY_PRIMARY_KEY = " SELECT id, table_name, output_file_name, xsl_stylesheet_id, plugin FROM importexport_export_config WHERE id = ? ";
53      private static final String SQL_QUERY_FIND_ALL_CONFIG = " SELECT id, table_name, output_file_name, xsl_stylesheet_id, plugin FROM importexport_export_config ";
54      private static final String SQL_QUERY_INSERT_CONFIG = " INSERT INTO importexport_export_config( id, table_name, output_file_name, xsl_stylesheet_id, plugin ) VALUES (?,?,?,?,?) ";
55      private static final String SQL_QUERY_UPDATE_CONFIG = " UPDATE importexport_export_config SET table_name = ?, output_file_name = ?, xsl_stylesheet_id = ?, plugin = ? WHERE id = ? ";
56      private static final String SQL_QUERY_DELETE_CONFIG = " DELETE FROM importexport_export_config WHERE id = ? ";
57      private static final String SQL_QUERY_FIND_COLUMNS = " SELECT column_name FROM importexport_export_config_columns WHERE id_config = ? ";
58      private static final String SQL_QUERY_INSERT_COLUMNS = " INSERT INTO importexport_export_config_columns(id_config, column_name) VALUES (?,?) ";
59      private static final String SQL_QUERY_DELETE_COLUMNS = " DELETE FROM importexport_export_config_columns WHERE id_config = ? ";
60  
61      private Plugin _plugin;
62  
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      public AutomaticExportConfig findById( int nId )
68      {
69          AutomaticExportConfig result = null;
70          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_CONFIG_BY_PRIMARY_KEY, getPlugin( ) );
71          daoUtil.setInt( 1, nId );
72          daoUtil.executeQuery( );
73          if ( daoUtil.next( ) )
74          {
75              int nIndex = 1;
76              result = new AutomaticExportConfig( );
77              result.setId( daoUtil.getInt( nIndex++ ) );
78              result.setTableName( daoUtil.getString( nIndex++ ) );
79              result.setOutputFileName( daoUtil.getString( nIndex++ ) );
80              result.setXslStylesheetId( daoUtil.getInt( nIndex++ ) );
81              result.setPlugin( PluginService.getPlugin( daoUtil.getString( nIndex ) ) );
82          }
83          daoUtil.free( );
84          getListColumns( result );
85          return result;
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      @Override
92      public List<AutomaticExportConfig> findAll( boolean bLoadColumns )
93      {
94          List<AutomaticExportConfig> listResult = new ArrayList<AutomaticExportConfig>( );
95          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_ALL_CONFIG, getPlugin( ) );
96          daoUtil.executeQuery( );
97          while ( daoUtil.next( ) )
98          {
99              int nIndex = 1;
100             AutomaticExportConfigrt/business/export/AutomaticExportConfig.html#AutomaticExportConfig">AutomaticExportConfig config = new AutomaticExportConfig( );
101             config.setId( daoUtil.getInt( nIndex++ ) );
102             config.setTableName( daoUtil.getString( nIndex++ ) );
103             config.setOutputFileName( daoUtil.getString( nIndex++ ) );
104             config.setXslStylesheetId( daoUtil.getInt( nIndex++ ) );
105             config.setPlugin( PluginService.getPlugin( daoUtil.getString( nIndex ) ) );
106             listResult.add( config );
107         }
108         daoUtil.free( );
109         if ( bLoadColumns )
110         {
111             for ( AutomaticExportConfig config : listResult )
112             {
113                 getListColumns( config );
114             }
115         }
116         return listResult;
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public synchronized void insert( AutomaticExportConfig config )
124     {
125         config.setId( getNewPrimaryKey( ) );
126         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_CONFIG, getPlugin( ) );
127         int nIndex = 1;
128         daoUtil.setInt( nIndex++, config.getId( ) );
129         daoUtil.setString( nIndex++, config.getTableName( ) );
130         daoUtil.setString( nIndex++, config.getOutputFileName( ) );
131         daoUtil.setInt( nIndex++, config.getXslStylesheetId( ) );
132         if ( config.getPlugin( ) == null )
133         {
134             daoUtil.setString( nIndex, StringUtils.EMPTY );
135         }
136         else
137         {
138             daoUtil.setString( nIndex, config.getPlugin( ).getName( ) );
139         }
140         daoUtil.executeUpdate( );
141         daoUtil.free( );
142         insertColumns( config.getId( ), config.getListColumns( ) );
143     }
144 
145     /**
146      * {@inheritDoc}
147      */
148     @Override
149     public void update( AutomaticExportConfig config )
150     {
151         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_CONFIG, getPlugin( ) );
152         int nIndex = 1;
153         daoUtil.setString( nIndex++, config.getTableName( ) );
154         daoUtil.setString( nIndex++, config.getOutputFileName( ) );
155         daoUtil.setInt( nIndex++, config.getXslStylesheetId( ) );
156         if ( config.getPlugin( ) == null )
157         {
158             daoUtil.setString( nIndex++, StringUtils.EMPTY );
159         }
160         else
161         {
162             daoUtil.setString( nIndex++, config.getPlugin( ).getName( ) );
163         }
164 
165         daoUtil.setInt( nIndex, config.getId( ) );
166         daoUtil.executeUpdate( );
167         daoUtil.free( );
168         removeColumns( config.getId( ) );
169         insertColumns( config.getId( ), config.getListColumns( ) );
170     }
171 
172     /**
173      * {@inheritDoc}
174      */
175     @Override
176     public void delete( int nId )
177     {
178         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_CONFIG, getPlugin( ) );
179         daoUtil.setInt( 1, nId );
180         daoUtil.executeUpdate( );
181         daoUtil.free( );
182         removeColumns( nId );
183     }
184 
185     /**
186      * Get the list of columns associated with a given configuration. The list can be retrieved through the method
187      * {@link AutomaticExportConfig#getListColumns()} of the configuration
188      * 
189      * @param config
190      *            The configuration to set the list of columns
191      */
192     private void getListColumns( AutomaticExportConfig config )
193     {
194         if ( config == null )
195         {
196             return;
197         }
198         List<String> listColumns = new ArrayList<String>( );
199         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_COLUMNS, getPlugin( ) );
200         daoUtil.setInt( 1, config.getId( ) );
201         daoUtil.executeQuery( );
202         while ( daoUtil.next( ) )
203         {
204             listColumns.add( daoUtil.getString( 1 ) );
205         }
206         daoUtil.free( );
207         config.setListColumns( listColumns );
208     }
209 
210     /**
211      * Insert a list of columns associated to a given configuration
212      * 
213      * @param nIdConfig
214      *            The id of the configuration
215      * @param listColumns
216      *            The list of columns to insert
217      */
218     private void insertColumns( int nIdConfig, List<String> listColumns )
219     {
220         if ( listColumns == null )
221         {
222             return;
223         }
224         for ( String strColumn : listColumns )
225         {
226             DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_COLUMNS, getPlugin( ) );
227             daoUtil.setInt( 1, nIdConfig );
228             daoUtil.setString( 2, strColumn );
229             daoUtil.executeUpdate( );
230             daoUtil.free( );
231         }
232     }
233 
234     /**
235      * Remove every columns associated with a given configuration
236      * 
237      * @param nIdConfig
238      *            The id of the configuration to remove columns of
239      */
240     private void removeColumns( int nIdConfig )
241     {
242         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_COLUMNS, getPlugin( ) );
243         daoUtil.setInt( 1, nIdConfig );
244         daoUtil.executeUpdate( );
245         daoUtil.free( );
246     }
247 
248     /**
249      * Get a new primary key from the database
250      * 
251      * @return The new primary key
252      */
253     private int getNewPrimaryKey( )
254     {
255         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PRIMARY_KEY, getPlugin( ) );
256         int nResult = 1;
257         daoUtil.executeQuery( );
258         if ( daoUtil.next( ) )
259         {
260             nResult = daoUtil.getInt( 1 ) + 1;
261         }
262         daoUtil.free( );
263         return nResult;
264     }
265 
266     /**
267      * Get the import export plugin
268      * 
269      * @return The import export plugin
270      */
271     private Plugin getPlugin( )
272     {
273         if ( _plugin == null )
274         {
275             _plugin = ImportExportPlugin.getPlugin( );
276         }
277         return _plugin;
278     }
279 
280 }