1 /***
2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
6 *
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
11 *
12 * The Original Code is pow2toolkit library.
13 *
14 * The Initial Owner of the Original Code is
15 * Power Of Two S.R.L. (www.pow2.com)
16 *
17 * Portions created by Power Of Two S.R.L. are
18 * Copyright (C) Power Of Two S.R.L.
19 * All Rights Reserved.
20 *
21 * Contributor(s):
22 */
23
24 package com.pow2.user.dao;
25
26 import com.pow2.dao.AbstractDAO;
27 import com.pow2.user.User;
28 import com.pow2.util.Util;
29
30 import java.sql.*;
31
32
33 /***
34 * User Data access object.
35 * <br>
36 * Encapsulates data access methods for the User entity.
37 * <br>
38 * Extends AbstractDAO, and use the singleton
39 * design pattern to provide an unique object instance
40 * per vm.
41 *
42 * @author Luca Fossato
43 * @created 22 aprile 2002
44 * @version $Id: DBUserDAO.java,v 1.1.1.1 2004/08/31 20:22:46 foxat Exp $
45 */
46 public class DBUserDAO extends AbstractDAO implements UserDAO
47 {
48 /*** an handle to the unique UserDAO instance. */
49 private static DBUserDAO instance = null;
50
51
52 /***
53 * Default private constructor.
54 */
55 protected DBUserDAO()
56 {
57 super();
58 }
59
60
61 /***
62 * Get the unique instance of UserDAO class.
63 * <br>
64 * This method is protected; use UserDAOFactory.getUserDAO() instead.
65 *
66 * @return the instance of UserDAO class
67 */
68 protected static synchronized DBUserDAO instance()
69 {
70 if (instance == null)
71 instance = new DBUserDAO();
72
73 return instance;
74 }
75
76
77 /***
78 * Get the data provider type
79 *
80 * @return the data provider type
81 */
82 public String getProviderType()
83 {
84 return UserDAOFactory.USER_DATA_PROVIDER_RDBMS;
85 }
86
87
88 /***
89 * Update the input User object with the user informations
90 * retrieved from the data provider.
91 * <br>
92 * This method uses the User <code>login</code>
93 * and <code>passwd</code> properties to execute the login procedure.
94 *
95 * @param dao the UserDAO instance
96 * @param login the user login
97 * @param passwd the user password
98 * @return the retrieved User object if the login is succesfull;
99 * null if login fails
100 * @exception Exception if any error occurs
101 */
102 public User getUser(String login, String passwd) throws Exception
103 {
104 return UserDAOUtil.getUser(this, login, passwd);
105 }
106
107
108 /***
109 * Update the input User object with the user informations
110 * retrieved from the data provider.
111 * <br>
112 * This method uses the User <code>login</code>
113 * and <code>passwd</code> properties to execute the login procedure.
114 *
115 * @param user the user object with the <code>login</code>
116 * and <code>passwd</code> properties properly set.
117 * @exception Exception if any error occurs
118 */
119 public void getUser(User user) throws Exception
120 {
121 Connection con = null;
122 PreparedStatement ps = null;
123 ResultSet rs = null;
124 String s = null;
125
126 if (user == null)
127 {
128 cat.error("::getUser - input user is null. Why ?");
129 return;
130 }
131
132 s = "select * from APP_USER where LOGIN_NAME = ? AND PASSWORD_VALUE = ?";
133
134 try
135 {
136 con = getConnection(Connection.TRANSACTION_READ_COMMITTED);
137 ps = con.prepareStatement(s);
138
139 ps.setString(1, user.getLogin());
140 ps.setString(2, user.getPasswd());
141 rs = ps.executeQuery();
142
143
144 user.setAuthentication(false);
145
146
147 if (rs.next())
148 {
149 user.setId (rs.getString("USER_ID"));
150 user.setFirstName(rs.getString("FIRST_NAME"));
151 user.setLastName (rs.getString("LAST_NAME"));
152 user.setEmail (rs.getString("EMAIL"));
153 user.setStreet (rs.getString("STREET"));
154 user.setZipCode (rs.getString("ZIPCODE"));
155 user.setState (rs.getString("STATE"));
156 user.setCity (rs.getString("CITY"));
157 user.setPhone (rs.getString("PHONE"));
158 user.setMobile (rs.getString("MOBILE"));
159 user.setFax (rs.getString("FAX"));
160
161
162
163
164
165 user.setAuthentication(true);
166 }
167 }
168 finally
169 {
170 closeResources(rs, ps, con, true);
171 }
172
173
174 if (cat.isDebugEnabled())
175 cat.debug(UserDAOUtil.getAuthenticationLog(user));
176 }
177
178
179 /***
180 * Persist the user informations into the database.
181 * <br>
182 * This method updates or insert a new User entry into the database
183 * depending on the user id value.
184 *
185 * @param user the user object to persist into the database
186 * @exception Exception if any error occurs
187 */
188 public void setUser(User user) throws Exception
189 {
190 Connection con = null;
191 PreparedStatement ps = null;
192 ResultSet rs = null;
193 String s = null;
194
195 String qryInsert = "insert into APP_USER (user_id, login_name, password_value, first_name, last_name, email, street, zipcode, state, city, phone, mobile, fax, modified, created) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
196 String qryUpdate = "update APP_USER set login_name = ?, password_value = ?, first_name = ?, last_name = ?, email = ?, street = ?, zipcode = ?, state = ?, city = ?, phone = ?, mobile = ?, fax = ?, modified = ? where user_id = ?";
197
198 try
199 {
200 con = getConnection(Connection.TRANSACTION_SERIALIZABLE);
201
202
203 if (user.getId().equals(user.USER_ID_NOTPERSISTED))
204 {
205 cat.info("::setUser - try to INSERT a new user");
206 user.setId(String.valueOf(getNewKey()));
207 ps = con.prepareStatement(qryInsert);
208 ps.setString(1, user.getId());
209 setDBUserAttributes(user, ps, 2);
210 ps.setDate (15, new Date(System.currentTimeMillis()));
211 }
212
213
214 else
215 {
216 cat.info("::setUser - try to UPDATE the profile of the current user");
217 ps = con.prepareStatement(qryUpdate);
218 setDBUserAttributes(user, ps, 1);
219 ps.setString(14, user.getId());
220 }
221
222 ps.executeUpdate();
223 }
224 finally
225 {
226 closeResources(ps, con, true);
227 }
228 }
229
230
231 /***
232 * Delete the User having the input id from the database.
233 *
234 * @param id the identifier of the user to remove from the database
235 * @throws Exception if any error occurs
236 */
237 public void deleteUser(String id) throws Exception
238 {
239 delete("APP_USER", "user_id = " + id);
240 }
241
242
243 /***
244 * Test if the input user <code>login</code> property
245 * already exists into the application database.
246 *
247 * @param user the user object
248 * @return true if the input user login already exists into the application database;
249 * false otherwise
250 * @exception Exception if any error occurs
251 */
252 public boolean loginExists(User user) throws Exception
253 {
254 return loginExists(user.getLogin());
255 }
256
257
258 /***
259 * Test if the input <code>login</code> property
260 * already exists into the application database.
261 *
262 * @param login the login string
263 * @return true if the input login already exists into the application database;
264 * false otherwise
265 * @exception Exception if any error occurs
266 */
267 public boolean loginExists(String login) throws Exception
268 {
269 if (Util.isNull(login))
270 throw new Exception("login is null or empty.");
271
272 return assertion("APP_USER", ("LOGIN_NAME = " + Util.dbString(login)));
273 }
274
275
276 /***
277 * PRIVATE METHODS HERE
278 */
279
280
281 /***
282 * set the database user attributes
283 *
284 * @param user the user object containing the informations to insert into the database
285 * @param ps the PreparedStatement object
286 * @param i the starting field index
287 */
288 private void setDBUserAttributes(User user, PreparedStatement ps, int i)
289 throws SQLException
290 {
291 ps.setString(i++, user.getLogin());
292 ps.setString(i++, user.getPasswd());
293 ps.setString(i++, user.getFirstName());
294 ps.setString(i++, user.getLastName());
295 ps.setString(i++, user.getEmail());
296 ps.setString(i++, user.getStreet());
297 ps.setString(i++, user.getZipCode());
298 ps.setString(i++, user.getState());
299 ps.setString(i++, user.getCity());
300 ps.setString(i++, user.getPhone());
301 ps.setString(i++, user.getMobile());
302 ps.setString(i++, user.getFax());
303 ps.setDate (i++, new Date(System.currentTimeMillis()));
304 }
305 }