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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
package at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dao;
import at.ac.tuwien.sepm.assignment.groupphase.exception.ElementNotFoundException;
import at.ac.tuwien.sepm.assignment.groupphase.exception.PersistenceException;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Employee;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Employee.EducationLevel;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Registration;
import at.ac.tuwien.sepm.assignment.groupphase.util.JDBCConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.Instant;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class RegistrationDatabaseDAO implements RegistrationDAO {
private JDBCConnectionManager jdbcConnectionManager;
private EmployeeDAO employeePersistence;
@Autowired
public RegistrationDatabaseDAO(
JDBCConnectionManager jdbcConnectionManager, EmployeeDAO employeePersistence) {
this.jdbcConnectionManager = jdbcConnectionManager;
this.employeePersistence = employeePersistence;
}
private long getVehicleVersionId(long vehicleId) throws PersistenceException {
String sqlGetVehicleVersionId = "SELECT * FROM vehicle WHERE id = ?";
try (PreparedStatement stmt =
jdbcConnectionManager.getConnection().prepareStatement(sqlGetVehicleVersionId)) {
stmt.setLong(1, vehicleId);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
return rs.getLong("version");
} else {
throw new PersistenceException("vehicle id not found");
}
}
} catch (SQLException e) {
throw new PersistenceException(e);
}
}
private long getEmployeeVersionId(long employeeId) throws PersistenceException {
String sqlGetEmployeeVersionId = "SELECT * FROM employee WHERE id = ?";
try (PreparedStatement stmt =
jdbcConnectionManager.getConnection().prepareStatement(sqlGetEmployeeVersionId)) {
stmt.setLong(1, employeeId);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
return rs.getLong("version");
} else {
throw new PersistenceException("employee id not found");
}
}
} catch (SQLException e) {
throw new PersistenceException(e);
}
}
@Override
public Set<Long> add(long vehicleId, Set<Registration> registrations)
throws PersistenceException {
String sql =
"INSERT INTO Registration (vehicleId, employeeId, start, end, active) VALUES (?,?,?,?,?)";
String sql2 = "UPDATE Vehicle SET status = 'FREI_WACHE' WHERE id = ?;";
Set<Long> vehicleIds = new HashSet<>();
try {
Connection con = jdbcConnectionManager.getConnection();
con.setAutoCommit(false);
try (PreparedStatement pstmt =
con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
// vehicleId is a Vehicle.id as it comes from GUI => fetch VehicleVersion.id
pstmt.setLong(1, getVehicleVersionId(vehicleId));
for (Registration r : registrations) {
pstmt.setLong(2, getEmployeeVersionId(r.employee().id()));
pstmt.setObject(3, OffsetDateTime.ofInstant(r.start(), ZoneId.systemDefault()));
pstmt.setObject(4, OffsetDateTime.ofInstant(r.end(), ZoneId.systemDefault()));
pstmt.setBoolean(5, true);
pstmt.addBatch();
}
pstmt.executeBatch();
try (ResultSet rs = pstmt.getGeneratedKeys()) {
while (rs.next()) vehicleIds.add(rs.getLong(1));
}
}
try (PreparedStatement pstmt = con.prepareStatement(sql2)) {
pstmt.setLong(1, vehicleId);
if (pstmt.executeUpdate() != 1) {
con.rollback();
throw new PersistenceException("Failed to persist registration");
}
}
con.commit();
return vehicleIds;
} catch (SQLException e) {
jdbcConnectionManager.rollbackConnection();
throw new PersistenceException(e);
}
}
@Override
public void remove(long id) throws ElementNotFoundException, PersistenceException {
String sql = "UPDATE Registration SET active = 0, end = ? WHERE id = ?";
try {
Connection con = jdbcConnectionManager.getConnection();
try (PreparedStatement pstmt = con.prepareStatement(sql)) {
pstmt.setObject(1, OffsetDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()));
pstmt.setLong(2, id);
if (pstmt.executeUpdate() != 1)
throw new ElementNotFoundException("No such registrationId exists");
}
} catch (SQLException e) {
throw new PersistenceException(e);
}
}
protected List<Registration> list(long vehicleId) throws PersistenceException {
String sql =
"SELECT * FROM Registration r "
+ "JOIN EmployeeVersion ev ON ev.id = r.employeeId "
+ "JOIN VehicleVersion vv ON vv.id = r.vehicleId "
+ "WHERE r.vehicleId = ?";
try (PreparedStatement stmt = jdbcConnectionManager.getConnection().prepareStatement(sql)) {
List<Registration> registrationList = new ArrayList<>();
stmt.setLong(1, vehicleId); // is vehicle version id!
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Employee employee =
Employee.builder()
.id(rs.getLong("EmployeeVersion.id"))
.name(rs.getString("EmployeeVersion.name"))
.birthday(rs.getObject("EmployeeVersion.birthday", LocalDate.class))
.educationLevel(
EducationLevel.valueOf(
rs.getString("EmployeeVersion.educationLevel")))
.isDriver(rs.getBoolean("EmployeeVersion.isDriver"))
.isPilot(rs.getBoolean("EmployeeVersion.isPilot"))
.build();
Registration registration =
Registration.builder()
.id(rs.getLong("Registration.id"))
.start(
(rs.getObject("Registration.start", OffsetDateTime.class))
.toInstant())
.end(
(rs.getObject("Registration.end", OffsetDateTime.class))
.toInstant())
.employee(employee)
.build();
registrationList.add(registration);
}
return registrationList;
} catch (SQLException e) {
throw new PersistenceException(e);
}
}
}
|