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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
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.showSuccessAlertAndWait;
import static at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.controller.Helper.showValidationErrorAlertAndWait;
import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidOperationException;
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.Operation;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Operation.Status;
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.service.OperationService;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.service.RegistrationService;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.service.VehicleService;
import java.io.IOException;
import java.util.EnumSet;
import java.util.LinkedList;
import java.util.Set;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
@Controller
public class CreateOperationController {
private static final Logger LOG = LoggerFactory.getLogger(CreateOperationController.class);
public AnchorPane apCreateOperation;
@FXML private GridPane grdWindowContainer;
@FXML private TextField txtCode;
@FXML private TextField txtAddress;
@FXML private TextField txtNote;
@FXML private Button btnCreateOperation;
@FXML private ListView<Vehicle> lvVehicles;
@FXML private ListView<Operation> lvActiveOperations;
@FXML private Label lblChosenVehicles;
@FXML private AnchorPane apInvisible;
@FXML private OperationDetailsController operationDetailsController;
@FXML private ManageEmployeesController manageEmployeesController;
@FXML private CreateCarController createCarController;
@FXML private RegistrationWindowController registrationWindowController;
@FXML private ArchiveOperationController archiveOperationController;
@FXML private FlowPane fpVehicles;
private LinkedList<Vehicle> chosenVehicles = new LinkedList<>();
private final OperationService operationService;
private final VehicleService vehicleService;
private final RegistrationService registrationService;
public CreateOperationController(
OperationService operationService,
VehicleService vehicleService,
RegistrationService registrationService) {
this.operationService = operationService;
this.vehicleService = vehicleService;
this.registrationService = registrationService;
}
@FXML
private void initialize() {
lblChosenVehicles.setText("keine ausgewählt");
lvActiveOperations.setCellFactory(param -> generateOpCodeListItem());
lvActiveOperations.setOnMouseClicked(
event -> {
if (event.getClickCount() == 2) {
if (lvActiveOperations.getSelectionModel().getSelectedItem() == null) {
return;
}
openDetailsWindow(lvActiveOperations.getSelectionModel().getSelectedItem());
}
});
setVisible(true);
createCarController.setVisible(false);
registrationWindowController.setVisible(false);
updateList();
}
public void updateList() {
try {
fpVehicles.getChildren().clear();
// TODO: this should probably be handled differently
Set<Vehicle> vehicles;
if (txtCode.getText().isEmpty()) {
vehicles =
vehicleService.list(
EnumSet.complementOf(EnumSet.of(Vehicle.Status.ABGEMELDET)));
} else {
vehicles = operationService.rankVehicles(txtCode.getText());
}
for (Vehicle vehicle : vehicles) {
VehiclePaneController controller = VehiclePaneController.createVehiclePane();
controller.setData(vehicle, true, false);
controller
.getRootElement()
.setOnMouseClicked(
event -> {
if (event.getButton().equals(MouseButton.SECONDARY)) {
createContextMenu(
vehicle,
vehicleService,
registrationService)
.show(
controller.getRootElement(),
event.getScreenX(),
event.getScreenY());
} else {
if (chosenVehicles.contains(vehicle)) {
chosenVehicles.remove(vehicle);
controller.setSelected(false);
} else {
chosenVehicles.add(vehicle);
controller.setSelected(true);
}
StringBuilder result = new StringBuilder();
for (int i = 0; i < chosenVehicles.size(); i++) {
if (i == chosenVehicles.size() - 1) {
result.append(chosenVehicles.get(i).name());
} else {
result.append(chosenVehicles.get(i).name())
.append(", ");
}
}
if (result.toString().equals("")) {
lblChosenVehicles.setText("keine ausgewählt");
} else {
lblChosenVehicles.setText(result.toString());
}
}
});
if (chosenVehicles.stream().anyMatch(v -> v.id() == vehicle.id()))
controller.setSelected(true);
fpVehicles.getChildren().add(controller.getRootElement());
}
} catch (ServiceException | IOException e) {
LOG.error("Exception in updateList(). ", e);
showServiceExceptionAlertAndWait(
"Beim Erstellen des Ranking ist ein Fehler aufgetreten.");
} catch (InvalidOperationException e) {
LOG.debug("Validation error in updateList(). ", e);
showValidationErrorAlertAndWait(e.getMessage());
}
try {
lvActiveOperations.setItems(
FXCollections.observableArrayList(
operationService.list(EnumSet.of(Status.ACTIVE))));
} catch (ServiceException e) {
LOG.error("ServiceException in updateList(). ", e);
showServiceExceptionAlertAndWait(
"Beim Holen der aktiven Einsätze ist ein Fehler aufgetreten");
}
}
private ContextMenu createContextMenu(
Vehicle data, VehicleService vehicleService, RegistrationService registrationService) {
ContextMenu menu = new ContextMenu();
for (Vehicle.Status status : Vehicle.Status.values()) {
if (status == Vehicle.Status.ABGEMELDET) continue;
MenuItem mi = new MenuItem(status.name());
if (status == Vehicle.Status.FREI_FUNK || status == Vehicle.Status.FREI_WACHE) {
mi.getStyleClass().add("mi-free");
} else {
mi.getStyleClass().add("mi-other");
}
mi.setOnAction(
event -> {
try {
vehicleService.update(data.toBuilder().status(status).build());
this.updateList();
} catch (InvalidVehicleException e) {
LOG.debug(
"Validation error in createContextMenu(). (mi.setOnAction) ",
e);
showValidationErrorAlertAndWait(e.getMessage());
} catch (ServiceException e) {
LOG.error("Exception in createContextMenu(). (mi.setOnAction) ", e);
showServiceExceptionAlertAndWait(
"Beim Aktualisieren der Fahrzeuge ist ein Fehler aufgetreten.");
}
});
menu.getItems().add(mi);
}
MenuItem abmelden = new MenuItem("abmelden");
abmelden.setOnAction(
event -> {
try {
if (data.registrations() == null) return;
for (Registration registration : data.registrations())
registrationService.remove(registration.id());
vehicleService.update(
data.toBuilder().status(Vehicle.Status.ABGEMELDET).build());
this.updateList();
} catch (InvalidVehicleException | InvalidRegistrationException e) {
LOG.debug(
"Validation error in createContextMenu(). (abmelden.setOnAction) ",
e);
showValidationErrorAlertAndWait(e.getMessage());
} catch (ServiceException e) {
LOG.error("Exception in createContextMenu(). (abmelden.setOnAction) ", e);
showServiceExceptionAlertAndWait(
"Beim Aktualisieren der Fahrzeuge ist ein Fehler aufgetreten.");
}
});
menu.getItems().add(abmelden);
return menu;
}
@FXML
protected void createOperationClicked() {
LOG.debug("Button \"Erstellen\" clicked.");
Vehicle[] vehicles = new Vehicle[chosenVehicles.size()];
for (int i = 0; i < chosenVehicles.size(); i++) {
vehicles[i] = chosenVehicles.get(i);
}
Operation operation =
Operation.builder()
.additionalInfo(txtNote.getText())
.destination(txtAddress.getText())
.opCode(txtCode.getText())
.status(Status.ACTIVE)
.vehicles(Set.of(vehicles))
.build();
try {
operationService.add(operation);
} catch (ServiceException e) {
LOG.error("Exception in createOperationClicked(). ", e);
showServiceExceptionAlertAndWait(
"Beim Erstellen des Einsatzes ist ein Fehler aufgetreten.");
return;
} catch (InvalidOperationException e) {
LOG.debug("Validation error in createOperationClicked(). ", e);
showValidationErrorAlertAndWait(e.getMessage());
return;
}
showSuccessAlertAndWait("Der Einsatz wurde erfolgreich gespeichert.");
updateList();
lblChosenVehicles.setText("keine ausgewählt");
txtAddress.setText("");
txtCode.setText("");
txtNote.setText("");
chosenVehicles = new LinkedList<>();
}
@FXML
private void onRegistrationLinkClicked() {
LOG.debug("Hyperlink \"Anmeldungen\" clicked.");
openRegistrationWindow();
}
@FXML
private void onEmployeeLinkClicked() {
LOG.debug("Hyperlink \"Personen\" clicked.");
openCreateNewEmployeeWindow();
}
@FXML
private void onVehicleLinkClicked() {
LOG.debug("Hyperlink \"Fahrzeuge\" clicked.");
openCreateCarWindow();
}
@FXML
private void onArchivLinkClicked() {
LOG.debug("Hyperlink \"Archiv\" clicked.");
archiveOperationController.update();
openArchivWindow();
}
private void openArchivWindow() {
archiveOperationController.setVisible(true);
this.setVisible(false);
}
void setVisible(boolean b) {
apInvisible.setVisible(!b);
grdWindowContainer.setVisible(!b);
// if (b) updateList();
}
private void openDetailsWindow(Operation operation) {
operationDetailsController.initOperation(operation);
this.setVisible(false);
}
private void openCreateNewEmployeeWindow() {
this.setVisible(false);
manageEmployeesController.setVisible(true);
}
private void openCreateCarWindow() {
this.setVisible(false);
createCarController.setVisible(true);
createCarController.updateVehiclePane();
}
private void openRegistrationWindow() {
this.setVisible(false);
registrationWindowController.setVisible(true);
}
@FXML
private void onOperationCodeChanged(KeyEvent keyEvent) {
if (keyEvent.getCode() == KeyCode.ENTER) {
updateList();
}
}
static ListCell<Operation> generateOpCodeListItem() {
return new ListCell<>() {
@Override
protected void updateItem(Operation item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null || item.opCode() == null) {
setText(null);
} else {
setText(item.opCode());
}
}
};
}
}
|