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