From 126b9a07facec2916156f74b5f632f161df19f11 Mon Sep 17 00:00:00 2001
From: Felix Kehrer <felix.kehrer@gmail.com>
Date: Mon, 7 May 2018 15:55:23 +0200
Subject: Rename Registration Dao & service to conventional style

---
 .../einsatzverwaltung/dao/H2RegistrationDAO.java   | 105 --------------------
 .../dao/RegistrationDatabaseDAO.java               | 106 +++++++++++++++++++++
 .../service/RegistrationServiceImpl.java           |  45 +++++++++
 .../service/SimpleRegistrationService.java         |  45 ---------
 4 files changed, 151 insertions(+), 150 deletions(-)
 delete mode 100644 src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/H2RegistrationDAO.java
 create mode 100644 src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDatabaseDAO.java
 create mode 100644 src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/RegistrationServiceImpl.java
 delete mode 100644 src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/SimpleRegistrationService.java

(limited to 'src/main/java')

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
deleted file mode 100644
index f76c706..0000000
--- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/H2RegistrationDAO.java
+++ /dev/null
@@ -1,105 +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 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.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 static final String UPDATE_VEHICLE =
-            "UPDATE Vehicle SET status = 'frei_wache' WHERE id = ?;";
-
-    private PreparedStatement addRegistration;
-    private PreparedStatement updateVehicle;
-
-    private Connection connection;
-
-    @Autowired
-    public H2RegistrationDAO(JDBCConnectionManager connectionManager) throws PersistenceException {
-        try {
-            connection = connectionManager.getConnection();
-            addRegistration =
-                    connection.prepareStatement(ADD_REGISTRATION, Statement.RETURN_GENERATED_KEYS);
-            updateVehicle = connection.prepareStatement(UPDATE_VEHICLE);
-        } catch (SQLException e) {
-            LOG.error("Could not get connection or preparation of statement failed");
-            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) {
-                addRegistration.setLong(1, vehicleId);
-                addRegistration.setLong(2, registration.employee().id());
-                addRegistration.setTimestamp(3, Timestamp.from(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.");
-                    }
-                }
-            }
-
-            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();
-    }
-}
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
new file mode 100644
index 0000000..e4bc0ab
--- /dev/null
+++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDatabaseDAO.java
@@ -0,0 +1,106 @@
+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.sql.Timestamp;
+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 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 PreparedStatement addRegistration;
+    private PreparedStatement updateVehicle;
+
+    private Connection connection;
+
+    @Autowired
+    public RegistrationDatabaseDAO(JDBCConnectionManager connectionManager)
+            throws PersistenceException {
+        try {
+            connection = connectionManager.getConnection();
+            addRegistration =
+                    connection.prepareStatement(ADD_REGISTRATION, Statement.RETURN_GENERATED_KEYS);
+            updateVehicle = connection.prepareStatement(UPDATE_VEHICLE);
+        } catch (SQLException e) {
+            LOG.error("Could not get connection or preparation of statement failed");
+            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) {
+                addRegistration.setLong(1, vehicleId);
+                addRegistration.setLong(2, registration.employee().id());
+                addRegistration.setTimestamp(3, Timestamp.from(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.");
+                    }
+                }
+            }
+
+            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();
+    }
+}
diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/RegistrationServiceImpl.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/RegistrationServiceImpl.java
new file mode 100644
index 0000000..b0605f0
--- /dev/null
+++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/RegistrationServiceImpl.java
@@ -0,0 +1,45 @@
+package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service;
+
+import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao.RegistrationDAO;
+import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Registration;
+import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.RegistrationValidator;
+import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Vehicle;
+import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidRegistrationException;
+import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidVehicleException;
+import at.ac.tuwien.sepm.assignment.groupphase.exception.PersistenceException;
+import at.ac.tuwien.sepm.assignment.groupphase.exception.ServiceException;
+import java.util.List;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+public class RegistrationServiceImpl implements RegistrationService {
+
+    private static final Logger LOG = LoggerFactory.getLogger(RegistrationServiceImpl.class);
+
+    private final RegistrationDAO registrationDAO;
+
+    @Autowired
+    public RegistrationServiceImpl(RegistrationDAO registrationDAO) {
+        this.registrationDAO = registrationDAO;
+    }
+
+    @Override
+    public List<Long> add(Vehicle vehicle, List<Registration> registrations)
+            throws InvalidVehicleException, InvalidRegistrationException, ServiceException {
+        RegistrationValidator.validate(vehicle, registrations);
+        try {
+            return registrationDAO.add(vehicle.id(), registrations);
+        } catch (PersistenceException e) {
+            LOG.warn("PersistenceException caught, throwing matching ServiceException");
+            throw new ServiceException(e);
+        }
+    }
+
+    @Override
+    public void remove(long registrationId) throws InvalidRegistrationException, ServiceException {
+        throw new UnsupportedOperationException();
+    }
+}
diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/SimpleRegistrationService.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/SimpleRegistrationService.java
deleted file mode 100644
index 5b26e39..0000000
--- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/SimpleRegistrationService.java
+++ /dev/null
@@ -1,45 +0,0 @@
-package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service;
-
-import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao.RegistrationDAO;
-import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Registration;
-import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.RegistrationValidator;
-import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Vehicle;
-import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidRegistrationException;
-import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidVehicleException;
-import at.ac.tuwien.sepm.assignment.groupphase.exception.PersistenceException;
-import at.ac.tuwien.sepm.assignment.groupphase.exception.ServiceException;
-import java.util.List;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-
-@Service
-public class SimpleRegistrationService implements RegistrationService {
-
-    private static final Logger LOG = LoggerFactory.getLogger(SimpleRegistrationService.class);
-
-    private final RegistrationDAO registrationDAO;
-
-    @Autowired
-    public SimpleRegistrationService(RegistrationDAO registrationDAO) {
-        this.registrationDAO = registrationDAO;
-    }
-
-    @Override
-    public List<Long> add(Vehicle vehicle, List<Registration> registrations)
-            throws InvalidVehicleException, InvalidRegistrationException, ServiceException {
-        RegistrationValidator.validate(vehicle, registrations);
-        try {
-            return registrationDAO.add(vehicle.id(), registrations);
-        } catch (PersistenceException e) {
-            LOG.warn("PersistenceException caught, throwing matching ServiceException");
-            throw new ServiceException(e);
-        }
-    }
-
-    @Override
-    public void remove(long registrationId) throws InvalidRegistrationException, ServiceException {
-        throw new UnsupportedOperationException();
-    }
-}
-- 
cgit v1.2.3-70-g09d2