/
MultipleAssignmentSelector.java
477 lines (413 loc) · 19.8 KB
/
MultipleAssignmentSelector.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
/*
* Copyright (c) 2010-2016 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.Collection;
import java.util.Iterator;
import java.util.List;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.api.util.WebModelServiceUtils;
import com.evolveum.midpoint.model.api.ModelInteractionService;
import com.evolveum.midpoint.model.api.RoleSelectionSpecification;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.query.ObjectFilter;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.RetrieveOption;
import com.evolveum.midpoint.schema.SelectorOptions;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.web.component.prism.ContainerStatus;
import com.evolveum.midpoint.web.component.prism.ObjectWrapper;
import com.evolveum.midpoint.web.component.prism.ObjectWrapperFactory;
import com.evolveum.midpoint.web.component.util.ListDataProvider;
import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour;
import com.evolveum.midpoint.web.page.admin.users.dto.UserDtoStatus;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import org.apache.commons.lang.StringUtils;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Component;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
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.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import com.evolveum.midpoint.gui.api.component.BasePanel;
import com.evolveum.midpoint.gui.api.component.FocusBrowserPanel;
import com.evolveum.midpoint.gui.api.model.LoadableModel;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.component.data.BaseSortableDataProvider;
import com.evolveum.midpoint.web.component.data.BoxedTablePanel;
import com.evolveum.midpoint.web.component.data.ObjectDataProvider;
import com.evolveum.midpoint.web.component.search.Search;
import com.evolveum.midpoint.web.component.search.SearchFactory;
import com.evolveum.midpoint.web.component.search.SearchPanel;
import com.evolveum.midpoint.web.component.util.SelectableBean;
import com.evolveum.midpoint.web.session.UserProfileStorage;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;
import javax.xml.namespace.QName;
/**
* Creates a panel with the list of focus type items
* with the possibility to filter by user (show only
* assigned to the specified user items), to search
* through the list and to reset the list to the
* initial state
*
* @author Kate Honchar
*/
public class MultipleAssignmentSelector<F extends FocusType, H extends FocusType> extends BasePanel<List<AssignmentEditorDto>> {
private static final long serialVersionUID = 1L;
private static final Trace LOGGER = TraceManager.getTrace(MultipleAssignmentSelector.class);
private static final String DOT_CLASS = MultipleAssignmentSelector.class.getName() + ".";
private static final String OPERATION_LOAD_AVAILABLE_ROLES = DOT_CLASS + "loadAvailableRoles";
private static final String OPERATION_LOAD_FILTER_OBJECT = DOT_CLASS + "loadFilterObject";
private static final String ID_USER_CHOOSER_DIALOG = "userChooserDialog";
private static final String ID_TABLE = "table";
private static final String ID_FILTER_BUTTON_CONTAINER = "filterButtonContainer";
private static final String ID_SEARCH_FORM = "searchForm";
private static final String ID_SEARCH = "search";
private static final int ITEMS_PER_PAGE = 10;
private static final String ID_FILTER_BY_USER_BUTTON = "filterByUserButton";
private static final String ID_LABEL = "label";
private static final String ID_DELETE_BUTTON = "deleteButton";
private boolean showDialog = true;
private IModel<Search> searchModel;
private BaseSortableDataProvider provider;
private Class type;
private Class<H> targetFocusClass;
private String labelValue ="";
private IModel<ObjectFilter> filterModel = null;
private ObjectQuery searchQuery = null;
private PrismObject<F> focus;
private H filterObject = null;
public MultipleAssignmentSelector(String id, IModel<List<AssignmentEditorDto>> selectorModel,
Class<H> targetFocusClass, Class type, PrismObject<F> focus, IModel<ObjectFilter> filterModel) {
super(id, selectorModel);
this.focus=focus;
this.filterModel = filterModel;
// this.provider = provider == null ? getListDataProvider(null) : provider;
this.type = type;
this.targetFocusClass = targetFocusClass;
searchModel = new LoadableModel<Search>(false) {
@Override
public Search load() {
Search search = SearchFactory.createSearch(RoleType.class, getPageBase().getPrismContext(), true);
return search;
}
};
if (focus == null){
provider = getListDataProvider(null);
} else {
provider = getAvailableAssignmentsDataProvider();
}
initLayout();
}
private void initLayout() {
setOutputMarkupId(true);
WebMarkupContainer filterButtonContainer = new WebMarkupContainer(ID_FILTER_BUTTON_CONTAINER);
AjaxLink<String> filterByUserButton = new AjaxLink<String>(ID_FILTER_BY_USER_BUTTON) {
@Override
public void onClick(AjaxRequestTarget target) {
if (showDialog) {
labelValue = createStringResource("MultipleAssignmentSelector.filterByUser").getString();
initUserDialog(createStringResource("MultipleAssignmentSelector.filterByUser"), target);
}
showDialog = true;
}
};
filterButtonContainer.add(filterByUserButton);
labelValue = createStringResource("MultipleAssignmentSelector.filterByUser").getString();
Label label = new Label(ID_LABEL, createLabelModel());
label.setRenderBodyOnly(true);
filterByUserButton.add(label);
AjaxLink deleteButton = new AjaxLink(ID_DELETE_BUTTON) {
@Override
public void onClick(AjaxRequestTarget target) {
labelValue = createStringResource("MultipleAssignmentSelector.filterByUser").getString();
showDialog = false;
deleteFilterPerformed(target);
}
};
filterByUserButton.add(deleteButton);
add(filterButtonContainer);
initSearchPanel();
add(initTablePanel());
}
private Component createRowLink(String id, final IModel<SelectableBean<AssignmentEditorDto>> rowModel) {
AjaxLink<SelectableBean<AssignmentEditorDto>> button = new AjaxLink<SelectableBean<AssignmentEditorDto>>(id, rowModel) {
@Override
public IModel<?> getBody() {
AssignmentEditorDto dto =(AssignmentEditorDto) rowModel.getObject();
String name = StringUtils.isNotEmpty(dto.getNameForTargetObject()) ?
dto.getNameForTargetObject() : dto.getName();
return new Model<String>(name);
}
@Override
public void onClick(AjaxRequestTarget target) {
LOGGER.trace("{} CLICK: {}", this, rowModel.getObject());
toggleRow(rowModel);
target.add(this);
}
@Override
protected void onComponentTag(ComponentTag tag) {
super.onComponentTag(tag);
if (rowModel.getObject().isSelected()) {
tag.put("class", "list-group-item active");
} else {
tag.put("class", "list-group-item");
}
String description = ((AssignmentEditorDto) rowModel.getObject()).getDescription();
if (description != null) {
tag.put("title", description);
}
}
};
button.add(new VisibleEnableBehaviour(){
@Override
public boolean isVisible(){
return ((AssignmentEditorDto)rowModel.getObject()).getStatus() != UserDtoStatus.DELETE;
}
});
button.setOutputMarkupId(true);
return button;
}
private List<IColumn<SelectableBean<AssignmentEditorDto>, String>> initColumns() {
List<IColumn<SelectableBean<AssignmentEditorDto>, String>> columns = new ArrayList<>();
columns.add(new AbstractColumn<SelectableBean<AssignmentEditorDto>, String>(new Model()) {
public void populateItem(Item<ICellPopulator<SelectableBean<AssignmentEditorDto>>> cellItem, String componentId,
IModel<SelectableBean<AssignmentEditorDto>> rowModel) {
cellItem.add(createRowLink(componentId, rowModel));
}
});
return columns;
}
private void updateBoxedTablePanelStyles(BoxedTablePanel panel) {
panel.getDataTableContainer().add(new AttributeAppender("class", " multiple-assignment-selector-table-container"));
panel.getDataTable().add(new AttributeModifier("class", "multiple-assignment-selector-table"));
panel.getFooterPaging().getParent().add(new AttributeModifier("class", "multiple-assignment-selector-table-footer"));
}
public BaseSortableDataProvider getProvider() {
return provider;
}
private void toggleRow(IModel<SelectableBean<AssignmentEditorDto>> rowModel){
rowModel.getObject().setSelected(!rowModel.getObject().isSelected());
List<AssignmentEditorDto> providerList = ((BaseSortableDataProvider) getProvider()).getAvailableData();
for (AssignmentEditorDto dto : providerList){
if (dto.equals(rowModel.getObject())){
dto.setSelected(rowModel.getObject().isSelected());
break;
}
}
}
private void initSearchPanel(){
final Form searchForm = new Form(ID_SEARCH_FORM);
add(searchForm);
searchForm.setOutputMarkupId(true);
SearchPanel search = new SearchPanel(ID_SEARCH, (IModel) searchModel, false) {
@Override
public void searchPerformed(ObjectQuery query, AjaxRequestTarget target) {
MultipleAssignmentSelector.this.searchPerformed(query, target);
}
};
searchForm.add(search);
}
private void searchPerformed(ObjectQuery query, AjaxRequestTarget target) {
searchQuery = query;
BoxedTablePanel panel = getTable();
panel.setCurrentPage(null);
provider.setQuery(query);
target.add(panel);
}
public BoxedTablePanel getTable() {
return (BoxedTablePanel) get(ID_TABLE);
}
private BoxedTablePanel initTablePanel(){
List<IColumn<SelectableBean<AssignmentEditorDto>, String>> columns = initColumns();
BoxedTablePanel table = new BoxedTablePanel(ID_TABLE, provider, columns,
UserProfileStorage.TableId.TABLE_ROLES, ITEMS_PER_PAGE);
updateBoxedTablePanelStyles(table);
//hide footer menu
table.getFooterMenu().setVisible(false);
//hide footer count label
table.getFooterCountLabel().setVisible(false);
table.setOutputMarkupId(true);
return table;
}
private void initUserDialog(IModel<String> title, AjaxRequestTarget target) {
List<QName> supportedTypes = new ArrayList<>();
supportedTypes.add(getPageBase().getPrismContext().getSchemaRegistry()
.findObjectDefinitionByCompileTimeClass(targetFocusClass).getTypeName());
FocusBrowserPanel<H> focusBrowser = new FocusBrowserPanel<H>(getPageBase().getMainPopupBodyId(),
targetFocusClass, supportedTypes, false, getPageBase()) {
@Override
protected void onSelectPerformed(AjaxRequestTarget target, H filterUser) {
super.onSelectPerformed(target, filterUser);
filterObject = filterUser;
filterByUserPerformed();
replaceTable(target);
labelValue += " " + filterUser.getName().toString();
target.add(getFilterButton());
}
};
getPageBase().showMainPopup(focusBrowser, title, target, 900, 500);
}
private void filterByUserPerformed(){
//we need to load filter object (e.g. user) totally because we
//need to know its assignments' tenantRef data (name)
//and orgRef data (name)
Task task = getPageBase().createSimpleTask(OPERATION_LOAD_FILTER_OBJECT);
OperationResult result = task.getResult();
PrismObject<H> loadedObject = WebModelServiceUtils.loadObject(targetFocusClass, filterObject.getOid(), null, getPageBase(), task,
result);
provider = getListDataProvider(loadedObject != null ? loadedObject.asObjectable() : filterObject);
}
private void deleteFilterPerformed(AjaxRequestTarget target){
filterObject = null;
provider = getAvailableAssignmentsDataProvider();
provider.setQuery(searchQuery);
replaceTable(target);
}
private void replaceTable(AjaxRequestTarget target){
BoxedTablePanel table = initTablePanel();
MultipleAssignmentSelector.this.replace(table);
target.add(MultipleAssignmentSelector.this);
}
public void setFilterButtonVisibility(boolean isVisible){
getFilterButton().setVisible(isVisible);
}
private IModel<String> createLabelModel(){
return new IModel<String>() {
@Override
public String getObject() {
return labelValue;
}
@Override
public void setObject(String s) {
}
@Override
public void detach() {
}
};
}
private Component getFilterButton(){
return get(createComponentPath(ID_FILTER_BUTTON_CONTAINER, ID_FILTER_BY_USER_BUTTON));
}
public <T extends FocusType> BaseSortableDataProvider getListDataProvider(final FocusType focus) {
BaseSortableDataProvider provider;
provider = new ListDataProvider<AssignmentEditorDto>(this, new IModel<List<AssignmentEditorDto>>() {
@Override
public List<AssignmentEditorDto> getObject() {
return getAvailableAssignmentsDataList(focus);
}
@Override
public void setObject(List<AssignmentEditorDto> list) {
}
@Override
public void detach() {
}
});
return provider;
}
private <T extends FocusType> List<AssignmentEditorDto> getAvailableAssignmentsDataList(FocusType focus){
ObjectQuery query = provider.getQuery() == null ? (searchQuery == null ? new ObjectQuery() : searchQuery) : provider.getQuery();
List<AssignmentEditorDto> assignmentsList = getListProviderDataList();
if (assignmentsList == null) {
if (focus != null) {
assignmentsList = getAssignmentEditorDtoList(focus.getAssignment());
} else {
assignmentsList = getModelObject();
}
}
List<AssignmentEditorDto> currentAssignments = getAssignmentsByType(assignmentsList);
if (filterModel != null && filterModel.getObject() != null) {
query.addFilter(filterModel.getObject());
}
return applyQueryToListProvider(query, currentAssignments);
}
protected List<AssignmentEditorDto> getListProviderDataList(){
return null;
}
private List<AssignmentEditorDto> getAssignmentsByType(List<AssignmentEditorDto> assignmentsList) {
List<AssignmentEditorDto> currentUsersAssignments = new ArrayList<>();
for (AssignmentEditorDto dto : assignmentsList) {
if (dto.getType().equals(AssignmentEditorDtoType.getType(type)) && !dto.getStatus().equals(UserDtoStatus.DELETE)) {
currentUsersAssignments.add(dto);
}
}
return currentUsersAssignments;
}
private List<AssignmentEditorDto> applyQueryToListProvider(ObjectQuery query, List<AssignmentEditorDto> providerList){
ObjectDataProvider temporaryProvider = new ObjectDataProvider(MultipleAssignmentSelector.this, type);
List<AssignmentEditorDto> displayAssignmentsList = new ArrayList<>();
temporaryProvider.setQuery(query);
for (AssignmentEditorDto dto : providerList) {
Iterator it = temporaryProvider.internalIterator(0, temporaryProvider.size());
while (it.hasNext()) {
SelectableBean selectableBean = (SelectableBean) it.next();
F object = (F) selectableBean.getValue();
if (object.getOid().equals(dto.getTargetRef().getOid())) {
displayAssignmentsList.add(dto);
break;
}
}
}
return displayAssignmentsList;
}
private List<AssignmentEditorDto> getAssignmentEditorDtoList(List<AssignmentType> assignmentTypeList){
List<AssignmentEditorDto> assignmentEditorDtoList = new ArrayList<>();
for (AssignmentType assignmentType : assignmentTypeList){
AssignmentEditorDto assignmentEditorDto = new AssignmentEditorDto(UserDtoStatus.MODIFY, assignmentType, getPageBase());
assignmentEditorDtoList.add(assignmentEditorDto);
}
return assignmentEditorDtoList;
}
public ObjectDataProvider getAvailableAssignmentsDataProvider() {
ObjectDataProvider<AssignmentEditorDto, F> provider = new ObjectDataProvider<AssignmentEditorDto, F>(this, type) {
@Override
public AssignmentEditorDto createDataObjectWrapper(PrismObject<F> obj) {
return AssignmentEditorDto.createDtoFromObject(obj.asObjectable(), UserDtoStatus.MODIFY, getPageBase());
}
@Override
public void setQuery(ObjectQuery query) {
super.setQuery(query);
searchQuery = query;
}
@Override
public ObjectQuery getQuery() {
if (searchQuery == null){
searchQuery = new ObjectQuery();
}
if (filterModel != null && filterModel.getObject() != null){
searchQuery.addFilter(filterModel.getObject());
}
return searchQuery;
}
};
return provider;
}
}