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.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 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 ) 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 = ? 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  
68              daoUtil.executeUpdate( );
69  
70              if ( daoUtil.nextGeneratedKey( ) )
71              {
72                  formMessage.setId( daoUtil.getGeneratedKeyInt( 1 ) );
73              }
74          }
75      }
76  
77      /**
78       * {@inheritDoc }
79       */
80      @Override
81      public FormMessage load( int nKey, Plugin plugin )
82      {
83          FormMessage formMessage = null;
84          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
85          {
86              daoUtil.setInt( 1, nKey );
87              daoUtil.executeQuery( );
88  
89              if ( daoUtil.next( ) )
90              {
91                  formMessage = dataToObject( daoUtil );
92              }
93          }
94          return formMessage;
95      }
96  
97      /**
98       * {@inheritDoc }
99       */
100     @Override
101     public void delete( int nKey, Plugin plugin )
102     {
103         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
104         {
105             daoUtil.setInt( 1, nKey );
106             daoUtil.executeUpdate( );
107         }
108     }
109 
110     /**
111      * {@inheritDoc }
112      */
113     @Override
114     public void deleteByForm( int nIdForm, Plugin plugin )
115     {
116         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_BY_FORM, plugin ) )
117         {
118             daoUtil.setInt( 1, nIdForm );
119             daoUtil.executeUpdate( );
120         }
121     }
122 
123     /**
124      * {@inheritDoc }
125      */
126     @Override
127     public void store( FormMessage formMessage, Plugin plugin )
128     {
129 
130         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
131         {
132             int nIndex = 1;
133             daoUtil.setInt( nIndex++, formMessage.getIdForm( ) );
134             daoUtil.setBoolean( nIndex++, formMessage.getEndMessageDisplay( ) );
135             daoUtil.setString( nIndex++, formMessage.getEndMessage( ) );
136 
137             daoUtil.setInt( nIndex++, formMessage.getId( ) );
138 
139             daoUtil.executeUpdate( );
140         }
141     }
142 
143     /**
144      * {@inheritDoc }
145      */
146     @Override
147     public FormMessage selectByForm( int nIdForm, Plugin plugin )
148     {
149         FormMessage formMessage = null;
150         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_FORM, plugin ) )
151         {
152             daoUtil.setInt( 1, nIdForm );
153             daoUtil.executeQuery( );
154 
155             if ( daoUtil.next( ) )
156             {
157                 formMessage = dataToObject( daoUtil );
158             }
159 
160         }
161 
162         return formMessage;
163     }
164 
165     /**
166      * 
167      * @param daoUtil
168      *            The daoutil
169      * @return The populated FormMessage object
170      */
171     private FormMessage dataToObject( DAOUtil daoUtil )
172     {
173         FormMessagesiness/FormMessage.html#FormMessage">FormMessage formMessage = new FormMessage( );
174         formMessage.setId( daoUtil.getInt( "id" ) );
175         formMessage.setIdForm( daoUtil.getInt( "id_form" ) );
176         formMessage.setEndMessageDisplay( daoUtil.getBoolean( "end_message_display" ) );
177         formMessage.setEndMessage( daoUtil.getString( "end_message" ) );
178 
179         return formMessage;
180     }
181 
182 }