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.rule;
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 Rule objects
42   * 
43   * @author Laurent Payen
44   *
45   */
46  public final class FormRuleDAO extends UtilDAO implements IFormRuleDAO
47  {
48  
49      private static final String SQL_QUERY_NEW_PK = "SELECT max(id_form_rule) FROM appointment_form_rule";
50      private static final String SQL_QUERY_INSERT = "INSERT INTO appointment_form_rule (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, 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 = ?, 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_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, id_form FROM appointment_form_rule";
54      private static final String SQL_QUERY_SELECT = SQL_QUERY_SELECT_COLUMNS + " WHERE id_form_rule = ?";
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( FormRule formRule, Plugin plugin )
59      {
60          formRule.setIdFormRule( getNewPrimaryKey( SQL_QUERY_NEW_PK, plugin ) );
61          DAOUtil daoUtil = buildDaoUtil( SQL_QUERY_INSERT, formRule, plugin, true );
62          executeUpdate( daoUtil );
63      }
64  
65      @Override
66      public void update( FormRule formRule, Plugin plugin )
67      {
68          DAOUtil daoUtil = buildDaoUtil( SQL_QUERY_UPDATE, formRule, plugin, false );
69          executeUpdate( daoUtil );
70      }
71  
72      @Override
73      public void delete( int nIdFormRule, Plugin plugin )
74      {
75          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
76          daoUtil.setInt( 1, nIdFormRule );
77          executeUpdate( daoUtil );
78      }
79  
80      @Override
81      public FormRule select( int nIdFormRule, Plugin plugin )
82      {
83          DAOUtil daoUtil = null;
84          FormRule formRule = null;
85          try
86          {
87              daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
88              daoUtil.setInt( 1, nIdFormRule );
89              daoUtil.executeQuery( );
90              if ( daoUtil.next( ) )
91              {
92                  formRule = buildFormRule( daoUtil );
93              }
94          }
95          finally
96          {
97              if ( daoUtil != null )
98              {
99                  daoUtil.free( );
100             }
101         }
102         return formRule;
103     }
104 
105     @Override
106     public FormRule findByIdForm( int nIdForm, Plugin plugin )
107     {
108         DAOUtil daoUtil = null;
109         FormRule formRule = 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                 formRule = buildFormRule( daoUtil );
118             }
119         }
120         finally
121         {
122             if ( daoUtil != null )
123             {
124                 daoUtil.free( );
125             }
126         }
127         return formRule;
128     }
129 
130     /**
131      * Build a Form rule business object from the resultset
132      * 
133      * @param daoUtil
134      *            the prepare statement util object
135      * @return a new formRule with all its attributes assigned
136      */
137     private FormRule buildFormRule( DAOUtil daoUtil )
138     {
139         int nIndex = 1;
140         FormRule formRule = new FormRule( );
141         formRule.setIdFormRule( daoUtil.getInt( nIndex++ ) );
142         formRule.setIsCaptchaEnabled( daoUtil.getBoolean( nIndex++ ) );
143         formRule.setIsMandatoryEmailEnabled( daoUtil.getBoolean( nIndex++ ) );
144         formRule.setIsActiveAuthentication( daoUtil.getBoolean( nIndex++ ) );
145         formRule.setNbDaysBeforeNewAppointment( daoUtil.getInt( nIndex++ ) );
146         formRule.setMinTimeBeforeAppointment( daoUtil.getInt( nIndex++ ) );
147         formRule.setNbMaxAppointmentsPerUser( daoUtil.getInt( nIndex++ ) );
148         formRule.setNbDaysForMaxAppointmentsPerUser( daoUtil.getInt( nIndex++ ) );
149         formRule.setIdForm( daoUtil.getInt( nIndex ) );
150         return formRule;
151     }
152 
153     /**
154      * Build a daoUtil object with the FormRule business object
155      * 
156      * @param query
157      *            the query
158      * @param formRule
159      *            the FormRule
160      * @param plugin
161      *            the plugin
162      * @param isInsert
163      *            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
164      *            set
165      * @return a new daoUtil with all its values assigned
166      */
167     private DAOUtil buildDaoUtil( String query, FormRule formRule, Plugin plugin, boolean isInsert )
168     {
169         int nIndex = 1;
170         DAOUtil daoUtil = new DAOUtil( query, plugin );
171         if ( isInsert )
172         {
173             daoUtil.setInt( nIndex++, formRule.getIdFormRule( ) );
174         }
175         daoUtil.setBoolean( nIndex++, formRule.getIsCaptchaEnabled( ) );
176         daoUtil.setBoolean( nIndex++, formRule.getIsMandatoryEmailEnabled( ) );
177         daoUtil.setBoolean( nIndex++, formRule.getIsActiveAuthentication( ) );
178         daoUtil.setInt( nIndex++, formRule.getNbDaysBeforeNewAppointment( ) );
179         daoUtil.setInt( nIndex++, formRule.getMinTimeBeforeAppointment( ) );
180         daoUtil.setInt( nIndex++, formRule.getNbMaxAppointmentsPerUser( ) );
181         daoUtil.setInt( nIndex++, formRule.getNbDaysForMaxAppointmentsPerUser( ) );
182         daoUtil.setInt( nIndex++, formRule.getIdForm( ) );
183         if ( !isInsert )
184         {
185             daoUtil.setInt( nIndex, formRule.getIdFormRule( ) );
186         }
187         return daoUtil;
188     }
189 
190     /**
191      * Execute a safe update (Free the connection in case of error when execute the query)
192      * 
193      * @param daoUtil
194      *            the daoUtil
195      */
196     private void executeUpdate( DAOUtil daoUtil )
197     {
198         try
199         {
200             daoUtil.executeUpdate( );
201         }
202         finally
203         {
204             if ( daoUtil != null )
205             {
206                 daoUtil.free( );
207             }
208         }
209     }
210 
211 }