diff options
| author | Tharre <tharre3@gmail.com> | 2018-06-19 00:06:20 +0200 | 
|---|---|---|
| committer | Tharre <tharre3@gmail.com> | 2018-06-19 00:09:20 +0200 | 
| commit | 2f6fa3024fc11b65a29624e12fb7a9a6cc851bd9 (patch) | |
| tree | 71e93c28d8b02d6af6b227eaf857984e340faae1 | |
| parent | 2e316a8540f2db3b36693f6e2c2754e4d66c82ee (diff) | |
| download | sepm-groupproject-2f6fa3024fc11b65a29624e12fb7a9a6cc851bd9.tar.gz sepm-groupproject-2f6fa3024fc11b65a29624e12fb7a9a6cc851bd9.tar.xz sepm-groupproject-2f6fa3024fc11b65a29624e12fb7a9a6cc851bd9.zip | |
Fixup OperationDao to convert version
| -rw-r--r-- | src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/dao/OperationDatabaseDAO.java | 48 | 
1 files changed, 24 insertions, 24 deletions
| diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/dao/OperationDatabaseDAO.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/dao/OperationDatabaseDAO.java index 1ccb4cf..10bba3d 100644 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/dao/OperationDatabaseDAO.java +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/dao/OperationDatabaseDAO.java @@ -37,7 +37,6 @@ public class OperationDatabaseDAO implements OperationDAO {          String sql =                  "INSERT INTO Operation(opCode, severity, created, destination, additionalInfo,"                          + "            status) VALUES (?, ?, ?, ?, ?, ?)"; -        String sql2 = "INSERT INTO VehicleOperation(vehicleId, operationId) VALUES (?, ?)";          long operationId;          try { @@ -59,16 +58,7 @@ public class OperationDatabaseDAO implements OperationDAO {                  }              } -            try (PreparedStatement pstmt = con.prepareStatement(sql2)) { -                pstmt.setLong(2, operationId); - -                for (long id : (Iterable<Long>) o.vehicles().stream().map(Vehicle::id)::iterator) { -                    pstmt.setLong(1, id); -                    pstmt.addBatch(); -                } - -                pstmt.executeBatch(); -            } +            createVehicleOperation(con, operationId, o.vehicles());              con.commit();              return operationId;          } catch (SQLException e) { @@ -84,7 +74,6 @@ public class OperationDatabaseDAO implements OperationDAO {                  "UPDATE Operation SET opCode = ?, severity = ?, destination = ?,"                          + " additionalInfo = ?, status = ? WHERE id = ?";          String sql2 = "DELETE FROM VehicleOperation WHERE operationId = ?"; -        String sql3 = "INSERT INTO VehicleOperation(vehicleId, operationId) VALUES (?, ?)";          try {              Connection con = jdbcConnectionManager.getConnection(); @@ -106,16 +95,7 @@ public class OperationDatabaseDAO implements OperationDAO {                  pstmt.executeUpdate();              } -            try (PreparedStatement pstmt = con.prepareStatement(sql3)) { -                pstmt.setLong(2, o.id()); - -                for (long id : (Iterable<Long>) o.vehicles().stream().map(Vehicle::id)::iterator) { -                    pstmt.setLong(1, id); -                    pstmt.addBatch(); -                } - -                pstmt.executeBatch(); -            } +            createVehicleOperation(con, o.id(), o.vehicles());              con.commit();          } catch (SQLException e) {              jdbcConnectionManager.rollbackConnection(); @@ -123,6 +103,24 @@ public class OperationDatabaseDAO implements OperationDAO {          }      } +    private void createVehicleOperation(Connection con, long operationId, Set<Vehicle> vehicles) +            throws SQLException { +        String sql = +                "INSERT INTO VehicleOperation(vehicleId, operationId)" +                        + " SELECT version, ? FROM Vehicle WHERE id = ?"; + +        try (PreparedStatement pstmt = con.prepareStatement(sql)) { +            pstmt.setLong(1, operationId); + +            for (long id : (Iterable<Long>) vehicles.stream().map(Vehicle::id)::iterator) { +                pstmt.setLong(2, id); +                pstmt.addBatch(); +            } + +            pstmt.executeBatch(); +        } +    } +      @Override      public Operation get(long operationId) throws ElementNotFoundException, PersistenceException {          String sql = "Select * from operation where id = ?"; @@ -195,7 +193,9 @@ public class OperationDatabaseDAO implements OperationDAO {      }      private Set<Vehicle> getVehiclesFromOperationId(long operationId) throws PersistenceException { -        String sql = "SELECT vehicleId FROM VehicleOperation WHERE operationId = ?"; +        String sql = +                "SELECT id FROM Vehicle WHERE version IN" +                        + " (SELECT vehicleId FROM VehicleOperation WHERE operationId = ?)";          Set<Vehicle> vehicles = new HashSet<>();          try { @@ -206,7 +206,7 @@ public class OperationDatabaseDAO implements OperationDAO {                  try (ResultSet rs = pstmt.getResultSet()) {                      while (rs.next()) { -                        vehicles.add(vehicleDAO.get(rs.getLong("vehicleId"))); +                        vehicles.add(vehicleDAO.get(rs.getLong("Vehicle.id")));                      }                  }              } | 
