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.portal.business.indexeraction;
35
36 import fr.paris.lutece.util.sql.DAOUtil;
37
38 import java.util.ArrayList;
39 import java.util.List;
40
41
42
43
44
45 public final class IndexerActionDAO implements IIndexerActionDAO
46 {
47
48
49
50
51 public static final String CONSTANT_WHERE = " WHERE ";
52
53
54
55
56 public static final String CONSTANT_AND = " AND ";
57 private static final String SQL_QUERY_NEW_PK = "SELECT max( id_action ) FROM core_indexer_action";
58 private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_action,id_document,id_task,indexer_name, id_portlet" +
59 " FROM core_indexer_action WHERE id_action = ?";
60 private static final String SQL_QUERY_INSERT = "INSERT INTO core_indexer_action( id_action,id_document,id_task ,indexer_name,id_portlet)" +
61 " VALUES(?,?,?,?,?)";
62 private static final String SQL_QUERY_DELETE = "DELETE FROM core_indexer_action WHERE id_action = ? ";
63 private static final String SQL_QUERY_DELETE_ALL = "DELETE FROM core_indexer_action";
64 private static final String SQL_QUERY_UPDATE = "UPDATE core_indexer_action SET id_action=?,id_document=?,id_task=?,indexer_name=?,id_portlet=? WHERE id_action = ? ";
65 private static final String SQL_QUERY_SELECT = "SELECT id_action,id_document,id_task,indexer_name,id_portlet" +
66 " FROM core_indexer_action ";
67 private static final String SQL_FILTER_ID_TASK = " id_task = ? ";
68
69
70
71
72 @Override
73 public int newPrimaryKey( )
74 {
75 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK );
76 daoUtil.executeQuery( );
77
78 int nKey;
79
80 if ( !daoUtil.next( ) )
81 {
82
83 nKey = 1;
84 }
85
86 nKey = daoUtil.getInt( 1 ) + 1;
87 daoUtil.free( );
88
89 return nKey;
90 }
91
92
93
94
95 @Override
96 public synchronized void insert( IndexerAction indexerAction )
97 {
98 indexerAction.setIdAction( newPrimaryKey( ) );
99
100 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
101 daoUtil.setString( 2, indexerAction.getIdDocument( ) );
102 daoUtil.setInt( 3, indexerAction.getIdTask( ) );
103 daoUtil.setString( 4, indexerAction.getIndexerName( ) );
104 daoUtil.setInt( 5, indexerAction.getIdPortlet( ) );
105 daoUtil.setInt( 1, indexerAction.getIdAction( ) );
106
107 daoUtil.executeUpdate( );
108
109 daoUtil.free( );
110 }
111
112
113
114
115 @Override
116 public IndexerAction load( int nId )
117 {
118 IndexerAction indexerAction = null;
119
120 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY );
121 daoUtil.setInt( 1, nId );
122 daoUtil.executeQuery( );
123
124 if ( daoUtil.next( ) )
125 {
126 indexerAction = new IndexerAction( );
127 indexerAction.setIdAction( daoUtil.getInt( 1 ) );
128 indexerAction.setIdDocument( daoUtil.getString( 2 ) );
129 indexerAction.setIdTask( daoUtil.getInt( 3 ) );
130 indexerAction.setIndexerName( daoUtil.getString( 4 ) );
131 indexerAction.setIdPortlet( daoUtil.getInt( 5 ) );
132 }
133
134 daoUtil.free( );
135
136 return indexerAction;
137 }
138
139
140
141
142 @Override
143 public void delete( int nId )
144 {
145 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
146 daoUtil.setInt( 1, nId );
147 daoUtil.executeUpdate( );
148 daoUtil.free( );
149 }
150
151
152
153
154 @Override
155 public void deleteAll( )
156 {
157 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_ALL );
158 daoUtil.executeUpdate( );
159 daoUtil.free( );
160 }
161
162
163
164
165 @Override
166 public void store( IndexerAction indexerAction )
167 {
168 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
169 daoUtil.setInt( 1, indexerAction.getIdAction( ) );
170 daoUtil.setString( 2, indexerAction.getIdDocument( ) );
171 daoUtil.setInt( 3, indexerAction.getIdTask( ) );
172 daoUtil.setString( 4, indexerAction.getIndexerName( ) );
173 daoUtil.setInt( 5, indexerAction.getIdPortlet( ) );
174
175 daoUtil.executeUpdate( );
176 daoUtil.free( );
177 }
178
179
180
181
182 @Override
183 public List<IndexerAction> selectList( IndexerActionFilter filter )
184 {
185 List<IndexerAction> indexerActionList = new ArrayList<IndexerAction>( );
186 IndexerAction indexerAction = null;
187 List<String> listStrFilter = new ArrayList<String>( );
188
189 if ( filter.containsIdTask( ) )
190 {
191 listStrFilter.add( SQL_FILTER_ID_TASK );
192 }
193
194 String strSQL = buildRequestWithFilter( SQL_QUERY_SELECT, listStrFilter, null );
195
196 DAOUtil daoUtil = new DAOUtil( strSQL );
197
198 int nIndex = 1;
199
200 if ( filter.containsIdTask( ) )
201 {
202 daoUtil.setInt( nIndex, filter.getIdTask( ) );
203 }
204
205 daoUtil.executeQuery( );
206
207 while ( daoUtil.next( ) )
208 {
209 indexerAction = new IndexerAction( );
210 indexerAction.setIdAction( daoUtil.getInt( 1 ) );
211 indexerAction.setIdDocument( daoUtil.getString( 2 ) );
212 indexerAction.setIdTask( daoUtil.getInt( 3 ) );
213 indexerAction.setIndexerName( daoUtil.getString( 4 ) );
214 indexerAction.setIdPortlet( daoUtil.getInt( 5 ) );
215 indexerActionList.add( indexerAction );
216 }
217
218 daoUtil.free( );
219
220 return indexerActionList;
221 }
222
223
224
225
226 @Override
227 public List<IndexerAction> selectList( )
228 {
229 List<IndexerAction> indexerActionList = new ArrayList<IndexerAction>( );
230 IndexerAction indexerAction = null;
231
232 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
233
234 daoUtil.executeQuery( );
235
236 while ( daoUtil.next( ) )
237 {
238 indexerAction = new IndexerAction( );
239 indexerAction.setIdAction( daoUtil.getInt( 1 ) );
240 indexerAction.setIdDocument( daoUtil.getString( 2 ) );
241 indexerAction.setIdTask( daoUtil.getInt( 3 ) );
242 indexerAction.setIndexerName( daoUtil.getString( 4 ) );
243 indexerAction.setIdPortlet( daoUtil.getInt( 5 ) );
244 indexerActionList.add( indexerAction );
245 }
246
247 daoUtil.free( );
248
249 return indexerActionList;
250 }
251
252
253
254
255
256
257
258
259 public static String buildRequestWithFilter( String strSelect, List<String> listStrFilter, String strOrder )
260 {
261 StringBuffer strBuffer = new StringBuffer( );
262 strBuffer.append( strSelect );
263
264 int nCount = 0;
265
266 for ( String strFilter : listStrFilter )
267 {
268 if ( ++nCount == 1 )
269 {
270 strBuffer.append( CONSTANT_WHERE );
271 }
272
273 strBuffer.append( strFilter );
274
275 if ( nCount != listStrFilter.size( ) )
276 {
277 strBuffer.append( CONSTANT_AND );
278 }
279 }
280
281 if ( strOrder != null )
282 {
283 strBuffer.append( strOrder );
284 }
285
286 return strBuffer.toString( );
287 }
288 }