diff options
Diffstat (limited to 'src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/OperationDatabaseDAO.java')
-rw-r--r-- | src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/OperationDatabaseDAO.java | 218 |
1 files changed, 0 insertions, 218 deletions
diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/OperationDatabaseDAO.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/OperationDatabaseDAO.java deleted file mode 100644 index 0a465f2..0000000 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/OperationDatabaseDAO.java +++ /dev/null @@ -1,218 +0,0 @@ -package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao; - -import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Operation; -import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Operation.Severity; -import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Operation.Status; -import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Vehicle; -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.util.JDBCConnectionManager; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Timestamp; -import java.util.EnumSet; -import java.util.HashSet; -import java.util.Objects; -import java.util.Set; -import java.util.stream.Collectors; -import org.springframework.lang.NonNull; -import org.springframework.stereotype.Repository; - -@Repository -public class OperationDatabaseDAO implements OperationDAO { - - private JDBCConnectionManager jdbcConnectionManager; - private VehicleDAO vehicleDAO; - - public OperationDatabaseDAO( - JDBCConnectionManager jdbcConnectionManager, VehicleDAO vehicleDAO) { - this.jdbcConnectionManager = jdbcConnectionManager; - this.vehicleDAO = vehicleDAO; - } - - @Override - public long add(@NonNull Operation o) throws PersistenceException { - String sql = - "INSERT INTO Operation(opCode, severity, created, destination, additionalInfo," - + " status) VALUES (?, ?, ?, ?, ?, ?)"; - String sql2 = "INSERT INTO VehicleOperation(vehicleId, operationId) VALUES (?, ?)"; - long operationId; - - try { - Connection con = jdbcConnectionManager.getConnection(); - con.setAutoCommit(false); - try (PreparedStatement pstmt = con.prepareStatement(sql)) { - pstmt.setString(1, o.opCode()); - pstmt.setInt(2, o.severity().ordinal()); - pstmt.setTimestamp(3, Timestamp.from(Objects.requireNonNull(o.created()))); - pstmt.setString(4, o.destination()); - pstmt.setString(5, o.additionalInfo()); - pstmt.setInt(6, o.status().ordinal()); - pstmt.executeUpdate(); - - try (ResultSet rs = pstmt.getGeneratedKeys()) { - if (!rs.next()) throw new PersistenceException("Failed to persist operation"); - - operationId = rs.getLong(1); - } - } - - 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(); - } - con.commit(); - con.setAutoCommit(true); - return operationId; - } catch (SQLException e) { - throw new PersistenceException(e); - } - } - - @Override - public void update(@NonNull Operation o) throws ElementNotFoundException, PersistenceException { - // Note this will, by design, not update created - String sql = - "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(); - con.setAutoCommit(false); - try (PreparedStatement pstmt = con.prepareStatement(sql)) { - pstmt.setString(1, o.opCode()); - pstmt.setInt(2, o.severity().ordinal()); - pstmt.setString(3, o.destination()); - pstmt.setString(4, o.additionalInfo()); - pstmt.setInt(5, o.status().ordinal()); - pstmt.setLong(6, o.id()); - - if (pstmt.executeUpdate() != 1) - throw new ElementNotFoundException("No such operationId exists"); - } - - try (PreparedStatement pstmt = con.prepareStatement(sql2)) { - pstmt.setLong(1, o.id()); - 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(); - } - con.commit(); - con.setAutoCommit(true); - } catch (SQLException e) { - throw new PersistenceException(e); - } - } - - @Override - public Operation get(long operationId) throws ElementNotFoundException, PersistenceException { - String sql = "Select * from operation where id = ?"; - - try { - Connection con = jdbcConnectionManager.getConnection(); - try (PreparedStatement pstmt = con.prepareStatement(sql)) { - pstmt.setLong(1, operationId); - pstmt.execute(); - - try (ResultSet rs = pstmt.getResultSet()) { - if (!rs.next()) - throw new ElementNotFoundException("No such element could be found"); - - return operationFromRS(rs); - } - } - } catch (SQLException e) { - throw new PersistenceException(e); - } - } - - @Override - public Set<Operation> list(EnumSet<Status> statuses) throws PersistenceException { - // This hack exists because H2 currently has a bug that prevents IN (?) with an array of - // ids, i.e. pstmt.setArray(1, con.createArrayOf("INT", intarray) from working. See - // commented code below. - String str = - statuses.stream() - .map(Enum::name) - .map(s -> "'" + s + "'") - .collect(Collectors.joining(",")); - String sql = "SELECT * FROM Operation WHERE status IN (" + str + ")"; - Set<Operation> operations = new HashSet<>(); - - try { - Connection con = jdbcConnectionManager.getConnection(); - - try (PreparedStatement pstmt = con.prepareStatement(sql)) { - // Object[] arr = statuses.stream().map(Enum::ordinal).toArray(); - // pstmt.setArray(1, con.createArrayOf("INT", arr)); - - try (ResultSet rs = pstmt.executeQuery()) { - while (rs.next()) operations.add(operationFromRS(rs)); - } - } - - return operations; - } catch (SQLException e) { - throw new PersistenceException(e); - } - } - - private Operation operationFromRS(ResultSet rs) throws PersistenceException, SQLException { - Long operationId = rs.getLong("id"); - - return Operation.builder() - .id(operationId) - .opCode(rs.getString("opCode")) - .severity(Severity.valueOf(rs.getString("severity"))) - .status(Status.valueOf(rs.getString("status"))) - .vehicles(getVehiclesFromOperationId(operationId)) - .created(rs.getTimestamp("created").toInstant()) - .destination(rs.getString("destination")) - .additionalInfo(rs.getString("additionalInfo")) - .build(); - } - - private Set<Vehicle> getVehiclesFromOperationId(long operationId) throws PersistenceException { - String sql = "SELECT vehicleId FROM VehicleOperation WHERE operationId = ?"; - Set<Vehicle> vehicles = new HashSet<>(); - - try { - Connection con = jdbcConnectionManager.getConnection(); - try (PreparedStatement pstmt = con.prepareStatement(sql)) { - pstmt.setLong(1, operationId); - pstmt.execute(); - - try (ResultSet rs = pstmt.getResultSet()) { - while (rs.next()) { - vehicles.add(vehicleDAO.get(rs.getLong("vehicleId"))); - } - } - } - } catch (SQLException e) { - throw new PersistenceException(e); - } catch (ElementNotFoundException e) { - throw new PersistenceException("VehicleOperation contained nonexistent vehicle", e); - } - - return vehicles; - } -} |