View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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.sql.Statement;
42  import java.util.ArrayList;
43  import java.util.List;
44  import java.util.Locale;
45  
46  /**
47   *
48   * AttributeDAO
49   *
50   */
51  public class AttributeDAO implements IAttributeDAO
52  {
53      // NEW POSITION
54      private static final String SQL_QUERY_NEW_POSITION = "SELECT MAX(attribute_position)" + " FROM core_attribute ";
55  
56      // SELECT
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      // INSERT
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      // UPDATE
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      // DELETE
76      private static final String SQL_QUERY_DELETE = " DELETE FROM core_attribute WHERE id_attribute = ?";
77  
78      /**
79       * Generates a new field position
80       * 
81       * @return the new entry position
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                  // if the table is empty
93                  nPos = 1;
94              }
95  
96              nPos = daoUtil.getInt( 1 ) + 1;
97          }
98  
99          return nPos;
100     }
101 
102     /**
103      * Load attribute
104      * 
105      * @param nIdAttribute
106      *            ID
107      * @param locale
108      *            Locale
109      * @return Attribute
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      * Insert a new attribute
156      * 
157      * @param attribute
158      *            the attribute
159      * @return new PK
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      * Update an attribute
188      * 
189      * @param attribute
190      *            the attribute
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      * Delete an attribute
212      * 
213      * @param nIdAttribute
214      *            The Id of the attribute
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      * Load every attributes
228      * 
229      * @param locale
230      *            locale
231      * @return list of attributes
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      * Load every attributes from plugin name
281      * 
282      * @param strPluginName
283      *            plugin name
284      * @param locale
285      *            locale
286      * @return list of attributes
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      * Load every attributes that do not come from a plugin
338      * 
339      * @param locale
340      *            locale
341      * @return list of attributes
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      * Update the anonymization status of the attribute.
387      * 
388      * @param nIdAttribute
389      *            Id of the attribute
390      * @param bAnonymize
391      *            New value of the anonymization status. True means the attribute should be anonymize, false means it doesn't.
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 }