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