-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserDAO.java
161 lines (148 loc) · 6.76 KB
/
UserDAO.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//Class: CST8288//
//Section: 033//
//Professor: Islam Gomaa//
//Assignment: Final Project//
//Date: 23/7/2024//
//Authors: Jake Elliott, Navjot Kaur, Heeseok Yang, Scott Valair//
package database;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import enums.UserType;
public class UserDAO {
private static final Logger LOGGER = Logger.getLogger(UserDAO.class.getName());
public boolean authenticate(String email, String password) {
String sql = "SELECT * FROM user WHERE user_email = ? AND user_password = ?";
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, email);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
boolean authenticated = rs.next();
LOGGER.info("User authenticated: " + authenticated);
return authenticated;
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, "Authentication failed", e);
}
return false;
}
public String getUserType(String email) {
String sql = "SELECT user_type FROM user WHERE user_email = ?";
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, email);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
String userType = rs.getString("user_type");
LOGGER.info("User type retrieved: " + userType);
return userType;
}
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, "Failed to get user type", e);
}
return null;
}
public boolean addUser(UserDTO user) {
String sql = "INSERT INTO user (user_id, first_name, last_name, user_password, user_email, join_date, user_type) VALUES (?, ?, ?, ?, ?, ?, ?)";
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, user.getUserId());
pstmt.setString(2, user.getFirstName());
pstmt.setString(3, user.getLastName());
pstmt.setString(4, user.getUserPassword());
pstmt.setString(5, user.getUserEmail());
pstmt.setDate(6, user.getJoinDate());
pstmt.setString(7, user.getUserType().name());
LOGGER.info("Executing query: " + pstmt.toString());
int result = pstmt.executeUpdate();
LOGGER.info("Insert result: " + result);
return result > 0;
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, "Failed to add user", e);
}
return false;
}
public UserDTO getUserById(String userId) {
String sql = "SELECT * FROM user WHERE user_id = ?";
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, userId);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
UserDTO user = new UserDTO();
user.setUserId(rs.getString("user_id"));
user.setFirstName(rs.getString("first_name"));
user.setLastName(rs.getString("last_name"));
user.setUserPassword(rs.getString("user_password"));
user.setUserEmail(rs.getString("user_email"));
user.setJoinDate(rs.getDate("join_date"));
user.setUserType(UserType.valueOf(rs.getString("user_type").toUpperCase()));
LOGGER.info("User retrieved: " + user);
return user;
}
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, "Failed to get user by ID", e);
}
return null;
}
public boolean updateUser(UserDTO user) {
String sql = "UPDATE user SET first_name = ?, last_name = ?, user_password = ?, user_email = ?, join_date = ?, user_type = ? WHERE user_id = ?";
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, user.getFirstName());
pstmt.setString(2, user.getLastName());
pstmt.setString(3, user.getUserPassword());
pstmt.setString(4, user.getUserEmail());
pstmt.setDate(5, user.getJoinDate());
pstmt.setString(6, user.getUserType().name());
pstmt.setString(7, user.getUserId());
int result = pstmt.executeUpdate();
LOGGER.info("Update result: " + result);
return result > 0;
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, "Failed to update user", e);
}
return false;
}
public boolean deleteUser(String userId) {
String sql = "DELETE FROM user WHERE user_id = ?";
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, userId);
int result = pstmt.executeUpdate();
LOGGER.info("Delete result: " + result);
return result > 0;
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, "Failed to delete user", e);
}
return false;
}
public List<UserDTO> getAllUsers() {
List<UserDTO> users = new ArrayList<>();
String sql = "SELECT * FROM user";
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
UserDTO user = new UserDTO();
user.setUserId(rs.getString("user_id"));
user.setFirstName(rs.getString("first_name"));
user.setLastName(rs.getString("last_name"));
user.setUserPassword(rs.getString("user_password"));
user.setUserEmail(rs.getString("user_email"));
user.setJoinDate(rs.getDate("join_date"));
user.setUserType(UserType.valueOf(rs.getString("user_type").toUpperCase()));
users.add(user);
}
LOGGER.info("Total users retrieved: " + users.size());
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, "Failed to get all users", e);
}
return users;
}
}