aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFelix Kehrer <felix.kehrer@gmail.com>2018-05-04 22:36:00 +0200
committerFelix Kehrer <felix.kehrer@gmail.com>2018-05-07 14:42:06 +0200
commitda1ac259eb6ad8f53bcd884d22ed8de672b0f024 (patch)
tree19c0559e403c60b97c6e1c48e1bb3d3442d26cd6
parent05c7e29e8d7cd474963b7636f54c3d6f1e4bd390 (diff)
downloadsepm-groupproject-da1ac259eb6ad8f53bcd884d22ed8de672b0f024.tar.gz
sepm-groupproject-da1ac259eb6ad8f53bcd884d22ed8de672b0f024.tar.xz
sepm-groupproject-da1ac259eb6ad8f53bcd884d22ed8de672b0f024.zip
First attempt at implementing RegistrationDAO
-rw-r--r--src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/H2RegistrationDAO.java96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/H2RegistrationDAO.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/H2RegistrationDAO.java
new file mode 100644
index 0000000..825dc80
--- /dev/null
+++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/H2RegistrationDAO.java
@@ -0,0 +1,96 @@
+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 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.LinkedList;
+import java.util.List;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public class H2RegistrationDAO implements RegistrationDAO {
+
+ private static final Logger LOG = LoggerFactory.getLogger(H2RegistrationDAO.class);
+
+ private static final String ADD_REGISTRATION =
+ "INSERT INTO Registration (vehicleId, employeeId, start, end, active) VALUES (?,?,?,?,?);";
+
+ private Connection connection;
+
+ @Autowired
+ public H2RegistrationDAO(JDBCConnectionManager connectionManager) throws PersistenceException {
+ try {
+ connection = connectionManager.getConnection();
+ } catch (SQLException e) {
+ LOG.error("Could not get connection!");
+ throw new PersistenceException(e);
+ }
+ }
+
+ @Override
+ public List<Long> add(long vehicleId, List<Registration> registrations)
+ throws PersistenceException {
+ List<Long> returnValues = new LinkedList<>();
+ try {
+ connection.setAutoCommit(false);
+ for (Registration registration : registrations) {
+ try (PreparedStatement addRegistration =
+ connection.prepareStatement(
+ ADD_REGISTRATION, Statement.RETURN_GENERATED_KEYS)) {
+ addRegistration.setLong(1, vehicleId);
+ addRegistration.setLong(2, registration.employee().id());
+ addRegistration.setObject(3, registration.start());
+ addRegistration.setObject(4, 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.");
+ }
+ }
+ }
+ }
+ 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();
+ }
+}