aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/at/ac/tuwien
diff options
context:
space:
mode:
authorTharre <tharre3@gmail.com>2018-05-30 18:06:12 +0200
committerTharre <tharre3@gmail.com>2018-05-30 18:06:12 +0200
commit406d99c083f9840d47722d3e52b521dc8c89e359 (patch)
treee5e91835b7b752d8a68c0a8cead3dfa89ca6d9ef /src/main/java/at/ac/tuwien
parent93d09d05cf7b0b32e3e16de3b2c2fa2266cf9abd (diff)
downloadsepm-groupproject-406d99c083f9840d47722d3e52b521dc8c89e359.tar.gz
sepm-groupproject-406d99c083f9840d47722d3e52b521dc8c89e359.tar.xz
sepm-groupproject-406d99c083f9840d47722d3e52b521dc8c89e359.zip
Fix VehicleDAO.update() version update mechanic
Before, it would update and insert a new VehicleVersion every time, even if only the status of the vehicle changed.
Diffstat (limited to 'src/main/java/at/ac/tuwien')
-rw-r--r--src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDatabaseDAO.java91
1 files changed, 38 insertions, 53 deletions
diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDatabaseDAO.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDatabaseDAO.java
index 6d50588..535987f 100644
--- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDatabaseDAO.java
+++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDatabaseDAO.java
@@ -88,70 +88,55 @@ public class VehicleDatabaseDAO implements VehicleDAO {
}
@Override
- public void update(Vehicle vehicle) throws ElementNotFoundException, PersistenceException {
- String query = "SELECT * FROM vehicle WHERE id=?";
+ public void update(Vehicle v) throws ElementNotFoundException, PersistenceException {
+ String sql = "SELECT version FROM Vehicle WHERE id = ?";
+ String sql2 =
+ "MERGE INTO VehicleVersion(name, constructionType, type, hasNef)"
+ + " KEY(name, constructionType, type, hasNef) VALUES(?, ?, ?, ?)";
+ String sql3 = "UPDATE Vehicle SET version = ?, status = ? WHERE id = ?";
- long vehicleID = -1;
- long vehicleVersion = -1;
- try {
- Connection connection = jdbcConnectionManager.getConnection();
- connection.setAutoCommit(false);
- try (PreparedStatement p =
- connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) {
-
- p.setLong(1, vehicle.id());
- p.executeQuery();
- try (ResultSet rs = p.getResultSet()) {
- while (rs.next()) {
- vehicleID = rs.getLong("id");
- }
- }
- }
- if (vehicleID == -1) {
- throw new ElementNotFoundException("Vehicle don´t found");
- }
-
- query =
- "INSERT INTO VehicleVersion (name,hasNef,constructionType,type) VALUES (?,?,?,?)";
- String name = "";
- try (PreparedStatement p =
- connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) {
- p.setString(1, name);
- p.setBoolean(2, vehicle.hasNef());
- p.setString(3, vehicle.constructionType().name());
+ long versionId;
- p.setString(4, vehicle.type().name());
+ try {
+ Connection con = jdbcConnectionManager.getConnection();
+ con.setAutoCommit(false);
- p.executeUpdate();
+ try (PreparedStatement pstmt = con.prepareStatement(sql)) {
+ pstmt.setLong(1, v.id());
- try (ResultSet keyResultSet = p.getGeneratedKeys()) {
+ try (ResultSet rs = pstmt.executeQuery()) {
+ if (!rs.next()) throw new ElementNotFoundException("No such vehicleId exists");
- if (keyResultSet.next()) {
- vehicleVersion = keyResultSet.getInt(1);
- }
- }
- if (vehicleVersion == -1) {
- throw new ElementNotFoundException("Vehicle don´t found");
+ versionId = rs.getLong(1);
}
}
- name = vehicle.type().name() + "-" + vehicleID;
- query = "UPDATE VehicleVersion SET name=? WHERE id=?";
- try (PreparedStatement p = connection.prepareStatement(query)) {
-
- p.setString(1, name);
- p.setLong(2, vehicleVersion);
- p.executeUpdate();
+ try (PreparedStatement pstmt =
+ con.prepareStatement(sql2, Statement.RETURN_GENERATED_KEYS)) {
+ pstmt.setString(1, v.type().name() + "-" + v.id());
+ pstmt.setString(2, v.constructionType().name());
+ pstmt.setString(3, v.type().name());
+ pstmt.setBoolean(4, v.hasNef());
+ pstmt.executeUpdate();
+
+ try (ResultSet rs = pstmt.getGeneratedKeys()) {
+ if (rs.next()) {
+ // version changed, update it
+ versionId = rs.getLong(1);
+ System.out.println("DEBUG: Changed version to " + versionId);
+ } else System.out.println("Version stayed the same");
+ }
}
- query = "UPDATE Vehicle SET version=? WHERE id=?";
- try (PreparedStatement p = connection.prepareStatement(query)) {
- p.setLong(1, vehicleVersion);
- p.setLong(2, vehicleID);
- p.executeUpdate();
+ try (PreparedStatement pstmt = con.prepareStatement(sql3)) {
+ pstmt.setLong(1, versionId);
+ pstmt.setString(2, v.status().name());
+ pstmt.setLong(3, v.id());
+ pstmt.executeUpdate();
}
- connection.commit();
- connection.setAutoCommit(true);
+
+ con.commit();
+ con.setAutoCommit(true);
} catch (SQLException e) {
throw new PersistenceException(e);
}