diff options
Diffstat (limited to 'src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDatabaseDAO.java')
-rw-r--r-- | src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDatabaseDAO.java | 147 |
1 files changed, 0 insertions, 147 deletions
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; - } -} |