View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * AttributeDAO
49   *
50   */
51  public class AttributeDAO implements IAttributeDAO
52  {
53      // NEW PK
54      private static final String SQL_QUERY_NEW_PK = " SELECT max(id_attribute) FROM core_attribute ";
55  
56      // NEW POSITION
57      private static final String SQL_QUERY_NEW_POSITION = "SELECT MAX(attribute_position)" + " FROM core_attribute ";
58  
59      // SELECT
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      // INSERT
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      // UPDATE
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      // DELETE
79      private static final String SQL_QUERY_DELETE = " DELETE FROM core_attribute WHERE id_attribute = ?";
80  
81      // NEW PK
82      /**
83       * Generate a new PK
84       * @return The new ID
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     * Generates a new field position
106     * @return the new entry position
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             // if the table is empty
118             nPos = 1;
119         }
120 
121         nPos = daoUtil.getInt( 1 ) + 1;
122         daoUtil.free(  );
123 
124         return nPos;
125     }
126 
127     /**
128      * Load attribute
129      * @param nIdAttribute ID
130      * @param locale Locale
131      * @return Attribute
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                 // class doesn't exist
152                 AppLogService.error( e );
153             }
154             catch ( InstantiationException e )
155             {
156                 // Class is abstract or is an interface or haven't accessible
157                 // builder
158                 AppLogService.error( e );
159             }
160             catch ( IllegalAccessException e )
161             {
162                 // can't access to rhe class
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      * Insert a new attribute
187      * @param attribute the attribute
188      * @return new PK
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      * Update an attribute
213      * @param attribute the attribute
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      * Delete an attribute
234      * @param nIdAttribute The Id of the attribute
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      * Load every attributes
247      * @param locale locale
248      * @return list of attributes
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                 // class doesn't exist
268                 AppLogService.error( e );
269             }
270             catch ( InstantiationException e )
271             {
272                 // Class is abstract or is an interface or haven't accessible
273                 // builder
274                 AppLogService.error( e );
275             }
276             catch ( IllegalAccessException e )
277             {
278                 // can't access to the class
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      * Load every attributes from plugin name
306      * @param strPluginName plugin name
307      * @param locale locale
308      * @return list of attributes
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                 // class doesn't exist
331                 AppLogService.error( e );
332             }
333             catch ( InstantiationException e )
334             {
335                 // Class is abstract or is an interface or haven't accessible
336                 // builder
337                 AppLogService.error( e );
338             }
339             catch ( IllegalAccessException e )
340             {
341                 // can't access to rhe class
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      * Load every attributes that do not come from a plugin
368      * @param locale locale
369      * @return list of attributes
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                 // class doesn't exist
389                 AppLogService.error( e );
390             }
391             catch ( InstantiationException e )
392             {
393                 // Class is abstract or is an interface or haven't accessible
394                 // builder
395                 AppLogService.error( e );
396             }
397             catch ( IllegalAccessException e )
398             {
399                 // can't access to rhe class
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      * Update the anonymization status of the attribute.
423      * @param nIdAttribute Id of the attribute
424      * @param bAnonymize New value of the anonymization status. True means the
425      *            attribute should be anonymize, false means it doesn't.
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 }