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 java.sql.Date;
37  import java.time.LocalDate;
38  import java.util.ArrayList;
39  import java.util.List;
40  
41  import fr.paris.lutece.plugins.appointment.business.UtilDAO;
42  import fr.paris.lutece.portal.service.plugin.Plugin;
43  import fr.paris.lutece.util.sql.DAOUtil;
44  
45  /**
46   * This class provides Data Access methods for Reservation Rule objects
47   * 
48   * @author Laurent Payen
49   *
50   */
51  public final class ReservationRuleDAO extends UtilDAO implements IReservationRuleDAO
52  {
53  
54      private static final String SQL_QUERY_NEW_PK = "SELECT max(id_reservation_rule) FROM appointment_reservation_rule";
55      private static final String SQL_QUERY_UPDATE = "UPDATE appointment_reservation_rule SET date_of_apply = ?, max_capacity_per_slot = ?, max_people_per_appointment =?, id_form = ? WHERE id_reservation_rule = ?";
56      private static final String SQL_QUERY_SELECT_COLUMNS = "SELECT id_reservation_rule, date_of_apply, max_capacity_per_slot, max_people_per_appointment, id_form FROM appointment_reservation_rule";
57      private static final String SQL_QUERY_INSERT = "INSERT INTO appointment_reservation_rule (id_reservation_rule, date_of_apply, max_capacity_per_slot, max_people_per_appointment, id_form) VALUES (?, ?, ?, ?, ?)";
58      private static final String SQL_QUERY_SELECT = SQL_QUERY_SELECT_COLUMNS + " WHERE id_reservation_rule = ?";
59      private static final String SQL_QUERY_DELETE = "DELETE FROM appointment_reservation_rule WHERE id_reservation_rule = ?";
60      private static final String SQL_QUERY_SELECT_BY_ID_FORM = SQL_QUERY_SELECT_COLUMNS + " WHERE id_form = ?";
61      private static final String SQL_QUERY_SELECT_BY_ID_FORM_AND_DATE_OF_APPLY = SQL_QUERY_SELECT_BY_ID_FORM + " AND date_of_apply = ?";
62  
63      @Override
64      public synchronized void insert( ReservationRule reservationRule, Plugin plugin )
65      {
66          reservationRule.setIdReservationRule( getNewPrimaryKey( SQL_QUERY_NEW_PK, plugin ) );
67          DAOUtil daoUtil = buildDaoUtil( SQL_QUERY_INSERT, reservationRule, plugin, true );
68          executeUpdate( daoUtil );
69      }
70  
71      @Override
72      public void update( ReservationRule reservationRule, Plugin plugin )
73      {
74          DAOUtil daoUtil = buildDaoUtil( SQL_QUERY_UPDATE, reservationRule, plugin, false );
75          executeUpdate( daoUtil );
76      }
77  
78      @Override
79      public void delete( int nIdReservationRule, Plugin plugin )
80      {
81          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
82          daoUtil.setInt( 1, nIdReservationRule );
83          executeUpdate( daoUtil );
84      }
85  
86      @Override
87      public ReservationRule select( int nIdReservationRule, Plugin plugin )
88      {
89          DAOUtil daoUtil = null;
90          ReservationRule reservationRule = null;
91          try
92          {
93              daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
94              daoUtil.setInt( 1, nIdReservationRule );
95              daoUtil.executeQuery( );
96              if ( daoUtil.next( ) )
97              {
98                  reservationRule = buildReservationRule( daoUtil );
99              }
100         }
101         finally
102         {
103             if ( daoUtil != null )
104             {
105                 daoUtil.free( );
106             }
107         }
108         return reservationRule;
109     }
110 
111     @Override
112     public List<ReservationRule> findByIdForm( int nIdForm, Plugin plugin )
113     {
114         DAOUtil daoUtil = null;
115         List<ReservationRule> listReservationRule = new ArrayList<>( );
116         try
117         {
118             daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_ID_FORM, plugin );
119             daoUtil.setInt( 1, nIdForm );
120             daoUtil.executeQuery( );
121             while ( daoUtil.next( ) )
122             {
123                 listReservationRule.add( buildReservationRule( daoUtil ) );
124             }
125         }
126         finally
127         {
128             if ( daoUtil != null )
129             {
130                 daoUtil.free( );
131             }
132         }
133         return listReservationRule;
134     }
135 
136     @Override
137     public ReservationRule findByIdFormAndDateOfApply( int nIdForm, LocalDate dateOfApply, Plugin plugin )
138     {
139         DAOUtil daoUtil = null;
140         ReservationRule reservationRule = null;
141         try
142         {
143             daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_ID_FORM_AND_DATE_OF_APPLY, plugin );
144             daoUtil.setInt( 1, nIdForm );
145             daoUtil.setDate( 2, Date.valueOf( dateOfApply ) );
146             daoUtil.executeQuery( );
147             if ( daoUtil.next( ) )
148             {
149                 reservationRule = buildReservationRule( daoUtil );
150             }
151         }
152         finally
153         {
154             if ( daoUtil != null )
155             {
156                 daoUtil.free( );
157             }
158         }
159         return reservationRule;
160     }
161 
162     /**
163      * Build a ReservationRule business object from the resultset
164      * 
165      * @param daoUtil
166      *            the prepare statement util object
167      * @return a new ReservationRule with all its attributes assigned
168      */
169     private ReservationRule buildReservationRule( DAOUtil daoUtil )
170     {
171         int nIndex = 1;
172         ReservationRule reservationRule = new ReservationRule( );
173         reservationRule.setIdReservationRule( daoUtil.getInt( nIndex++ ) );
174         reservationRule.setSqlDateOfApply( daoUtil.getDate( nIndex++ ) );
175         reservationRule.setMaxCapacityPerSlot( daoUtil.getInt( nIndex++ ) );
176         reservationRule.setMaxPeoplePerAppointment( daoUtil.getInt( nIndex++ ) );
177         reservationRule.setIdForm( daoUtil.getInt( nIndex ) );
178         return reservationRule;
179     }
180 
181     /**
182      * Build a daoUtil object with the ReservationRule business object
183      * 
184      * @param query
185      *            the query
186      * @param reservationRule
187      *            the ReservationRule
188      * @param plugin
189      *            the plugin
190      * @param isInsert
191      *            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
192      *            set
193      * @return a new daoUtil with all its values assigned
194      */
195     private DAOUtil buildDaoUtil( String query, ReservationRule reservationRule, Plugin plugin, boolean isInsert )
196     {
197         int nIndex = 1;
198         DAOUtil daoUtil = new DAOUtil( query, plugin );
199         if ( isInsert )
200         {
201             daoUtil.setInt( nIndex++, reservationRule.getIdReservationRule( ) );
202         }
203         daoUtil.setDate( nIndex++, reservationRule.getSqlDateOfApply( ) );
204         daoUtil.setInt( nIndex++, reservationRule.getMaxCapacityPerSlot( ) );
205         daoUtil.setInt( nIndex++, reservationRule.getMaxPeoplePerAppointment( ) );
206         daoUtil.setInt( nIndex++, reservationRule.getIdForm( ) );
207         if ( !isInsert )
208         {
209             daoUtil.setInt( nIndex, reservationRule.getIdReservationRule( ) );
210         }
211         return daoUtil;
212     }
213 
214     /**
215      * Execute a safe update (Free the connection in case of error when execute the query)
216      * 
217      * @param daoUtil
218      *            the daoUtil
219      */
220     private void executeUpdate( DAOUtil daoUtil )
221     {
222         try
223         {
224             daoUtil.executeUpdate( );
225         }
226         finally
227         {
228             if ( daoUtil != null )
229             {
230                 daoUtil.free( );
231             }
232         }
233     }
234 
235 }