View Javadoc
1   /*
2    * Copyright (c) 2002-2018, Mairie de 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.appointment.business.message;
35  
36  import fr.paris.lutece.plugins.appointment.business.UtilDAO;
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  
40  /**
41   * This class provides Data Access methods for Form Message objects
42   * 
43   * @author Laurent Payen
44   *
45   */
46  public final class FormMessageDAO extends UtilDAO implements IFormMessageDAO
47  {
48  
49      private static final String SQL_QUERY_NEW_PK = "SELECT max(id_form_message) FROM appointment_form_message";
50      private static final String SQL_QUERY_INSERT = "INSERT INTO appointment_form_message(id_form_message, calendar_title, field_firstname_title, field_firstname_help, field_lastname_title, field_lastname_help, field_email_title, field_email_help, field_confirmationEmail_title, field_confirmationEmail_help, text_appointment_created, url_redirect_after_creation, text_appointment_canceled, label_button_redirection, no_available_slot, calendar_description, calendar_reserve_label, calendar_full_label, id_form) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
51      private static final String SQL_QUERY_UPDATE = "UPDATE appointment_form_message SET calendar_title = ?, field_firstname_title = ?, field_firstname_help = ?, field_lastname_title = ?, field_lastname_help = ?, field_email_title = ?, field_email_help = ?, field_confirmationEmail_title = ?, field_confirmationEmail_help = ?, text_appointment_created = ?, url_redirect_after_creation = ?, text_appointment_canceled = ?, label_button_redirection = ?, no_available_slot = ?, calendar_description = ?, calendar_reserve_label = ?, calendar_full_label = ?, id_form = ? WHERE id_form_message = ?";
52      private static final String SQL_QUERY_DELETE = "DELETE FROM appointment_form_message WHERE id_form_message = ?";
53      private static final String SQL_QUERY_SELECT_COLUMNS = "SELECT id_form_message, calendar_title, field_firstname_title, field_firstname_help, field_lastname_title, field_lastname_help, field_email_title, field_email_help,field_confirmationEmail_title, field_confirmationEmail_help, text_appointment_created, url_redirect_after_creation, text_appointment_canceled, label_button_redirection, no_available_slot, calendar_description, calendar_reserve_label, calendar_full_label, id_form FROM appointment_form_message";
54      private static final String SQL_QUERY_SELECT = SQL_QUERY_SELECT_COLUMNS + " WHERE id_form_message = ?";
55      private static final String SQL_QUERY_SELECT_BY_ID_FORM = SQL_QUERY_SELECT_COLUMNS + " WHERE id_form = ?";
56  
57      @Override
58      public synchronized void insert( FormMessage formMessage, Plugin plugin )
59      {
60          formMessage.setIdFormMessage( getNewPrimaryKey( SQL_QUERY_NEW_PK, plugin ) );
61          DAOUtil daoUtil = buildDaoUtil( SQL_QUERY_INSERT, formMessage, plugin, true );
62          executeUpdate( daoUtil );
63      }
64  
65      @Override
66      public void update( FormMessage formMessage, Plugin plugin )
67      {
68          DAOUtil daoUtil = buildDaoUtil( SQL_QUERY_UPDATE, formMessage, plugin, false );
69          executeUpdate( daoUtil );
70      }
71  
72      @Override
73      public void delete( int nIdFormMessage, Plugin plugin )
74      {
75          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
76          daoUtil.setInt( 1, nIdFormMessage );
77          executeUpdate( daoUtil );
78      }
79  
80      @Override
81      public FormMessage select( int nIdFormMessage, Plugin plugin )
82      {
83          DAOUtil daoUtil = null;
84          FormMessage formMessage = null;
85          try
86          {
87              daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
88              daoUtil.setInt( 1, nIdFormMessage );
89              daoUtil.executeQuery( );
90              if ( daoUtil.next( ) )
91              {
92                  formMessage = buildFormMessage( daoUtil );
93              }
94          }
95          finally
96          {
97              if ( daoUtil != null )
98              {
99                  daoUtil.free( );
100             }
101         }
102         return formMessage;
103     }
104 
105     @Override
106     public FormMessage findByIdForm( int nIdForm, Plugin plugin )
107     {
108         DAOUtil daoUtil = null;
109         FormMessage formMessage = null;
110         try
111         {
112             daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_ID_FORM, plugin );
113             daoUtil.setInt( 1, nIdForm );
114             daoUtil.executeQuery( );
115             if ( daoUtil.next( ) )
116             {
117                 formMessage = buildFormMessage( daoUtil );
118             }
119         }
120         finally
121         {
122             if ( daoUtil != null )
123             {
124                 daoUtil.free( );
125             }
126         }
127         return formMessage;
128     }
129 
130     /**
131      * Build a Form Message business object from the resultset
132      * 
133      * @param daoUtil
134      *            the prepare statement util object
135      * @return a new Form Message with all its attributes assigned
136      */
137     private FormMessage buildFormMessage( DAOUtil daoUtil )
138     {
139         int nIndex = 1;
140         FormMessage formMessage = new FormMessage( );
141         formMessage.setIdFormMessage( daoUtil.getInt( nIndex++ ) );
142         formMessage.setCalendarTitle( daoUtil.getString( nIndex++ ) );
143         formMessage.setFieldFirstNameTitle( daoUtil.getString( nIndex++ ) );
144         formMessage.setFieldFirstNameHelp( daoUtil.getString( nIndex++ ) );
145         formMessage.setFieldLastNameTitle( daoUtil.getString( nIndex++ ) );
146         formMessage.setFieldLastNameHelp( daoUtil.getString( nIndex++ ) );
147         formMessage.setFieldEmailTitle( daoUtil.getString( nIndex++ ) );
148         formMessage.setFieldEmailHelp( daoUtil.getString( nIndex++ ) );
149         formMessage.setFieldConfirmationEmail( daoUtil.getString( nIndex++ ) );
150         formMessage.setFieldConfirmationEmailHelp( daoUtil.getString( nIndex++ ) );
151         formMessage.setTextAppointmentCreated( daoUtil.getString( nIndex++ ) );
152         formMessage.setUrlRedirectAfterCreation( daoUtil.getString( nIndex++ ) );
153         formMessage.setTextAppointmentCanceled( daoUtil.getString( nIndex++ ) );
154         formMessage.setLabelButtonRedirection( daoUtil.getString( nIndex++ ) );
155         formMessage.setNoAvailableSlot( daoUtil.getString( nIndex++ ) );
156         formMessage.setCalendarDescription( daoUtil.getString( nIndex++ ) );
157         formMessage.setCalendarReserveLabel( daoUtil.getString( nIndex++ ) );
158         formMessage.setCalendarFullLabel( daoUtil.getString( nIndex++ ) );
159         formMessage.setIdForm( daoUtil.getInt( nIndex ) );
160         return formMessage;
161     }
162 
163     /**
164      * Build a daoUtil object with the form message
165      * 
166      * @param query
167      *            the query
168      * @param formMessage
169      *            the form message
170      * @param plugin
171      *            the plugin
172      * @param isInsert
173      *            true if it is an insert query (in this case, need to set the id). If false, it is an update, in this case, there is a where parameter id to
174      *            set
175      * @return a new daoUtil with all its values assigned
176      */
177     private DAOUtil buildDaoUtil( String query, FormMessage formMessage, Plugin plugin, boolean isInsert )
178     {
179         int nIndex = 1;
180         DAOUtil daoUtil = new DAOUtil( query, plugin );
181         if ( isInsert )
182         {
183             daoUtil.setInt( nIndex++, formMessage.getIdFormMessage( ) );
184         }
185         daoUtil.setString( nIndex++, formMessage.getCalendarTitle( ) );
186         daoUtil.setString( nIndex++, formMessage.getFieldFirstNameTitle( ) );
187         daoUtil.setString( nIndex++, formMessage.getFieldFirstNameHelp( ) );
188         daoUtil.setString( nIndex++, formMessage.getFieldLastNameTitle( ) );
189         daoUtil.setString( nIndex++, formMessage.getFieldLastNameHelp( ) );
190         daoUtil.setString( nIndex++, formMessage.getFieldEmailTitle( ) );
191         daoUtil.setString( nIndex++, formMessage.getFieldEmailHelp( ) );
192         daoUtil.setString( nIndex++, formMessage.getFieldConfirmationEmail( ) );
193         daoUtil.setString( nIndex++, formMessage.getFieldConfirmationEmailHelp( ) );
194         daoUtil.setString( nIndex++, formMessage.getTextAppointmentCreated( ) );
195         daoUtil.setString( nIndex++, formMessage.getUrlRedirectAfterCreation( ) );
196         daoUtil.setString( nIndex++, formMessage.getTextAppointmentCanceled( ) );
197         daoUtil.setString( nIndex++, formMessage.getLabelButtonRedirection( ) );
198         daoUtil.setString( nIndex++, formMessage.getNoAvailableSlot( ) );
199         daoUtil.setString( nIndex++, formMessage.getCalendarDescription( ) );
200         daoUtil.setString( nIndex++, formMessage.getCalendarReserveLabel( ) );
201         daoUtil.setString( nIndex++, formMessage.getCalendarFullLabel( ) );
202         daoUtil.setInt( nIndex++, formMessage.getIdForm( ) );
203         if ( !isInsert )
204         {
205             daoUtil.setInt( nIndex, formMessage.getIdFormMessage( ) );
206         }
207         return daoUtil;
208     }
209 
210     /**
211      * Execute a safe update (Free the connection in case of error when execute the query)
212      * 
213      * @param daoUtil
214      *            the daoUtil
215      */
216     private void executeUpdate( DAOUtil daoUtil )
217     {
218         try
219         {
220             daoUtil.executeUpdate( );
221         }
222         finally
223         {
224             if ( daoUtil != null )
225             {
226                 daoUtil.free( );
227             }
228         }
229     }
230 
231 }