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.modules.resource.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  /**
43   * Appointment form resource type DAO
44   */
45  public class AppointmentFormResourceTypeDAO implements IAppointmentFormResourceTypeDAO
46  {
47      private static final String SQL_QUERY_NEW_PRIMARY_KEY = " SELECT MAX(id) FROM appointment_resource_form_rt ";
48      private static final String SQL_QUERY_SELECT = " SELECT id, id_appointment_form, resource_type_name, description, is_app_admin_user, is_localization FROM appointment_resource_form_rt ";
49      private static final String SQL_QUERY_SELECT_BY_PRIMARY_KEY = SQL_QUERY_SELECT + " WHERE id = ? ";
50      private static final String SQL_QUERY_SELECT_BY_ID_APPOINTMENT_FORM = SQL_QUERY_SELECT + " WHERE id_appointment_form = ? ";
51  
52      // Insert, update, delete
53      private static final String SQL_QUERY_INSERT = " INSERT INTO appointment_resource_form_rt ( id, id_appointment_form, resource_type_name, description, is_app_admin_user, is_localization ) VALUES (?,?,?,?,?,?) ";
54      private static final String SQL_QUERY_UPDATE = " UPDATE appointment_resource_form_rt SET description = ?, is_app_admin_user = ?, is_localization = ? WHERE id = ? ";
55      private static final String SQL_QUERY_RESET_APP_ADMIN_USER = "UPDATE appointment_resource_form_rt SET is_app_admin_user = 0 WHERE id_appointment_form= ? ";
56      private static final String SQL_QUERY_RESET_APP_LOCALIZATION = "UPDATE appointment_resource_form_rt SET is_localization = 0 WHERE id_appointment_form= ? ";
57      private static final String SQL_QUERY_DELETE = " DELETE FROM appointment_resource_form_rt WHERE id = ? ";
58      private static final String SQL_QUERY_DELETE_FROM_ID_FORM = " DELETE FROM appointment_resource_form_rt WHERE id_appointment_form = ? ";
59  
60      /**
61       * Get a new primary key
62       * 
63       * @param plugin
64       *            The plugin
65       * @return the new value of the primary key
66       */
67      private int newPrimaryKey( Plugin plugin )
68      {
69          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PRIMARY_KEY, plugin );
70          daoUtil.executeQuery( );
71  
72          int nRes = 1;
73  
74          if ( daoUtil.next( ) )
75          {
76              nRes = daoUtil.getInt( 1 ) + 1;
77          }
78  
79          daoUtil.free( );
80  
81          return nRes;
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public synchronized void insert( AppointmentFormResourceType formResourceType, Plugin plugin )
89      {
90          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
91          int nIndex = 1;
92          formResourceType.setId( newPrimaryKey( plugin ) );
93          daoUtil.setInt( nIndex++, formResourceType.getId( ) );
94          daoUtil.setInt( nIndex++, formResourceType.getIdAppointmentForm( ) );
95          daoUtil.setString( nIndex++, formResourceType.getResourceTypeName( ) );
96          daoUtil.setString( nIndex++, formResourceType.getDescription( ) );
97          daoUtil.setBoolean( nIndex++, formResourceType.getIsAppointmentAdminUser( ) );
98          daoUtil.setBoolean( nIndex, formResourceType.getIsLocation( ) );
99          daoUtil.executeUpdate( );
100         daoUtil.free( );
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     public void update( AppointmentFormResourceType formResourceType, Plugin plugin )
108     {
109         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
110         int nIndex = 1;
111         daoUtil.setString( nIndex++, formResourceType.getDescription( ) );
112         daoUtil.setBoolean( nIndex++, formResourceType.getIsAppointmentAdminUser( ) );
113         daoUtil.setBoolean( nIndex++, formResourceType.getIsLocation( ) );
114         daoUtil.setInt( nIndex, formResourceType.getId( ) );
115         daoUtil.executeUpdate( );
116         daoUtil.free( );
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public void remove( int nId, Plugin plugin )
124     {
125         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
126         int nIndex = 1;
127         daoUtil.setInt( nIndex, nId );
128         daoUtil.executeUpdate( );
129         daoUtil.free( );
130     }
131 
132     /**
133      * {@inheritDoc}
134      */
135     @Override
136     public void removeFromIdAppointmentForm( int nIdAppointmentForm, Plugin plugin )
137     {
138         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_FROM_ID_FORM, plugin );
139         int nIndex = 1;
140         daoUtil.setInt( nIndex, nIdAppointmentForm );
141         daoUtil.executeUpdate( );
142         daoUtil.free( );
143     }
144 
145     /**
146      * {@inheritDoc}
147      */
148     @Override
149     public AppointmentFormResourceType findByPrimaryKey( int nId, Plugin plugin )
150     {
151         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_PRIMARY_KEY, plugin );
152         daoUtil.setInt( 1, nId );
153         daoUtil.executeQuery( );
154 
155         AppointmentFormResourceType formResourceType = null;
156 
157         if ( daoUtil.next( ) )
158         {
159             formResourceType = getFormResourceTypeFromDAO( daoUtil );
160         }
161 
162         daoUtil.free( );
163 
164         return formResourceType;
165     }
166 
167     /**
168      * {@inheritDoc}
169      */
170     @Override
171     public List<AppointmentFormResourceType> findResourceTypesListFromIdForm( int nIdAppointmentForm, Plugin plugin )
172     {
173         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_ID_APPOINTMENT_FORM, plugin );
174         daoUtil.setInt( 1, nIdAppointmentForm );
175         daoUtil.executeQuery( );
176 
177         List<AppointmentFormResourceType> listFormResourceTypes = new ArrayList<AppointmentFormResourceType>( );
178 
179         while ( daoUtil.next( ) )
180         {
181             listFormResourceTypes.add( getFormResourceTypeFromDAO( daoUtil ) );
182         }
183 
184         daoUtil.free( );
185 
186         return listFormResourceTypes;
187     }
188 
189     /**
190      * {@inheritDoc}
191      */
192     @Override
193     public void resetAppAdminUser( int nIdAppointmentForm, Plugin plugin )
194     {
195         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_RESET_APP_ADMIN_USER, plugin );
196         daoUtil.setInt( 1, nIdAppointmentForm );
197         daoUtil.executeUpdate( );
198         daoUtil.free( );
199     }
200 
201     /**
202      * {@inheritDoc}
203      */
204     @Override
205     public void resetLocalization( int nIdAppointmentForm, Plugin plugin )
206     {
207         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_RESET_APP_LOCALIZATION, plugin );
208         daoUtil.setInt( 1, nIdAppointmentForm );
209         daoUtil.executeUpdate( );
210         daoUtil.free( );
211     }
212 
213     /**
214      * Get an appointment form resource type from a DAOUtil.<br >
215      * the call to the DAOUtil.next( ) must have been made before this method is called.<br >
216      * The call to the DAOUtil.free( ) will not be made by this method.
217      * 
218      * @param daoUtil
219      *            The daoUtil
220      * @return The AppointmentFormResourceType
221      */
222     private AppointmentFormResourceType getFormResourceTypeFromDAO( DAOUtil daoUtil )
223     {
224         AppointmentFormResourceTypeource/business/AppointmentFormResourceType.html#AppointmentFormResourceType">AppointmentFormResourceType formResourceType = new AppointmentFormResourceType( );
225         int nIndex = 1;
226         formResourceType.setId( daoUtil.getInt( nIndex++ ) );
227         formResourceType.setIdAppointmentForm( daoUtil.getInt( nIndex++ ) );
228         formResourceType.setResourceTypeName( daoUtil.getString( nIndex++ ) );
229         formResourceType.setDescription( daoUtil.getString( nIndex++ ) );
230         formResourceType.setIsAppointmentAdminUser( daoUtil.getBoolean( nIndex++ ) );
231         formResourceType.setIsLocation( daoUtil.getBoolean( nIndex ) );
232 
233         return formResourceType;
234     }
235 }