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