aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/controller/DetailArchiveOperationController.java
blob: a866653edd43f66af38b66f1ee5ef20391ae44db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.controller;

import static at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.controller.Helper.showServiceExceptionAlertAndWait;

import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Employee;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Operation;
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.util.SpringFXMLLoader;
import at.ac.tuwien.sepm.assignment.groupphase.util.SpringFXMLLoader.FXMLWrapper;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.layout.VBox;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

@Controller
public class DetailArchiveOperationController {
    private static final Logger LOG =
            LoggerFactory.getLogger(DetailArchiveOperationController.class);

    @FXML private VBox vBoxVehicle;
    @FXML private VBox vBoxPeople;
    private final SpringFXMLLoader fxmlLoader;

    public DetailArchiveOperationController(SpringFXMLLoader fxmlLoader) {
        this.fxmlLoader = fxmlLoader;
    }

    static DetailArchiveOperationController create(SpringFXMLLoader fxmlLoader) throws IOException {
        FXMLWrapper<Object, DetailArchiveOperationController> wrapper =
                fxmlLoader.loadAndWrap(
                        "/fxml/DetailArchiveOperation.fxml",
                        DetailArchiveOperationController.class);

        Node root = (Node) wrapper.getLoadedObject();
        DetailArchiveOperationController result = wrapper.getController();
        result.rootElement = root;

        return result;
    }

    public Node getRoot() {
        return rootElement;
    }

    private Node rootElement;

    public void set(Vehicle vehicle, Operation operation) {
        VehiclePaneController controller;
        try {
            controller = VehiclePaneController.createVehiclePane();
            controller.setData(vehicle, false, false);
            vBoxVehicle.getChildren().add(controller.getRootElement());
        } catch (IOException e) {
            LOG.error("IOException in set(Vehicle). (vBoxVehicle) ", e);
            showServiceExceptionAlertAndWait(
                    "Ein interner Fehler ist aufgetreten. Bitte wenden Sie sich an den/die SystemadministratorIn.");
        }
        try {
            List<Registration> registrations =
                    Objects.requireNonNull(vehicle.registrations())
                            .stream()
                            .filter(
                                    registration ->
                                            registration
                                                            .start()
                                                            .isBefore(
                                                                    Objects.requireNonNull(
                                                                            operation.created()))
                                                    && registration
                                                            .end()
                                                            .isAfter(
                                                                    Objects.requireNonNull(
                                                                            operation.created())))
                            .collect(Collectors.toList());

            for (Registration registration : registrations) {
                Employee employee = registration.employee();
                EmployeeListItemController employeeListItemController =
                        EmployeeListItemController.createEmployeeListItemController(
                                fxmlLoader, employee);
                vBoxPeople.getChildren().add(employeeListItemController.getRootElement());
            }
        } catch (IOException e) {
            LOG.error("IOException in set(Vehicle). (vBoxPeople) ", e);
            showServiceExceptionAlertAndWait(
                    "Ein interner Fehler ist aufgetreten. Bitte wenden Sie sich an den/die SystemadministratorIn.");
        }
    }
}