View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de 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.identitystore.modules.indexer.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  /**
43   * This class provides Data Access methods for Indexer Action objects
44   */
45  public final class IndexerActionDAO implements IIndexerActionDAO
46  {
47      // Constants
48      public static final String CONSTANT_WHERE = " WHERE ";
49      public static final String CONSTANT_AND = " AND ";
50  
51      // Constants
52      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_action ) FROM identitystore_identity_indexer_action";
53      private static final String SQL_QUERY_INSERT = "INSERT INTO identitystore_identity_indexer_action( id_action,id_customer,id_task)" + " VALUES(?,?,?)";
54      private static final String SQL_QUERY_INSERT_ALL = "INSERT INTO identitystore_identity_indexer_action( id_action,id_customer,id_task) VALUES";
55      private static final String SQL_QUERY_INSERT_ALL_VALUES = " (?,?,?),";
56      private static final String SQL_QUERY_SELECT = "SELECT id_action,id_customer,id_task" + " FROM identitystore_identity_indexer_action  ";
57      private static final String SQL_QUERY_DELETE = "DELETE FROM identitystore_identity_indexer_action WHERE id_action = ? ";
58      private static final String SQL_FILTER_ID_TASK = " id_task = ? ";
59      private static final String SQL_FILTER_ID_CUSTOMER = " id_customer = ? ";
60      private static final String SQL_LIMIT = " LIMIT ?, ? ";
61      private static final String SQL_QUERY_INSERT_ALL_BY_ID_TASK = "INSERT INTO identitystore_identity_indexer_action SELECT ? + id_identity, customer_id, ? FROM identitystore_identity";
62      private static final String SQL_QUERY_DELETE_ALL = "DELETE FROM identitystore_identity_indexer_action";
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public int newPrimaryKey( Plugin plugin )
69      {
70          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
71          daoUtil.executeQuery( );
72  
73          int nKey = 1;
74  
75          if ( daoUtil.next( ) )
76          {
77              nKey = daoUtil.getInt( 1 ) + 1;
78          }
79  
80          daoUtil.free( );
81  
82          return nKey;
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      @Override
89      public synchronized void insert( IndexerAction indexerAction, Plugin plugin )
90      {
91          int nIdAction = newPrimaryKey( plugin );
92          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
93          daoUtil.setString( 2, indexerAction.getCustomerId( ) );
94          daoUtil.setInt( 3, indexerAction.getTask( ).getValue( ) );
95  
96          indexerAction.setIdAction( nIdAction );
97          daoUtil.setInt( 1, indexerAction.getIdAction( ) );
98  
99          daoUtil.executeUpdate( );
100 
101         daoUtil.free( );
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public synchronized void insertAll( List<IndexerAction> listIndexerActions, Plugin plugin )
109     {
110         if ( ( listIndexerActions == null ) || listIndexerActions.isEmpty( ) )
111         {
112             return;
113         }
114 
115         StringBuilder sbQuery = new StringBuilder( SQL_QUERY_INSERT_ALL );
116         int nIdAction = newPrimaryKey( plugin );
117 
118         // First, builds the query
119         for ( int i = 0; i < listIndexerActions.size( ); i++ )
120         {
121             sbQuery.append( SQL_QUERY_INSERT_ALL_VALUES );
122         }
123 
124         // Removes trailing comma
125         sbQuery.deleteCharAt( sbQuery.length( ) - 1 );
126 
127         DAOUtil daoUtil = new DAOUtil( sbQuery.toString( ), plugin );
128 
129         int nIndex = 1;
130 
131         // Secondly, injects the parameters
132         for ( IndexerAction indexerAction : listIndexerActions )
133         {
134             daoUtil.setInt( nIndex++, nIdAction++ );
135             daoUtil.setString( nIndex++, indexerAction.getCustomerId( ) );
136             daoUtil.setInt( nIndex++, indexerAction.getTask( ).getValue( ) );
137         }
138 
139         daoUtil.executeUpdate( );
140 
141         daoUtil.free( );
142     }
143 
144     /**
145      * {@inheritDoc}
146      */
147     @Override
148     public void delete( int nId, Plugin plugin )
149     {
150         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
151         daoUtil.setInt( 1, nId );
152         daoUtil.executeUpdate( );
153         daoUtil.free( );
154     }
155 
156     /**
157      * {@inheritDoc}
158      */
159     @Override
160     public List<IndexerAction> selectListLimit( IndexerActionFilter filter, int nStart, int nLimit, Plugin plugin )
161     {
162         List<IndexerAction> indexerActionList = new ArrayList<IndexerAction>( );
163         IndexerAction indexerAction = null;
164         List<String> listStrFilter = new ArrayList<String>( );
165 
166         if ( filter.containsTask( ) )
167         {
168             listStrFilter.add( SQL_FILTER_ID_TASK );
169         }
170 
171         if ( filter.containsCustomerId( ) )
172         {
173             listStrFilter.add( SQL_FILTER_ID_CUSTOMER );
174         }
175 
176         String strSQL = buildRequestWithFilter( SQL_QUERY_SELECT, listStrFilter, null );
177 
178         boolean bLimit = ( nLimit != -1 );
179         if ( bLimit )
180         {
181             strSQL = new StringBuffer( strSQL ).append( SQL_LIMIT ).toString( );
182         }
183 
184         DAOUtil daoUtil = new DAOUtil( strSQL, plugin );
185 
186         int nIndex = 1;
187 
188         if ( filter.containsTask( ) )
189         {
190             daoUtil.setInt( nIndex, filter.getIndexerTask( ).getValue( ) );
191             nIndex++;
192         }
193 
194         if ( filter.containsCustomerId( ) )
195         {
196             daoUtil.setString( nIndex, filter.getCustomerId( ) );
197             nIndex++;
198         }
199 
200         if ( bLimit )
201         {
202             daoUtil.setInt( nIndex++, nStart );
203             daoUtil.setInt( nIndex, nLimit );
204         }
205 
206         daoUtil.executeQuery( );
207 
208         while ( daoUtil.next( ) )
209         {
210             indexerAction = new IndexerAction( );
211             indexerAction.setIdAction( daoUtil.getInt( 1 ) );
212             indexerAction.setCustomerId( daoUtil.getString( 2 ) );
213             indexerAction.setTask( IndexerTask.valueOf( daoUtil.getInt( 3 ) ) );
214 
215             indexerActionList.add( indexerAction );
216         }
217 
218         daoUtil.free( );
219 
220         return indexerActionList;
221     }
222 
223     /**
224      * Builds a query with filters placed in parameters
225      * 
226      * @param strSelect
227      *            the select of the query
228      * @param listStrFilter
229      *            the list of filter to add in the query
230      * @param strOrder
231      *            the order by of the query
232      * @return a query
233      */
234     private static String buildRequestWithFilter( String strSelect, List<String> listStrFilter, String strOrder )
235     {
236         StringBuffer strBuffer = new StringBuffer( );
237         strBuffer.append( strSelect );
238 
239         int nCount = 0;
240 
241         for ( String strFilter : listStrFilter )
242         {
243             if ( ++nCount == 1 )
244             {
245                 strBuffer.append( CONSTANT_WHERE );
246             }
247 
248             strBuffer.append( strFilter );
249 
250             if ( nCount != listStrFilter.size( ) )
251             {
252                 strBuffer.append( CONSTANT_AND );
253             }
254         }
255 
256         if ( strOrder != null )
257         {
258             strBuffer.append( strOrder );
259         }
260 
261         return strBuffer.toString( );
262     }
263 
264     /**
265      * {@inheritDoc}
266      */
267     @Override
268     public synchronized void deleteByFilter( IndexerActionFilter filter, Plugin plugin )
269     {
270         List<String> listStrFilter = new ArrayList<String>( );
271 
272         if ( filter.containsTask( ) )
273         {
274             listStrFilter.add( SQL_FILTER_ID_TASK );
275         }
276         String strSQL = buildRequestWithFilter( SQL_QUERY_DELETE_ALL, listStrFilter, null );
277         DAOUtil daoUtil = new DAOUtil( strSQL, plugin );
278         if ( filter.containsTask( ) )
279         {
280             daoUtil.setInt( 1, filter.getIndexerTask( ).getValue( ) );
281         }
282         daoUtil.executeUpdate( );
283         daoUtil.free( );
284     }
285 
286     /**
287      * {@inheritDoc}
288      */
289     @Override
290     public synchronized void insertAllByIdTask( int nIdTask, Plugin plugin )
291     {
292         int nIdActionMax = newPrimaryKey( plugin );
293         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_ALL_BY_ID_TASK, plugin );
294         daoUtil.setInt( 1, nIdActionMax + 1 );
295         daoUtil.setInt( 2, nIdTask );
296         daoUtil.executeUpdate( );
297         daoUtil.free( );
298     }
299 }