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.extend.business.extender;
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.Arrays;
41 import java.util.List;
42
43
44
45
46
47
48 public class ResourceExtenderDAO implements IResourceExtenderDAO
49 {
50 private static final String SQL_QUERY_NEW_PK = " SELECT max( id_extender ) FROM extend_resource_extender ";
51 private static final String SQL_QUERY_SELECT_ALL = " SELECT id_extender, extender_type, id_resource, resource_type, is_active FROM extend_resource_extender ";
52 private static final String SQL_QUERY_SELECT_ID_EXTENDER = " SELECT id_extender FROM extend_resource_extender ";
53 private static final String SQL_QUERY_SELECT = SQL_QUERY_SELECT_ALL + " WHERE id_extender = ? ";
54 private static final String SQL_QUERY_UPDATE = " UPDATE extend_resource_extender SET extender_type = ?, id_resource = ?, resource_type = ?, is_active = ? WHERE id_extender = ? ";
55 private static final String SQL_QUERY_INSERT = " INSERT INTO extend_resource_extender (id_extender, extender_type, id_resource, resource_type) VALUES ( ?,?,?,? ) ";
56 private static final String SQL_QUERY_DELETE = " DELETE FROM extend_resource_extender where id_extender = ? ";
57
58
59 private static final String SQL_WHERE = " WHERE ";
60 private static final String SQL_FILTER_ID_EXTENDER = " id_extender IN ";
61
62
63 private static final String OPEN_BRACKET = " ( ";
64 private static final String CLOSED_BRACKET = " ) ";
65 private static final String QUESTION_MARK = " ? ";
66 private static final String COMMA = " , ";
67
68
69
70
71
72
73
74
75 private int newPrimaryKey( Plugin plugin )
76 {
77 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
78 daoUtil.executeQuery( );
79
80 int nKey = 1;
81
82 if ( daoUtil.next( ) )
83 {
84 nKey = daoUtil.getInt( 1 ) + 1;
85 }
86
87 daoUtil.free( );
88
89 return nKey;
90 }
91
92
93
94
95 @Override
96 public synchronized void insert( ResourceExtenderDTO extender, Plugin plugin )
97 {
98 int nIndex = 1;
99 int nIdExtender = newPrimaryKey( plugin );
100 extender.setIdExtender( nIdExtender );
101
102 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
103 daoUtil.setInt( nIndex++, extender.getIdExtender( ) );
104 daoUtil.setString( nIndex++, extender.getExtenderType( ) );
105 daoUtil.setString( nIndex++, extender.getIdExtendableResource( ) );
106 daoUtil.setString( nIndex, extender.getExtendableResourceType( ) );
107
108 daoUtil.executeUpdate( );
109 daoUtil.free( );
110 }
111
112
113
114
115 @Override
116 public void store( ResourceExtenderDTO extender, Plugin plugin )
117 {
118 int nIndex = 1;
119 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
120 daoUtil.setString( nIndex++, extender.getExtenderType( ) );
121 daoUtil.setString( nIndex++, extender.getIdExtendableResource( ) );
122 daoUtil.setString( nIndex++, extender.getExtendableResourceType( ) );
123 daoUtil.setBoolean( nIndex++, extender.isIsActive( ) );
124
125 daoUtil.setInt( nIndex, extender.getIdExtender( ) );
126
127 daoUtil.executeUpdate( );
128 daoUtil.free( );
129 }
130
131
132
133
134 @Override
135 public void delete( int nIdExtender, Plugin plugin )
136 {
137 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
138 daoUtil.setInt( 1, nIdExtender );
139
140 daoUtil.executeUpdate( );
141 daoUtil.free( );
142 }
143
144
145
146
147 @Override
148 public ResourceExtenderDTO load( int nIdExtender, Plugin plugin )
149 {
150 ResourceExtenderDTO extender = null;
151 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
152 daoUtil.setInt( 1, nIdExtender );
153 daoUtil.executeQuery( );
154
155 if ( daoUtil.next( ) )
156 {
157 extender = populateDTO( daoUtil );
158 }
159
160 daoUtil.free( );
161
162 return extender;
163 }
164
165
166
167
168 @Override
169 public List<ResourceExtenderDTO> loadAll( Plugin plugin )
170 {
171 List<ResourceExtenderDTO> listExtenders = new ArrayList<ResourceExtenderDTO>( );
172 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL, plugin );
173 daoUtil.executeQuery( );
174
175 while ( daoUtil.next( ) )
176 {
177 ResourceExtenderDTO extender = populateDTO( daoUtil );
178 listExtenders.add( extender );
179 }
180
181 daoUtil.free( );
182
183 return listExtenders;
184 }
185
186
187
188
189 @Override
190 public List<ResourceExtenderDTO> loadByFilter( ResourceExtenderDTOFilter filter, Plugin plugin )
191 {
192 List<ResourceExtenderDTO> listExtenders = new ArrayList<ResourceExtenderDTO>( );
193 DAOUtil daoUtil = new DAOUtil( filter.buildSQLQuery( SQL_QUERY_SELECT_ALL ), plugin );
194 filter.setFilterValues( daoUtil );
195 daoUtil.executeQuery( );
196
197 while ( daoUtil.next( ) )
198 {
199 ResourceExtenderDTO extender = populateDTO( daoUtil );
200 listExtenders.add( extender );
201 }
202
203 daoUtil.free( );
204
205 return listExtenders;
206 }
207
208
209
210
211 @Override
212 public List<Integer> loadIdsByFilter( ResourceExtenderDTOFilter filter, Plugin plugin )
213 {
214 List<Integer> listIdsExtender = new ArrayList<Integer>( );
215 DAOUtil daoUtil = new DAOUtil( filter.buildSQLQuery( SQL_QUERY_SELECT_ID_EXTENDER ), plugin );
216 filter.setFilterValues( daoUtil );
217 daoUtil.executeQuery( );
218
219 while ( daoUtil.next( ) )
220 {
221 listIdsExtender.add( daoUtil.getInt( 1 ) );
222 }
223
224 daoUtil.free( );
225
226 return listIdsExtender;
227 }
228
229
230
231
232 @Override
233 public List<ResourceExtenderDTO> loadByListIds( List<Integer> listIds, Plugin plugin )
234 {
235 List<ResourceExtenderDTO> listExtenders = new ArrayList<ResourceExtenderDTO>( );
236
237 if ( ( listIds != null ) && !listIds.isEmpty( ) )
238 {
239
240
241 ResourceExtenderDTOnder/ResourceExtenderDTO.html#ResourceExtenderDTO">ResourceExtenderDTO [ ] arrayExtenders = new ResourceExtenderDTO [ listIds.size( )];
242 StringBuilder sbSQL = new StringBuilder( SQL_QUERY_SELECT_ALL );
243 sbSQL.append( SQL_WHERE ).append( SQL_FILTER_ID_EXTENDER );
244 sbSQL.append( OPEN_BRACKET );
245
246 for ( int i = 0; i < listIds.size( ); i++ )
247 {
248 sbSQL.append( QUESTION_MARK );
249
250 if ( i < ( listIds.size( ) - 1 ) )
251 {
252 sbSQL.append( COMMA );
253 }
254 }
255
256 sbSQL.append( CLOSED_BRACKET );
257
258 DAOUtil daoUtil = new DAOUtil( sbSQL.toString( ), plugin );
259 int nIndex = 1;
260
261 for ( int nId : listIds )
262 {
263 daoUtil.setInt( nIndex++, nId );
264 }
265
266 daoUtil.executeQuery( );
267
268 while ( daoUtil.next( ) )
269 {
270 ResourceExtenderDTO extender = populateDTO( daoUtil );
271 listExtenders.add( extender );
272
273 arrayExtenders [listIds.indexOf( extender.getIdExtender( ) )] = extender;
274 }
275
276 daoUtil.free( );
277
278 listExtenders = Arrays.asList( arrayExtenders );
279 }
280
281 return listExtenders;
282 }
283
284
285
286
287
288
289
290
291 private ResourceExtenderDTO populateDTO( DAOUtil daoUtil )
292 {
293 int nIndex = 1;
294 ResourceExtenderDTOd/business/extender/ResourceExtenderDTO.html#ResourceExtenderDTO">ResourceExtenderDTO dto = new ResourceExtenderDTO( );
295 dto.setIdExtender( daoUtil.getInt( nIndex++ ) );
296 dto.setExtenderType( daoUtil.getString( nIndex++ ) );
297 dto.setIdExtendableResource( daoUtil.getString( nIndex++ ) );
298 dto.setExtendableResourceType( daoUtil.getString( nIndex++ ) );
299 dto.setIsActive( daoUtil.getBoolean( nIndex ) );
300
301 return dto;
302 }
303 }