aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao
diff options
context:
space:
mode:
authorTharre <tharre3@gmail.com>2018-06-20 22:07:36 +0200
committerTharre <tharre3@gmail.com>2018-06-20 22:07:36 +0200
commit0c995a05985da749d93aa56eba976c7fc621a4fa (patch)
tree5b80394920705aae5e2b6004c3dfbd839c8b8fa3 /src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao
parentf5bc7925a8fbbe247972a6f0e0571cc7e92fbefa (diff)
parente21feb3ac772a5394dc5381b58142c3c061de716 (diff)
downloadsepm-groupproject-master.tar.gz
sepm-groupproject-master.tar.xz
sepm-groupproject-master.zip
Merge branch 'develop'HEADv3.0master
Diffstat (limited to 'src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao')
-rw-r--r--src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/EmployeeDAO.java44
-rw-r--r--src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/EmployeeDatabaseDAO.java201
-rw-r--r--src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/OperationDAO.java48
-rw-r--r--src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/OperationDatabaseDAO.java218
-rw-r--r--src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDAO.java28
-rw-r--r--src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDatabaseDAO.java147
-rw-r--r--src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDAO.java54
-rw-r--r--src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDatabaseDAO.java220
8 files changed, 0 insertions, 960 deletions
diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/EmployeeDAO.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/EmployeeDAO.java
deleted file mode 100644
index 539a8e5..0000000
--- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/EmployeeDAO.java
+++ /dev/null
@@ -1,44 +0,0 @@
-package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao;
-
-import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Employee;
-import at.ac.tuwien.sepm.assignment.groupphase.exception.ElementNotFoundException;
-import at.ac.tuwien.sepm.assignment.groupphase.exception.PersistenceException;
-import java.util.Set;
-
-public interface EmployeeDAO {
-
- /**
- * Persist the given employee.
- *
- * @param employee that should be stored
- * @return the id that was assigned
- * @throws PersistenceException if the employee could not be persisted
- */
- long add(Employee employee) throws PersistenceException;
-
- /**
- * Update the given employee.
- *
- * @param employee that should be updated
- * @throws ElementNotFoundException if no employee with the given id exists
- * @throws PersistenceException if the employee could not be updated
- */
- void update(Employee employee) throws ElementNotFoundException, PersistenceException;
-
- /**
- * Get all stored employees.
- *
- * @return list containing all stored employees
- * @throws PersistenceException if loading the stored employees failed
- */
- Set<Employee> list() throws PersistenceException;
-
- /**
- * Remove employee with the given id from the store.
- *
- * @param id of the employee that should be removed
- * @throws ElementNotFoundException if no employee with the given id exists
- * @throws PersistenceException if the employee could not be removed
- */
- void remove(long id) throws ElementNotFoundException, PersistenceException;
-}
diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/EmployeeDatabaseDAO.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/EmployeeDatabaseDAO.java
deleted file mode 100644
index 43a5c9d..0000000
--- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/EmployeeDatabaseDAO.java
+++ /dev/null
@@ -1,201 +0,0 @@
-package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao;
-
-import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Employee;
-import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Employee.EducationLevel;
-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.lang.invoke.MethodHandles;
-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.sql.Timestamp;
-import java.util.HashSet;
-import java.util.Set;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.stereotype.Repository;
-
-@Repository
-public class EmployeeDatabaseDAO implements EmployeeDAO {
-
- private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
- private static final String INSERT_EMPLOYEE_VERSION =
- "INSERT INTO EmployeeVersion(name, birthday, educationLevel, isDriver, isPilot) "
- + "VALUES(?, ?, ?, ?, ?)";
- private static final String INSERT_EMPLOYEE = "INSERT INTO Employee(version) VALUES(?)";
- private static final String LIST_EMPLOYEE =
- "SELECT emp.id, v.name, v.birthday, v.educationLevel, v.isDriver, v.isPilot "
- + "FROM employee emp "
- + "JOIN EmployeeVersion v ON v.id = emp.version";
- private static final String UPDATE_EMPLOYEE = "UPDATE Employee SET version = ? WHERE id = ?";
-
- private final PreparedStatement insertEmployeeVersion,
- insertEmployee,
- listEmployee,
- updateEmployee;
-
- private final Connection connection;
-
- public EmployeeDatabaseDAO(JDBCConnectionManager connectionManager)
- throws PersistenceException {
-
- try {
-
- connection = connectionManager.getConnection();
- insertEmployeeVersion =
- connection.prepareStatement(
- INSERT_EMPLOYEE_VERSION, Statement.RETURN_GENERATED_KEYS);
- insertEmployee =
- connection.prepareStatement(INSERT_EMPLOYEE, Statement.RETURN_GENERATED_KEYS);
-
- listEmployee = connection.prepareStatement(LIST_EMPLOYEE);
-
- updateEmployee = connection.prepareStatement(UPDATE_EMPLOYEE);
-
- } catch (SQLException e) {
- throw new PersistenceException(e);
- }
- }
-
- @Override
- public long add(Employee employee) throws PersistenceException {
-
- // Assumption: the given employee is already validated (from service)
- Savepoint savepoint = null;
- try {
- savepoint = connection.setSavepoint();
- connection.setAutoCommit(false);
- insertEmployeeVersion.setString(1, employee.name());
- insertEmployeeVersion.setTimestamp(
- 2, Timestamp.valueOf(employee.birthday().atStartOfDay()));
- insertEmployeeVersion.setString(3, employee.educationLevel().toString());
- insertEmployeeVersion.setBoolean(4, employee.isDriver());
- insertEmployeeVersion.setBoolean(5, employee.isPilot());
- insertEmployeeVersion.executeUpdate();
- try (ResultSet resultSetEmployeeVersion = insertEmployeeVersion.getGeneratedKeys()) {
- if (resultSetEmployeeVersion.next()) {
- long versionId = resultSetEmployeeVersion.getLong(1);
-
- insertEmployee.setLong(1, versionId);
- insertEmployee.executeUpdate();
-
- try (ResultSet resultSetEmployee = insertEmployee.getGeneratedKeys()) {
- if (resultSetEmployee.next()) {
- connection.commit();
- return resultSetEmployee.getLong(1);
- }
- }
- }
- }
-
- throw new PersistenceException("Employee was not updated");
-
- } catch (SQLException e) {
- try {
- if (savepoint != null) {
- connection.rollback(savepoint);
- }
- } catch (SQLException e1) {
- throw new PersistenceException(e);
- }
- throw new PersistenceException(e);
- } finally {
- try {
- connection.setAutoCommit(true);
- } catch (SQLException e) {
- throw new PersistenceException(e);
- }
- }
- }
-
- @Override
- public void update(Employee employee) throws ElementNotFoundException, PersistenceException {
-
- Savepoint savepoint = null;
- try {
- savepoint = connection.setSavepoint();
- connection.setAutoCommit(false);
-
- insertEmployeeVersion.setString(1, employee.name());
- insertEmployeeVersion.setTimestamp(
- 2, Timestamp.valueOf(employee.birthday().atStartOfDay()));
- insertEmployeeVersion.setString(3, employee.educationLevel().toString());
- insertEmployeeVersion.setBoolean(4, employee.isDriver());
- insertEmployeeVersion.setBoolean(5, employee.isPilot());
- insertEmployeeVersion.executeUpdate();
- try (ResultSet resultSetEmployeeVersion = insertEmployeeVersion.getGeneratedKeys()) {
-
- if (resultSetEmployeeVersion.next()) {
- long versionId = resultSetEmployeeVersion.getLong(1);
-
- updateEmployee.setLong(1, versionId);
- updateEmployee.setLong(2, employee.id());
- int affectedRows = updateEmployee.executeUpdate();
-
- if (affectedRows == 1) {
- connection.commit();
- } else {
- throw new ElementNotFoundException(
- "element not found with id: " + employee.id());
- }
- }
- }
-
- } catch (SQLException e) {
- try {
- if (savepoint != null) {
- connection.rollback(savepoint);
- }
- } catch (SQLException e1) {
- throw new PersistenceException(e);
- }
- throw new PersistenceException(e);
- } finally {
- try {
- connection.setAutoCommit(true);
- } catch (SQLException e) {
- throw new PersistenceException(e);
- }
- }
- }
-
- @Override
- public Set<Employee> list() throws PersistenceException {
-
- try {
- Set<Employee> employees;
- try (ResultSet rs = listEmployee.executeQuery()) {
-
- employees = new HashSet<>();
- while (rs.next()) {
-
- Employee employee =
- Employee.builder()
- .id(rs.getLong(1))
- .name(rs.getString(2))
- .birthday(rs.getTimestamp(3).toLocalDateTime().toLocalDate())
- .educationLevel(EducationLevel.valueOf(rs.getString(4)))
- .isDriver(rs.getBoolean(5))
- .isPilot(rs.getBoolean(6))
- .build();
-
- employees.add(employee);
- }
- }
-
- return employees;
-
- } catch (SQLException e) {
- throw new PersistenceException(e);
- }
- }
-
- @Override
- public void remove(long id) throws ElementNotFoundException, PersistenceException {
- throw new UnsupportedOperationException();
- }
-}
diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/OperationDAO.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/OperationDAO.java
deleted file mode 100644
index d82f768..0000000
--- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/OperationDAO.java
+++ /dev/null
@@ -1,48 +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.Status;
-import at.ac.tuwien.sepm.assignment.groupphase.exception.ElementNotFoundException;
-import at.ac.tuwien.sepm.assignment.groupphase.exception.PersistenceException;
-import java.util.EnumSet;
-import java.util.Set;
-
-public interface OperationDAO {
-
- /**
- * Persist the given operation.
- *
- * @param operation that should be stored
- * @return the id that was assigned
- * @throws PersistenceException if the operation could not be persisted
- */
- long add(Operation operation) throws PersistenceException;
-
- /**
- * Update the given operation.
- *
- * @param operation that should be updated
- * @throws ElementNotFoundException if no operation with the given id exists
- * @throws PersistenceException if the operation could not be updated
- */
- void update(Operation operation) throws ElementNotFoundException, PersistenceException;
-
- /**
- * Returns the operation with the given id.
- *
- * @param operationId id of the operation that should be returned
- * @return operation with the given id
- * @throws ElementNotFoundException if no operation with the given id exists
- * @throws PersistenceException if the operation could not be loaded
- */
- Operation get(long operationId) throws ElementNotFoundException, PersistenceException;
-
- /**
- * Get all stored operations with matching status.
- *
- * @param statuses set containing all statuses that should be matched
- * @return list containing all matched operations
- * @throws PersistenceException if loading the stored operations failed
- */
- Set<Operation> list(EnumSet<Status> statuses) throws PersistenceException;
-}
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;
- }
-}
diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDAO.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDAO.java
deleted file mode 100644
index 36b6f1b..0000000
--- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDAO.java
+++ /dev/null
@@ -1,28 +0,0 @@
-package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao;
-
-import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Registration;
-import at.ac.tuwien.sepm.assignment.groupphase.exception.ElementNotFoundException;
-import at.ac.tuwien.sepm.assignment.groupphase.exception.PersistenceException;
-import java.util.Set;
-
-public interface RegistrationDAO {
-
- /**
- * Persist the given registration.
- *
- * @param vehicleId the id of the target vehicle
- * @param registrations that should be stored
- * @return a list of the ids that were assigned
- * @throws PersistenceException if the registration could not be persisted
- */
- Set<Long> add(long vehicleId, Set<Registration> registrations) throws PersistenceException;
-
- /**
- * Make registration with the given id inactive.
- *
- * @param id of the registration that should be made inactive
- * @throws ElementNotFoundException if no registration with the given id exists
- * @throws PersistenceException if the registration could not be made inactive
- */
- void remove(long id) throws ElementNotFoundException, PersistenceException;
-}
diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDatabaseDAO.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDatabaseDAO.java
deleted file mode 100644
index 13f2c0f..0000000
--- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDatabaseDAO.java
+++ /dev/null
@@ -1,147 +0,0 @@
-package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao;
-
-import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Employee;
-import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Registration;
-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.Statement;
-import java.sql.Timestamp;
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Repository;
-
-@Repository
-public class RegistrationDatabaseDAO implements RegistrationDAO {
-
- private static final Logger LOG = LoggerFactory.getLogger(RegistrationDatabaseDAO.class);
-
- private static final String ADD_REGISTRATION =
- "INSERT INTO Registration (vehicleId, employeeId, start, end, active) VALUES (?,?,?,?,?);";
- private static final String UPDATE_VEHICLE =
- "UPDATE Vehicle SET status = 'FREI_WACHE' WHERE id = ?;";
- private static final String FETCH_REGISTRATIONS =
- "SELECT * FROM Registration WHERE vehicleId = ?";
-
- private PreparedStatement addRegistration;
- private PreparedStatement updateVehicle;
- private PreparedStatement fetchRegistrations;
-
- private Connection connection;
- private EmployeeDAO employeePersistence;
-
- @Autowired
- public RegistrationDatabaseDAO(
- JDBCConnectionManager connectionManager, EmployeeDAO employeePersistence)
- throws PersistenceException {
- this.employeePersistence = employeePersistence;
- try {
- connection = connectionManager.getConnection();
- addRegistration =
- connection.prepareStatement(ADD_REGISTRATION, Statement.RETURN_GENERATED_KEYS);
- updateVehicle = connection.prepareStatement(UPDATE_VEHICLE);
- fetchRegistrations = connection.prepareStatement(FETCH_REGISTRATIONS);
- } catch (SQLException e) {
- LOG.error("Could not get connection or preparation of statement failed");
- throw new PersistenceException(e);
- }
- }
-
- @Override
- public Set<Long> add(long vehicleId, Set<Registration> registrations)
- throws PersistenceException {
- Set<Long> returnValues = new HashSet<>();
- try {
- connection.setAutoCommit(false);
- for (Registration registration : registrations) {
- addRegistration.setLong(1, vehicleId);
- addRegistration.setLong(2, registration.employee().id());
- addRegistration.setTimestamp(3, Timestamp.from(registration.start()));
- addRegistration.setTimestamp(4, Timestamp.from(registration.end()));
- addRegistration.setBoolean(
- 5, true); // ASSUMPTION: Registration gets created as active
- addRegistration.executeUpdate();
- try (ResultSet rs = addRegistration.getGeneratedKeys()) {
- if (rs.next()) {
- returnValues.add(rs.getLong(1));
- } else {
- LOG.error("No ResultSet was created while adding registration");
- throw new PersistenceException(
- "Anmeldung konnte nicht gespeichert werden.");
- }
- }
- }
-
- updateVehicle.setLong(1, vehicleId);
- updateVehicle.executeUpdate();
-
- connection.commit();
- return returnValues;
- } catch (SQLException e) {
- LOG.error(
- "An SQLException occurred while trying to save registrations to database. "
- + "Attempting a rollback. Error message: {}",
- e.getMessage());
- try {
- connection.rollback();
- } catch (SQLException e1) {
- LOG.error("Rollback failed :(");
- }
- throw new PersistenceException(e);
- } finally {
- try {
- connection.setAutoCommit(true);
- } catch (SQLException e) {
- LOG.error(
- "Setting back AutoCommit to false failed! Error message: {}",
- e.getMessage());
- // SonarLint insists on me not throwing anything here...
- }
- }
- }
-
- @Override
- public void remove(long id) throws ElementNotFoundException, PersistenceException {
- throw new UnsupportedOperationException();
- }
-
- public List<Registration> list(long vehicleId) throws PersistenceException {
- List<Registration> registrationList = new ArrayList<>();
- try {
- fetchRegistrations.setLong(1, vehicleId);
- ResultSet rs = fetchRegistrations.executeQuery();
- while (rs.next()) {
- long employeeId = rs.getLong("employeeId");
- // TODO: replace the following with employeePersistence.get once implemented
- Employee emp =
- employeePersistence
- .list()
- .stream()
- .filter(employee -> employee.id() == employeeId)
- .findAny()
- .orElse(null);
- Registration registration =
- Registration.builder()
- .id(rs.getLong("id"))
- .start(rs.getTimestamp("start").toInstant())
- .end(rs.getTimestamp("end").toInstant())
- .employee(emp)
- .build();
- registrationList.add(registration);
- }
- } catch (SQLException e) {
- throw new PersistenceException(e);
- }
-
- return registrationList;
- }
-}
diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDAO.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDAO.java
deleted file mode 100644
index 5782fd9..0000000
--- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDAO.java
+++ /dev/null
@@ -1,54 +0,0 @@
-package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao;
-
-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 java.util.Set;
-
-public interface VehicleDAO {
-
- /**
- * Persist the given vehicle.
- *
- * @param vehicle that should be stored
- * @return the id that was assigned
- * @throws PersistenceException if the vehicle could not be persisted
- */
- long add(Vehicle vehicle) throws PersistenceException;
-
- /**
- * Update the given vehicle.
- *
- * @param vehicle that should be updated
- * @throws ElementNotFoundException if no vehicle with the given id exists
- * @throws PersistenceException if the vehicle could not be updated
- */
- void update(Vehicle vehicle) throws ElementNotFoundException, PersistenceException;
-
- /**
- * Get all stored vehicles.
- *
- * @return list containing all stored vehicles
- * @throws PersistenceException if loading the stored vehicles failed
- */
- Set<Vehicle> list() throws PersistenceException;
-
- /**
- * Returns the vehicle with the given id.
- *
- * @param vehicleId id of the vehicle that should be returned
- * @return vehicle with the given id
- * @throws ElementNotFoundException if no vehicle with the given id exists
- * @throws PersistenceException if the vehicle could not be loaded
- */
- Vehicle get(long vehicleId) throws ElementNotFoundException, PersistenceException;
-
- /**
- * Remove vehicle with the given id from the store.
- *
- * @param id of the vehicle that should be removed
- * @throws ElementNotFoundException if no vehicle with the given id exists
- * @throws PersistenceException if the vehicle could not be removed
- */
- void remove(long id) throws ElementNotFoundException, PersistenceException;
-}
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
deleted file mode 100644
index 6d50588..0000000
--- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDatabaseDAO.java
+++ /dev/null
@@ -1,220 +0,0 @@
-package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao;
-
-import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Vehicle;
-import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Vehicle.ConstructionType;
-import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Vehicle.Status;
-import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Vehicle.VehicleType;
-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.Statement;
-import java.util.HashSet;
-import java.util.Set;
-import org.springframework.stereotype.Repository;
-
-@Repository
-public class VehicleDatabaseDAO implements VehicleDAO {
-
- private final JDBCConnectionManager jdbcConnectionManager;
- private RegistrationDatabaseDAO registrationDatabaseDao;
-
- public VehicleDatabaseDAO(
- JDBCConnectionManager j, RegistrationDatabaseDAO registrationDatabaseDao) {
- jdbcConnectionManager = j;
- this.registrationDatabaseDao = registrationDatabaseDao;
- }
-
- public long add(Vehicle vehicle) throws PersistenceException {
- String query1 =
- "INSERT INTO VehicleVersion (name,hasNef,constructionType,type) VALUES (?,?,?,?)";
- String query2 = "INSERT INTO Vehicle (version,status) VALUES (?,?)";
-
- String status = "ABGEMELDET";
- String name = "";
- int id = -1;
- int version = -1;
- try {
- Connection connection = jdbcConnectionManager.getConnection();
- connection.setAutoCommit(false);
- 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());
-
- p1.setString(4, vehicle.type().name());
-
- p1.executeUpdate();
- try (ResultSet keyResultSet = p1.getGeneratedKeys()) {
-
- if (keyResultSet.next()) {
- version = keyResultSet.getInt(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()) {
-
- if (keyResultSet.next()) {
- id = keyResultSet.getInt(1);
- }
- }
-
- name = vehicle.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();
- }
-
- connection.commit();
- connection.setAutoCommit(true);
- } catch (SQLException e) {
- throw new PersistenceException(e);
- }
- return id;
- }
-
- @Override
- public void update(Vehicle vehicle) throws ElementNotFoundException, PersistenceException {
- String query = "SELECT * FROM vehicle 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());
-
- p.setString(4, vehicle.type().name());
-
- p.executeUpdate();
-
- try (ResultSet keyResultSet = p.getGeneratedKeys()) {
-
- if (keyResultSet.next()) {
- vehicleVersion = keyResultSet.getInt(1);
- }
- }
- if (vehicleVersion == -1) {
- throw new ElementNotFoundException("Vehicle don´t found");
- }
- }
- 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();
- }
- query = "UPDATE Vehicle SET version=? WHERE id=?";
- try (PreparedStatement p = connection.prepareStatement(query)) {
-
- p.setLong(1, vehicleVersion);
- p.setLong(2, vehicleID);
- p.executeUpdate();
- }
- connection.commit();
- connection.setAutoCommit(true);
- } catch (SQLException e) {
- throw new PersistenceException(e);
- }
- }
-
- @Override
- public Set<Vehicle> list() throws PersistenceException {
- Set<Vehicle> result = new HashSet<>();
-
- String sql =
- "Select * from VehicleVersion, Vehicle where VehicleVersion.id=Vehicle.version";
-
- try (PreparedStatement pstmt =
- jdbcConnectionManager.getConnection().prepareStatement(sql)) {
- pstmt.executeQuery();
- try (ResultSet rs = pstmt.getResultSet()) {
- while (rs.next()) {
- result.add(vehicleFromRS(rs));
- }
- }
- } catch (SQLException e) {
- throw new PersistenceException("Die Werte konnten nicht geladen werden.", e);
- }
- return result;
- }
-
- @Override
- public Vehicle get(long id) throws ElementNotFoundException, PersistenceException {
- String sql =
- "SELECT a.id, b.name, b.constructionType, b.type, a.status, b.hasNef"
- + " FROM Vehicle a"
- + " INNER JOIN VehicleVersion b"
- + " ON version = b.id"
- + " WHERE a.id = ?";
-
- try {
- Connection con = jdbcConnectionManager.getConnection();
- try (PreparedStatement pstmt = con.prepareStatement(sql)) {
- pstmt.setLong(1, id);
-
- try (ResultSet rs = pstmt.executeQuery()) {
- if (!rs.first()) throw new ElementNotFoundException("No such vehicle exists");
-
- return vehicleFromRS(rs);
- }
- }
- } catch (SQLException e) {
- throw new PersistenceException(e);
- }
- }
-
- @Override
- public void remove(long id) throws ElementNotFoundException, PersistenceException {}
-
- private Vehicle vehicleFromRS(ResultSet rs) throws SQLException, PersistenceException {
- return Vehicle.builder()
- .id(rs.getLong("Vehicle.id"))
- .name(rs.getString("name"))
- .constructionType(ConstructionType.values()[rs.getInt("constructionType")])
- .type(VehicleType.valueOf(rs.getString("type")))
- .status(Status.values()[rs.getInt("status")])
- .hasNef(rs.getBoolean("hasNef"))
- .registrations(registrationDatabaseDao.list(rs.getLong("id")))
- .build();
- }
-}