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.user.attribute;
35
36 import fr.paris.lutece.portal.service.plugin.Plugin;
37 import fr.paris.lutece.portal.service.plugin.PluginService;
38 import fr.paris.lutece.portal.service.util.AppLogService;
39 import fr.paris.lutece.util.sql.DAOUtil;
40
41 import java.util.ArrayList;
42 import java.util.List;
43 import java.util.Locale;
44
45
46
47
48
49
50
51 public class AttributeDAO implements IAttributeDAO
52 {
53
54 private static final String SQL_QUERY_NEW_PK = " SELECT max(id_attribute) FROM core_attribute ";
55
56
57 private static final String SQL_QUERY_NEW_POSITION = "SELECT MAX(attribute_position)" + " FROM core_attribute ";
58
59
60 private static final String SQL_QUERY_SELECT = " SELECT type_class_name, id_attribute, title, help_message, is_mandatory, is_shown_in_search, is_shown_in_result_list, is_field_in_line, attribute_position, plugin_name " +
61 " FROM core_attribute WHERE id_attribute = ? ";
62 private static final String SQL_QUERY_SELECT_ALL = " SELECT type_class_name, id_attribute, title, help_message, is_mandatory, is_shown_in_search, is_shown_in_result_list, is_field_in_line, attribute_position, anonymize, plugin_name " +
63 " FROM core_attribute ORDER BY attribute_position ASC ";
64 private static final String SQL_QUERY_SELECT_PLUGIN_ATTRIBUTES = " SELECT type_class_name, id_attribute, title, help_message, is_mandatory, is_shown_in_search, is_shown_in_result_list, is_field_in_line, attribute_position " +
65 " FROM core_attribute WHERE plugin_name = ? ORDER BY attribute_position ASC ";
66 private static final String SQL_QUERY_SELECT_CORE_ATTRIBUTES = " SELECT type_class_name, id_attribute, title, help_message, is_mandatory, is_shown_in_search, is_shown_in_result_list, is_field_in_line, attribute_position " +
67 " FROM core_attribute WHERE plugin_name IS NULL OR plugin_name = '' ORDER BY attribute_position ASC ";
68
69
70 private static final String SQL_QUERY_INSERT = " INSERT INTO core_attribute (id_attribute, type_class_name, title, help_message, is_mandatory, is_shown_in_search, is_shown_in_result_list, is_field_in_line, attribute_position)" +
71 " VALUES (?,?,?,?,?,?,?,?,?) ";
72
73
74 private static final String SQL_QUERY_UPDATE = " UPDATE core_attribute SET title = ?, help_message = ?, is_mandatory = ?, is_shown_in_search = ?, is_shown_in_result_list = ?, is_field_in_line = ?, attribute_position = ? " +
75 " WHERE id_attribute = ? ";
76 private static final String SQL_QUERY_UPDATE_ANONYMIZATION = " UPDATE core_attribute SET anonymize = ? WHERE id_attribute = ? ";
77
78
79 private static final String SQL_QUERY_DELETE = " DELETE FROM core_attribute WHERE id_attribute = ?";
80
81
82
83
84
85
86 private int newPrimaryKey( )
87 {
88 StringBuilder sbSQL = new StringBuilder( SQL_QUERY_NEW_PK );
89 DAOUtil daoUtil = new DAOUtil( sbSQL.toString( ) );
90 daoUtil.executeQuery( );
91
92 int nKey = 1;
93
94 if ( daoUtil.next( ) )
95 {
96 nKey = daoUtil.getInt( 1 ) + 1;
97 }
98
99 daoUtil.free( );
100
101 return nKey;
102 }
103
104
105
106
107
108 private int newPosition( )
109 {
110 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_POSITION );
111 daoUtil.executeQuery( );
112
113 int nPos;
114
115 if ( !daoUtil.next( ) )
116 {
117
118 nPos = 1;
119 }
120
121 nPos = daoUtil.getInt( 1 ) + 1;
122 daoUtil.free( );
123
124 return nPos;
125 }
126
127
128
129
130
131
132
133 public IAttribute load( int nIdAttribute, Locale locale )
134 {
135 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
136 daoUtil.setInt( 1, nIdAttribute );
137 daoUtil.executeQuery( );
138
139 IAttribute attribute = null;
140
141 if ( daoUtil.next( ) )
142 {
143 int nIndex = 1;
144
145 try
146 {
147 attribute = (IAttribute) Class.forName( daoUtil.getString( nIndex++ ) ).newInstance( );
148 }
149 catch ( ClassNotFoundException e )
150 {
151
152 AppLogService.error( e );
153 }
154 catch ( InstantiationException e )
155 {
156
157
158 AppLogService.error( e );
159 }
160 catch ( IllegalAccessException e )
161 {
162
163 AppLogService.error( e );
164 }
165
166 attribute.setIdAttribute( daoUtil.getInt( nIndex++ ) );
167 attribute.setTitle( daoUtil.getString( nIndex++ ) );
168 attribute.setHelpMessage( daoUtil.getString( nIndex++ ) );
169 attribute.setMandatory( daoUtil.getBoolean( nIndex++ ) );
170 attribute.setShownInSearch( daoUtil.getBoolean( nIndex++ ) );
171 attribute.setShownInResultList( daoUtil.getBoolean( nIndex++ ) );
172 attribute.setFieldInLine( daoUtil.getBoolean( nIndex++ ) );
173 attribute.setPosition( daoUtil.getInt( nIndex++ ) );
174 attribute.setAttributeType( locale );
175
176 Plugin plugin = PluginService.getPlugin( daoUtil.getString( nIndex++ ) );
177 attribute.setPlugin( plugin );
178 }
179
180 daoUtil.free( );
181
182 return attribute;
183 }
184
185
186
187
188
189
190 public int insert( IAttribute attribute )
191 {
192 int nIndex = 1;
193 int nNewPrimaryKey = newPrimaryKey( );
194 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
195 daoUtil.setInt( nIndex++, nNewPrimaryKey );
196 daoUtil.setString( nIndex++, attribute.getClass( ).getName( ) );
197 daoUtil.setString( nIndex++, attribute.getTitle( ) );
198 daoUtil.setString( nIndex++, attribute.getHelpMessage( ) );
199 daoUtil.setBoolean( nIndex++, attribute.isMandatory( ) );
200 daoUtil.setBoolean( nIndex++, attribute.isShownInSearch( ) );
201 daoUtil.setBoolean( nIndex++, attribute.isShownInResultList( ) );
202 daoUtil.setBoolean( nIndex++, attribute.isFieldInLine( ) );
203 daoUtil.setInt( nIndex++, newPosition( ) );
204
205 daoUtil.executeUpdate( );
206 daoUtil.free( );
207
208 return nNewPrimaryKey;
209 }
210
211
212
213
214
215 public void store( IAttribute attribute )
216 {
217 int nIndex = 1;
218 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
219 daoUtil.setString( nIndex++, attribute.getTitle( ) );
220 daoUtil.setString( nIndex++, attribute.getHelpMessage( ) );
221 daoUtil.setBoolean( nIndex++, attribute.isMandatory( ) );
222 daoUtil.setBoolean( nIndex++, attribute.isShownInSearch( ) );
223 daoUtil.setBoolean( nIndex++, attribute.isShownInResultList( ) );
224 daoUtil.setBoolean( nIndex++, attribute.isFieldInLine( ) );
225 daoUtil.setInt( nIndex++, attribute.getPosition( ) );
226 daoUtil.setInt( nIndex++, attribute.getIdAttribute( ) );
227
228 daoUtil.executeUpdate( );
229 daoUtil.free( );
230 }
231
232
233
234
235
236 public void delete( int nIdAttribute )
237 {
238 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
239 daoUtil.setInt( 1, nIdAttribute );
240
241 daoUtil.executeUpdate( );
242 daoUtil.free( );
243 }
244
245
246
247
248
249
250 public List<IAttribute> selectAll( Locale locale )
251 {
252 List<IAttribute> listAttributes = new ArrayList<IAttribute>( );
253 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL );
254 daoUtil.executeQuery( );
255
256 while ( daoUtil.next( ) )
257 {
258 int nIndex = 1;
259 IAttribute attribute = null;
260
261 try
262 {
263 attribute = (IAttribute) Class.forName( daoUtil.getString( nIndex++ ) ).newInstance( );
264 }
265 catch ( ClassNotFoundException e )
266 {
267
268 AppLogService.error( e );
269 }
270 catch ( InstantiationException e )
271 {
272
273
274 AppLogService.error( e );
275 }
276 catch ( IllegalAccessException e )
277 {
278
279 AppLogService.error( e );
280 }
281
282 attribute.setIdAttribute( daoUtil.getInt( nIndex++ ) );
283 attribute.setTitle( daoUtil.getString( nIndex++ ) );
284 attribute.setHelpMessage( daoUtil.getString( nIndex++ ) );
285 attribute.setMandatory( daoUtil.getBoolean( nIndex++ ) );
286 attribute.setShownInSearch( daoUtil.getBoolean( nIndex++ ) );
287 attribute.setShownInResultList( daoUtil.getBoolean( nIndex++ ) );
288 attribute.setFieldInLine( daoUtil.getBoolean( nIndex++ ) );
289 attribute.setPosition( daoUtil.getInt( nIndex++ ) );
290 attribute.setAnonymize( daoUtil.getBoolean( nIndex++ ) );
291 attribute.setAttributeType( locale );
292
293 Plugin plugin = PluginService.getPlugin( daoUtil.getString( nIndex++ ) );
294 attribute.setPlugin( plugin );
295
296 listAttributes.add( attribute );
297 }
298
299 daoUtil.free( );
300
301 return listAttributes;
302 }
303
304
305
306
307
308
309
310 public List<IAttribute> selectPluginAttributes( String strPluginName, Locale locale )
311 {
312 int nIndex = 1;
313 List<IAttribute> listAttributes = new ArrayList<IAttribute>( );
314 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PLUGIN_ATTRIBUTES );
315 daoUtil.setString( 1, strPluginName );
316 daoUtil.executeQuery( );
317
318 while ( daoUtil.next( ) )
319 {
320 nIndex = 1;
321
322 IAttribute attribute = null;
323
324 try
325 {
326 attribute = (IAttribute) Class.forName( daoUtil.getString( nIndex++ ) ).newInstance( );
327 }
328 catch ( ClassNotFoundException e )
329 {
330
331 AppLogService.error( e );
332 }
333 catch ( InstantiationException e )
334 {
335
336
337 AppLogService.error( e );
338 }
339 catch ( IllegalAccessException e )
340 {
341
342 AppLogService.error( e );
343 }
344
345 attribute.setIdAttribute( daoUtil.getInt( nIndex++ ) );
346 attribute.setTitle( daoUtil.getString( nIndex++ ) );
347 attribute.setHelpMessage( daoUtil.getString( nIndex++ ) );
348 attribute.setMandatory( daoUtil.getBoolean( nIndex++ ) );
349 attribute.setShownInSearch( daoUtil.getBoolean( nIndex++ ) );
350 attribute.setShownInResultList( daoUtil.getBoolean( nIndex++ ) );
351 attribute.setFieldInLine( daoUtil.getBoolean( nIndex++ ) );
352 attribute.setPosition( daoUtil.getInt( nIndex++ ) );
353 attribute.setAttributeType( locale );
354
355 Plugin plugin = PluginService.getPlugin( strPluginName );
356 attribute.setPlugin( plugin );
357
358 listAttributes.add( attribute );
359 }
360
361 daoUtil.free( );
362
363 return listAttributes;
364 }
365
366
367
368
369
370
371 public List<IAttribute> selectCoreAttributes( Locale locale )
372 {
373 List<IAttribute> listAttributes = new ArrayList<IAttribute>( );
374 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CORE_ATTRIBUTES );
375 daoUtil.executeQuery( );
376
377 while ( daoUtil.next( ) )
378 {
379 int nIndex = 1;
380 IAttribute attribute = null;
381
382 try
383 {
384 attribute = (IAttribute) Class.forName( daoUtil.getString( nIndex++ ) ).newInstance( );
385 }
386 catch ( ClassNotFoundException e )
387 {
388
389 AppLogService.error( e );
390 }
391 catch ( InstantiationException e )
392 {
393
394
395 AppLogService.error( e );
396 }
397 catch ( IllegalAccessException e )
398 {
399
400 AppLogService.error( e );
401 }
402
403 attribute.setIdAttribute( daoUtil.getInt( nIndex++ ) );
404 attribute.setTitle( daoUtil.getString( nIndex++ ) );
405 attribute.setHelpMessage( daoUtil.getString( nIndex++ ) );
406 attribute.setMandatory( daoUtil.getBoolean( nIndex++ ) );
407 attribute.setShownInSearch( daoUtil.getBoolean( nIndex++ ) );
408 attribute.setShownInResultList( daoUtil.getBoolean( nIndex++ ) );
409 attribute.setFieldInLine( daoUtil.getBoolean( nIndex++ ) );
410 attribute.setPosition( daoUtil.getInt( nIndex++ ) );
411 attribute.setAttributeType( locale );
412
413 listAttributes.add( attribute );
414 }
415
416 daoUtil.free( );
417
418 return listAttributes;
419 }
420
421
422
423
424
425
426
427 public void updateAttributeAnonymization( int nIdAttribute, boolean bAnonymize )
428 {
429 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_ANONYMIZATION );
430 daoUtil.setBoolean( 1, bAnonymize );
431 daoUtil.setInt( 2, nIdAttribute );
432 daoUtil.executeUpdate( );
433 daoUtil.free( );
434 }
435 }