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