diff options
Diffstat (limited to 'src/main/java')
9 files changed, 623 insertions, 14 deletions
diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/controller/RegistrationWindowController.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/controller/RegistrationWindowController.java new file mode 100644 index 0000000..0683c77 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/controller/RegistrationWindowController.java @@ -0,0 +1,202 @@ +package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.controller; + +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.einsatzverwaltung.dto.Vehicle; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Vehicle.Status; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service.EmployeeService; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service.RegistrationService; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service.VehicleService; +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.ServiceException; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.OffsetDateTime; +import java.util.EnumSet; +import java.util.LinkedList; +import java.util.List; +import javafx.beans.property.SimpleStringProperty; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.fxml.FXML; +import javafx.scene.control.Alert; +import javafx.scene.control.Alert.AlertType; +import javafx.scene.control.ChoiceBox; +import javafx.scene.control.Label; +import javafx.scene.control.TableColumn; +import javafx.scene.control.TableView; +import javafx.scene.control.TextField; +import javafx.stage.Stage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; + +@Controller +public class RegistrationWindowController { + +    private static final Logger LOG = LoggerFactory.getLogger(RegistrationWindowController.class); + +    private EmployeeService employeeService; + +    private VehicleService vehicleService; + +    private RegistrationService registrationService; + +    @Autowired +    public void setEmployeeService(EmployeeService employeeService) { +        this.employeeService = employeeService; +    } + +    @Autowired +    public void setVehicleService(VehicleService vehicleService) { +        this.vehicleService = vehicleService; +    } + +    @Autowired +    public void setRegistrationService(RegistrationService registrationService) { +        this.registrationService = registrationService; +    } + +    @FXML public ChoiceBox<Integer> cbStart; +    @FXML public ChoiceBox<Integer> cbEnd; +    @FXML public Label lVehicles; +    @FXML public Label lEmployees; +    @FXML public TextField tfVehicleSearch; +    @FXML public TextField tfEmployeeSearch; +    @FXML public TableView<Vehicle> tvVehicles; +    @FXML public TableView<Employee> tvEmployees; +    @FXML public TableColumn<Vehicle, String> tcVehicles; +    @FXML public TableColumn<Employee, String> tcEmployees; + +    private Vehicle chosenVehicle; +    private List<Employee> chosenEmployees = new LinkedList<>(); + +    @FXML +    public void initialize() { +        // will have to be replaced for FlowPane +        try { +            List<Vehicle> vehicles = vehicleService.list(EnumSet.of(Status.ABGEMELDET)); +            tcVehicles.setCellValueFactory(x -> new SimpleStringProperty(x.getValue().name())); +            tvVehicles.setItems(FXCollections.observableArrayList(vehicles)); +        } catch (ServiceException e) { +            LOG.warn( +                    "Caught ServiceException while getting vehicles. Showing it to user. Error message: {}", +                    e.getMessage()); +            Alert alert = new Alert(AlertType.ERROR); +            alert.setTitle("Fahrzeuge - Fehler!"); +            alert.setHeaderText("Beim Auflisten der Fahrzeug ist ein Fehler aufgetreten."); +            alert.setContentText(e.getMessage()); +            alert.show(); +        } +        try { +            List<Employee> employees = employeeService.list(); +            tcEmployees.setCellValueFactory(x -> new SimpleStringProperty(x.getValue().name())); +            tvEmployees.setItems(FXCollections.observableArrayList(employees)); +        } catch (ServiceException e) { +            LOG.warn( +                    "Caught ServiceException while getting employees. Showing it to user. Error message: {}", +                    e.getMessage()); +            Alert alert = new Alert(AlertType.ERROR); +            alert.setTitle("Personal - Fehler!"); +            alert.setHeaderText("Beim Auflisten des Personals ist ein Fehler aufgetreten."); +            alert.setContentText(e.getMessage()); +            alert.show(); +        } +        tvVehicles.setOnMousePressed( +                mouseEvent -> { +                    if (mouseEvent.isPrimaryButtonDown() && mouseEvent.getClickCount() == 2) { +                        chosenVehicle = tvVehicles.getSelectionModel().getSelectedItem(); +                        if (chosenVehicle == null) { +                            return; +                        } +                        lVehicles.setText(chosenVehicle.name()); +                    } +                }); +        tvEmployees.setOnMousePressed( +                mouseEvent -> { +                    if (mouseEvent.isPrimaryButtonDown() && mouseEvent.getClickCount() == 2) { +                        chosenEmployees.add(tvEmployees.getSelectionModel().getSelectedItem()); +                        if (chosenEmployees == null) { +                            return; +                        } +                        StringBuilder text = new StringBuilder(); +                        for (Employee employee : chosenEmployees) { +                            text.append(employee.name()).append("\n"); +                        } +                        lEmployees.setText(text.toString()); +                    } +                }); +        ObservableList<Integer> hours = +                FXCollections.observableArrayList( +                        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, +                        21, 22, 23); +        cbStart.setItems(hours); +        cbEnd.setItems(hours); +    } + +    public void cancel() { +        LOG.debug("Cancel Button clicked"); +        ((Stage) lVehicles.getScene().getWindow()).close(); +    } + +    public void create() { +        LOG.debug("Create Button clicked"); + +        List<Registration> registrations = new LinkedList<>(); + +        for (Employee employee : chosenEmployees) { +            registrations.add( +                    Registration.builder() +                            .id(chosenVehicle.id()) +                            .employee(employee) +                            .start( +                                    LocalDateTime.of( +                                                    LocalDate.now(), +                                                    LocalTime.of(cbStart.getValue(), 0)) +                                            .toInstant(OffsetDateTime.now().getOffset())) +                            .end( +                                    LocalDateTime.of( +                                                    LocalDate.now(), +                                                    LocalTime.of(cbEnd.getValue(), 0)) +                                            .toInstant(OffsetDateTime.now().getOffset())) +                            .build()); +        } +        try { +            registrationService.add(chosenVehicle, registrations); +            ((Stage) lVehicles.getScene().getWindow()).close(); +        } catch (InvalidVehicleException e) { +            // NOT THROWN ANYWHERE RIGHT NOW +            LOG.info( +                    "Caught InvalidVehicleException. Showing it to user. Error message: {}", +                    e.getClass().toString(), +                    e.getMessage()); +            Alert alert = new Alert(AlertType.WARNING); +            alert.setTitle("Ungültiges Fahrzeug"); +            alert.setHeaderText("Das spezifizierte Fahrzeug ist nicht gültig."); +            alert.setContentText(e.getMessage()); +            alert.show(); +        } catch (ServiceException e) { +            LOG.warn( +                    "Caught ServiceException while getting vehicles. Showing it to user. Error message: {}", +                    e.getMessage()); +            Alert alert = new Alert(AlertType.ERROR); +            alert.setTitle("Anmeldung - Fehler!"); +            alert.setHeaderText("Beim Erstellen der Anmeldung ist ein Fehler aufgetreten."); +            alert.setContentText(e.getMessage()); +            alert.show(); +        } catch (InvalidRegistrationException e) { +            LOG.info( +                    "Caught InvalidRegistrationException. Showing it to user. Error message: {}", +                    e.getMessage()); +            Alert alert = new Alert(AlertType.WARNING); +            alert.setTitle("Ungültige Eingabe"); +            alert.setHeaderText( +                    "Die gewählte Kombination von Fahrzeug und Personal ist nicht gültig!"); +            alert.setContentText(e.getMessage()); +            alert.show(); +        } +    } +} 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 index 900fd0e..3e4ba12 100644 --- 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 @@ -1,6 +1,7 @@  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; @@ -10,6 +11,7 @@ import java.sql.ResultSet;  import java.sql.SQLException;  import java.sql.Statement;  import java.sql.Timestamp; +import java.util.ArrayList;  import java.util.List;  import org.slf4j.Logger;  import org.slf4j.LoggerFactory; @@ -23,8 +25,12 @@ public class EmployeeDatabaseDao implements EmployeeDAO {              "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 final PreparedStatement insertEmployeeVersion, insertEmployee; +    private final PreparedStatement insertEmployeeVersion, insertEmployee, listEmployee;      public EmployeeDatabaseDao(JDBCConnectionManager connectionManager)              throws PersistenceException { @@ -38,6 +44,8 @@ public class EmployeeDatabaseDao implements EmployeeDAO {              insertEmployee =                      connection.prepareStatement(INSERT_EMPLOYEE, Statement.RETURN_GENERATED_KEYS); +            listEmployee = connection.prepareStatement(LIST_EMPLOYEE); +          } catch (SQLException e) {              throw new PersistenceException(e);          } @@ -82,7 +90,31 @@ public class EmployeeDatabaseDao implements EmployeeDAO {      @Override      public List<Employee> list() throws PersistenceException { -        throw new UnsupportedOperationException(); + +        try { +            ResultSet rs = listEmployee.executeQuery(); + +            List<Employee> employees = new ArrayList<>(); +            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 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/dto/RegistrationValidator.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dto/RegistrationValidator.java new file mode 100644 index 0000000..295b615 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dto/RegistrationValidator.java @@ -0,0 +1,194 @@ +package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto; + +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Employee.EducationLevel; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Vehicle.VehicleType; +import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidRegistrationException; +import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidVehicleException; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RegistrationValidator { + +    private static final Logger LOG = LoggerFactory.getLogger(RegistrationValidator.class); + +    private RegistrationValidator() {} + +    public static void validate(Vehicle vehicle, List<Registration> registrations) +            throws InvalidVehicleException, InvalidRegistrationException { +        /* +        Vehicles and Employees are assumed to be valid. +        They have been checked at creation, and for them to be checked again, access to +        VehicleValidator and EmployeeValidator are needed, which are not available at this time. +         */ +        /* +        The method used here goes as follows: All given employees are inspected in regards to their +        qualifications, and added to the appropriate lists of the roles they could fill. +        For example, an NFS, who is also a driver, would be added to the lists driverIds, nfsIds +        and rsIds (because an NFS can always substitute an RS). +        Afterwards, the number of people is checked according to the chosen vehicle type, and if +        the number is okay, the program tries to find a valid combination of roles for the vehicle. +        For example, for an RTW, first a driver is chosen, their ID marked as found in the aptly +        titled HashMap, and then for the second RS, the list of RS is checked, excluding the chosen +        driver. If no other valid RS is found, the next possible driver is chosen, and so on. If no +        valid combination is found, an InvalidRegistrationException is thrown. +         */ +        List<Long> pilotIds = new LinkedList<>(); +        List<Long> driverIds = new LinkedList<>(); +        List<Long> naIds = new LinkedList<>(); +        List<Long> nfsIds = new LinkedList<>(); +        List<Long> rsIds = new LinkedList<>(); +        HashMap<Long, Boolean> found = +                new HashMap<>(); // needed later in DFS, checks that no person is chosen twice +        int total = 0; +        for (Registration registration : registrations) { +            total++; +            if (found.put(registration.employee().id(), false) != null) { +                LOG.info("Employee with ID {} was added twice", registration.employee().id()); +                throw new InvalidRegistrationException( +                        "Person with the ID: " +                                + registration.employee().id() +                                + " was added more than once!"); +            } +            if (registration.employee().isPilot()) { +                pilotIds.add(registration.employee().id()); +            } +            if (registration.employee().isDriver()) { +                driverIds.add(registration.employee().id()); +            } +            if (registration.employee().educationLevel() == EducationLevel.NA) { +                naIds.add(registration.employee().id()); +                nfsIds.add(registration.employee().id()); +                rsIds.add(registration.employee().id()); +            } else if (isNFS(registration.employee())) { +                nfsIds.add(registration.employee().id()); +                rsIds.add(registration.employee().id()); +            } else { // only RS left +                rsIds.add(registration.employee().id()); +            } +        } +        if (total <= 0) { +            LOG.info("No employees were added"); +            throw new InvalidRegistrationException("Kein Personal ausgewählt!"); +        } +        if (vehicle.type() == VehicleType.NAH) { +            /* +            NAH +            1 Pilot +            1 NFS +            1 NA +            3-4 Personen +             */ +            if (total < 3) { +                LOG.info("Too few employees for NAH"); +                throw new InvalidRegistrationException("Zu wenig Personal für NAH!"); +            } else if (total > 4) { +                LOG.info("Too many employees for NAH"); +                throw new InvalidRegistrationException("Zu viel Personal für NAH!"); +            } +            for (long pilot_id : pilotIds) { +                found.put(pilot_id, true); +                for (long na_id : naIds) { +                    if (found.get(na_id)) continue; +                    found.put(na_id, true); +                    for (long nfs_id : nfsIds) { +                        if (found.get(nfs_id)) continue; +                        LOG.info("Valid combination found for NAH"); +                        return; +                    } +                    found.put(na_id, false); +                } +                found.put(pilot_id, false); +            } +            LOG.info("No valid combination of employees found for NAH"); +            throw new InvalidRegistrationException( +                    "Keine gültige Kombination von Personen für NAH!"); +        } else if (vehicle.type() == VehicleType.NEF) { +            /* +            NEF +            1 Driver (has to be NFS) +            1 NA +             */ +            if (total < 2) { +                LOG.info("Too few employees for NEF"); +                throw new InvalidRegistrationException("Zu wenig Personal für NEF!"); +            } else if (total > 3) { +                LOG.info("Too many employees for NEF"); +                throw new InvalidRegistrationException("Zu viel Personal für NEF!"); +            } +            for (long driver_id : driverIds) { +                if (!nfsIds.contains(driver_id)) +                    continue; // if possible driver is not NFS, skip him +                found.put(driver_id, true); +                for (long na_id : naIds) { +                    if (found.get(na_id)) continue; +                    LOG.info("Valid combinaion found for NEF"); +                    return; +                } +                found.put(driver_id, false); +            } +            LOG.info("No valid combination of employees found for NEF"); +            throw new InvalidRegistrationException( +                    "Keine gültige Kombination von Personen für NEF!"); +        } else if (vehicle.type() == VehicleType.BKTW) { +            /* +            BKTW +            1 Driver +             */ +            if (total > 3) { +                LOG.info("Too many employees for BKTW"); +                throw new InvalidRegistrationException("Zu viel Personal für BKTW!"); +            } +            if (!driverIds.isEmpty()) { +                LOG.info("Valid combination found for BKTW"); +                return; +            } +            LOG.info("No driver was found for BKTW"); +            throw new InvalidRegistrationException("Kein Fahrer gefunden für BKTW!"); +        } else { // KTW or RTW, both have the same requirements +            /* +            RTW/KTW +            1 Driver +            1 RS +             */ +            if (total < 2) { +                LOG.info("Too few employees for {}", vehicle.type().name()); +                throw new InvalidRegistrationException( +                        "Zu wenig Personal für " + vehicle.type().name() + "!"); +            } else if (total > 4) { +                LOG.info("Too many employees for {}", vehicle.type().name()); +                throw new InvalidRegistrationException( +                        "Zu viel Persoanl für " + vehicle.type().name() + "!"); +            } +            for (long driver_id : driverIds) { // driver includes rs +                found.put(driver_id, true); +                for (long rs_id : rsIds) { +                    if (found.get(rs_id)) continue; +                    LOG.info("Valid combination found for {}", vehicle.type().name()); +                    return; +                } +            } +            LOG.info("No valid combination of employees found for {}", vehicle.type().name()); +            throw new InvalidRegistrationException( +                    "Keine gültige Kombination von Personen für " + vehicle.type().name() + "!"); +        } +    } + +    private static boolean isNFS(Employee employee) { +        EducationLevel educationLevel = employee.educationLevel(); +        switch (educationLevel) { +            case NFS: +                return true; +            case NKA: +                return true; +            case NKI: +                return true; +            case NKV: +                return true; +            default: +                return false; +        } +    } +} diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/EmployeeServiceImpl.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/EmployeeServiceImpl.java index 144ccc6..ed0fb1c 100644 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/EmployeeServiceImpl.java +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/EmployeeServiceImpl.java @@ -36,7 +36,12 @@ public class EmployeeServiceImpl implements EmployeeService {      @Override      public List<Employee> list() throws ServiceException { -        return null; + +        try { +            return employeePersistence.list(); +        } catch (PersistenceException e) { +            throw new ServiceException(e); +        }      }      @Override diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/RegistrationService.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/RegistrationService.java index 801148c..c20ed3c 100644 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/RegistrationService.java +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/RegistrationService.java @@ -1,6 +1,7 @@  package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service;  import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Registration; +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.ServiceException; @@ -11,14 +12,14 @@ public interface RegistrationService {      /**       * Register employee to a vehicle.       * -     * @param vehicleId the id of the target vehicle +     * @param vehicle the target vehicle       * @param registrations that should be added to the vehicle -     * @return a list of the ids that were assigned +     * @return the id that was assigned       * @throws InvalidVehicleException if the vehicleId is invalid or does not exist       * @throws InvalidRegistrationException if the registration is invalid       * @throws ServiceException if the registration could not be persisted       */ -    List<Long> add(long vehicleId, List<Registration> registrations) +    List<Long> add(Vehicle vehicle, List<Registration> registrations)              throws InvalidVehicleException, InvalidRegistrationException, ServiceException;      /** 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/VehicleServiceImpl.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/VehicleServiceImpl.java index 4a11298..bbe668b 100644 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/VehicleServiceImpl.java +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/VehicleServiceImpl.java @@ -9,14 +9,16 @@ import at.ac.tuwien.sepm.assignment.groupphase.exception.PersistenceException;  import at.ac.tuwien.sepm.assignment.groupphase.exception.ServiceException;  import java.util.EnumSet;  import java.util.List; +import java.util.stream.Collectors;  import org.springframework.stereotype.Service;  @Service  public class VehicleServiceImpl implements VehicleService { -    private VehicleDAO vehicleDAO; -    public VehicleServiceImpl(VehicleDAO vehicleDAO) { -        this.vehicleDAO = vehicleDAO; +    private VehicleDAO vehiclePersistence; + +    public VehicleServiceImpl(VehicleDAO vehiclePersistence) { +        this.vehiclePersistence = vehiclePersistence;      }      public long add(Vehicle vehicle) throws InvalidVehicleException, ServiceException { @@ -59,7 +61,7 @@ public class VehicleServiceImpl implements VehicleService {                  throw new ServiceException("not a Valid type");          }          try { -            vehicleDAO.add(vehicle); +            vehiclePersistence.add(vehicle);          } catch (PersistenceException e) {              throw new ServiceException(e);          } @@ -70,10 +72,27 @@ public class VehicleServiceImpl implements VehicleService {          throw new UnsupportedOperationException();      } +    @Override      public List<Vehicle> list(EnumSet<Status> statuses) throws ServiceException { -        throw new UnsupportedOperationException(); + +        if (statuses == null) { +            throw new ServiceException("statuses may not be null"); +        } + +        List<Vehicle> vehicles; + +        try { +            vehicles = vehiclePersistence.list(); +        } catch (PersistenceException e) { +            throw new ServiceException(e); +        } + +        return vehicles.stream() +                .filter(vehicle -> statuses.contains(vehicle.status())) +                .collect(Collectors.toList());      } +    @Override      public void remove(long id) throws InvalidVehicleException, ServiceException {          throw new UnsupportedOperationException();      } diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util/JDBCConnectionManager.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util/JDBCConnectionManager.java index 5494471..6eb15ec 100644 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util/JDBCConnectionManager.java +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/util/JDBCConnectionManager.java @@ -12,12 +12,17 @@ import org.springframework.stereotype.Component;  public class JDBCConnectionManager {      private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); -    private static final String CONNECTION_URL = +    private static final String DEFAULT_CONNECTION_URL =              "jdbc:h2:~/sepm;INIT=RUNSCRIPT FROM 'classpath:sql/database.sql'"; - +    private String connectionUrl;      private Connection connection;      public JDBCConnectionManager() { +        this(DEFAULT_CONNECTION_URL); +    } + +    public JDBCConnectionManager(String connectionUrl) { +        this.connectionUrl = connectionUrl;          try {              Class.forName("org.h2.Driver");          } catch (ClassNotFoundException e) { @@ -27,7 +32,7 @@ public class JDBCConnectionManager {      }      public Connection getConnection() throws SQLException { -        if (connection == null) connection = DriverManager.getConnection(CONNECTION_URL); +        if (connection == null) connection = DriverManager.getConnection(connectionUrl);          return connection;      }  | 
