Project

General

Profile

1
package eu.dnetlib.validator.commons.dao.users;
2

    
3
import java.sql.Connection;
4
import java.sql.PreparedStatement;
5
import java.sql.ResultSet;
6
import java.sql.SQLException;
7

    
8
import eu.dnetlib.domain.functionality.UserProfile;
9
import eu.dnetlib.validator.commons.dao.AbstractDAO;
10

    
11
public class UsersDAOimpl extends AbstractDAO<UserProfile> implements UsersDAO {
12

    
13

    
14
	@Override
15
	protected PreparedStatement getUpdateStatement(UserProfile t, Connection con)
16
			throws SQLException {
17
		String query="UPDATE users SET email=?, institution=?, firstname=?, lastname=? WHERE username=?";
18
		PreparedStatement stmt = con.prepareStatement(query);
19
		stmt.setString(1, t.getEmail());
20
		stmt.setString(2, t.getInstitution());
21
		stmt.setString(3, t.getFirstname());
22
		stmt.setString(4, t.getLastname());
23
		stmt.setString(5, t.getUsername());
24
		return stmt;
25
	}
26

    
27
	
28
	@Override
29
	protected PreparedStatement getInsertStatement(UserProfile t, Connection con)
30
			throws SQLException {
31
		String query="INSERT INTO users(email,password,institution,username,firstname,lastname,activation_id) VALUES(?,?,?,?,?,?,?)";
32
		PreparedStatement stmt = con.prepareStatement(query);
33
		stmt.setString(1, t.getEmail());
34
		stmt.setString(2, t.getPassword());
35
		stmt.setString(3, t.getInstitution());
36
		stmt.setString(4, t.getUsername());
37
		stmt.setString(5, t.getFirstname());
38
		stmt.setString(6, t.getLastname());
39
		stmt.setString(7, t.getActivationId());		
40
		return stmt;
41
	}
42

    
43
	@Override
44
	protected PreparedStatement getDeleteStatement(int id, Connection con)
45
			throws SQLException {
46
		// TODO Auto-generated method stub
47
		return null;
48
	}
49

    
50
	@Override
51
	protected int getLastId() throws SQLException {
52
		// TODO Auto-generated method stub
53
		return 0;
54
	}
55

    
56
	@Override
57
	public UserProfile get(String email) {
58
		ResultSet rs = null;
59
		Connection con = null;
60
		PreparedStatement stmt = null;
61
		UserProfile retUser = null;
62
		logger.debug("Accessing DB to get User with email: " + email);
63
		try {
64
			con = getConnection();
65
			String query="SELECT * FROM users WHERE email=? OR username=?";
66
			stmt = con.prepareStatement(query);
67
			stmt.setString(1, email);
68
			stmt.setString(2, email);
69
			rs = stmt.executeQuery();
70
			if (rs!=null){
71
				if (rs.next()) {
72
					retUser = new UserProfile();
73
					retUser.setEmail(rs.getString("email"));
74
					retUser.setUsername(rs.getString("username"));
75
					retUser.setFirstname(rs.getString("firstname"));
76
					retUser.setLastname(rs.getString("lastname"));
77
					retUser.setInstitution(rs.getString("institution"));
78
					retUser.setActivationId(rs.getString("activation_id"));
79
				}				
80
			}
81
		} catch (SQLException e) {
82
			logger.error("Error Accessing DB to get User: "+e);
83
		} finally {
84
			if (stmt != null) {
85
				try {
86
					stmt.close();
87
				} catch (SQLException e) {
88
					logger.error("Error Accessing DB to get User: "+e);
89
				}
90
			}
91
		}
92
		return retUser;
93
	
94
	}
95
	
96
	@Override
97
	public boolean activateUser(String activation_id) {
98
		Connection con = null;
99
		PreparedStatement stmt = null;
100
		logger.debug("Accessing DB to activate user");
101
		try {
102
			con = getConnection();
103
			String query="UPDATE users SET activation_id=? WHERE activation_id=?";
104
			stmt = con.prepareStatement(query);
105
			stmt.setString(1, null);
106
			stmt.setString(2, activation_id);
107
			if (stmt.executeUpdate() > 0){
108
				return true;
109
			}
110
		} catch (SQLException e) {
111
			logger.error("Error while Accessing DB to activate user: "+e);
112
		} finally {
113
			if (stmt != null) {
114
				try {
115
					stmt.close();
116
				} catch (SQLException e) {
117
					logger.error("Error while Accessing DB to activate user: "+e);
118
				}
119
			}
120
		}
121
		return false;
122
	}
123
	
124
	@Override
125
	public UserProfile get(int id) {
126
		// TODO Auto-generated method stub
127
		return null;
128
	}
129

    
130

    
131
	@Override
132
	public boolean checkCorrectCreds(String email, String password) {
133
		ResultSet rs = null;
134
		Connection con = null;
135
		PreparedStatement stmt = null;
136
		logger.debug("Accessing DB to check correct credentials");
137
		try {
138
			con = getConnection();
139
			String query="SELECT * FROM users WHERE email=? AND password=?";
140
			stmt = con.prepareStatement(query);
141
			stmt.setString(1,email);
142
			stmt.setString(2,password);
143
			rs = stmt.executeQuery();
144
			if (rs.next()){
145
				return true;
146
			}
147
		} catch (SQLException e) {
148
			logger.error("Error while accessing DB to check correct credentials: "+e);
149
		} finally {
150
			if (stmt != null) {
151
				try {
152
					stmt.close();
153
				} catch (SQLException e) {
154
					logger.error("Error while accessing DB to check correct credentials: "+e);
155
				}
156
			}
157
		}
158
		return false;
159
	}
160

    
161

    
162
	@Override
163
	public boolean isAdmin(String email) {
164
		ResultSet rs = null;
165
		Connection con = null;
166
		PreparedStatement stmt = null;
167
		logger.debug("Accessing DB to check if user is admin");
168
		try {
169
			con = getConnection();
170
			String query="SELECT * FROM admins WHERE username=? AND level=?";
171
			stmt = con.prepareStatement(query);
172
			stmt.setString(1,email);
173
			stmt.setString(2,"master");
174
			rs = stmt.executeQuery();
175
			if (rs.next()){
176
				return true;
177
			}
178
		} catch (SQLException e) {
179
			logger.error("Error while accessing DB to check if user is admin: "+e);
180
		} finally {
181
			if (stmt != null) {
182
				try {
183
					stmt.close();
184
				} catch (SQLException e) {
185
					logger.error("Error while accessing DB to check if user is admin: "+e);
186
				}
187
			}
188
		}
189
		return false;
190
	}
191

    
192
	@Override
193
	public boolean isRepoAdmin(String email) {
194
		ResultSet rs = null;
195
		Connection con = null;
196
		PreparedStatement stmt = null;
197
		logger.debug("Accessing DB to check if user is repoAdmin");
198
		try {
199
			con = getConnection();
200
			String query="SELECT * FROM admins WHERE username=? AND level=?";
201
			stmt = con.prepareStatement(query);
202
			stmt.setString(1,email);
203
			stmt.setString(2,"secondary");
204
			rs = stmt.executeQuery();
205
			if (rs.next()){
206
				return true;
207
			}
208
		} catch (SQLException e) {
209
			logger.error("Error while accessing DB to check if user is repoAdmin: "+e);
210
		} finally {
211
			if (stmt != null) {
212
				try {
213
					stmt.close();
214
				} catch (SQLException e) {
215
					logger.error("Error while accessing DB to check if user is repoAdmin: "+e);
216
				}
217
			}
218
		}
219
		return false;
220
	}
221

    
222
	@Override
223
	public boolean isActivated(String email) {
224
		ResultSet rs = null;
225
		Connection con = null;
226
		PreparedStatement stmt = null;
227
		logger.debug("Accessing DB to check if user is activated");
228
		try {
229
			con = getConnection();
230
			String query="SELECT * FROM users WHERE email=? AND activation_id is null";
231
			stmt = con.prepareStatement(query);
232
			stmt.setString(1,email);
233
//			stmt.setString(2,"NULL");
234
			rs = stmt.executeQuery();
235
			if (rs.next()){
236
				return true;
237
			}
238
		} catch (SQLException e) {
239
			logger.error("Error while accessing DB to check if user is activated "+e);
240
		} finally {
241
			if (stmt != null) {
242
				try {
243
					stmt.close();
244
				} catch (SQLException e) {
245
					logger.error("Error while accessing DB to check if user is activated "+e);
246
				}
247
			}
248
		}
249
		return false;
250
	}
251

    
252

    
253
	@Override
254
	public boolean userExists(String email) {
255
		ResultSet rs = null;
256
		Connection con = null;
257
		PreparedStatement stmt = null;
258
		logger.debug("Accessing DB to check if user "+email+" exists");
259
		try {
260
			con = getConnection();
261
			String query="SELECT * FROM users WHERE email=?";
262
			stmt = con.prepareStatement(query);
263
			stmt.setString(1,email);
264
			rs = stmt.executeQuery();
265
			if (rs.next()){
266
				logger.debug("user exists");
267
				return true;
268
			}
269
		} catch (SQLException e) {
270
			logger.error("Error while accessing DB to check if user exists: "+e);
271
		} finally {
272
			if (stmt != null) {
273
				try {
274
					stmt.close();
275
				} catch (SQLException e) {
276
					logger.error("Error while accessing DB to check if user exists: "+e);
277
				}
278
			}
279
		}
280
		return false;
281
	}
282
	
283
	@Override
284
	public boolean usernameExists(String username) {
285
		ResultSet rs = null;
286
		Connection con = null;
287
		PreparedStatement stmt = null;
288
		logger.debug("Accessing DB to check if user "+ username +" exists");
289
		try {
290
			con = getConnection();
291
			String query="SELECT * FROM users WHERE username=?";
292
			stmt = con.prepareStatement(query);
293
			stmt.setString(1,username);
294
			rs = stmt.executeQuery();
295
			if (rs.next()){
296
				logger.debug("user exists");
297
				return true;
298
			}
299
		} catch (SQLException e) {
300
			logger.error("Error while accessing DB to check if user exists: "+e);
301
		} finally {
302
			if (stmt != null) {
303
				try {
304
					stmt.close();
305
				} catch (SQLException e) {
306
					logger.error("Error while accessing DB to check if user exists: "+e);
307
				}
308
			}
309
		}
310
		return false;
311
	}
312

    
313

    
314
	@Override
315
	public void prepareResetPassword(String uuid, String email) {
316
		Connection con = null;
317
		PreparedStatement stmt = null;
318
		logger.debug("Accessing DB to prepare reset password");
319
		try {
320
			con = getConnection();
321
			String query="UPDATE users SET activation_id=? WHERE email=?";;
322
			stmt = con.prepareStatement(query);
323
			stmt.setString(1,uuid);
324
			stmt.setString(2,email);
325
			stmt.executeUpdate();
326
		} catch (SQLException e) {
327
			logger.error("Error while accessing DB to prepare reset password: "+e);
328
		} finally {
329
			if (stmt != null) {
330
				try {
331
					stmt.close();
332
				} catch (SQLException e) {
333
					logger.error("Error while accessing DB to prepare reset password: "+e);
334
				}
335
			}
336
		}
337
	}
338

    
339

    
340
	@Override
341
	public void ResetPassword(String uuid, String password) {
342
		Connection con = null;
343
		PreparedStatement stmt = null;
344
		logger.debug("Accessing DB to reset password");
345
		try {
346
			con = getConnection();
347
			String query="UPDATE users SET password=?, activation_id=? WHERE activation_id=?";;
348
			stmt = con.prepareStatement(query);
349
			stmt.setString(1,password);
350
			stmt.setString(2,null);
351
			stmt.setString(3,uuid);
352
			stmt.executeUpdate();
353
		} catch (SQLException e) {
354
			logger.error("Error while accessing DB to reset password: "+e);
355
		} finally {
356
			if (stmt != null) {
357
				try {
358
					stmt.close();
359
				} catch (SQLException e) {
360
					logger.error("Error while accessing DB to reset password: "+e);
361
				}
362
			}
363
		}
364
	}
365

    
366
}
(2-2/2)