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