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