-
Notifications
You must be signed in to change notification settings - Fork 188
/
TaskTablePanel.java
1009 lines (851 loc) · 46.2 KB
/
TaskTablePanel.java
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2010-2019 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.web.page.admin.server;
import com.evolveum.midpoint.gui.api.GuiStyleConstants;
import com.evolveum.midpoint.gui.api.component.MainObjectListPanel;
import com.evolveum.midpoint.gui.api.model.ReadOnlyModel;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.model.api.ModelPublicConstants;
import com.evolveum.midpoint.model.api.TaskService;
import com.evolveum.midpoint.prism.xml.XmlTypeConverter;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.SelectorOptions;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.result.OperationResultStatus;
import com.evolveum.midpoint.schema.util.TaskTypeUtil;
import com.evolveum.midpoint.schema.util.TaskWorkStateTypeUtil;
import com.evolveum.midpoint.security.api.AuthorizationConstants;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.task.api.TaskManager;
import com.evolveum.midpoint.util.exception.*;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.application.AuthorizationAction;
import com.evolveum.midpoint.web.application.PageDescriptor;
import com.evolveum.midpoint.web.application.Url;
import com.evolveum.midpoint.web.component.AjaxIconButton;
import com.evolveum.midpoint.web.component.data.column.*;
import com.evolveum.midpoint.web.component.menu.cog.ButtonInlineMenuItem;
import com.evolveum.midpoint.web.component.menu.cog.InlineMenuItem;
import com.evolveum.midpoint.web.component.menu.cog.InlineMenuItemAction;
import com.evolveum.midpoint.web.component.util.SelectableBean;
import com.evolveum.midpoint.web.component.util.SelectableBeanImpl;
import com.evolveum.midpoint.web.page.admin.server.dto.OperationResultStatusPresentationProperties;
import com.evolveum.midpoint.web.session.UserProfileStorage;
import com.evolveum.midpoint.web.util.OnePageParameterEncoder;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.export.AbstractExportableColumn;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.jetbrains.annotations.NotNull;
import javax.xml.datatype.XMLGregorianCalendar;
import java.util.*;
import java.util.stream.Collectors;
@PageDescriptor(
urls = {
@Url(mountUrl = "/admin/tasks2", matchUrlForSecurity = "/admin/tasks2")
},
action = {
@AuthorizationAction(actionUri = PageAdminTasks.AUTHORIZATION_TASKS_ALL,
label = PageAdminTasks.AUTH_TASKS_ALL_LABEL,
description = PageAdminTasks.AUTH_TASKS_ALL_DESCRIPTION),
@AuthorizationAction(actionUri = AuthorizationConstants.AUTZ_UI_TASKS_URL,
label = "PageTasks.auth.tasks.label",
description = "PageTasks.auth.tasks.description")})
public class TaskTablePanel extends MainObjectListPanel<TaskType> {
private static final Trace LOGGER = TraceManager.getTrace(TaskTablePanel.class);
private static final String DOT_CLASS = TaskTablePanel.class.getName() + ".";
public static final String OPERATION_SUSPEND_TASKS = DOT_CLASS + "suspendTasks";
public static final String OPERATION_SUSPEND_TASK = DOT_CLASS + "suspendTask";
public static final String OPERATION_RESUME_TASKS = DOT_CLASS + "resumeTasks";
public static final String OPERATION_RESUME_TASK = DOT_CLASS + "resumeTask";
public static final String OPERATION_DELETE_TASKS = DOT_CLASS + "deleteTasks";
public static final String OPERATION_RECONCILE_WORKERS = DOT_CLASS + "reconcileWorkers";
public static final String OPERATION_DELETE_WORKERS_AND_WORK_STATE = DOT_CLASS + "deleteWorkersAndWorkState";
public static final String OPERATION_DELETE_WORK_STATE = DOT_CLASS + "deleteWorkState";
public static final String OPERATION_DELETE_ALL_CLOSED_TASKS = DOT_CLASS + "deleteAllClosedTasks";
public static final String OPERATION_SCHEDULE_TASKS = DOT_CLASS + "scheduleTasks";
private static final String OPERATION_SYNCHRONIZE_TASKS = DOT_CLASS + "synchronizeTasks";
public static final long WAIT_FOR_TASK_STOP = 2000L;
public TaskTablePanel(String id, UserProfileStorage.TableId tableId, Collection<SelectorOptions<GetOperationOptions>> options) {
super(id, TaskType.class, tableId, options);
}
@Override
protected void objectDetailsPerformed(AjaxRequestTarget target, TaskType object) {
taskDetailsPerformed(target, object.getOid());
}
@Override
protected boolean isObjectDetailsEnabled(IModel<SelectableBean<TaskType>> rowModel) {
return rowModel.getObject().getValue().getOid() != null;
}
@Override
protected List<IColumn<SelectableBean<TaskType>, String>> createColumns() {
return initTaskColumns();
}
@Override
protected List<InlineMenuItem> createInlineMenu() {
return createTasksInlineMenu(true);
}
@Override
protected List<Component> createToolbarButtonsList(String buttonId) {
List<Component> buttonsList = super.createToolbarButtonsList(buttonId);
AjaxIconButton synchronizeTasks = createSynchronizeTasksButton(buttonId);
buttonsList.add(synchronizeTasks);
return buttonsList;
}
private AjaxIconButton createSynchronizeTasksButton(String buttonId) {
AjaxIconButton synchronize = new AjaxIconButton(buttonId, new Model<>(GuiStyleConstants.CLASS_RECONCILE_MENU_ITEM),
createStringResource("pageTasks.button.synchronizeTasks")) {
@Override
public void onClick(AjaxRequestTarget target) {
synchronizeTasksPerformed(target);
}
};
synchronize.add(AttributeAppender.append("class", "btn btn-default btn-margin-left btn-sm"));
return synchronize;
}
private Task createSimpleTask(String taskName) {
return getPageBase().createSimpleTask(taskName);
}
private TaskService getTaskService() {
return getPageBase().getTaskService();
}
private TaskManager getTaskManager() {
return getPageBase().getTaskManager();
}
private void showResult(OperationResult result) {
getPageBase().showResult(result);
}
private WebMarkupContainer getFeedbackPanel() {
return getPageBase().getFeedbackPanel();
}
private void navigateToNext(Class<? extends WebPage> page, PageParameters parameters) {
getPageBase().navigateToNext(page, parameters);
}
private void synchronizeTasksPerformed(AjaxRequestTarget target) {
Task opTask = createSimpleTask(OPERATION_SYNCHRONIZE_TASKS);
OperationResult result = opTask.getResult();
try {
getTaskService().synchronizeTasks(opTask, result);
result.computeStatus();
if (result.isSuccess()) { // brutal hack - the subresult's message
// contains statistics
result.recordStatus(OperationResultStatus.SUCCESS, result.getLastSubresult().getMessage());
}
} catch (RuntimeException | SchemaException | SecurityViolationException | ExpressionEvaluationException
| ObjectNotFoundException | CommunicationException | ConfigurationException e) {
result.recordFatalError(createStringResource("pageTasks.message.synchronizeTasksPerformed.fatalError").getString(),
e);
}
showResult(result);
// refresh feedback and table
refreshTable(TaskType.class, target);
target.add(getTable());
}
private void taskDetailsPerformed(AjaxRequestTarget target, String oid) {
PageParameters parameters = new PageParameters();
parameters.add(OnePageParameterEncoder.PARAMETER, oid);
navigateToNext(PageTask.class, parameters);
}
private List<IColumn<SelectableBean<TaskType>, String>> initTaskColumns() {
List<IColumn<SelectableBean<TaskType>, String>> columns = new ArrayList<>();
if (!isCollectionViewPanel()){
columns.add(createTaskCategoryColumn());
}
columns.addAll(initCustomTaskColumns());
return columns;
}
private IColumn<SelectableBean<TaskType>, String> createTaskCategoryColumn() {
return new AbstractExportableColumn<SelectableBean<TaskType>, String>(createStringResource("pageTasks.task.category"), TaskType.F_CATEGORY.getLocalPart()) {
@Override
public void populateItem(Item<ICellPopulator<SelectableBean<TaskType>>> item, String componentId,
final IModel<SelectableBean<TaskType>> rowModel) {
item.add(new Label(componentId,
WebComponentUtil.createCategoryNameModel(TaskTablePanel.this, new PropertyModel<>(rowModel, SelectableBeanImpl.F_VALUE + "." + TaskType.F_CATEGORY.getLocalPart()))));
}
@Override
public IModel<String> getDataModel(IModel<SelectableBean<TaskType>> rowModel) {
return WebComponentUtil.createCategoryNameModel(TaskTablePanel.this, new PropertyModel<>(rowModel, SelectableBeanImpl.F_VALUE + "." + TaskType.F_CATEGORY.getLocalPart()));
}
};
}
protected List<IColumn<SelectableBean<TaskType>, String>> initCustomTaskColumns() {
List<IColumn<SelectableBean<TaskType>, String>> columns = new ArrayList<>();
columns.add(createTaskExecutionStatusColumn());
columns.add(createProgressColumn("pageTasks.task.progress"));
columns.add(new IconColumn<SelectableBean<TaskType>>(createStringResource("pageTasks.task.status"), TaskType.F_RESULT_STATUS.getLocalPart()) {
@Override
protected DisplayType getIconDisplayType(final IModel<SelectableBean<TaskType>> rowModel) {
String icon = "";
if (rowModel != null && rowModel.getObject() != null && rowModel.getObject().getValue().getResultStatus() != null) {
icon = OperationResultStatusPresentationProperties
.parseOperationalResultStatus(rowModel.getObject().getValue().getResultStatus()).getIcon()
+ " fa-lg";
} else {
icon = OperationResultStatusPresentationProperties.UNKNOWN.getIcon() + " fa-lg";
}
String title = "";
TaskType dto = rowModel.getObject().getValue();
if (dto != null && dto.getResultStatus() != null) {
title = createStringResource(dto.getResultStatus()).getString();
} else {
title = createStringResource(OperationResultStatusType.UNKNOWN).getString();
}
return WebComponentUtil.createDisplayType(icon, "", title);
}
});
return columns;
}
private EnumPropertyColumn<SelectableBean<TaskType>> createTaskExecutionStatusColumn() {
return new EnumPropertyColumn<SelectableBean<TaskType>>(createStringResource("pageTasks.task.execution"), TaskType.F_EXECUTION_STATUS.getLocalPart(), SelectableBeanImpl.F_VALUE + ".executionStatus") {
@Override
protected String translate(Enum en) {
return TaskTablePanel.this.createStringResource(en).getString();
}
};
}
private AbstractExportableColumn<SelectableBean<TaskType>, String> createProgressColumn(String titleKey) {
return new AbstractExportableColumn<SelectableBean<TaskType>, String>(createStringResource(titleKey)) {
@Override
public void populateItem(Item<ICellPopulator<SelectableBean<TaskType>>> cellItem, String componentId, final IModel<SelectableBean<TaskType>> rowModel) {
if (!TaskTypeUtil.isPartitionedMaster(rowModel.getObject().getValue())) {
cellItem.add(new Label(componentId,
(IModel<Object>) () -> getProgressDescription(rowModel.getObject())));
} else {
cellItem.add(new LinkPanel(componentId, createStringResource("PageTasks.show.child.progress")) {
@Override
public void onClick(AjaxRequestTarget target) {
PageParameters pageParams = new PageParameters();
pageParams.add(OnePageParameterEncoder.PARAMETER, rowModel.getObject().getValue().getOid());
navigateToNext(PageTask.class, pageParams);
}
});
}
}
@Override
public IModel<String> getDataModel(IModel<SelectableBean<TaskType>> rowModel) {
return Model.of(getProgressDescription(rowModel.getObject()));
}
};
}
private String getProgressDescription(SelectableBean<TaskType> task) {
Long stalledSince = getStalledSince(task.getValue());
if (stalledSince != null) {
return getString("pageTasks.stalledSince", new Date(stalledSince).toLocaleString(), getRealProgressDescription(task));
} else {
return getRealProgressDescription(task);
}
}
private String getRealProgressDescription(SelectableBean<TaskType> task) {
if (isWorkStateHolder(task)) {
return getBucketedTaskProgressDescription(task.getValue());
} else {
return getPlainTaskProgressDescription(task.getValue());
}
}
private boolean isWorkStateHolder(SelectableBean<TaskType> taskType) {
return (TaskTypeUtil.isCoordinator(taskType.getValue()) || hasBuckets(taskType.getValue())) && !isCoordinatedWorker(taskType.getValue());
}
private boolean isCoordinatedWorker(TaskType taskType) {
return taskType.getWorkManagement() != null && taskType.getWorkManagement().getTaskKind() == TaskKindType.WORKER;
}
private boolean hasBuckets(TaskType taskType) {
if (taskType.getWorkState() == null) {
return false;
}
if (taskType.getWorkState().getNumberOfBuckets() != null && taskType.getWorkState().getNumberOfBuckets() > 1) {
return true;
}
List<WorkBucketType> buckets = taskType.getWorkState().getBucket();
if (buckets.size() > 1) {
return true;
} else {
return buckets.size() == 1 && buckets.get(0).getContent() != null;
}
}
private String getBucketedTaskProgressDescription(TaskType taskType) {
int completeBuckets = getCompleteBuckets(taskType);
Integer expectedBuckets = getExpectedBuckets(taskType);
if (expectedBuckets == null) {
return String.valueOf(completeBuckets);
} else {
return (completeBuckets*100/expectedBuckets) + "%";
}
}
private Integer getExpectedBuckets(TaskType taskType) {
return taskType.getWorkState() != null ? taskType.getWorkState().getNumberOfBuckets() : null;
}
private Integer getCompleteBuckets(TaskType taskType) {
return TaskWorkStateTypeUtil.getCompleteBucketsNumber(taskType);
}
private String getPlainTaskProgressDescription(TaskType taskType) {
Long currentProgress = taskType.getProgress();
if (currentProgress == null && taskType.getExpectedTotal() == null) {
return ""; // the task handler probably does not report progress at all
} else {
StringBuilder sb = new StringBuilder();
if (currentProgress != null){
sb.append(currentProgress);
} else {
sb.append("0");
}
if (taskType.getExpectedTotal() != null) {
sb.append("/").append(taskType.getExpectedTotal());
}
return sb.toString();
}
}
private Long getStalledSince(TaskType taskType) {
return xgc2long(taskType.getStalledSince());
}
private Long xgc2long(XMLGregorianCalendar gc) {
return gc != null ? XmlTypeConverter.toMillis(gc) : null;
}
private List<InlineMenuItem> createTasksInlineMenu(boolean isHeader) {
List<InlineMenuItem> items = new ArrayList<>();
items.add(new ButtonInlineMenuItem(createStringResource("pageTasks.button.suspendTask")) {
private static final long serialVersionUID = 1L;
@Override
public InlineMenuItemAction initAction() {
return new ColumnMenuAction<SelectableBean<TaskType>>() {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
suspendTasksPerformed(target, getRowModel());
}
};
}
@Override
public String getButtonIconCssClass() {
return GuiStyleConstants.CLASS_SUSPEND_MENU_ITEM;
}
@Override
public IModel<String> getConfirmationMessageModel() {
String actionName = createStringResource("pageTasks.message.suspendAction").getString();
return TaskTablePanel.this.getTaskConfirmationMessageModel((ColumnMenuAction) getAction(), actionName);
}
@Override
public IModel<Boolean> getVisible() {
IModel<SelectableBean<TaskType>> rowModel = ((ColumnMenuAction) getAction()).getRowModel();
if (rowModel == null){
return Model.of(Boolean.TRUE);
}
SelectableBean<TaskType> rowModelObj = (SelectableBean<TaskType>)rowModel.getObject();
boolean visible = WebComponentUtil.canSuspendTask(rowModelObj.getValue(), TaskTablePanel.this.getPageBase());
return Model.of(visible);
}
});
items.add(new ButtonInlineMenuItem(createStringResource("pageTasks.button.resumeTask")) {
private static final long serialVersionUID = 1L;
@Override
public InlineMenuItemAction initAction() {
return new ColumnMenuAction<SelectableBean<TaskType>>() {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
resumeTasksPerformed(target, getRowModel());
}
};
}
@Override
public String getButtonIconCssClass() {
return GuiStyleConstants.CLASS_RESUME_MENU_ITEM;
}
@Override
public IModel<String> getConfirmationMessageModel() {
String actionName = createStringResource("pageTasks.message.resumeAction").getString();
return TaskTablePanel.this.getTaskConfirmationMessageModel((ColumnMenuAction) getAction(), actionName);
}
@Override
public IModel<Boolean> getVisible() {
IModel<SelectableBean<TaskType>> rowModel = ((ColumnMenuAction) getAction()).getRowModel();
if (rowModel == null){
return Model.of(Boolean.TRUE);
}
SelectableBean<TaskType> rowModelObj = (SelectableBean<TaskType>)rowModel.getObject();
boolean visible = WebComponentUtil.canResumeTask(rowModelObj.getValue(), TaskTablePanel.this.getPageBase());
return Model.of(visible);
}
});
items.add(new ButtonInlineMenuItem(createStringResource("pageTasks.button.scheduleTask")) {
private static final long serialVersionUID = 1L;
@Override
public InlineMenuItemAction initAction() {
return new ColumnMenuAction<SelectableBean<TaskType>>() {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
scheduleTasksPerformed(target, getRowModel());
}
};
}
@Override
public String getButtonIconCssClass() {
return GuiStyleConstants.CLASS_START_MENU_ITEM;
}
@Override
public IModel<String> getConfirmationMessageModel() {
String actionName = createStringResource("pageTasks.message.runNowAction").getString();
return TaskTablePanel.this.getTaskConfirmationMessageModel((ColumnMenuAction) getAction(), actionName);
}
@Override
public IModel<Boolean> getVisible() {
IModel<SelectableBean<TaskType>> rowModel = ((ColumnMenuAction) getAction()).getRowModel();
if (rowModel == null){
return Model.of(Boolean.TRUE);
}
SelectableBean<TaskType> rowModelObj = (SelectableBean<TaskType>)rowModel.getObject();
return Model.of(WebComponentUtil.canRunNowTask(rowModelObj.getValue(), TaskTablePanel.this.getPageBase()));
}
});
items.add(new InlineMenuItem(createStringResource("pageTasks.button.deleteTask")) {
private static final long serialVersionUID = 1L;
@Override
public InlineMenuItemAction initAction() {
return new ColumnMenuAction<SelectableBean<TaskType>>() {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
deleteTaskConfirmedPerformed(target, getRowModel());
}
};
}
@Override
public IModel<String> getConfirmationMessageModel() {
String actionName = createStringResource("pageTasks.message.deleteAction").getString();
return TaskTablePanel.this.getTaskConfirmationMessageModel((ColumnMenuAction) getAction(), actionName);
}
@Override
public boolean isHeaderMenuItem() {
return false;
}
});
InlineMenuItem reconcileWorkers = new InlineMenuItem(createStringResource("pageTasks.button.reconcileWorkers")) {
private static final long serialVersionUID = 1L;
@Override
public InlineMenuItemAction initAction() {
return new ColumnMenuAction<SelectableBean<TaskType>>() {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
reconcileWorkersConfirmedPerformed(target, getRowModel());
}
};
}
@Override
public IModel<String> getConfirmationMessageModel() {
String actionName = createStringResource("pageTasks.message.reconcileWorkersAction").getString();
return TaskTablePanel.this.getTaskConfirmationMessageModel((ColumnMenuAction) getAction(), actionName);
}
@Override
public boolean isHeaderMenuItem() {
return false;
}
};
reconcileWorkers.setVisibilityChecker(TaskTablePanel::isCoordinator);
items.add(reconcileWorkers);
InlineMenuItem suspendRootOnly = new InlineMenuItem(createStringResource("pageTasks.button.suspendRootOnly")) {
private static final long serialVersionUID = 1L;
@Override
public InlineMenuItemAction initAction() {
return new ColumnMenuAction<SelectableBean<TaskType>>() {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
suspendRootOnly(target, getRowModel());
}
};
}
@Override
public IModel<String> getConfirmationMessageModel() {
String actionName = createStringResource("pageTasks.message.suspendAction").getString();
return TaskTablePanel.this.getTaskConfirmationMessageModel((ColumnMenuAction) getAction(), actionName);
}
@Override
public boolean isHeaderMenuItem() {
return false;
}
};
suspendRootOnly.setVisibilityChecker(TaskTablePanel::isManageableTreeRoot);
items.add(suspendRootOnly);
InlineMenuItem resumeRootOnly = new InlineMenuItem(createStringResource("pageTasks.button.resumeRootOnly")) {
private static final long serialVersionUID = 1L;
@Override
public InlineMenuItemAction initAction() {
return new ColumnMenuAction<SelectableBean<TaskType>>() {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
resumeRootOnly(target, getRowModel());
}
};
}
@Override
public IModel<String> getConfirmationMessageModel() {
String actionName = createStringResource("pageTasks.message.resumeAction").getString();
return TaskTablePanel.this.getTaskConfirmationMessageModel((ColumnMenuAction) getAction(), actionName);
}
@Override
public boolean isHeaderMenuItem() {
return false;
}
};
resumeRootOnly.setVisibilityChecker(TaskTablePanel::isManageableTreeRoot);
items.add(resumeRootOnly);
InlineMenuItem deleteWorkStateAndWorkers = new InlineMenuItem(createStringResource("pageTasks.button.deleteWorkersAndWorkState")) {
private static final long serialVersionUID = 1L;
@Override
public InlineMenuItemAction initAction() {
return new ColumnMenuAction<SelectableBean<TaskType>>() {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
deleteWorkersAndWorkState(target, getRowModel());
}
};
}
@Override
public IModel<String> getConfirmationMessageModel() {
String actionName = createStringResource("pageTasks.message.deleteWorkersAndWorkState").getString();
return TaskTablePanel.this.getTaskConfirmationMessageModel((ColumnMenuAction) getAction(), actionName);
}
@Override
public boolean isHeaderMenuItem() {
return false;
}
};
deleteWorkStateAndWorkers.setVisibilityChecker(TaskTablePanel::isManageableTreeRoot);
items.add(deleteWorkStateAndWorkers);
InlineMenuItem deleteWorkState = new InlineMenuItem(createStringResource("pageTasks.button.deleteWorkState")) {
private static final long serialVersionUID = 1L;
@Override
public InlineMenuItemAction initAction() {
return new ColumnMenuAction<SelectableBean<TaskType>>() {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
deleteWorkState(target, getRowModel());
}
};
}
@Override
public IModel<String> getConfirmationMessageModel() {
String actionName = createStringResource("pageTasks.message.deleteWorkState").getString();
return TaskTablePanel.this.getTaskConfirmationMessageModel((ColumnMenuAction) getAction(), actionName);
}
@Override
public IModel<Boolean> getVisible() {
IModel<SelectableBean<TaskType>> rowModel = ((ColumnMenuAction) getAction()).getRowModel();
if (rowModel == null){
return Model.of(Boolean.TRUE);
}
SelectableBean<TaskType> rowModelObj = (SelectableBean<TaskType>)rowModel.getObject();
return Model.of(WebComponentUtil.canSuspendTask(rowModelObj.getValue(), TaskTablePanel.this.getPageBase()));
}
};
items.add(deleteWorkState);
if (isHeader) {
items.add(new InlineMenuItem(createStringResource("pageTasks.button.deleteAllClosedTasks")) {
private static final long serialVersionUID = 1L;
@Override
public InlineMenuItemAction initAction() {
return new ColumnMenuAction<SelectableBean<TaskType>>() {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
deleteAllClosedTasksConfirmedPerformed(target);
}
};
}
@Override
public IModel<String> getConfirmationMessageModel() {
return createStringResource("pageTasks.message.deleteAllClosedTasksConfirm");
}
});
}
return items;
}
//region Task-level actions
//TODO unify with TaskOperationUtils
private void suspendTasksPerformed(AjaxRequestTarget target, IModel<SelectableBean<TaskType>> selectedTask) {
List<TaskType> selectedTasks = getSelectedTasks(target, selectedTask);
if (selectedTasks == null) {
return;
}
Task opTask = createSimpleTask(OPERATION_SUSPEND_TASKS);
OperationResult result = opTask.getResult();
try {
List<TaskType> plainTasks = selectedTasks.stream().filter(dto -> !isManageableTreeRoot(dto)).collect(Collectors.toList());
List<TaskType> trees = selectedTasks.stream().filter(dto -> isManageableTreeRoot(dto)).collect(Collectors.toList());
boolean suspendedPlain = suspendPlainTasks(plainTasks, result, opTask);
boolean suspendedTrees = suspendTrees(trees, result, opTask);
result.computeStatus();
if (result.isSuccess()) {
if (suspendedPlain && suspendedTrees) {
result.recordStatus(OperationResultStatus.SUCCESS, createStringResource("pageTasks.message.suspendTasksPerformed.success").getString());
} else {
result.recordWarning( createStringResource("pageTasks.message.suspendTasksPerformed.warning").getString());
}
}
} catch (ObjectNotFoundException | SchemaException | SecurityViolationException | ExpressionEvaluationException | RuntimeException | CommunicationException | ConfigurationException e) {
result.recordFatalError(createStringResource("pageTasks.message.suspendTasksPerformed.fatalError").getString(), e);
}
showResult(result);
//refresh feedback and table
refreshTable(TaskType.class, target);
}
private void resumeTasksPerformed(AjaxRequestTarget target, IModel<SelectableBean<TaskType>> selectedTask) {
List<TaskType> selectedTasks = getSelectedTasks(target, selectedTask);
if (selectedTasks == null) {
return;
}
Task opTask = createSimpleTask(OPERATION_RESUME_TASKS);
OperationResult result = opTask.getResult();
try {
List<TaskType> plainTasks = selectedTasks.stream().filter(dto -> !isManageableTreeRoot(dto)).collect(Collectors.toList());
List<TaskType> trees = selectedTasks.stream().filter(dto -> isManageableTreeRoot(dto)).collect(Collectors.toList());
getTaskService().resumeTasks(getOids(plainTasks), opTask, result);
for (TaskType tree : trees) {
getTaskService().resumeTaskTree(tree.getOid(), opTask, result);
}
result.computeStatus();
if (result.isSuccess()) {
result.recordStatus(OperationResultStatus.SUCCESS, createStringResource("pageTasks.message.resumeTasksPerformed.success").getString());
}
} catch (ObjectNotFoundException | SchemaException | SecurityViolationException | ExpressionEvaluationException | RuntimeException | CommunicationException | ConfigurationException e) {
result.recordFatalError(createStringResource("pageTasks.message.resumeTasksPerformed.fatalError").getString(), e);
}
showResult(result);
//refresh feedback and table
refreshTable(TaskType.class, target);
}
private boolean suspendPlainTasks(List<TaskType> plainTasks, OperationResult result, Task opTask)
throws SecurityViolationException, ObjectNotFoundException, SchemaException, ExpressionEvaluationException,
CommunicationException, ConfigurationException {
if (!plainTasks.isEmpty()) {
return getTaskService().suspendTasks(getOids(plainTasks), PageTasks.WAIT_FOR_TASK_STOP, opTask, result);
} else {
return true;
}
}
private boolean suspendTrees(List<TaskType> roots, OperationResult result, Task opTask)
throws SecurityViolationException, ObjectNotFoundException, SchemaException, ExpressionEvaluationException,
CommunicationException, ConfigurationException {
boolean suspended = true;
if (!roots.isEmpty()) {
for (TaskType root : roots) {
boolean s = getTaskService().suspendTaskTree(root.getOid(), PageTasks.WAIT_FOR_TASK_STOP, opTask, result);
suspended = suspended && s;
}
}
return suspended;
}
private List<TaskType> getSelectedTasks(AjaxRequestTarget target, IModel<SelectableBean<TaskType>> selectedTask) {
List<TaskType> selectedTasks = new ArrayList<>();
if (selectedTask != null) {
selectedTasks.add(selectedTask.getObject().getValue());
} else {
selectedTasks = getSelectedObjects();
}
if (selectedTasks.isEmpty()) {
getSession().warn("PagetTasks.nothing.selected");
target.add(getFeedbackPanel());
return null;
}
return selectedTasks;
}
private void scheduleTasksPerformed(AjaxRequestTarget target, IModel<SelectableBean<TaskType>> selectedTask) {
List<TaskType> selectedTasks = getSelectedTasks(target, selectedTask);
if (selectedTasks == null) {
return;
}
Task opTask = createSimpleTask(OPERATION_SCHEDULE_TASKS);
OperationResult result = opTask.getResult();
try {
getTaskService().scheduleTasksNow(getOids(selectedTasks), opTask, result);
result.computeStatus();
if (result.isSuccess()) {
result.recordStatus(OperationResultStatus.SUCCESS, createStringResource("pageTasks.message.scheduleTasksPerformed.success").getString());
}
} catch (ObjectNotFoundException | SchemaException | SecurityViolationException | ExpressionEvaluationException | RuntimeException | CommunicationException | ConfigurationException e) {
result.recordFatalError(createStringResource("pageTasks.message.scheduleTasksPerformed.fatalError").getString(), e);
}
showResult(result);
//refresh feedback and table
refreshTable(TaskType.class, target);
}
private void deleteTaskConfirmedPerformed(AjaxRequestTarget target, IModel<SelectableBean<TaskType>> task) {
List<TaskType> selectedTasks = getSelectedTasks(target, task);
if (selectedTasks == null) {
return;
}
Task opTask = createSimpleTask(OPERATION_DELETE_TASKS);
OperationResult result = opTask.getResult();
try {
getTaskService().suspendAndDeleteTasks(getOids(selectedTasks), WAIT_FOR_TASK_STOP, true, opTask, result);
result.computeStatus();
if (result.isSuccess()) {
result.recordStatus(OperationResultStatus.SUCCESS,
createStringResource("pageTasks.message.deleteTaskConfirmedPerformed.success").getString());
}
} catch (ObjectNotFoundException | SchemaException | SecurityViolationException | ExpressionEvaluationException
| RuntimeException | CommunicationException | ConfigurationException e) {
result.recordFatalError(createStringResource("pageTasks.message.deleteTaskConfirmedPerformed.fatalError").getString(),
e);
}
showResult(result);
// TaskDtoProvider provider = (TaskDtoProvider) getTaskTable().getDataTable().getDataProvider();
// provider.clearCache();
// refresh feedback and table
refreshTable(TaskType.class, target);
// refreshTable(target);
}
private void reconcileWorkersConfirmedPerformed(AjaxRequestTarget target, @NotNull IModel<SelectableBean<TaskType>> task) {
Task opTask = createSimpleTask(OPERATION_RECONCILE_WORKERS);
OperationResult result = opTask.getResult();
try {
getTaskService().reconcileWorkers(task.getObject().getValue().getOid(), opTask, result);
result.computeStatus();
if (result.isSuccess() && result.getSubresults().size() == 1) { // brutal hack: to show statistics
result.setMessage(result.getSubresults().get(0).getMessage());
}
} catch (ObjectAlreadyExistsException | ObjectNotFoundException | SchemaException | SecurityViolationException
| ExpressionEvaluationException | RuntimeException | CommunicationException | ConfigurationException e) {
result.recordFatalError(
createStringResource("pageTasks.message.reconcileWorkersConfirmedPerformed.fatalError").getString(), e);
}
showResult(result);
refreshTable(TaskType.class, target);
}
private void suspendRootOnly(AjaxRequestTarget target, @NotNull IModel<SelectableBean<TaskType>> task) {
Task opTask = createSimpleTask(OPERATION_SUSPEND_TASK);
OperationResult result = opTask.getResult();
try {
getTaskService().suspendTasks(Collections.singleton(task.getObject().getValue().getOid()), WAIT_FOR_TASK_STOP, opTask, result);
// TODO check whether the suspension was complete
result.computeStatus();
} catch (ObjectNotFoundException | SchemaException | SecurityViolationException | ExpressionEvaluationException
| RuntimeException | CommunicationException | ConfigurationException e) {
result.recordFatalError(createStringResource("pageTasks.message.suspendRootOnly.fatalError").getString(), e);
}
showResult(result);
refreshTable(TaskType.class, target);
}
private void resumeRootOnly(AjaxRequestTarget target, @NotNull IModel<SelectableBean<TaskType>> task) {
Task opTask = createSimpleTask(OPERATION_RESUME_TASK);
OperationResult result = opTask.getResult();
try {
getTaskService().resumeTasks(Collections.singleton(task.getObject().getValue().getOid()), opTask, result);
result.computeStatus();
} catch (ObjectNotFoundException | SchemaException | SecurityViolationException | ExpressionEvaluationException
| RuntimeException | CommunicationException | ConfigurationException e) {
result.recordFatalError(createStringResource("pageTasks.message.resumeRootOnly.fatalError").getString(), e);
}
showResult(result);
refreshTable(TaskType.class, target);
}
private void deleteWorkersAndWorkState(AjaxRequestTarget target, @NotNull IModel<SelectableBean<TaskType>> task) {
Task opTask = createSimpleTask(OPERATION_DELETE_WORKERS_AND_WORK_STATE);
OperationResult result = opTask.getResult();
try {
getTaskService().deleteWorkersAndWorkState(task.getObject().getValue().getOid(), true, WAIT_FOR_TASK_STOP, opTask, result);
result.computeStatus();
} catch (ObjectNotFoundException | SchemaException | SecurityViolationException | ExpressionEvaluationException
| RuntimeException | CommunicationException | ConfigurationException e) {
result.recordFatalError(createStringResource("pageTasks.message.deleteWorkersAndWorkState.fatalError").getString(),
e);
}
showResult(result);
refreshTable(TaskType.class, target);
}
private void deleteWorkState(AjaxRequestTarget target, @NotNull IModel<SelectableBean<TaskType>> task) {
Task opTask = createSimpleTask(OPERATION_DELETE_WORK_STATE);
OperationResult result = opTask.getResult();
try {
getTaskService().deleteWorkersAndWorkState(task.getObject().getValue().getOid(), false, WAIT_FOR_TASK_STOP, opTask, result);
result.computeStatus();
} catch (ObjectNotFoundException | SchemaException | SecurityViolationException | ExpressionEvaluationException
| RuntimeException | CommunicationException | ConfigurationException e) {
result.recordFatalError(createStringResource("pageTasks.message.deleteWorkState.fatalError").getString(),
e);
}
showResult(result);
refreshTable(TaskType.class, target);
}
private void deleteAllClosedTasksConfirmedPerformed(AjaxRequestTarget target) {
OperationResult launchResult = new OperationResult(OPERATION_DELETE_ALL_CLOSED_TASKS);
Task task = createSimpleTask(OPERATION_DELETE_ALL_CLOSED_TASKS);
task.setHandlerUri(ModelPublicConstants.CLEANUP_TASK_HANDLER_URI);
task.setName("Closed tasks cleanup");
try {
CleanupPolicyType policy = new CleanupPolicyType();
policy.setMaxAge(XmlTypeConverter.createDuration(0));
CleanupPoliciesType policies = new CleanupPoliciesType(getPrismContext());
policies.setClosedTasks(policy);
task.setExtensionContainerValue(SchemaConstants.MODEL_EXTENSION_CLEANUP_POLICIES, policies);
} catch (SchemaException e) {
LOGGER.error("Error dealing with schema (task {})", task, e);
launchResult.recordFatalError(
createStringResource("pageTasks.message.deleteAllClosedTasksConfirmedPerformed.fatalError").getString(), e);
throw new IllegalStateException("Error dealing with schema", e);
}
getTaskManager().switchToBackground(task, launchResult);
launchResult.setBackgroundTaskOid(task.getOid());
showResult(launchResult);
target.add(getFeedbackPanel());
}
private IModel<String> getTaskConfirmationMessageModel(ColumnMenuAction action, String actionName) {
if (action.getRowModel() == null) {
return createStringResource("pageTasks.message.confirmationMessageForMultipleTaskObject", actionName, 1);
// WebComponentUtil.getSelectedData(()).size());
} else {
String objectName = ((SelectableBean<TaskType>) (action.getRowModel().getObject())).getValue().getName().getOrig();
return createStringResource("pageTasks.message.confirmationMessageForSingleTaskObject", actionName, objectName);
}
}
// must be static, otherwise JVM crashes (probably because of some wicket serialization issues)
private static boolean isCoordinator(IModel<?> rowModel, boolean isHeader) {
SelectableBean<TaskType> dto = getDto(rowModel, isHeader);
return dto != null && TaskTypeUtil.isCoordinator(dto.getValue());
}
// must be static, otherwise JVM crashes (probably because of some wicket serialization issues)
private static boolean isManageableTreeRoot(IModel<?> rowModel, boolean isHeader) {
SelectableBean<TaskType> dto = getDto(rowModel, isHeader);
return dto != null && isManageableTreeRoot(dto.getValue());
}
private static boolean isManageableTreeRoot(TaskType taskType) {
return TaskTypeUtil.isCoordinator(taskType) || TaskTypeUtil.isPartitionedMaster(taskType);
}
private static SelectableBean<TaskType> getDto(IModel<?> rowModel, boolean isHeader) {
if (rowModel != null && !isHeader) {
Object object = rowModel.getObject();
if (object instanceof SelectableBean) {
return (SelectableBean<TaskType>) object;
}
}
return null;
}