aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/controller/RegistrationWindowController.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/controller/RegistrationWindowController.java')
-rw-r--r--src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/controller/RegistrationWindowController.java289
1 files changed, 289 insertions, 0 deletions
diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/controller/RegistrationWindowController.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/controller/RegistrationWindowController.java
new file mode 100644
index 0000000..c445a12
--- /dev/null
+++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/controller/RegistrationWindowController.java
@@ -0,0 +1,289 @@
+package at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.controller;
+
+import static at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.controller.Helper.showServiceExceptionAlertAndWait;
+import static at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.controller.Helper.showValidationErrorAlertAndWait;
+
+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 at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Employee;
+import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Registration;
+import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Vehicle;
+import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Vehicle.Status;
+import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.service.EmployeeService;
+import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.service.RegistrationService;
+import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.service.VehicleService;
+import at.ac.tuwien.sepm.assignment.groupphase.util.SpringFXMLLoader;
+import java.io.IOException;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.OffsetDateTime;
+import java.util.EnumSet;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+import javafx.collections.FXCollections;
+import javafx.collections.ObservableList;
+import javafx.fxml.FXML;
+import javafx.geometry.Insets;
+import javafx.scene.Node;
+import javafx.scene.control.ChoiceBox;
+import javafx.scene.control.Label;
+import javafx.scene.control.ScrollPane;
+import javafx.scene.control.TextField;
+import javafx.scene.input.KeyEvent;
+import javafx.scene.input.MouseButton;
+import javafx.scene.layout.GridPane;
+import javafx.scene.layout.VBox;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Controller;
+
+@Controller
+public class RegistrationWindowController {
+
+ private static final Logger LOG = LoggerFactory.getLogger(RegistrationWindowController.class);
+
+ private final EmployeeService employeeService;
+ private final VehicleService vehicleService;
+ private final RegistrationService registrationService;
+ private final CreateOperationController createOperationController;
+ private final SpringFXMLLoader fxmlLoader;
+
+ @FXML private GridPane root;
+ @FXML private VBox vbVehicles;
+ @FXML private ScrollPane listEmployee;
+ @FXML private ChoiceBox<Integer> cbStart;
+ @FXML private ChoiceBox<Integer> cbEnd;
+ @FXML private Label lVehicles;
+ @FXML private Label lEmployees;
+ @FXML private TextField tfVehicleSearch;
+ @FXML private TextField tfEmployeeSearch;
+ private EmployeeListController employeeListController;
+
+ private Vehicle chosenVehicle;
+ private List<Employee> chosenEmployees = new LinkedList<>();
+
+ public RegistrationWindowController(
+ EmployeeService employeeService,
+ VehicleService vehicleService,
+ CreateOperationController createOperationController,
+ RegistrationService registrationService,
+ SpringFXMLLoader fxmlLoader) {
+ this.employeeService = employeeService;
+ this.vehicleService = vehicleService;
+ this.createOperationController = createOperationController;
+ this.registrationService = registrationService;
+ this.fxmlLoader = fxmlLoader;
+ }
+
+ @FXML
+ private void initialize() throws IOException {
+ employeeListController = EmployeeListController.createEmployeeListController(fxmlLoader);
+ employeeListController.setListItemMargins(new Insets(10, 6, 0, 6));
+ // listEmployee. .getChildren().add(employeeListController.getRootElement());
+ Node emplList = employeeListController.getRootElement();
+ // emplList.(360);
+ listEmployee.setContent(emplList);
+
+ 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);
+ setDefaultTime();
+ // reset();
+ }
+
+ private void setDefaultTime() {
+ cbStart.setValue(LocalDateTime.now().getHour());
+ cbEnd.setValue((LocalDateTime.now().getHour() + 4) % 24);
+ }
+
+ private void updateEmplList() {
+ employeeListController.deselectAllEmployees();
+
+ try {
+ Set<Employee> employees =
+ employeeService
+ .list()
+ .stream()
+ .filter(
+ e ->
+ e.name()
+ .toLowerCase()
+ .contains(
+ tfEmployeeSearch
+ .getText()
+ .toLowerCase()))
+ .collect(Collectors.toCollection(HashSet::new));
+ employeeListController.setData(
+ employees,
+ selection -> {
+ if (selection == null) {
+ return;
+ } else if (chosenEmployees.contains(selection)) {
+ chosenEmployees.remove(selection);
+ } else {
+ chosenEmployees.add(selection);
+ }
+
+ StringBuilder text = new StringBuilder();
+ boolean first = true;
+ for (Employee employee : chosenEmployees) {
+ if (!first) {
+ text.append(", ");
+ }
+ text.append(employee.name());
+ first = false;
+ }
+ lEmployees.setText(text.toString());
+ },
+ contr -> contr.setSelected(chosenEmployees.contains(contr.getEmployee())));
+
+ employees.forEach(
+ e -> {
+ if (chosenEmployees.contains(e)) employeeListController.selectEmployee(e);
+ });
+ } catch (ServiceException e) {
+ LOG.error("ServiceException in updateEmplList(). ", e);
+ showServiceExceptionAlertAndWait(
+ "Beim Auflisten des Personals ist ein Fehler aufgetreten.");
+ }
+ }
+
+ private void updateVehList() {
+ vbVehicles.getChildren().clear();
+
+ try {
+ Set<Vehicle> vehicles = vehicleService.list(EnumSet.of(Status.ABGEMELDET));
+
+ boolean anyMatch = false;
+
+ for (Vehicle vehicle : vehicles) {
+ if (!vehicle.name().toLowerCase().contains(tfVehicleSearch.getText().toLowerCase()))
+ continue;
+
+ anyMatch = true;
+
+ VehiclePaneController vp = VehiclePaneController.createVehiclePane();
+ vp.setData(vehicle, false, false);
+ vbVehicles.getChildren().add(vp.getRootElement());
+
+ vp.getRootElement()
+ .setOnMouseClicked(
+ event -> {
+ if (event.getButton() == MouseButton.PRIMARY) {
+ chosenVehicle = vehicle;
+ lVehicles.setText(chosenVehicle.name());
+ updateVehList();
+ }
+ });
+ if (chosenVehicle != null && chosenVehicle.id() == vehicle.id())
+ vp.setSelected(true);
+ }
+
+ if (!anyMatch) {
+ // Kind of ugly, but best way to get the size of a VehiclePane
+ VehiclePaneController vp = VehiclePaneController.createVehiclePane();
+ vp.getRootElement().setVisible(false);
+ vbVehicles.getChildren().add(vp.getRootElement());
+ }
+ } catch (ServiceException e) {
+ LOG.error("ServiceException in updateVehList(). ", e);
+ showServiceExceptionAlertAndWait(
+ "Beim Auflisten der Fahrzeuge ist ein Fehler aufgetreten");
+ } catch (IOException e) {
+ LOG.error("IOException in updateVehList(). ", e);
+ showServiceExceptionAlertAndWait("Beim Laden der Fahrzeuge ist ein Fehler aufgetreten");
+ }
+ }
+
+ public void cancel() {
+ LOG.debug("Hyperlink \"schließen\" clicked");
+ this.setVisible(false);
+ createOperationController.setVisible(true);
+ }
+
+ private void reset() {
+ chosenEmployees.clear();
+ chosenVehicle = null;
+ tfEmployeeSearch.setText("");
+ tfVehicleSearch.setText("");
+ lEmployees.setText("-");
+ lVehicles.setText("-");
+ updateVehList();
+ updateEmplList();
+ setDefaultTime();
+ }
+
+ public void create() {
+ LOG.debug("Button \"ERSTELLEN\" clicked");
+
+ Set<Registration> registrations = new HashSet<>();
+ try {
+ if (chosenVehicle == null) {
+ throw new InvalidVehicleException("no Vehicle");
+ }
+
+ LocalDateTime startDate =
+ LocalDateTime.of(
+ LocalDate.now(),
+ LocalTime.of(
+ cbStart.getValue(),
+ LocalDateTime.now().getMinute(),
+ LocalDateTime.now().getSecond()));
+
+ LocalDateTime endDate =
+ LocalDateTime.of(
+ LocalDate.now()
+ .plusDays(cbStart.getValue() >= cbEnd.getValue() ? 1 : 0),
+ LocalTime.of(cbEnd.getValue(), 0));
+
+ for (Employee employee : chosenEmployees) {
+ registrations.add(
+ Registration.builder()
+ .id(chosenVehicle.id())
+ .employee(employee)
+ .start(startDate.toInstant(OffsetDateTime.now().getOffset()))
+ .end(endDate.toInstant(OffsetDateTime.now().getOffset()))
+ .build());
+ }
+
+ registrationService.add(chosenVehicle.id(), registrations);
+ chosenEmployees.clear();
+ // ((Stage) lVehicles.getScene().getWindow()).close();
+ this.setVisible(false);
+ createOperationController.setVisible(true);
+ createOperationController.updateList();
+ // reset();
+ } catch (InvalidVehicleException e) {
+ LOG.debug("Validation of Vehicle in Registration failed.");
+ showValidationErrorAlertAndWait(e.getMessage());
+ } catch (ServiceException e) {
+ LOG.error("ServiceException in create(). ", e);
+ showServiceExceptionAlertAndWait(
+ "Beim Erstellen der Anmeldung ist ein Fehler aufgetreten.");
+ } catch (InvalidRegistrationException e) {
+ LOG.debug("Validation of Registration failed.");
+ showValidationErrorAlertAndWait(e.getMessage());
+ }
+ }
+
+ public void setVisible(boolean b) {
+ if (b) reset();
+ root.setVisible(b);
+ }
+
+ public void tfVehicleSearch_TextChanged(KeyEvent keyEvent) {
+ updateVehList();
+ }
+
+ public void tfEmployeeSearch_TextChanged(KeyEvent keyEvent) {
+ updateEmplList();
+ }
+}