/
AbstractRoleAssignmentPanel.java
362 lines (301 loc) · 15.3 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
/*
* 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.PrismContainerValue;
import com.evolveum.midpoint.schema.constants.ObjectTypes;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.web.component.prism.*;
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.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.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.prism.PrismConstants;
import com.evolveum.midpoint.prism.PrismContainerDefinition;
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.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 String ID_RELATION = "relation";
private static final String ID_RELATION_CONTAINER = "relationContainer";
private static final String ID_SHOW_ALL_ASSIGNMENTS_BUTTON = "showAllAssignmentsButton";
private RelationTypes relationValue = null;
public AbstractRoleAssignmentPanel(String id, IModel<ContainerWrapper<AssignmentType>> assignmentContainerWrapperModel){
super(id, assignmentContainerWrapperModel);
}
protected void initCustomLayout(WebMarkupContainer assignmentsContainer){
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();
}
});
assignmentsContainer.addOrReplace(relationContainer);
DropDownChoicePanel<RelationTypes> relation = WebComponentUtil.createEnumPanel(RelationTypes.class, ID_RELATION,
WebComponentUtil.createReadonlyModelFromEnum(RelationTypes.class),
new IModel<RelationTypes>() {
@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) {
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);
}
};
assignmentsContainer.addOrReplace(showAllAssignmentsButton);
showAllAssignmentsButton.setOutputMarkupId(true);
showAllAssignmentsButton.add(new VisibleEnableBehaviour(){
private static final long serialVersionUID = 1L;
public boolean isVisible(){
return showAllAssignmentsVisible();
}
});
}
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> getObjectTypesList(){
// return AbstractRoleAssignmentPanel.this.getObjectTypesList();
// }
};
popupPanel.setOutputMarkupId(true);
getPageBase().showMainPopup(popupPanel, target);
}
protected Class getDefaultNewAssignmentFocusType(){
return RoleType.class;
}
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 (ResourceType.class.equals(object.getClass())) {
// ConstructionType constructionType = new ConstructionType();
// constructionType.setResourceRef(ref);
// constructionType.setKind(kind);
// constructionType.setIntent(intent);
// assignmentType.setConstruction(constructionType);
// } else {
assignmentType.setTargetRef(assignment.getTargetRef());
// }
createNewAssignmentContainerValueWrapper(newAssignment);
refreshTable(target);
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("ObjectReferenceType.relation")) {
@Override
public void populateItem(Item<ICellPopulator<ContainerValueWrapper<AssignmentType>>> item, String componentId, IModel<ContainerValueWrapper<AssignmentType>> assignmentModel) {
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())));
}
});
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();
}
protected boolean showAllAssignmentsVisible(){
return true;
}
private VisibleEnableBehaviour visibleIfRoleBehavior(IModel<AssignmentEditorDto> assignmentModel){
return new VisibleEnableBehaviour(){
private static final long serialVersionUID = 1L;
@Override
public boolean isVisible(){
return AssignmentEditorDtoType.ROLE.equals(assignmentModel.getObject().getType());
}
};
}
protected void initPaging(){
getAssignmentsStorage().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();
}
@Override
protected AbstractAssignmentDetailsPanel createDetailsPanel(String idAssignmentDetails, Form<?> form, IModel<ContainerValueWrapper<AssignmentType>> model) {
return new AbstractRoleAssignmentDetailsPanel(ID_ASSIGNMENT_DETAILS, form, model);
}
private IModel<String> getTenantLabelModel(ContainerValueWrapper<AssignmentType> assignmentContainer){
if (assignmentContainer == null || assignmentContainer.getContainerValue() == null){
return Model.of("");
}
AssignmentType assignment = assignmentContainer.getContainerValue().asContainerable();
return Model.of(WebComponentUtil.getReferencedObjectDisplayNamesAndNames(Arrays.asList(assignment.getTenantRef()), false));
}
private IModel<String> getOrgRefLabelModel(ContainerValueWrapper<AssignmentType> assignmentContainer){
if (assignmentContainer == null || assignmentContainer.getContainerValue() == null){
return Model.of("");
}
AssignmentType assignment = assignmentContainer.getContainerValue().asContainerable();
return Model.of(WebComponentUtil.getReferencedObjectDisplayNamesAndNames(Arrays.asList(assignment.getOrgRef()), false));
}
protected boolean isRelationVisible() {
return true;
}
protected List<ObjectTypes> getObjectTypesList(){
return WebComponentUtil.createAssignableTypesList();
}
}