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