View Javadoc
1   /*
2    * Copyright (c) 2002-2025, 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.forms.business;
35  
36  import java.sql.Statement;
37  
38  import fr.paris.lutece.portal.service.plugin.Plugin;
39  import fr.paris.lutece.util.sql.DAOUtil;
40  
41  /**
42   * This class provides Data Access methods for FormMessage objects
43   */
44  public final class FormMessageDAO implements IFormMessageDAO
45  {
46      // Constants
47      private static final String SQL_QUERY_SELECTALL = "SELECT id, id_form, end_message_display, end_message, label_end_message_button FROM forms_message";
48      private static final String SQL_QUERY_SELECT = SQL_QUERY_SELECTALL + " WHERE id = ?";
49      private static final String SQL_QUERY_SELECT_BY_FORM = SQL_QUERY_SELECTALL + " WHERE id_form = ?";
50      private static final String SQL_QUERY_INSERT = "INSERT INTO forms_message ( id_form, end_message_display, end_message, label_end_message_button ) VALUES ( ?, ?, ?, ? ) ";
51      private static final String SQL_QUERY_DELETE = "DELETE FROM forms_message WHERE id = ? ";
52      private static final String SQL_QUERY_DELETE_BY_FORM = "DELETE FROM forms_message WHERE id_form = ? ";
53      private static final String SQL_QUERY_UPDATE = "UPDATE forms_message SET id_form = ?, end_message_display = ?, end_message = ?, label_end_message_button = ? WHERE id = ?";
54  
55      /**
56       * {@inheritDoc }
57       */
58      @Override
59      public void insert( FormMessage formMessage, Plugin plugin )
60      {
61          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, plugin ) )
62          {
63              int nIndex = 1;
64              daoUtil.setInt( nIndex++, formMessage.getIdForm( ) );
65              daoUtil.setBoolean( nIndex++, formMessage.getEndMessageDisplay( ) );
66              daoUtil.setString( nIndex++, formMessage.getEndMessage( ) );
67              daoUtil.setString( nIndex++, formMessage.getLabelEndMessageButton( ) );
68  
69              daoUtil.executeUpdate( );
70  
71              if ( daoUtil.nextGeneratedKey( ) )
72              {
73                  formMessage.setId( daoUtil.getGeneratedKeyInt( 1 ) );
74              }
75          }
76      }
77  
78      /**
79       * {@inheritDoc }
80       */
81      @Override
82      public FormMessage load( int nKey, Plugin plugin )
83      {
84          FormMessage formMessage = null;
85          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
86          {
87              daoUtil.setInt( 1, nKey );
88              daoUtil.executeQuery( );
89  
90              if ( daoUtil.next( ) )
91              {
92                  formMessage = dataToObject( daoUtil );
93              }
94          }
95          return formMessage;
96      }
97  
98      /**
99       * {@inheritDoc }
100      */
101     @Override
102     public void delete( int nKey, Plugin plugin )
103     {
104         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
105         {
106             daoUtil.setInt( 1, nKey );
107             daoUtil.executeUpdate( );
108         }
109     }
110 
111     /**
112      * {@inheritDoc }
113      */
114     @Override
115     public void deleteByForm( int nIdForm, Plugin plugin )
116     {
117         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_BY_FORM, plugin ) )
118         {
119             daoUtil.setInt( 1, nIdForm );
120             daoUtil.executeUpdate( );
121         }
122     }
123 
124     /**
125      * {@inheritDoc }
126      */
127     @Override
128     public void store( FormMessage formMessage, Plugin plugin )
129     {
130 
131         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
132         {
133             int nIndex = 1;
134             daoUtil.setInt( nIndex++, formMessage.getIdForm( ) );
135             daoUtil.setBoolean( nIndex++, formMessage.getEndMessageDisplay( ) );
136             daoUtil.setString( nIndex++, formMessage.getEndMessage( ) );
137             daoUtil.setString( nIndex++, formMessage.getLabelEndMessageButton( ) );
138 
139             daoUtil.setInt( nIndex++, formMessage.getId( ) );
140 
141             daoUtil.executeUpdate( );
142         }
143     }
144 
145     /**
146      * {@inheritDoc }
147      */
148     @Override
149     public FormMessage selectByForm( int nIdForm, Plugin plugin )
150     {
151         FormMessage formMessage = null;
152         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_FORM, plugin ) )
153         {
154             daoUtil.setInt( 1, nIdForm );
155             daoUtil.executeQuery( );
156 
157             if ( daoUtil.next( ) )
158             {
159                 formMessage = dataToObject( daoUtil );
160             }
161 
162         }
163 
164         return formMessage;
165     }
166 
167     /**
168      * 
169      * @param daoUtil
170      *            The daoutil
171      * @return The populated FormMessage object
172      */
173     private FormMessage dataToObject( DAOUtil daoUtil )
174     {
175         FormMessagesiness/FormMessage.html#FormMessage">FormMessage formMessage = new FormMessage( );
176         formMessage.setId( daoUtil.getInt( "id" ) );
177         formMessage.setIdForm( daoUtil.getInt( "id_form" ) );
178         formMessage.setEndMessageDisplay( daoUtil.getBoolean( "end_message_display" ) );
179         formMessage.setEndMessage( daoUtil.getString( "end_message" ) );
180         formMessage.setLabelEndMessageButton( daoUtil.getString( "label_end_message_button" ) );
181 
182         return formMessage;
183     }
184 
185 }