1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
44
45 public final class IndexerActionDAO implements IIndexerActionDAO
46 {
47
48 public static final String CONSTANT_WHERE = " WHERE ";
49 public static final String CONSTANT_AND = " AND ";
50
51
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
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
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
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
119 for ( int i = 0; i < listIndexerActions.size( ); i++ )
120 {
121 sbQuery.append( SQL_QUERY_INSERT_ALL_VALUES );
122 }
123
124
125 sbQuery.deleteCharAt( sbQuery.length( ) - 1 );
126
127 DAOUtil daoUtil = new DAOUtil( sbQuery.toString( ), plugin );
128
129 int nIndex = 1;
130
131
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
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
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
225
226
227
228
229
230
231
232
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
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
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 }