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.util.AppLogService;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.sql.Statement;
40  import java.util.ArrayList;
41  import java.util.List;
42  
43  /**
44   *
45   * AttributeFieldDAO
46   *
47   */
48  public class AttributeFieldDAO implements IAttributeFieldDAO
49  {
50  
51      // NEW POSITION
52      private static final String SQL_QUERY_NEW_POSITION = "SELECT MAX(field_position)" + " FROM core_attribute_field ";
53  
54      // SELECT
55      private static final String SQL_QUERY_SELECT = " SELECT id_field, id_attribute, title, DEFAULT_value, is_DEFAULT_value, height, width, max_size_enter, is_multiple, field_position "
56              + " FROM core_attribute_field WHERE id_field = ? ";
57      private static final String SQL_QUERY_SELECT_ATTRIBUTE_BY_ID_FIELD = " SELECT a.type_class_name, a.id_attribute, a.title, a.help_message, a.is_mandatory, a.attribute_position "
58              + " FROM core_attribute a INNER JOIN core_attribute_field af ON a.id_attribute = af.id_attribute " + " WHERE af.id_field = ? ";
59      private static final String SQL_QUERY_SELECT_ATTRIBUTE_FIELDS_BY_ID_ATTRIBUTE = " SELECT id_field, id_attribute, title, DEFAULT_value, is_DEFAULT_value, height, width, max_size_enter, is_multiple, field_position "
60              + " FROM core_attribute_field WHERE id_attribute = ? ORDER BY field_position ";
61  
62      // INSERT
63      private static final String SQL_QUERY_INSERT = " INSERT INTO core_attribute_field (id_attribute, title, DEFAULT_value, is_DEFAULT_value, height, width, max_size_enter, is_multiple, field_position) "
64              + " VALUES(?,?,?,?,?,?,?,?,?) ";
65  
66      // UPDATE
67      private static final String SQL_QUERY_UPDATE = " UPDATE core_attribute_field SET title = ?, DEFAULT_value = ?, is_DEFAULT_value = ?, height = ?, width = ?, max_size_enter = ?, is_multiple = ?, field_position = ? "
68              + " WHERE id_field = ? ";
69  
70      // DELETE
71      private static final String SQL_QUERY_DELETE = " DELETE FROM core_attribute_field WHERE id_field = ? ";
72      private static final String SQL_QUERY_DELETE_BY_ID_ATTRIBUTE = " DELETE FROM core_attribute_field WHERE id_attribute = ? ";
73  
74      /**
75       * Generates a new field position
76       * 
77       * @return the new entry position
78       */
79      private int newPosition( )
80      {
81          int nPos;
82          try ( DAOUtilsql/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_POSITION ) )
83          {
84              daoUtil.executeQuery( );
85  
86              if ( !daoUtil.next( ) )
87              {
88                  // if the table is empty
89                  nPos = 1;
90              }
91  
92              nPos = daoUtil.getInt( 1 ) + 1;
93          }
94  
95          return nPos;
96      }
97  
98      /**
99       * Load attribute field
100      * 
101      * @param nIdField
102      *            ID Field
103      * @return Attribute Field
104      */
105     @Override
106     public AttributeField load( int nIdField )
107     {
108         AttributeField attributeField = null;
109         try ( DAOUtilsql/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
110         {
111             daoUtil.setInt( 1, nIdField );
112             daoUtil.executeQuery( );
113 
114             if ( daoUtil.next( ) )
115             {
116                 attributeField = new AttributeField( );
117                 attributeField.setIdField( daoUtil.getInt( 1 ) );
118 
119                 IAttribute attribute = selectAttributeByIdField( nIdField );
120                 attributeField.setAttribute( attribute );
121                 attributeField.setTitle( daoUtil.getString( 3 ) );
122                 attributeField.setValue( daoUtil.getString( 4 ) );
123                 attributeField.setDefaultValue( daoUtil.getBoolean( 5 ) );
124                 attributeField.setHeight( daoUtil.getInt( 6 ) );
125                 attributeField.setWidth( daoUtil.getInt( 7 ) );
126                 attributeField.setMaxSizeEnter( daoUtil.getInt( 8 ) );
127                 attributeField.setMultiple( daoUtil.getBoolean( 9 ) );
128                 attributeField.setPosition( daoUtil.getInt( 10 ) );
129             }
130 
131         }
132 
133         return attributeField;
134     }
135 
136     /**
137      * Select attribute by id field
138      * 
139      * @param nIdField
140      *            id field
141      * @return user attribute
142      */
143     @Override
144     public IAttribute selectAttributeByIdField( int nIdField )
145     {
146         IAttribute attribute = null;
147         try ( DAOUtilsql/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ATTRIBUTE_BY_ID_FIELD ) )
148         {
149             daoUtil.setInt( 1, nIdField );
150             daoUtil.executeQuery( );
151 
152             if ( daoUtil.next( ) )
153             {
154                 try
155                 {
156                     attribute = (IAttribute) Class.forName( daoUtil.getString( 1 ) ).newInstance( );
157                 }
158                 catch( IllegalAccessException | InstantiationException | ClassNotFoundException e )
159                 {
160                     AppLogService.error( e.getMessage( ), e );
161                 }
162                 if ( attribute != null )
163                 {
164                     attribute.setIdAttribute( daoUtil.getInt( 2 ) );
165                     attribute.setTitle( daoUtil.getString( 3 ) );
166                     attribute.setHelpMessage( daoUtil.getString( 4 ) );
167                     attribute.setMandatory( daoUtil.getBoolean( 5 ) );
168                     attribute.setPosition( daoUtil.getInt( 6 ) );
169                 }
170             }
171 
172         }
173 
174         return attribute;
175     }
176 
177     /**
178      * Load the lists of attribute field associated to an attribute
179      * 
180      * @param nIdAttribute
181      *            the ID attribute
182      * @return the list of attribute fields
183      */
184     @Override
185     public List<AttributeField> selectAttributeFieldsByIdAttribute( int nIdAttribute )
186     {
187         List<AttributeField> listAttributeFields = new ArrayList<>( );
188         try ( DAOUtilsql/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ATTRIBUTE_FIELDS_BY_ID_ATTRIBUTE ) )
189         {
190             daoUtil.setInt( 1, nIdAttribute );
191             daoUtil.executeQuery( );
192 
193             while ( daoUtil.next( ) )
194             {
195                 AttributeFieldr/attribute/AttributeField.html#AttributeField">AttributeField attributeField = new AttributeField( );
196                 attributeField.setIdField( daoUtil.getInt( 1 ) );
197 
198                 IAttribute attribute = selectAttributeByIdField( attributeField.getIdField( ) );
199                 attributeField.setAttribute( attribute );
200                 attributeField.setTitle( daoUtil.getString( 3 ) );
201                 attributeField.setValue( daoUtil.getString( 4 ) );
202                 attributeField.setDefaultValue( daoUtil.getBoolean( 5 ) );
203                 attributeField.setHeight( daoUtil.getInt( 6 ) );
204                 attributeField.setWidth( daoUtil.getInt( 7 ) );
205                 attributeField.setMaxSizeEnter( daoUtil.getInt( 8 ) );
206                 attributeField.setMultiple( daoUtil.getBoolean( 9 ) );
207                 attributeField.setPosition( daoUtil.getInt( 10 ) );
208                 listAttributeFields.add( attributeField );
209             }
210 
211         }
212 
213         return listAttributeFields;
214     }
215 
216     /**
217      * Insert a new attribute field
218      * 
219      * @param attributeField
220      *            the attribute field
221      * @return new PK
222      */
223     @Override
224     public int insert( AttributeField attributeField )
225     {
226         try ( DAOUtilsql/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS ) )
227         {
228             int nIndex = 1;
229             daoUtil.setInt( nIndex++, attributeField.getAttribute( ).getIdAttribute( ) );
230             daoUtil.setString( nIndex++, attributeField.getTitle( ) );
231             daoUtil.setString( nIndex++, attributeField.getValue( ) );
232             daoUtil.setBoolean( nIndex++, attributeField.isDefaultValue( ) );
233             daoUtil.setInt( nIndex++, attributeField.getHeight( ) );
234             daoUtil.setInt( nIndex++, attributeField.getWidth( ) );
235             daoUtil.setInt( nIndex++, attributeField.getMaxSizeEnter( ) );
236             daoUtil.setBoolean( nIndex++, attributeField.isMultiple( ) );
237             daoUtil.setInt( nIndex, newPosition( ) );
238 
239             daoUtil.executeUpdate( );
240 
241             if ( daoUtil.nextGeneratedKey( ) )
242             {
243                 attributeField.setIdField( daoUtil.getGeneratedKeyInt( 1 ) );
244             }
245         }
246 
247         return attributeField.getIdField( );
248     }
249 
250     /**
251      * Update an attribute field
252      * 
253      * @param attributeField
254      *            the attribute field
255      */
256     @Override
257     public void store( AttributeField attributeField )
258     {
259         try ( DAOUtilsql/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
260         {
261             daoUtil.setString( 1, attributeField.getTitle( ) );
262             daoUtil.setString( 2, attributeField.getValue( ) );
263             daoUtil.setBoolean( 3, attributeField.isDefaultValue( ) );
264             daoUtil.setInt( 4, attributeField.getHeight( ) );
265             daoUtil.setInt( 5, attributeField.getWidth( ) );
266             daoUtil.setInt( 6, attributeField.getMaxSizeEnter( ) );
267             daoUtil.setBoolean( 7, attributeField.isMultiple( ) );
268             daoUtil.setInt( 8, attributeField.getPosition( ) );
269             daoUtil.setInt( 9, attributeField.getIdField( ) );
270 
271             daoUtil.executeUpdate( );
272         }
273     }
274 
275     /**
276      * Delete an attribute field
277      * 
278      * @param nIdField
279      *            The id field
280      */
281     @Override
282     public void delete( int nIdField )
283     {
284         try ( DAOUtilsql/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
285         {
286             daoUtil.setInt( 1, nIdField );
287 
288             daoUtil.executeUpdate( );
289         }
290     }
291 
292     /**
293      * Delete all attribute field from an attribute id
294      * 
295      * @param nIdAttribute
296      *            the ID attribute
297      */
298     @Override
299     public void deleteAttributeFieldsFromIdAttribute( int nIdAttribute )
300     {
301         try ( DAOUtilsql/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_BY_ID_ATTRIBUTE ) )
302         {
303             daoUtil.setInt( 1, nIdAttribute );
304 
305             daoUtil.executeUpdate( );
306         }
307     }
308 }