-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserApprovalPanel.java
More file actions
116 lines (100 loc) · 4.37 KB
/
UserApprovalPanel.java
File metadata and controls
116 lines (100 loc) · 4.37 KB
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
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.sql.*;
public class UserApprovalPanel extends JPanel {
private JTable pendingTable;
private DefaultTableModel tableModel;
public UserApprovalPanel() {
setLayout(new BorderLayout(10, 10));
setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
initializeComponents();
loadPendingApprovals();
}
private void initializeComponents() {
String[] columns = {"ID", "Username", "Full Name", "Email", "Role"};
tableModel = new DefaultTableModel(columns, 0) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
pendingTable = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(pendingTable);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JButton approveButton = new JButton("Approve");
JButton rejectButton = new JButton("Reject");
JButton refreshButton = new JButton("Refresh");
buttonPanel.add(approveButton);
buttonPanel.add(rejectButton);
buttonPanel.add(refreshButton);
approveButton.addActionListener(e -> handleApproval(true));
rejectButton.addActionListener(e -> handleApproval(false));
refreshButton.addActionListener(e -> loadPendingApprovals());
add(scrollPane, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
}
private void handleApproval(boolean isApproved) {
int selectedRow = pendingTable.getSelectedRow();
if (selectedRow == -1) {
JOptionPane.showMessageDialog(this, "Please select a user to " +
(isApproved ? "approve" : "reject"));
return;
}
int userId = (int) tableModel.getValueAt(selectedRow, 0);
String username = (String) tableModel.getValueAt(selectedRow, 1);
try {
Connection conn = DatabaseConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement(
"UPDATE users SET is_active = ? WHERE user_id = ?"
);
stmt.setBoolean(1, isApproved);
stmt.setInt(2, userId);
int result = stmt.executeUpdate();
if (result > 0) {
tableModel.removeRow(selectedRow);
JOptionPane.showMessageDialog(this,
"User " + username + " has been " +
(isApproved ? "approved" : "rejected") + " successfully");
// Add notification
PreparedStatement notifyStmt = conn.prepareStatement(
"INSERT INTO notifications (user_id, message) VALUES (?, ?)"
);
notifyStmt.setInt(1, userId);
notifyStmt.setString(2, "Your account has been " +
(isApproved ? "approved" : "rejected") + " by the administrator.");
notifyStmt.executeUpdate();
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this,
"Error " + (isApproved ? "approving" : "rejecting") +
" user: " + ex.getMessage());
}
}
private void loadPendingApprovals() {
tableModel.setRowCount(0);
try {
Connection conn = DatabaseConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement(
"SELECT user_id, username, full_name, email, role " +
"FROM users WHERE is_active = false ORDER BY user_id"
);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Object[] row = {
rs.getInt("user_id"),
rs.getString("username"),
rs.getString("full_name"),
rs.getString("email"),
rs.getString("role")
};
tableModel.addRow(row);
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this,
"Error loading pending approvals: " + ex.getMessage());
}
}
}