/
AssignmentPanel.java
490 lines (385 loc) · 17.2 KB
/
AssignmentPanel.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
/*
* Copyright (c) 2010-2017 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.web.component.assignment;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang.StringUtils;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
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.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.util.tester.WicketTester;
import com.evolveum.midpoint.gui.api.GuiStyleConstants;
import com.evolveum.midpoint.gui.api.component.BasePanel;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.prism.query.ObjectPaging;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.security.api.AuthorizationConstants;
import com.evolveum.midpoint.util.MiscUtil;
import com.evolveum.midpoint.web.component.AjaxButton;
import com.evolveum.midpoint.web.component.AjaxIconButton;
import com.evolveum.midpoint.web.component.data.BoxedTablePanel;
import com.evolveum.midpoint.web.component.data.column.CheckBoxHeaderColumn;
import com.evolveum.midpoint.web.component.data.column.ColumnMenuAction;
import com.evolveum.midpoint.web.component.data.column.ColumnUtils;
import com.evolveum.midpoint.web.component.data.column.DoubleButtonColumn;
import com.evolveum.midpoint.web.component.data.column.IconColumn;
import com.evolveum.midpoint.web.component.data.column.InlineMenuButtonColumn;
import com.evolveum.midpoint.web.component.data.column.LinkColumn;
import com.evolveum.midpoint.web.component.form.Form;
import com.evolveum.midpoint.web.component.menu.cog.InlineMenuItem;
import com.evolveum.midpoint.web.component.util.AssignmentListDataProvider;
import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour;
import com.evolveum.midpoint.web.page.admin.users.dto.UserDtoStatus;
import com.evolveum.midpoint.web.session.AssignmentsTabStorage;
import com.evolveum.midpoint.web.session.UserProfileStorage;
import com.evolveum.midpoint.web.session.UserProfileStorage.TableId;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ActivationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.TimeIntervalStatusType;
public abstract class AssignmentPanel extends BasePanel<List<AssignmentDto>> {
private static final long serialVersionUID = 1L;
public static final String ID_ASSIGNMENTS = "assignments";
private static final String ID_NEW_ASSIGNMENT_BUTTON = "newAssignmentButton";
private static final String ID_ASSIGNMENTS_TABLE = "assignmentsTable";
public static final String ID_ASSIGNMENTS_DETAILS = "assignmentsDetails";
public static final String ID_ASSIGNMENT_DETAILS = "assignmentDetails";
public static final String ID_DETAILS = "details";
private final static String ID_DONE_BUTTON = "doneButton";
private final static String ID_CANCEL_BUTTON = "cancelButton";
protected boolean assignmentDetailsVisible;
// private PageBase pageBase;
public AssignmentPanel(String id, IModel<List<AssignmentDto>> assignmentsModel) {
super(id, assignmentsModel);
// this.pageBase = pageBase;
// initPaging();
// initLayout();
}
protected abstract void initPaging();
@Override
protected void onInitialize() {
super.onInitialize();
initPaging();
initLayout();
}
private void initLayout() {
initListPanel();
initDetailsPanel();
setOutputMarkupId(true);
}
private void initListPanel() {
WebMarkupContainer assignmentsContainer = new WebMarkupContainer(ID_ASSIGNMENTS);
assignmentsContainer.setOutputMarkupId(true);
add(assignmentsContainer);
BoxedTablePanel<AssignmentDto> assignmentTable = initAssignmentTable();
assignmentsContainer.add(assignmentTable);
AjaxIconButton newObjectIcon = new AjaxIconButton(ID_NEW_ASSIGNMENT_BUTTON, new Model<>("fa fa-plus"),
createStringResource("MainObjectListPanel.newObject")) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
newAssignmentClickPerformed(target);
}
};
newObjectIcon.add(new VisibleEnableBehaviour() {
private static final long serialVersionUID = 1L;
@Override
public boolean isVisible() {
return WebComponentUtil.isAuthorized(AuthorizationConstants.AUTZ_UI_ADMIN_ASSIGN_ACTION_URI);
}
});
assignmentsContainer.add(newObjectIcon);
initCustomLayout(assignmentsContainer);
assignmentsContainer.add(new VisibleEnableBehaviour() {
private static final long serialVersionUID = 1L;
@Override
public boolean isVisible() {
return !assignmentDetailsVisible;
}
});
}
private BoxedTablePanel<AssignmentDto> initAssignmentTable() {
AssignmentListDataProvider assignmentsProvider = new AssignmentListDataProvider(this, getModel()) {
private static final long serialVersionUID = 1L;
@Override
protected void saveProviderPaging(ObjectQuery query, ObjectPaging paging) {
getAssignmentsStorage().setPaging(paging);
}
@Override
public ObjectQuery getQuery() {
return createObjectQuery();
}
};
List<IColumn<AssignmentDto, String>> columns = initBasicColumns();
if (WebComponentUtil.isAuthorized(AuthorizationConstants.AUTZ_UI_ADMIN_UNASSIGN_ACTION_URI)) {
columns.add(new InlineMenuButtonColumn<AssignmentDto>(getAssignmentMenuActions(), 2, getPageBase()));
}
BoxedTablePanel<AssignmentDto> assignmentTable = new BoxedTablePanel<AssignmentDto>(ID_ASSIGNMENTS_TABLE,
assignmentsProvider, columns, getTableId(), getItemsPerPage()) {
private static final long serialVersionUID = 1L;
@Override
public int getItemsPerPage() {
return getPageBase().getSessionStorage().getUserProfile().getTables()
.get(UserProfileStorage.TableId.ASSIGNMENTS_TAB_TABLE);
}
@Override
protected Item<AssignmentDto> customizeNewRowItem(Item<AssignmentDto> item, IModel<AssignmentDto> model) {
item.add(AttributeModifier.append("class", AssignmentsUtil.createAssignmentStatusClassModel(model)));
return item;
}
};
assignmentTable.setOutputMarkupId(true);
assignmentTable.setCurrentPage(getAssignmentsStorage().getPaging());
return assignmentTable;
}
protected AssignmentsTabStorage getAssignmentsStorage() {
return getPageBase().getSessionStorage().getAssignmentsTabStorage();
}
protected abstract ObjectQuery createObjectQuery();
protected List<IColumn<AssignmentDto, String>> initBasicColumns() {
List<IColumn<AssignmentDto, String>> columns = new ArrayList<>();
columns.add(new CheckBoxHeaderColumn<AssignmentDto>());
columns.add(new IconColumn<AssignmentDto>(Model.of("")) {
private static final long serialVersionUID = 1L;
@Override
protected IModel<String> createIconModel(IModel<AssignmentDto> rowModel) {
return new AbstractReadOnlyModel<String>() {
private static final long serialVersionUID = 1L;
@Override
public String getObject() {
return WebComponentUtil.createDefaultBlackIcon(rowModel.getObject().getTargetType());
}
};
}
});
columns.add(new LinkColumn<AssignmentDto>(createStringResource("PolicyRulesPanel.nameColumn")){
private static final long serialVersionUID = 1L;
@Override
protected IModel<String> createLinkModel(IModel<AssignmentDto> rowModel) {
String name = AssignmentsUtil.getName(rowModel.getObject().getAssignment(), getParentPage());
if (StringUtils.isBlank(name)) {
return createStringResource("AssignmentPanel.noName");
}
return Model.of(name);
}
@Override
public void onClick(AjaxRequestTarget target, IModel<AssignmentDto> rowModel) {
assignmentDetailsPerformed(target, rowModel);
}
});
columns.add(new LinkColumn<AssignmentDto>(createStringResource("AssignmentType.activation")){
private static final long serialVersionUID = 1L;
@Override
protected IModel<String> createLinkModel(IModel<AssignmentDto> rowModel) {
return AssignmentsUtil.createActivationTitleModelExperimental(rowModel, AssignmentPanel.this);
}
@Override
public void onClick(AjaxRequestTarget target, IModel<AssignmentDto> rowModel) {
updateAssignmnetActivation(target, rowModel);
}
});
// columns.add(new IconColumn<AssignmentDto>(Model.of("")){
// private static final long serialVersionUID = 1L;
//
// @Override
// protected IModel<String> createIconModel(IModel<AssignmentDto> rowModel) {
// if (AssignmentsUtil.getType(rowModel.getObject().getAssignment()) == null){
// return Model.of("");
// }
// return Model.of(AssignmentsUtil.getType(rowModel.getObject().getAssignment()).getIconCssClass());
// }
//
// @Override
// protected IModel<String> createTitleModel(IModel<AssignmentDto> rowModel) {
// return AssignmentsUtil.createAssignmentIconTitleModel(AbstractRoleAssignmentPanel.this, AssignmentsUtil.getType(rowModel.getObject().getAssignment()));
// }
//
// });
//
// columns.add(new IconColumn<AssignmentDto>(Model.of("")){
// private static final long serialVersionUID = 1L;
//
// @Override
// protected IModel<String> createIconModel(IModel<AssignmentDto> rowModel) {
// return Model.of(GuiStyleConstants.CLASS_POLICY_RULES);
// }
//
// @Override
// protected IModel<String> createTitleModel(IModel<AssignmentDto> rowModel) {
// return createStringResource("PolicyRulesPanel.imageTitle");
// }
//
// });
columns.addAll(initColumns());
return columns;
}
protected abstract List<IColumn<AssignmentDto, String>> initColumns();
protected abstract void newAssignmentClickPerformed(AjaxRequestTarget target);
protected void initCustomLayout(WebMarkupContainer assignmentsContainer) {
}
private void initDetailsPanel() {
WebMarkupContainer details = new WebMarkupContainer(ID_DETAILS);
details.setOutputMarkupId(true);
details.add(new VisibleEnableBehaviour() {
private static final long serialVersionUID = 1L;
@Override
public boolean isVisible() {
return assignmentDetailsVisible;
}
});
add(details);
IModel<List<AssignmentDto>> selectedAssignmnetList = new AbstractReadOnlyModel<List<AssignmentDto>>() {
private static final long serialVersionUID = 1L;
@Override
public List<AssignmentDto> getObject() {
return getAssignmentListProvider().getSelectedData();
}
};
ListView<AssignmentDto> assignmentDetailsView = new ListView<AssignmentDto>(ID_ASSIGNMENTS_DETAILS,
selectedAssignmnetList) {
private static final long serialVersionUID = 1L;
@Override
protected void populateItem(ListItem<AssignmentDto> item) {
Form form = this.findParent(Form.class);
AbstractAssignmentDetailsPanel details = createDetailsPanel(ID_ASSIGNMENT_DETAILS, form, item.getModel(),
getParentPage());
item.add(details);
details.setOutputMarkupId(true);
}
};
assignmentDetailsView.setOutputMarkupId(true);
details.add(assignmentDetailsView);
AjaxButton doneButton = new AjaxButton(ID_DONE_BUTTON,
createStringResource("AssignmentPanel.doneButton")) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget ajaxRequestTarget) {
assignmentDetailsVisible = false;
getSelectedAssignments().stream().forEach(a -> a.setSelected(false));
ajaxRequestTarget.add(AssignmentPanel.this);
}
};
details.add(doneButton);
AjaxButton cancelButton = new AjaxButton(ID_CANCEL_BUTTON,
createStringResource("AssignmentPanel.cancelButton")) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget ajaxRequestTarget) {
assignmentDetailsVisible = false;
getSelectedAssignments().stream().forEach(a -> {a.revertChanges(); a.setSelected(false);});
ajaxRequestTarget.add(AssignmentPanel.this);
}
};
details.add(cancelButton);
}
protected AssignmentListDataProvider getAssignmentListProvider() {
return (AssignmentListDataProvider) getAssignmentTable().getDataTable().getDataProvider();
}
protected BoxedTablePanel<AssignmentDto> getAssignmentTable() {
return (BoxedTablePanel<AssignmentDto>) get(createComponentPath(ID_ASSIGNMENTS, ID_ASSIGNMENTS_TABLE));
}
protected abstract AbstractAssignmentDetailsPanel createDetailsPanel(String idAssignmentDetails, Form<?> form, IModel<AssignmentDto> model,
PageBase parentPage);
private List<AssignmentDto> getSelectedAssignments() {
BoxedTablePanel<AssignmentDto> assignemntTable = getAssignmentTable();
AssignmentListDataProvider assignmentProvider = (AssignmentListDataProvider) assignemntTable.getDataTable()
.getDataProvider();
return assignmentProvider.getAvailableData().stream().filter(a -> a.isSelected()).collect(Collectors.toList());
}
private List<InlineMenuItem> getAssignmentMenuActions() {
List<InlineMenuItem> menuItems = new ArrayList<>();
menuItems.add(new InlineMenuItem(createStringResource("PageBase.button.delete"), new Model<Boolean>(true),
new Model<Boolean>(true), false, createDeleteColumnAction(), 0, GuiStyleConstants.CLASS_DELETE_MENU_ITEM,
DoubleButtonColumn.BUTTON_COLOR_CLASS.DANGER.toString()));
menuItems.add(new InlineMenuItem(createStringResource("PageBase.button.edit"), new Model<Boolean>(true),
new Model<Boolean>(true), false, createEditColumnAction(), 1, GuiStyleConstants.CLASS_EDIT_MENU_ITEM,
DoubleButtonColumn.BUTTON_COLOR_CLASS.DEFAULT.toString()));
return menuItems;
}
private ColumnMenuAction<AssignmentDto> createDeleteColumnAction() {
return new ColumnMenuAction<AssignmentDto>() {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
if (getRowModel() == null) {
deleteAssignmentPerformed(target, getSelectedAssignments());
} else {
AssignmentDto rowDto = (AssignmentDto) getRowModel().getObject();
List<AssignmentDto> toDelete = new ArrayList<>();
toDelete.add(rowDto);
deleteAssignmentPerformed(target, toDelete);
}
}
};
}
private ColumnMenuAction<AssignmentDto> createEditColumnAction() {
return new ColumnMenuAction<AssignmentDto>() {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
if (getRowModel() == null) {
assignmentDetailsPerformed(target, getSelectedAssignments());
} else {
assignmentDetailsPerformed(target, getRowModel());
}
}
};
}
protected void assignmentDetailsPerformed(AjaxRequestTarget target, IModel<AssignmentDto> rowModel) {
assignmentDetailsVisible = true;
getModelObject().forEach(a -> a.setSelected(false));
rowModel.getObject().setSelected(true);
target.add(AssignmentPanel.this);
}
protected void assignmentDetailsPerformed(AjaxRequestTarget target, List<AssignmentDto> rowModel) {
assignmentDetailsVisible = true;
rowModel.stream().forEach(a -> a.setSelected(true));
target.add(AssignmentPanel.this);
}
protected void updateAssignmnetActivation(AjaxRequestTarget target, IModel<AssignmentDto> rowModel) {
AssignmentActivationPopupablePanel activationPanel = new AssignmentActivationPopupablePanel(
getPageBase().getMainPopupBodyId(),
new PropertyModel<>(rowModel, AssignmentDto.F_VALUE + "." + AssignmentType.F_ACTIVATION.getLocalPart()));
activationPanel.setOutputMarkupId(true);
getPageBase().showMainPopup(activationPanel, target);
}
protected abstract TableId getTableId();
protected abstract int getItemsPerPage();
protected void refreshTable(AjaxRequestTarget target) {
target.add(getAssignmentContainer().addOrReplace(initAssignmentTable()));
}
protected void deleteAssignmentPerformed(AjaxRequestTarget target, List<AssignmentDto> toDelete) {
toDelete.forEach(a -> a.setStatus(UserDtoStatus.DELETE));
refreshTable(target);
}
protected WebMarkupContainer getAssignmentContainer() {
return (WebMarkupContainer) get(ID_ASSIGNMENTS);
}
protected PageBase getParentPage() {
return getPageBase();
}
}