-
Notifications
You must be signed in to change notification settings - Fork 188
/
AbstractRoleAssignmentPanel.java
453 lines (376 loc) · 20.9 KB
/
AbstractRoleAssignmentPanel.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
/*
* 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.Arrays;
import java.util.Collections;
import java.util.List;
import javax.xml.namespace.QName;
import com.evolveum.midpoint.gui.api.component.AssignmentPopup;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.schema.constants.ObjectTypes;
import com.evolveum.midpoint.schema.constants.RelationTypes;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.web.component.prism.*;
import com.evolveum.midpoint.web.component.search.SearchFactory;
import com.evolveum.midpoint.web.component.search.SearchItem;
import com.evolveum.midpoint.web.component.search.SearchItemDefinition;
import com.evolveum.midpoint.web.page.admin.PageAdminFocus;
import com.evolveum.midpoint.web.page.admin.users.component.AssignmentInfoDto;
import com.evolveum.midpoint.web.page.admin.users.component.AllAssignmentsPreviewDialog;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import org.apache.commons.lang.StringUtils;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
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.IColumn;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Fragment;
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.TypedAssignablePanel;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.gui.api.util.WebModelServiceUtils;
import com.evolveum.midpoint.gui.impl.component.MultivalueContainerListPanelWithDetailsPanel;
import com.evolveum.midpoint.gui.impl.model.PropertyWrapperFromContainerValueWrapperModel;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.query.ObjectPaging;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.prism.query.builder.QueryBuilder;
import com.evolveum.midpoint.schema.util.ObjectTypeUtil;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.component.AjaxButton;
import com.evolveum.midpoint.web.component.form.Form;
import com.evolveum.midpoint.web.component.input.DropDownChoicePanel;
import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour;
import com.evolveum.midpoint.web.session.UserProfileStorage;
import com.evolveum.midpoint.web.session.UserProfileStorage.TableId;
/**
* Created by honchar.
*/
public class AbstractRoleAssignmentPanel extends AssignmentPanel {
private static final long serialVersionUID = 1L;
private static final Trace LOGGER = TraceManager.getTrace(AssignmentPanel.class);
private static final String ID_RELATION = "relation";
private static final String ID_RELATION_CONTAINER = "relationContainer";
private static final String ID_SHOW_ALL_ASSIGNMENTS_BUTTON = "showAllAssignmentsButton";
protected static final String DOT_CLASS = AbstractRoleAssignmentPanel.class.getName() + ".";
private static final String OPERATION_LOAD_TARGET_REF_OBJECT = DOT_CLASS + "loadAssignmentTargetRefObject";
private RelationTypes relationValue = null;
public AbstractRoleAssignmentPanel(String id, IModel<ContainerWrapper<AssignmentType>> assignmentContainerWrapperModel){
super(id, assignmentContainerWrapperModel);
}
protected Fragment getCustomSearchPanel(String contentAreaId){
Fragment searchContainer = new Fragment(contentAreaId, AssignmentPanel.ID_SEARCH_FRAGMENT, this);
WebMarkupContainer relationContainer = new WebMarkupContainer(ID_RELATION_CONTAINER);
relationContainer.setOutputMarkupId(true);
relationContainer.add(new VisibleEnableBehaviour() {
private static final long serialVersionUID = 1L;
@Override
public boolean isVisible() {
return AbstractRoleAssignmentPanel.this.isRelationVisible();
}
});
searchContainer.addOrReplace(relationContainer);
DropDownChoicePanel<RelationTypes> relation = WebComponentUtil.createEnumPanel(RelationTypes.class, ID_RELATION,
WebComponentUtil.createReadonlyModelFromEnum(RelationTypes.class),
new IModel<RelationTypes>() {
private static final long serialVersionUID = 1L;
@Override
public RelationTypes getObject() {
return relationValue;
}
@Override
public void setObject(RelationTypes relationTypes) {
relationValue = relationTypes;
}
@Override
public void detach() {
}
}, this, true,
createStringResource("RelationTypes.ANY").getString());
relation.getBaseFormComponent().add(new AjaxFormComponentUpdatingBehavior("change") {
private static final long serialVersionUID = 1L;
@Override
protected void onUpdate(AjaxRequestTarget target) {
getMultivalueContainerListPanel().refreshTable(target);
}
});
relation.setOutputMarkupId(true);
relation.setOutputMarkupPlaceholderTag(true);
relationContainer.addOrReplace(relation);
AjaxButton showAllAssignmentsButton = new AjaxButton(ID_SHOW_ALL_ASSIGNMENTS_BUTTON,
createStringResource("AssignmentTablePanel.menu.showAllAssignments")) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget ajaxRequestTarget) {
showAllAssignments(ajaxRequestTarget);
}
};
searchContainer.addOrReplace(showAllAssignmentsButton);
showAllAssignmentsButton.setOutputMarkupId(true);
showAllAssignmentsButton.add(new VisibleEnableBehaviour(){
private static final long serialVersionUID = 1L;
public boolean isVisible(){
return showAllAssignmentsVisible();
}
});
return searchContainer;
}
private DropDownChoicePanel<RelationTypes> getRelationPanel() {
return (DropDownChoicePanel<RelationTypes>) getAssignmentContainer().get(ID_RELATION_CONTAINER).get(ID_RELATION);
}
protected void showAllAssignments(AjaxRequestTarget target) {
PageBase pageBase = getPageBase();
List<AssignmentInfoDto> previewAssignmentsList;
if (pageBase instanceof PageAdminFocus) {
previewAssignmentsList = ((PageAdminFocus<?>) pageBase).showAllAssignmentsPerformed(target);
} else {
previewAssignmentsList = Collections.emptyList();
}
AllAssignmentsPreviewDialog assignmentsDialog = new AllAssignmentsPreviewDialog(pageBase.getMainPopupBodyId(), previewAssignmentsList,
pageBase);
pageBase.showMainPopup(assignmentsDialog, target);
}
@Override
protected void newAssignmentClickPerformed(AjaxRequestTarget target) {
AssignmentPopup popupPanel = new AssignmentPopup(getPageBase().getMainPopupBodyId()) {
private static final long serialVersionUID = 1L;
@Override
protected void addPerformed(AjaxRequestTarget target, List newAssignmentsList) {
super.addPerformed(target, newAssignmentsList);
addSelectedAssignmentsPerformed(target, newAssignmentsList);
}
@Override
protected List<ObjectTypes> getAvailableObjectTypesList(){
return getObjectTypesList();
}
};
popupPanel.setOutputMarkupId(true);
getPageBase().showMainPopup(popupPanel, target);
}
protected void addSelectedAssignmentsPerformed(AjaxRequestTarget target, List<AssignmentType> newAssignmentsList){
if (newAssignmentsList == null || newAssignmentsList.isEmpty()){
warn(getParentPage().getString("AssignmentTablePanel.message.noAssignmentSelected"));
target.add(getPageBase().getFeedbackPanel());
return;
}
newAssignmentsList.forEach(assignment -> {
PrismContainerDefinition<AssignmentType> definition = getModelObject().getItem().getDefinition();
PrismContainerValue<AssignmentType> newAssignment;
try {
newAssignment = definition.instantiate().createNewValue();
AssignmentType assignmentType = newAssignment.asContainerable();
if (assignment.getConstruction() != null && assignment.getConstruction().getResourceRef() != null) {
assignmentType.setConstruction(assignment.getConstruction());
} else {
assignmentType.setTargetRef(assignment.getTargetRef());
}
getMultivalueContainerListPanel().createNewItemContainerValueWrapper(newAssignment, getModel());
getMultivalueContainerListPanel().refreshTable(target);
getMultivalueContainerListPanel().reloadSavePreviewButtons(target);
} catch (SchemaException e) {
getSession().error("Cannot create new assignment " + e.getMessage());
target.add(getPageBase().getFeedbackPanel());
target.add(this);
}
});
}
protected List<IColumn<ContainerValueWrapper<AssignmentType>, String>> initColumns() {
List<IColumn<ContainerValueWrapper<AssignmentType>, String>> columns = new ArrayList<>();
columns.add(new AbstractColumn<ContainerValueWrapper<AssignmentType>, String>(
createStringResource("AbstractRoleAssignmentPanel.relationKindIntentColumn")) {
@Override
public void populateItem(Item<ICellPopulator<ContainerValueWrapper<AssignmentType>>> item, String componentId, IModel<ContainerValueWrapper<AssignmentType>> assignmentModel) {
AssignmentType assignment = assignmentModel.getObject().getContainerValue().asContainerable();
if (assignment.getConstruction() != null){
item.add(new Label(componentId, getKindIntentLabelValue(assignment.getConstruction())));
} else {
item.add(new Label(componentId, getRelationLabelValue(assignmentModel.getObject())));
}
}
});
columns.add(new AbstractColumn<ContainerValueWrapper<AssignmentType>, String>(createStringResource("AssignmentType.tenant")){
private static final long serialVersionUID = 1L;
@Override
public void populateItem(Item<ICellPopulator<ContainerValueWrapper<AssignmentType>>> item, String componentId,
final IModel<ContainerValueWrapper<AssignmentType>> rowModel) {
item.add(new Label(componentId, getTenantLabelModel(rowModel.getObject())));
}
});
columns.add(new AbstractColumn<ContainerValueWrapper<AssignmentType>, String>(createStringResource("AssignmentType.orgReferenceShorten")){
private static final long serialVersionUID = 1L;
@Override
public void populateItem(Item<ICellPopulator<ContainerValueWrapper<AssignmentType>>> item, String componentId,
final IModel<ContainerValueWrapper<AssignmentType>> rowModel) {
item.add(new Label(componentId, getOrgRefLabelModel(rowModel.getObject())));
}
});
columns.add(new AbstractColumn<ContainerValueWrapper<AssignmentType>, String>(createStringResource("AbstractRoleAssignmentPanel.identifierLabel")){
private static final long serialVersionUID = 1L;
@Override
public void populateItem(Item<ICellPopulator<ContainerValueWrapper<AssignmentType>>> item, String componentId,
final IModel<ContainerValueWrapper<AssignmentType>> rowModel) {
item.add(new Label(componentId, getIdentifierLabelModel(rowModel.getObject())));
}
});
return columns;
}
private String getRelationLabelValue(ContainerValueWrapper<AssignmentType> assignmentWrapper){
if (assignmentWrapper == null || assignmentWrapper.getContainerValue() == null || assignmentWrapper.getContainerValue().getValue() == null
|| assignmentWrapper.getContainerValue().getValue().getTargetRef() == null
|| assignmentWrapper.getContainerValue().getValue().getTargetRef().getRelation() == null){
return "";
}
return assignmentWrapper.getContainerValue().getValue().getTargetRef().getRelation().getLocalPart();
}
private String getKindIntentLabelValue(ConstructionType constructionType){
if (constructionType == null){
return "";
}
StringBuilder sb = new StringBuilder();
sb.append(constructionType.getKind() != null && !StringUtils.isEmpty(constructionType.getKind().value()) ?
constructionType.getKind().value() : createStringResource("AssignmentEditorPanel.undefined").getString());
sb.append("/");
sb.append(!StringUtils.isEmpty(constructionType.getIntent()) ? constructionType.getIntent()
: createStringResource("AssignmentEditorPanel.undefined").getString());
return sb.toString();
}
protected boolean showAllAssignmentsVisible(){
return true;
}
protected void initCustomPaging(){
getAssignmentsTabStorage().setPaging(ObjectPaging.createPaging(0, (int) getParentPage().getItemsPerPage(UserProfileStorage.TableId.ASSIGNMENTS_TAB_TABLE)));
}
@Override
protected TableId getTableId() {
return UserProfileStorage.TableId.ASSIGNMENTS_TAB_TABLE;
}
@Override
protected int getItemsPerPage() {
return (int) getParentPage().getItemsPerPage(UserProfileStorage.TableId.ASSIGNMENTS_TAB_TABLE);
}
protected ObjectQuery createObjectQuery() {
QName relation = getRelation();
if (PrismConstants.Q_ANY.equals(relation)){
return QueryBuilder.queryFor(AssignmentType.class, getParentPage().getPrismContext())
.block()
.not()
.item(new ItemPath(AssignmentType.F_CONSTRUCTION, ConstructionType.F_RESOURCE_REF))
.isNull()
.endBlock()
.or()
.block()
.not()
.item(new ItemPath(AssignmentType.F_TARGET_REF))
.ref(SchemaConstants.ORG_DEPUTY)
.endBlock()
.and()
.not()
.exists(AssignmentType.F_POLICY_RULE)
.build();
} else {
return QueryBuilder.queryFor(AssignmentType.class, getParentPage().getPrismContext())
.item(new ItemPath(AssignmentType.F_TARGET_REF))
.ref(relation)
.build();
}
}
private QName getRelation() {
return relationValue == null ? PrismConstants.Q_ANY : relationValue.getRelation();
}
private IModel<String> getTenantLabelModel(ContainerValueWrapper<AssignmentType> assignmentContainer){
if (assignmentContainer == null || assignmentContainer.getContainerValue() == null){
return Model.of("");
}
PropertyOrReferenceWrapper policyRuleWrapper = (PropertyOrReferenceWrapper)assignmentContainer.findPropertyWrapper(new ItemPath(assignmentContainer.getPath(), AssignmentType.F_TENANT_REF));
return Model.of(WebComponentUtil.getReferencedObjectDisplayNamesAndNames((DefaultReferencableImpl)((ValueWrapper<DefaultReferencableImpl>)policyRuleWrapper.getValues().get(0)).getValue().getRealValue(), false));
}
private IModel<String> getOrgRefLabelModel(ContainerValueWrapper<AssignmentType> assignmentContainer){
if (assignmentContainer == null || assignmentContainer.getContainerValue() == null){
return Model.of("");
}
PropertyOrReferenceWrapper policyRuleWrapper = (PropertyOrReferenceWrapper)assignmentContainer.findPropertyWrapper(new ItemPath(assignmentContainer.getPath(), AssignmentType.F_ORG_REF));
return Model.of(WebComponentUtil.getReferencedObjectDisplayNamesAndNames((DefaultReferencableImpl)((ValueWrapper<DefaultReferencableImpl>)policyRuleWrapper.getValues().get(0)).getValue().getRealValue(), false));
}
private <O extends ObjectType> IModel<String> getIdentifierLabelModel(ContainerValueWrapper<AssignmentType> assignmentContainer){
if (assignmentContainer == null || assignmentContainer.getContainerValue() == null){
return Model.of("");
}
AssignmentType assignment = assignmentContainer.getContainerValue().asContainerable();
if (assignment.getTargetRef() == null){
return Model.of("");
}
PrismObject<O> object = WebModelServiceUtils.loadObject(assignment.getTargetRef(), getPageBase(),
getPageBase().createSimpleTask(OPERATION_LOAD_TARGET_REF_OBJECT), new OperationResult(OPERATION_LOAD_TARGET_REF_OBJECT));
if (object == null || !(object.asObjectable() instanceof AbstractRoleType)){
return Model.of("");
}
AbstractRoleType targetRefObject = (AbstractRoleType) object.asObjectable();
if (StringUtils.isNotEmpty(targetRefObject.getIdentifier())){
return Model.of(targetRefObject.getIdentifier());
}
if (targetRefObject.getDisplayName() != null && !targetRefObject.getName().getOrig().equals(targetRefObject.getDisplayName().getOrig())){
return Model.of(targetRefObject.getName().getOrig());
}
return Model.of("");
}
protected boolean isRelationVisible() {
return true;
}
protected List<ObjectTypes> getObjectTypesList(){
return WebComponentUtil.createAssignableTypesList();
}
@Override
protected Fragment getCustomSpecificContainers(String contentAreaId, ContainerValueWrapper<AssignmentType> modelObject) {
Fragment specificContainers = new Fragment(contentAreaId, AssignmentPanel.ID_SPECIFIC_CONTAINERS_FRAGMENT, this);
specificContainers.add(getSpecificContainerPanel(modelObject));
return specificContainers;
}
@Override
protected IModel<ContainerWrapper> getSpecificContainerModel(ContainerValueWrapper<AssignmentType> modelObject) {
if (ConstructionType.COMPLEX_TYPE.equals(AssignmentsUtil.getTargetType(modelObject.getContainerValue().getValue()))) {
ContainerWrapper<ConstructionType> constructionWrapper = modelObject.findContainerWrapper(new ItemPath(modelObject.getPath(),
AssignmentType.F_CONSTRUCTION));
return Model.of(constructionWrapper);
}
if (PersonaConstructionType.COMPLEX_TYPE.equals(AssignmentsUtil.getTargetType(modelObject.getContainerValue().getValue()))) {
ContainerWrapper<PolicyRuleType> personasWrapper = modelObject.findContainerWrapper(new ItemPath(modelObject.getPath(),
AssignmentType.F_PERSONA_CONSTRUCTION));
return Model.of(personasWrapper);
}
return Model.of();
}
@Override
protected List<SearchItemDefinition> createSearchableItems(PrismContainerDefinition<AssignmentType> containerDef) {
List<SearchItemDefinition> defs = new ArrayList<>();
SearchFactory.addSearchRefDef(containerDef, new ItemPath(AssignmentType.F_TARGET_REF), defs, AreaCategoryType.ADMINISTRATION, getPageBase());
SearchFactory.addSearchRefDef(containerDef, new ItemPath(AssignmentType.F_CONSTRUCTION, ConstructionType.F_RESOURCE_REF), defs, AreaCategoryType.ADMINISTRATION, getPageBase());
SearchFactory.addSearchPropertyDef(containerDef, new ItemPath(AssignmentType.F_ACTIVATION, ActivationType.F_ADMINISTRATIVE_STATUS), defs);
SearchFactory.addSearchPropertyDef(containerDef, new ItemPath(AssignmentType.F_ACTIVATION, ActivationType.F_EFFECTIVE_STATUS), defs);
defs.addAll(SearchFactory.createExtensionDefinitionList(containerDef));
return defs;
}
}