-
Notifications
You must be signed in to change notification settings - Fork 188
/
PageTasks.java
310 lines (264 loc) · 14.1 KB
/
PageTasks.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
/*
* 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.page.PageBase;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.prism.query.ObjectFilter;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.prism.xml.XmlTypeConverter;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.GetOperationOptionsBuilder;
import com.evolveum.midpoint.schema.SelectorOptions;
import com.evolveum.midpoint.security.api.AuthorizationConstants;
import com.evolveum.midpoint.task.api.TaskExecutionStatus;
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.DateLabelComponent;
import com.evolveum.midpoint.web.component.data.column.ObjectReferenceColumn;
import com.evolveum.midpoint.web.component.util.SelectableBean;
import com.evolveum.midpoint.web.component.util.SelectableBeanImpl;
import com.evolveum.midpoint.web.page.admin.PageAdmin;
import com.evolveum.midpoint.web.page.admin.server.dto.TaskDtoExecutionStatus;
import com.evolveum.midpoint.web.session.UserProfileStorage;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import org.apache.commons.lang.time.DurationFormatUtils;
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.PropertyColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.export.AbstractExportableColumn;
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.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.StringValue;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
@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 PageTasks extends PageAdmin {
private static final long serialVersionUID = 1L;
private static final String ID_TABLE = "table";
public static final String SELECTED_CATEGORY = "category";
public static final long WAIT_FOR_TASK_STOP = 2000L;
public static final long RUNS_CONTINUALLY = -1L;
public static final long ALREADY_PASSED = -2L;
public static final long NOW = 0L;
public PageTasks() {
this(null);
}
public PageTasks(PageParameters params) {
this(null, params);
}
public PageTasks(ObjectQuery predefinedQuery, PageParameters params) {
super(params);
TaskTablePanel tablePanel = new TaskTablePanel(ID_TABLE, UserProfileStorage.TableId.TABLE_TASKS, createOperationOptions()) {
private static final long serialVersionUID = 1L;
@Override
protected ObjectQuery addFilterToContentQuery(ObjectQuery query) {
if (query == null) {
query = getPrismContext().queryFactory().createQuery();
}
if (predefinedQuery != null){
query.addFilter(predefinedQuery.getFilter());
}
query.addFilter(getPrismContext().queryFor(TaskType.class)
.item(TaskType.F_PARENT)
.isNull()
.buildFilter());
return query;
}
@Override
protected List<IColumn<SelectableBean<TaskType>, String>> createColumns() {
List<IColumn<SelectableBean<TaskType>, String>> columns = super.createColumns();
addCustomColumns(columns);
return columns;
}
};
add(tablePanel);
}
private Collection<? extends IColumn<SelectableBean<TaskType>, String>> addCustomColumns(List<IColumn<SelectableBean<TaskType>, String>> columns) {
columns.add(2, new ObjectReferenceColumn<SelectableBean<TaskType>>(createStringResource("pageTasks.task.objectRef"), SelectableBeanImpl.F_VALUE+"."+TaskType.F_OBJECT_REF.getLocalPart()){
private static final long serialVersionUID = 1L;
@Override
public IModel<ObjectReferenceType> extractDataModel(IModel<SelectableBean<TaskType>> rowModel) {
SelectableBean<TaskType> bean = rowModel.getObject();
return Model.of(bean.getValue().getObjectRef());
}
});
columns.add(3, new PropertyColumn<>(createStringResource("pageTasks.task.executingAt"), SelectableBeanImpl.F_VALUE + "." + TaskType.F_NODE_AS_OBSERVED.getLocalPart()));
columns.add(4, new AbstractExportableColumn<SelectableBean<TaskType>, String>(createStringResource("pageTasks.task.currentRunTime"), TaskType.F_COMPLETION_TIMESTAMP.getLocalPart()) {
private static final long serialVersionUID = 1L;
@Override
public void populateItem(final Item<ICellPopulator<SelectableBean<TaskType>>> item, final String componentId,
final IModel<SelectableBean<TaskType>> rowModel) {
DateLabelComponent dateLabel = new DateLabelComponent(componentId, new IModel<Date>() {
private static final long serialVersionUID = 1L;
@Override
public Date getObject() {
Date date = getCurrentRuntime(rowModel);
SelectableBean<TaskType> task = rowModel.getObject();
if (getRawExecutionStatus(task.getValue()) == TaskExecutionStatus.CLOSED && date != null) {
((DateLabelComponent) item.get(componentId)).setBefore(createStringResource("pageTasks.task.closedAt").getString() + " ");
} else if (date != null) {
((DateLabelComponent) item.get(componentId))
.setBefore(WebComponentUtil.formatDurationWordsForLocal(date.getTime(), true, true, PageTasks.this));
}
return date;
}
}, WebComponentUtil.getShortDateTimeFormat(PageTasks.this));
item.add(dateLabel);
}
@Override
public IModel<String> getDataModel(IModel<SelectableBean<TaskType>> rowModel) {
SelectableBean<TaskType> task = rowModel.getObject();
Date date = getCurrentRuntime(rowModel);
String displayValue = "";
if (date != null) {
if (getRawExecutionStatus(task.getValue()) == TaskExecutionStatus.CLOSED) {
displayValue =
createStringResource("pageTasks.task.closedAt").getString() +
WebComponentUtil.getShortDateTimeFormattedValue(date, PageTasks.this);
} else {
displayValue = WebComponentUtil.formatDurationWordsForLocal(date.getTime(), true, true, PageTasks.this);
}
}
return Model.of(displayValue);
}
});
columns.add(5, new AbstractExportableColumn<SelectableBean<TaskType>, String>(createStringResource("pageTasks.task.scheduledToRunAgain")) {
private static final long serialVersionUID = 1L;
@Override
public void populateItem(Item<ICellPopulator<SelectableBean<TaskType>>> item, String componentId,
final IModel<SelectableBean<TaskType>> rowModel) {
item.add(new Label(componentId, () -> createScheduledToRunAgain(rowModel)));
}
@Override
public IModel<String> getDataModel(IModel<SelectableBean<TaskType>> rowModel) {
return Model.of(createScheduledToRunAgain(rowModel));
}
});
return columns;
}
private Collection<SelectorOptions<GetOperationOptions>> createOperationOptions() {
List<QName> propertiesToGet = new ArrayList<>();
propertiesToGet.add(TaskType.F_NODE_AS_OBSERVED);
propertiesToGet.add(TaskType.F_NEXT_RUN_START_TIMESTAMP);
propertiesToGet.add(TaskType.F_NEXT_RETRY_TIMESTAMP);
propertiesToGet.add(TaskType.F_SUBTASK_REF);
GetOperationOptionsBuilder getOperationOptionsBuilder = getSchemaHelper().getOperationOptionsBuilder();
getOperationOptionsBuilder = getOperationOptionsBuilder.resolveNames();
Collection<SelectorOptions<GetOperationOptions>> searchOptions = getOperationOptionsBuilder
.items(propertiesToGet.toArray(new Object[0])).retrieve()
.build();
return searchOptions;
}
private Date getCurrentRuntime(IModel<SelectableBean<TaskType>> taskModel) {
TaskType task = taskModel.getObject().getValue();
if (getRawExecutionStatus(task) == TaskExecutionStatus.CLOSED) {
Long time = getCompletionTimestamp(task);
if (time == null) {
return null;
}
return new Date(time);
}
return null;
}
public Long getCompletionTimestamp(TaskType taskType) {
return xgc2long(taskType.getCompletionTimestamp());
}
private String createScheduledToRunAgain(IModel<SelectableBean<TaskType>> taskModel) {
TaskType task = taskModel.getObject().getValue();
boolean runnable = getRawExecutionStatus(task) == TaskExecutionStatus.RUNNABLE;
Long scheduledAfter = getScheduledToStartAgain(taskModel.getObject());
Long retryAfter = runnable ? getRetryAfter(task) : null;
if (scheduledAfter == null) {
if (retryAfter == null || retryAfter <= 0) {
return "";
}
} else if (scheduledAfter == NOW) { // TODO what about retryTime?
return getString(runnable ? "pageTasks.now" : "pageTasks.nowForNotRunningTasks");
} else if (scheduledAfter == RUNS_CONTINUALLY) { // retryTime is probably null here
return getString("pageTasks.runsContinually");
} else if (scheduledAfter == ALREADY_PASSED && retryAfter == null) {
return getString(runnable ? "pageTasks.alreadyPassed" : "pageTasks.alreadyPassedForNotRunningTasks");
}
long displayTime;
boolean displayAsRetry;
if (retryAfter != null && retryAfter > 0 && (scheduledAfter == null || scheduledAfter < 0
|| retryAfter < scheduledAfter)) {
displayTime = retryAfter;
displayAsRetry = true;
} else {
displayTime = scheduledAfter;
displayAsRetry = false;
}
String key;
if (runnable) {
key = displayAsRetry ? "pageTasks.retryIn" : "pageTasks.in";
} else {
key = "pageTasks.inForNotRunningTasks";
}
return PageBase.createStringResourceStatic(this, key, DurationFormatUtils.formatDurationWords(displayTime, true, true))
.getString();
}
public Long getScheduledToStartAgain(SelectableBean<TaskType> taskBean) {
long current = System.currentTimeMillis();
if (getExecution(taskBean.getValue()) == TaskDtoExecutionStatus.RUNNING) {
if (TaskRecurrenceType.RECURRING != taskBean.getValue().getRecurrence()) {
return null;
} else if (TaskBindingType.TIGHT == taskBean.getValue().getBinding()) {
return RUNS_CONTINUALLY; // runs continually; todo provide some information also in this case
}
}
Long nextRunStartTimeLong = getNextRunStartTimeLong(taskBean.getValue());
if (nextRunStartTimeLong == null || nextRunStartTimeLong == 0) {
return null;
}
if (nextRunStartTimeLong > current + 1000) {
return nextRunStartTimeLong - System.currentTimeMillis();
} else if (nextRunStartTimeLong < current - 60000) {
return ALREADY_PASSED;
} else {
return NOW;
}
}
public Long getRetryAfter(TaskType taskType) {
Long retryAt = getNextRetryTimeLong(taskType);
return retryAt != null ? retryAt - System.currentTimeMillis() : null;
}
public Long getNextRetryTimeLong(TaskType taskType) {
return xgc2long(taskType.getNextRetryTimestamp());
}
public Long getNextRunStartTimeLong(TaskType taskType) {
return xgc2long(taskType.getNextRunStartTimestamp());
}
public TaskDtoExecutionStatus getExecution(TaskType taskType) {
return TaskDtoExecutionStatus.fromTaskExecutionStatus(taskType.getExecutionStatus(), taskType.getNodeAsObserved() != null);
}
//TODO why?
public TaskExecutionStatus getRawExecutionStatus(TaskType taskType) {
return TaskExecutionStatus.fromTaskType(taskType.getExecutionStatus());
}
private Long xgc2long(XMLGregorianCalendar gc) {
return gc != null ? XmlTypeConverter.toMillis(gc) : null;
}
}