aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/controller/RegistrationWindowController.java
blob: c445a12a775ebbe23ce285c20bd6c476eb10bddb (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
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();
    }
}