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.appointment.business.rule;
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 Form Rule objects
43   * 
44   * @author Laurent Payen
45   *
46   */
47  public final class FormRuleDAO implements IFormRuleDAO
48  {
49  
50      private static final String SQL_QUERY_INSERT = "INSERT INTO appointment_form_rule ( is_captcha_enabled, is_mandatory_email_enabled, is_active_authentication, nb_days_before_new_appointment, min_time_before_appointment, nb_max_appointments_per_user, nb_days_for_max_appointments_per_user, bo_overbooking, id_form) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?)";
51      private static final String SQL_QUERY_UPDATE = "UPDATE appointment_form_rule SET is_captcha_enabled = ?, is_mandatory_email_enabled = ?, is_active_authentication = ?, nb_days_before_new_appointment = ?, min_time_before_appointment = ?, nb_max_appointments_per_user = ?, nb_days_for_max_appointments_per_user = ?, bo_overbooking=? , id_form = ? WHERE id_form_rule = ?";
52      private static final String SQL_QUERY_DELETE = "DELETE FROM appointment_form_rule WHERE id_form_rule = ?";
53      private static final String SQL_QUERY_DELETE_BY_ID_FORM = "DELETE FROM appointment_form_rule WHERE id_form = ?";
54      private static final String SQL_QUERY_SELECT_COLUMNS = "SELECT id_form_rule, is_captcha_enabled, is_mandatory_email_enabled, is_active_authentication, nb_days_before_new_appointment, min_time_before_appointment, nb_max_appointments_per_user, nb_days_for_max_appointments_per_user, bo_overbooking, id_form FROM appointment_form_rule";
55      private static final String SQL_QUERY_SELECT = SQL_QUERY_SELECT_COLUMNS + " WHERE id_form_rule = ?";
56      private static final String SQL_QUERY_SELECT_BY_ID_FORM = SQL_QUERY_SELECT_COLUMNS + " WHERE id_form = ?";
57  
58      @Override
59      public synchronized void insert( FormRule formRule, Plugin plugin )
60      {
61          try ( DAOUtil daoUtil = buildDaoUtil( SQL_QUERY_INSERT, formRule, plugin, true ) )
62          {
63              daoUtil.executeUpdate( );
64              if ( daoUtil.nextGeneratedKey( ) )
65              {
66                  formRule.setIdFormRule( daoUtil.getGeneratedKeyInt( 1 ) );
67              }
68          }
69      }
70  
71      @Override
72      public void update( FormRule formRule, Plugin plugin )
73      {
74          try ( DAOUtil daoUtil = buildDaoUtil( SQL_QUERY_UPDATE, formRule, plugin, false ) )
75          {
76              daoUtil.executeUpdate( );
77          }
78      }
79  
80      @Override
81      public void delete( int nIdFormRule, Plugin plugin )
82      {
83          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
84          {
85              daoUtil.setInt( 1, nIdFormRule );
86              daoUtil.executeUpdate( );
87          }
88      }
89  
90      @Override
91      public void deleteByIdFom( int nIdForm, Plugin plugin )
92      {
93          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_BY_ID_FORM, plugin ) )
94          {
95              daoUtil.setInt( 1, nIdForm );
96              daoUtil.executeUpdate( );
97          }
98      }
99  
100     @Override
101     public FormRule select( int nIdFormRule, Plugin plugin )
102     {
103         FormRule formRule = null;
104         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
105         {
106             daoUtil.setInt( 1, nIdFormRule );
107             daoUtil.executeQuery( );
108             if ( daoUtil.next( ) )
109             {
110                 formRule = buildFormRule( daoUtil );
111             }
112         }
113         return formRule;
114     }
115 
116     @Override
117     public FormRule findByIdForm( int nIdForm, Plugin plugin )
118     {
119         FormRule formRule = null;
120         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_ID_FORM, plugin ) )
121         {
122             daoUtil.setInt( 1, nIdForm );
123             daoUtil.executeQuery( );
124             if ( daoUtil.next( ) )
125             {
126                 formRule = buildFormRule( daoUtil );
127             }
128         }
129         return formRule;
130     }
131 
132     /**
133      * Build a Form rule business object from the resultset
134      * 
135      * @param daoUtil
136      *            the prepare statement util object
137      * @return a new formRule with all its attributes assigned
138      */
139     private FormRule buildFormRule( DAOUtil daoUtil )
140     {
141         int nIndex = 1;
142         FormRule/appointment/business/rule/FormRule.html#FormRule">FormRule formRule = new FormRule( );
143         formRule.setIdFormRule( daoUtil.getInt( nIndex++ ) );
144         formRule.setIsCaptchaEnabled( daoUtil.getBoolean( nIndex++ ) );
145         formRule.setIsMandatoryEmailEnabled( daoUtil.getBoolean( nIndex++ ) );
146         formRule.setIsActiveAuthentication( daoUtil.getBoolean( nIndex++ ) );
147         formRule.setNbDaysBeforeNewAppointment( daoUtil.getInt( nIndex++ ) );
148         formRule.setMinTimeBeforeAppointment( daoUtil.getInt( nIndex++ ) );
149         formRule.setNbMaxAppointmentsPerUser( daoUtil.getInt( nIndex++ ) );
150         formRule.setNbDaysForMaxAppointmentsPerUser( daoUtil.getInt( nIndex++ ) );
151         formRule.setBoOverbooking( daoUtil.getBoolean( nIndex++ ) );
152         formRule.setIdForm( daoUtil.getInt( nIndex ) );
153         return formRule;
154     }
155 
156     /**
157      * Build a daoUtil object with the FormRule business object
158      * 
159      * @param query
160      *            the query
161      * @param formRule
162      *            the FormRule
163      * @param plugin
164      *            the plugin
165      * @param isInsert
166      *            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
167      *            set
168      * @return a new daoUtil with all its values assigned
169      */
170     private DAOUtil buildDaoUtil( String query, FormRule formRule, Plugin plugin, boolean isInsert )
171     {
172         int nIndex = 1;
173         DAOUtil daoUtil = null;
174         if ( isInsert )
175         {
176             daoUtil = new DAOUtil( query, Statement.RETURN_GENERATED_KEYS, plugin );
177         }
178         else
179         {
180             daoUtil = new DAOUtil( query, plugin );
181         }
182         daoUtil.setBoolean( nIndex++, formRule.getIsCaptchaEnabled( ) );
183         daoUtil.setBoolean( nIndex++, formRule.getIsMandatoryEmailEnabled( ) );
184         daoUtil.setBoolean( nIndex++, formRule.getIsActiveAuthentication( ) );
185         daoUtil.setInt( nIndex++, formRule.getNbDaysBeforeNewAppointment( ) );
186         daoUtil.setInt( nIndex++, formRule.getMinTimeBeforeAppointment( ) );
187         daoUtil.setInt( nIndex++, formRule.getNbMaxAppointmentsPerUser( ) );
188         daoUtil.setInt( nIndex++, formRule.getNbDaysForMaxAppointmentsPerUser( ) );
189         daoUtil.setBoolean( nIndex++, formRule.getBoOverbooking( ) );
190         daoUtil.setInt( nIndex++, formRule.getIdForm( ) );
191         if ( !isInsert )
192         {
193             daoUtil.setInt( nIndex, formRule.getIdFormRule( ) );
194         }
195         return daoUtil;
196     }
197 }