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  
35  package fr.paris.lutece.plugins.elasticdata.business;
36  
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.util.ReferenceList;
39  import fr.paris.lutece.util.sql.DAOUtil;
40  import java.sql.Statement;
41  
42  import java.util.ArrayList;
43  import java.util.List;
44  import java.util.Optional;
45  
46  /**
47   * This class provides Data Access methods for IndexerAction objects
48   */
49  public final class IndexerActionDAO implements IIndexerActionDAO
50  {
51      // Constants
52      private static final String SQL_QUERY_SELECT = "SELECT id_action, id_resource, id_task, id_datasource FROM elasticdata_indexer_action WHERE id_action = ?";
53      private static final String SQL_QUERY_SELECT_BY_ID_RESOURCE = "SELECT id_action, id_resource, id_task, id_datasource FROM elasticdata_indexer_action WHERE id_resource = ? AND id_datasource = ?";
54      private static final String SQL_QUERY_INSERT = "INSERT INTO elasticdata_indexer_action ( id_resource, id_task, id_datasource ) VALUES ( ?, ?, ? ) ";
55      private static final String SQL_QUERY_DELETE = "DELETE FROM elasticdata_indexer_action WHERE id_action = ? ";
56      private static final String SQL_QUERY_UPDATE = "UPDATE elasticdata_indexer_action SET id_action = ?, id_resource = ?, id_task = ?, id_datasource = ? WHERE id_action = ?";
57      private static final String SQL_QUERY_SELECTALL = "SELECT id_action, id_resource, id_task, id_datasource FROM elasticdata_indexer_action";
58      private static final String SQL_QUERY_SELECTALL_ID = "SELECT id_action FROM elasticdata_indexer_action";
59      private static final String SQL_QUERY_SELECTALL_ID_RESOURCE_BY_DATASOURCE_ID_TASK = "SELECT id_resource FROM elasticdata_indexer_action WHERE id_datasource = ? AND id_task = ?";
60      private static final String SQL_QUERY_SELECTALL_BY_DATASOURCE_ID_TASK = "SELECT id_action, id_resource, id_task, id_datasource FROM elasticdata_indexer_action WHERE id_datasource = ? AND id_task = ?";
61      private static final String SQL_QUERY_SELECTALL_BY_DATASOURCE = "SELECT id_action, id_resource, id_task, id_datasource FROM elasticdata_indexer_action WHERE id_datasource = ?";
62      private static final String SQL_QUERY_SELECTALL_BY_IDS = "SELECT id_action, id_resource, id_task, id_datasource FROM elasticdata_indexer_action WHERE id_action IN (  ";
63      private static final String SQL_QUERY_DELETE_BY_LIST = "DELETE FROM elasticdata_indexer_action WHERE id_datasource = ? AND id_resource IN (?";
64      private static final String SQL_CLOSE_PARENTHESIS = " ) ";
65      private static final String SQL_ADITIONAL_PARAMETER = ",?";
66  
67      /**
68       * {@inheritDoc }
69       */
70      @Override
71      public void insert( IndexerAction indexerAction, Plugin plugin )
72      {
73          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, plugin ) )
74          {
75              int nIndex = 1;
76              daoUtil.setString( nIndex++, indexerAction.getIdResource( ) );
77              daoUtil.setInt( nIndex++, indexerAction.getIdTask( ) );
78              daoUtil.setString( nIndex++, indexerAction.getIdDataSource( ) );
79  
80              daoUtil.executeUpdate( );
81              if ( daoUtil.nextGeneratedKey( ) )
82              {
83                  indexerAction.setId( daoUtil.getGeneratedKeyInt( 1 ) );
84              }
85          }
86  
87      }
88  
89      /**
90       * {@inheritDoc }
91       */
92      @Override
93      public Optional<IndexerAction> load( int nKey, Plugin plugin )
94      {
95          try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
96          {
97  	        daoUtil.setInt( 1 , nKey );
98  	        daoUtil.executeQuery( );
99  	        IndexerAction indexerAction = null;
100 	
101 	        if ( daoUtil.next( ) )
102 	        {
103 	            indexerAction = new IndexerAction();
104 	            int nIndex = 1;
105 	            
106                 indexerAction.setId( daoUtil.getInt( nIndex++ ) );
107                 indexerAction.setIdResource( daoUtil.getString( nIndex++ ) );
108                 indexerAction.setIdTask( daoUtil.getInt( nIndex++ ) );
109                 indexerAction.setIdDataSource( daoUtil.getString( nIndex ) );
110 	        }
111 	
112 	        return Optional.ofNullable( indexerAction );
113         }
114     }
115 
116     /**
117      * {@inheritDoc }
118      */
119     @Override
120     public IndexerAction loadByIdResource( String strIdResource, String strIdDataSource, Plugin plugin )
121     {
122         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_ID_RESOURCE, plugin ) )
123         {
124             daoUtil.setString( 1, strIdResource );
125             daoUtil.setString( 2, strIdDataSource );
126             daoUtil.executeQuery( );
127             IndexerAction indexerAction = null;
128 
129             if ( daoUtil.next( ) )
130             {
131                 indexerAction = new IndexerAction( );
132                 int nIndex = 1;
133 
134                 indexerAction.setId( daoUtil.getInt( nIndex++ ) );
135                 indexerAction.setIdResource( daoUtil.getString( nIndex++ ) );
136                 indexerAction.setIdTask( daoUtil.getInt( nIndex++ ) );
137                 indexerAction.setIdDataSource( daoUtil.getString( nIndex ) );
138             }
139 
140             return indexerAction;
141         }
142     }
143 
144     /**
145      * {@inheritDoc }
146      */
147     @Override
148     public void delete( int nKey, Plugin plugin )
149     {
150         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
151         {
152             daoUtil.setInt( 1, nKey );
153             daoUtil.executeUpdate( );
154         }
155     }
156 
157     /**
158      * {@inheritDoc }
159      */
160     @Override
161     public void deleteByIdResourceList( List<String> listIdResource, String strIdDataSource, Plugin plugin )
162     {
163         int nlistIdResourceSize = listIdResource.size( );
164 
165         if ( nlistIdResourceSize > 0 )
166         {
167             StringBuilder sbSQL = new StringBuilder( SQL_QUERY_DELETE_BY_LIST );
168 
169             for ( int i = 1; i < nlistIdResourceSize; i++ )
170             {
171                 sbSQL.append( SQL_ADITIONAL_PARAMETER );
172             }
173 
174             sbSQL.append( SQL_CLOSE_PARENTHESIS );
175 
176             try ( DAOUtil daoUtil = new DAOUtil( sbSQL.toString( ), plugin ) )
177             {
178                 daoUtil.setString( 1, strIdDataSource );
179 
180                 for ( int i = 0; i < nlistIdResourceSize; i++ )
181                 {
182                     daoUtil.setString( i + 2, listIdResource.get( i ) );
183                 }
184 
185                 daoUtil.executeUpdate( );
186             }
187         }
188     }
189 
190     /**
191      * {@inheritDoc }
192      */
193     @Override
194     public void store( IndexerAction indexerAction, Plugin plugin )
195     {
196         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
197         {
198             int nIndex = 1;
199 
200             daoUtil.setInt( nIndex++, indexerAction.getId( ) );
201             daoUtil.setString( nIndex++, indexerAction.getIdResource( ) );
202             daoUtil.setInt( nIndex++, indexerAction.getIdTask( ) );
203             daoUtil.setString( nIndex++, indexerAction.getIdDataSource( ) );
204             daoUtil.setInt( nIndex, indexerAction.getId( ) );
205 
206             daoUtil.executeUpdate( );
207         }
208     }
209 
210     /**
211      * {@inheritDoc }
212      */
213     @Override
214     public List<IndexerAction> selectIndexerActionsList( Plugin plugin )
215     {
216         List<IndexerAction> indexerActionList = new ArrayList<>( );
217         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
218         {
219             daoUtil.executeQuery( );
220 
221             while ( daoUtil.next( ) )
222             {
223                 IndexerActionbusiness/IndexerAction.html#IndexerAction">IndexerAction indexerAction = new IndexerAction( );
224                 int nIndex = 1;
225 
226                 indexerAction.setId( daoUtil.getInt( nIndex++ ) );
227                 indexerAction.setIdResource( daoUtil.getString( nIndex++ ) );
228                 indexerAction.setIdTask( daoUtil.getInt( nIndex++ ) );
229                 indexerAction.setIdDataSource( daoUtil.getString( nIndex ) );
230 
231                 indexerActionList.add( indexerAction );
232             }
233 
234             return indexerActionList;
235         }
236     }
237 
238     /**
239      * {@inheritDoc }
240      */
241     @Override
242     public List<IndexerAction> selectIndexerActionsList( String strIdDataSource, int nIdTask, Plugin plugin )
243     {
244 
245         List<IndexerAction> indexerActionList = new ArrayList<>( );
246         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_BY_DATASOURCE_ID_TASK, plugin ) )
247         {
248 
249             daoUtil.setString( 1, strIdDataSource );
250             daoUtil.setInt( 2, nIdTask );
251 
252             daoUtil.executeQuery( );
253 
254             while ( daoUtil.next( ) )
255             {
256                 IndexerActionbusiness/IndexerAction.html#IndexerAction">IndexerAction indexerAction = new IndexerAction( );
257                 int nIndex = 1;
258 
259                 indexerAction.setId( daoUtil.getInt( nIndex++ ) );
260                 indexerAction.setIdResource( daoUtil.getString( nIndex++ ) );
261                 indexerAction.setIdTask( daoUtil.getInt( nIndex++ ) );
262                 indexerAction.setIdDataSource( daoUtil.getString( nIndex ) );
263 
264                 indexerActionList.add( indexerAction );
265             }
266 
267             return indexerActionList;
268         }
269     }
270 
271     /**
272      * {@inheritDoc }
273      */
274     @Override
275     public List<IndexerAction> selectIndexerActionsList( String strIdDataSource, Plugin plugin )
276     {
277 
278         List<IndexerAction> indexerActionList = new ArrayList<>( );
279         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_BY_DATASOURCE, plugin ) )
280         {
281 
282             daoUtil.setString( 1, strIdDataSource );
283 
284             daoUtil.executeQuery( );
285 
286             while ( daoUtil.next( ) )
287             {
288                 IndexerActionbusiness/IndexerAction.html#IndexerAction">IndexerAction indexerAction = new IndexerAction( );
289                 int nIndex = 1;
290 
291                 indexerAction.setId( daoUtil.getInt( nIndex++ ) );
292                 indexerAction.setIdResource( daoUtil.getString( nIndex++ ) );
293                 indexerAction.setIdTask( daoUtil.getInt( nIndex++ ) );
294                 indexerAction.setIdDataSource( daoUtil.getString( nIndex ) );
295 
296                 indexerActionList.add( indexerAction );
297             }
298 
299             return indexerActionList;
300         }
301     }
302 
303     /**
304      * {@inheritDoc }
305      */
306     @Override
307     public List<String> selectIdResourceIndexerActionsList( String strIdDataSource, int nIdTask, Plugin plugin )
308     {
309 
310         List<String> indexerActionList = new ArrayList<>( );
311         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ID_RESOURCE_BY_DATASOURCE_ID_TASK, plugin ) )
312         {
313 
314             daoUtil.setString( 1, strIdDataSource );
315             daoUtil.setInt( 2, nIdTask );
316 
317             daoUtil.executeQuery( );
318 
319             while ( daoUtil.next( ) )
320             {
321                 indexerActionList.add( daoUtil.getString( 1 ) );
322 
323             }
324 
325             return indexerActionList;
326         }
327     }
328 
329     /**
330      * {@inheritDoc }
331      */
332     @Override
333     public List<Integer> selectIdIndexerActionsList( Plugin plugin )
334     {
335         List<Integer> indexerActionList = new ArrayList<>( );
336         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ID, plugin ) )
337         {
338             daoUtil.executeQuery( );
339 
340             while ( daoUtil.next( ) )
341             {
342                 indexerActionList.add( daoUtil.getInt( 1 ) );
343             }
344 
345             return indexerActionList;
346         }
347     }
348 
349     /**
350      * {@inheritDoc }
351      */
352     @Override
353     public ReferenceList selectIndexerActionsReferenceList( Plugin plugin )
354     {
355         ReferenceList indexerActionList = new ReferenceList( );
356         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
357         {
358             daoUtil.executeQuery( );
359 
360             while ( daoUtil.next( ) )
361             {
362                 indexerActionList.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
363             }
364 
365             return indexerActionList;
366         }
367     }
368 
369         /**
370      * {@inheritDoc }
371      */
372 	@Override
373 	public List<IndexerAction> selectIndexerActionsListByIds( Plugin plugin, List<Integer> listIds ) {
374 		List<IndexerAction> indexerActionList = new ArrayList<>(  );
375 		
376 		StringBuilder builder = new StringBuilder( );
377 
378 		if ( !listIds.isEmpty( ) )
379 		{
380 			for( int i = 0 ; i < listIds.size(); i++ ) {
381 			    builder.append( "?," );
382 			}
383 	
384 			String placeHolders =  builder.deleteCharAt( builder.length( ) -1 ).toString( );
385 			String stmt = SQL_QUERY_SELECTALL_BY_IDS + placeHolders + ")";
386 			
387 			
388 	        try ( DAOUtil daoUtil = new DAOUtil( stmt, plugin ) )
389 	        {
390 	        	int index = 1;
391 				for( Integer n : listIds ) {
392 					daoUtil.setInt(  index++, n ); 
393 				}
394 	        	
395 	        	daoUtil.executeQuery(  );
396 	        	while ( daoUtil.next(  ) )
397 		        {
398                     IndexerActionbusiness/IndexerAction.html#IndexerAction">IndexerAction indexerAction = new IndexerAction( );
399                     int nIndex = 1;
400     
401                     indexerAction.setId( daoUtil.getInt( nIndex++ ) );
402                     indexerAction.setIdResource( daoUtil.getString( nIndex++ ) );
403                     indexerAction.setIdTask( daoUtil.getInt( nIndex++ ) );
404                     indexerAction.setIdDataSource( daoUtil.getString( nIndex ) );
405     
406                     indexerActionList.add( indexerAction );
407 		        }
408 		
409 		        daoUtil.free( );
410 		        
411 	        }
412 	    }
413 		return indexerActionList;
414 		
415 	}
416 }