diff options
Diffstat (limited to 'src/main/java/at/ac/tuwien/sepm/assignment')
| -rw-r--r-- | src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDatabaseDAO.java | 126 | 
1 files changed, 46 insertions, 80 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 37915ea..3144288 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 @@ -11,19 +11,14 @@ import java.sql.Connection;  import java.sql.PreparedStatement;  import java.sql.ResultSet;  import java.sql.SQLException; -import java.sql.Savepoint;  import java.sql.Statement;  import java.util.HashSet;  import java.util.Set; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory;  import org.springframework.stereotype.Repository;  @Repository  public class VehicleDatabaseDAO implements VehicleDAO { -    private static final Logger LOG = LoggerFactory.getLogger(VehicleDatabaseDAO.class); -      private final JDBCConnectionManager jdbcConnectionManager;      private RegistrationDatabaseDAO registrationDatabaseDao; @@ -33,85 +28,63 @@ public class VehicleDatabaseDAO implements VehicleDAO {          this.registrationDatabaseDao = registrationDatabaseDao;      } -    public long add(Vehicle vehicle) throws PersistenceException { -        String query1 = +    @Override +    public long add(Vehicle v) throws PersistenceException { +        String sql =                  "INSERT INTO VehicleVersion (name,hasNef,constructionType,type) VALUES (?,?,?,?)"; -        String query2 = "INSERT INTO Vehicle (version,status) VALUES (?,?)"; +        String sql2 = "INSERT INTO Vehicle (version,status) VALUES (?,?)"; +        String sql3 = "UPDATE VehicleVersion SET name=? WHERE id=?"; -        String status = "ABGEMELDET"; -        String name = ""; -        int id = -1; -        int version = -1; -        Connection connection = null; -        Savepoint savepoint = null;          try { -            connection = jdbcConnectionManager.getConnection(); -            connection.setAutoCommit(false); -            savepoint = connection.setSavepoint(); -            try (PreparedStatement p1 = -                    connection.prepareStatement(query1, PreparedStatement.RETURN_GENERATED_KEYS)) { - -                p1.setString(1, name); -                p1.setBoolean(2, vehicle.hasNef()); -                p1.setString(3, vehicle.constructionType().name()); +            Connection con = jdbcConnectionManager.getConnection(); +            con.setAutoCommit(false); +            String name = ""; +            long version, id; -                p1.setString(4, vehicle.type().name()); +            try (PreparedStatement pstmt = +                    con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { +                pstmt.setString(1, name); +                pstmt.setBoolean(2, v.hasNef()); +                pstmt.setInt(3, v.constructionType().ordinal()); +                pstmt.setString(4, v.type().name()); +                pstmt.executeUpdate(); -                p1.executeUpdate(); -                try (ResultSet keyResultSet = p1.getGeneratedKeys()) { +                try (ResultSet rs = pstmt.getGeneratedKeys()) { +                    if (!rs.next()) +                        throw new PersistenceException("Failed to insert into VehicleVersion"); -                    if (keyResultSet.next()) { -                        version = keyResultSet.getInt(1); -                    } +                    version = rs.getLong(1);                  }              } -            try (PreparedStatement p2 = -                    connection.prepareStatement(query2, Statement.RETURN_GENERATED_KEYS)) { -                p2.setInt(1, version); -                p2.setString(2, status); -                p2.executeUpdate(); -                try (ResultSet keyResultSet = p2.getGeneratedKeys()) { +            try (PreparedStatement pstmt = +                    con.prepareStatement(sql2, Statement.RETURN_GENERATED_KEYS)) { +                pstmt.setLong(1, version); +                pstmt.setInt(2, Status.ABGEMELDET.ordinal()); +                pstmt.executeUpdate(); + +                try (ResultSet rs = pstmt.getGeneratedKeys()) { +                    if (!rs.next()) throw new PersistenceException("Failed to insert into Vehicle"); -                    if (keyResultSet.next()) { -                        id = keyResultSet.getInt(1); -                    } +                    id = rs.getLong(1);                  } -                name = vehicle.type().name() + "-" + id; +                name = v.type().name() + "-" + id;              } -            query1 = "UPDATE VehicleVersion SET name=? WHERE id=?"; -            try (PreparedStatement p3 = connection.prepareStatement(query1)) { -                p3.setString(1, name); -                p3.setInt(2, version); -                p3.executeUpdate(); + +            try (PreparedStatement pstmt = con.prepareStatement(sql3)) { +                pstmt.setString(1, name); +                pstmt.setLong(2, version); + +                if (pstmt.executeUpdate() != 1) +                    throw new PersistenceException("Failed to update VehicleVersion");              } -            connection.commit(); +            con.commit(); +            return id;          } catch (SQLException e) { -            rollbackAndEnableAutoCommit(connection, savepoint); +            jdbcConnectionManager.rollbackConnection();              throw new PersistenceException(e); -        } finally { -            try { -                jdbcConnectionManager.getConnection().setAutoCommit(true); -            } catch (SQLException e) { -                LOG.error("Setting AutoCommit to true failed! ", e); -            } -        } -        return id; -    } - -    private void rollbackAndEnableAutoCommit(Connection connection, Savepoint savepoint) -            throws PersistenceException { -        try { -            if (connection != null) { -                if (savepoint != null) { -                    connection.rollback(savepoint); -                } -                connection.setAutoCommit(true); -            } -        } catch (SQLException e1) { -            throw new PersistenceException(e1);          }      } @@ -125,12 +98,9 @@ public class VehicleDatabaseDAO implements VehicleDAO {          long versionId; -        Connection con = null; -        Savepoint savepoint = null;          try { -            con = jdbcConnectionManager.getConnection(); +            Connection con = jdbcConnectionManager.getConnection();              con.setAutoCommit(false); -            savepoint = con.setSavepoint();              try (PreparedStatement pstmt = con.prepareStatement(sql)) {                  pstmt.setLong(1, v.id()); @@ -166,14 +136,8 @@ public class VehicleDatabaseDAO implements VehicleDAO {              con.commit();          } catch (SQLException e) { -            rollbackAndEnableAutoCommit(con, savepoint); +            jdbcConnectionManager.rollbackConnection();              throw new PersistenceException(e); -        } finally { -            try { -                jdbcConnectionManager.getConnection().setAutoCommit(true); -            } catch (SQLException e) { -                LOG.error("Setting AutoCommit to true failed! ", e); -            }          }      } @@ -193,7 +157,7 @@ public class VehicleDatabaseDAO implements VehicleDAO {                  }              }          } catch (SQLException e) { -            throw new PersistenceException("Die Werte konnten nicht geladen werden.", e); +            throw new PersistenceException(e);          }          return result;      } @@ -224,7 +188,9 @@ public class VehicleDatabaseDAO implements VehicleDAO {      }      @Override -    public void remove(long id) throws ElementNotFoundException, PersistenceException {} +    public void remove(long id) throws ElementNotFoundException, PersistenceException { +        throw new UnsupportedOperationException(); +    }      private Vehicle vehicleFromRS(ResultSet rs) throws SQLException, PersistenceException {          return Vehicle.builder()  | 
