Repository navigation
Expand file tree
/
Copy pathEmployeeDeletionService.java
More file actions
117 lines (105 loc) · 4.66 KB
/
Copy pathEmployeeDeletionService.java
File metadata and controls
117 lines (105 loc) · 4.66 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
117
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Optional;
/** Database operations for PT4. Uses an existing JDBC connection from the application. */
public final class EmployeeDeletionService {
private final Connection connection;
public EmployeeDeletionService(Connection connection) {
if (connection == null) throw new IllegalArgumentException("Connection is required");
this.connection = connection;
}
/** Lightweight record: never display SSN or other sensitive attributes in confirmation. */
public static final class EmployeeSummary {
public final int empID;
public final String firstName;
public final String lastName;
EmployeeSummary(int empID, String firstName, String lastName) {
this.empID = empID;
this.firstName = firstName;
this.lastName = lastName;
}
}
/** Retrieve the employee for the confirmation prompt. */
public Optional<EmployeeSummary> findById(int empID, UserRole role) throws SQLException {
checkAdmin(role);
if (empID <= 0) throw new IllegalArgumentException("Employee ID must be positive");
String sql = "SELECT empID, Fname, Lname FROM employees WHERE empID = ?";
try (PreparedStatement ps = connection.prepareStatement(sql)) {
ps.setInt(1, empID);
try (ResultSet rs = ps.executeQuery()) {
if (!rs.next()) return Optional.empty();
return Optional.of(new EmployeeSummary(
rs.getInt("empID"), rs.getString("Fname"), rs.getString("Lname")));
}
}
}
/**
* Permanently delete one employee and its child rows as a single transaction.
* HR authorization must originate from the application's authenticated session.
* Caller must pass an ordinary, non-transactional JDBC connection (autoCommit=true).
* Throws on failure, and rolls back all deletes.
*/
public boolean deleteEmployee(int empID, UserRole role) throws SQLException {
checkAdmin(role);
if (empID <= 0) throw new IllegalArgumentException("Employee ID must be positive");
if (!connection.getAutoCommit()) {
throw new SQLException("Deletion requires a connection with autoCommit enabled");
}
connection.setAutoCommit(false);
try {
// Lock target employee while deleting; avoids conflicting writes to the row.
if (!employeeExistsForUpdate(empID)) {
connection.rollback();
return false;
}
// Delete children first, because the MySQL foreign keys may restrict parent deletion.
deleteChildren("DELETE FROM payroll WHERE empID = ?", empID);
deleteChildren("DELETE FROM employee_job_titles WHERE empID = ?", empID);
deleteChildren("DELETE FROM employee_division WHERE empid = ?", empID);
deleteChildren("DELETE FROM demographics WHERE empID = ?", empID);
deleteChildren("DELETE FROM addresses WHERE empID = ?", empID);
int deleted;
try (PreparedStatement ps = connection.prepareStatement(
"DELETE FROM employees WHERE empID = ?")) {
ps.setInt(1, empID);
deleted = ps.executeUpdate();
}
if (deleted != 1) {
throw new SQLException("Expected exactly one employee deletion, got " + deleted);
}
connection.commit();
return true;
} catch (SQLException | RuntimeException ex) {
try {
connection.rollback();
} catch (SQLException rollbackError) {
ex.addSuppressed(rollbackError);
}
throw ex;
} finally {
connection.setAutoCommit(true);
}
}
private boolean employeeExistsForUpdate(int empID) throws SQLException {
try (PreparedStatement ps = connection.prepareStatement(
"SELECT empID FROM employees WHERE empID = ? FOR UPDATE")) {
ps.setInt(1, empID);
try (ResultSet rs = ps.executeQuery()) {
return rs.next();
}
}
}
private void deleteChildren(String sql, int empID) throws SQLException {
try (PreparedStatement ps = connection.prepareStatement(sql)) {
ps.setInt(1, empID);
ps.executeUpdate();
}
}
private static void checkAdmin(UserRole role) {
if (role != UserRole.HR_ADMIN) {
throw new SecurityException("Only HR Administrators may delete employees");
}
}
}