Repository navigation
Expand file tree
/
Copy pathResourceListController.java
More file actions
342 lines (292 loc) · 10.6 KB
/
Copy pathResourceListController.java
File metadata and controls
342 lines (292 loc) · 10.6 KB
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
package com.DesktopApplicationClientJava.controllers;
import com.DesktopApplicationClientJava.controllers.utils.Controller;
import com.DesktopApplicationClientJava.entities.Resource;
import com.DesktopApplicationClientJava.services.filter.ResourceFilter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
public class ResourceListController extends Controller {
private static final DateTimeFormatter DATE_TIME_FORMAT =
DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");
private static final int PAGE_SIZE = 20;
@FXML private Label userInfoLabel;
@FXML private Label errorLabel;
@FXML private Label pageInfoLabel;
@FXML private Label statusLabel;
@FXML private TextField searchField;
@FXML private TextField descriptionField;
@FXML private ComboBox<String> sortCombo;
@FXML private TableView<Resource> resourceTable;
@FXML private TableColumn<Resource, String> titleColumn;
@FXML private TableColumn<Resource, String> descriptionColumn;
@FXML private TableColumn<Resource, String> createdColumn;
@FXML private TableColumn<Resource, String> updatedColumn;
@FXML private Button createButton;
@FXML private Button deleteButton;
@FXML private Button prevButton;
@FXML private Button nextButton;
@FXML private Button logoutButton;
@FXML private Button usersButton;
private final ObservableList<Resource> rows = FXCollections.observableArrayList();
private long offset = 0;
private boolean canModify = false;
@Override
protected Label getErrorLabel() {
return errorLabel;
}
// ===== Lifecycle =====
@FXML
private void initialize() {
hideError();
setupUser();
setupSort();
setupTable();
setupRbac();
searchField.setOnAction(e -> reloadFromStart());
descriptionField.setOnAction(e -> reloadFromStart());
sortCombo.setOnAction(e -> reloadFromStart());
resourceTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY_FLEX_LAST_COLUMN);
if (services != null && services.getResourceService() != null) {
reloadFromStart();
}
}
// ===== UI setup =====
private void setupUser() {
// NB: services/getAuthService may be a partial mock in tests — stay null-safe.
var authService = services == null ? null : services.getAuthService();
var me = authService == null ? null : authService.getCurrentUser();
userInfoLabel.setText(
me == null ? "" : me.getFirstName() + " " + me.getLastName() + " · " + me.getRole());
}
private void setupSort() {
sortCombo
.getItems()
.setAll("Сначала новые", "Сначала старые", "По названию (А-Я)", "По названию (Я-А)");
sortCombo.getSelectionModel().selectFirst();
}
private void setupTable() {
titleColumn.setCellValueFactory(new PropertyValueFactory<>("title"));
descriptionColumn.setCellValueFactory(new PropertyValueFactory<>("description"));
createdColumn.setCellValueFactory(
c -> new SimpleStringProperty(formatDate(c.getValue().getCreatedAt())));
updatedColumn.setCellValueFactory(
c -> new SimpleStringProperty(formatDate(c.getValue().getUpdatedAt())));
resourceTable.setItems(rows);
resourceTable.setPlaceholder(new Label("Документов пока нет"));
resourceTable
.getSelectionModel()
.selectedItemProperty()
.addListener((obs, oldSel, newSel) -> updateDeleteButtonState());
resourceTable.setRowFactory(
tv -> {
var row = new TableRow<Resource>();
row.setOnMouseClicked(
e -> {
if (e.getClickCount() == 2 && !row.isEmpty()) {
openDetails(row.getItem());
}
});
return row;
});
}
private void setupRbac() {
var authService = services == null ? null : services.getAuthService();
canModify = authService != null && (authService.isAdmin() || authService.isModerator());
createButton.setVisible(canModify);
createButton.setManaged(canModify);
deleteButton.setVisible(canModify);
deleteButton.setManaged(canModify);
updateDeleteButtonState();
// Кнопка «Пользователи» доступна только администраторам.
boolean isAdmin = authService != null && authService.isAdmin();
usersButton.setVisible(isAdmin);
usersButton.setManaged(isAdmin);
}
private void updateDeleteButtonState() {
boolean hasSelection =
resourceTable != null && resourceTable.getSelectionModel().getSelectedItem() != null;
deleteButton.setDisable(!(canModify && hasSelection));
}
@FXML
private void onOpenUsers(ActionEvent e) {
var authService = services == null ? null : services.getAuthService();
if (authService == null || !authService.isAdmin()) {
return;
}
navigator.goTo("/fxml/UserList.fxml");
}
// ===== Data loading =====
private void reloadFromStart() {
offset = 0;
loadAsync();
}
private void loadAsync() {
hideError();
statusLabel.setText("Загрузка…");
var filter = buildFilter();
var sort = resolveSort();
long currentOffset = offset;
var task =
new Task<List<Resource>>() {
@Override
protected List<Resource> call() {
return services
.getResourceService()
.search(filter, currentOffset, PAGE_SIZE, sort.sortBy(), sort.sortDir());
}
};
task.setOnSucceeded(
e -> {
var page = task.getValue();
rows.setAll(page);
updatePaginationUi(page.size());
statusLabel.setText("");
});
task.setOnFailed(
e -> {
var ex = task.getException();
showError(ex, "Не удалось загрузить список");
rows.clear();
updatePaginationUi(0);
statusLabel.setText("");
});
var thread = new Thread(task, "resource-list-load");
thread.setDaemon(true);
thread.start();
}
private record SortSpec(String sortBy, String sortDir) {}
private ResourceFilter buildFilter() {
return new ResourceFilter(normalizeQuery(searchField), normalizeQuery(descriptionField));
}
private static String normalizeQuery(TextField field) {
if (field == null || field.getText() == null || field.getText().isBlank()) {
return null;
}
return field.getText().trim();
}
private SortSpec resolveSort() {
String choice = sortCombo.getValue();
if (choice == null) return new SortSpec("createdAt", "desc");
return switch (choice) {
case "Сначала старые" -> new SortSpec("createdAt", "asc");
case "По названию (А-Я)" -> new SortSpec("title", "asc");
case "По названию (Я-А)" -> new SortSpec("title", "desc");
default -> new SortSpec("createdAt", "desc");
};
}
private void updatePaginationUi(int pageSize) {
long from = rows.isEmpty() ? 0 : offset + 1;
long to = offset + pageSize;
pageInfoLabel.setText("Показано " + from + "–" + to);
prevButton.setDisable(offset == 0);
nextButton.setDisable(pageSize < PAGE_SIZE);
}
// ===== Event handlers =====
@FXML
private void onCreate(ActionEvent e) {
navigator.goTo("/fxml/ResourceForm.fxml");
}
@FXML
void onDelete(ActionEvent e) {
var selected = resourceTable.getSelectionModel().getSelectedItem();
if (selected == null) {
return;
}
if (!confirmDelete(selected)) {
return;
}
deleteAsync(selected);
}
private void deleteAsync(Resource resource) {
hideError();
statusLabel.setText("Удаление…");
var task =
new Task<Void>() {
@Override
protected Void call() {
services.getResourceService().delete(resource.getUuid());
return null;
}
};
task.setOnSucceeded(e -> loadAsync());
task.setOnFailed(
e -> {
var ex = task.getException();
showError(ex, "Не удалось удалить ресурс");
statusLabel.setText("");
});
var thread = new Thread(task, "resource-list-delete");
thread.setDaemon(true);
thread.start();
}
/**
* Диалог подтверждения удаления. Выделен в метод ради тестов: headless {@code Alert} блокирует
* поток, поэтому тесты переопределяют его без UI.
*/
protected boolean confirmDelete(Resource resource) {
var alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Удаление ресурса");
alert.setHeaderText("Удалить ресурс?");
alert.setContentText(
"«" + resource.getTitle() + "» будет удалён без возможности восстановления.");
return alert.showAndWait().map(button -> button == ButtonType.OK).orElse(false);
}
@FXML
private void onLogout(ActionEvent e) {
try {
services.getAuthService().logout();
} catch (RuntimeException ignored) {
// Выход съедает ошибку сервиса без мелькания: редирект ниже выполняется в любом случае.
} finally {
if (session != null) {
session.clear();
}
navigator.goToLoginOnce();
}
}
@FXML
private void onSearch(ActionEvent e) {
offset = 0;
loadAsync();
}
@FXML
private void onSort(ActionEvent e) {
offset = 0;
loadAsync();
}
@FXML
private void onNext(ActionEvent e) {
offset += PAGE_SIZE;
loadAsync();
}
@FXML
private void onPrev(ActionEvent e) {
offset = Math.max(0, offset - PAGE_SIZE);
loadAsync();
}
private void openDetails(Resource resource) {
navigator.goTo(
"/fxml/ResourceDetails.fxml",
c -> ((ResourceDetailsController) c).setResourceId(resource.getUuid()));
}
// ===== helpers =====
private static String formatDate(LocalDateTime value) {
return value == null ? "" : DATE_TIME_FORMAT.format(value);
}
}