-
Notifications
You must be signed in to change notification settings - Fork 188
/
PageResources.java
473 lines (394 loc) · 17.8 KB
/
PageResources.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
/*
* Copyright (c) 2010-2015 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.page.admin.resources;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.evolveum.midpoint.web.component.data.column.DoubleButtonColumn;
import com.evolveum.midpoint.web.component.data.column.InlineMenuButtonColumn;
import com.evolveum.midpoint.web.component.dialog.ConfirmationPanel;
import com.evolveum.midpoint.web.component.search.*;
import com.evolveum.midpoint.web.session.PageStorage;
import com.evolveum.midpoint.web.session.SessionStorage;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import org.apache.commons.lang.StringUtils;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
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.markup.html.form.Form;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import com.evolveum.midpoint.gui.api.GuiStyleConstants;
import com.evolveum.midpoint.gui.api.component.MainObjectListPanel;
import com.evolveum.midpoint.gui.api.model.LoadableModel;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
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.schema.result.OperationResultStatus;
import com.evolveum.midpoint.security.api.AuthorizationConstants;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.application.AuthorizationAction;
import com.evolveum.midpoint.web.application.PageDescriptor;
import com.evolveum.midpoint.web.component.data.Table;
import com.evolveum.midpoint.web.component.data.column.ColumnMenuAction;
import com.evolveum.midpoint.web.component.data.column.InlineMenuHeaderColumn;
import com.evolveum.midpoint.web.component.menu.cog.InlineMenuItem;
import com.evolveum.midpoint.web.component.util.SelectableBean;
import com.evolveum.midpoint.web.page.admin.configuration.PageDebugView;
import com.evolveum.midpoint.web.page.admin.configuration.component.HeaderMenuAction;
import com.evolveum.midpoint.web.session.ResourcesStorage;
import com.evolveum.midpoint.web.session.UserProfileStorage.TableId;
import com.evolveum.midpoint.web.util.OnePageParameterEncoder;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType;
/**
* @author lazyman
*/
@PageDescriptor(url = "/admin/resources", action = {
@AuthorizationAction(actionUri = PageAdminResources.AUTH_RESOURCE_ALL, label = PageAdminResources.AUTH_RESOURCE_ALL_LABEL, description = PageAdminResources.AUTH_RESOURCE_ALL_DESCRIPTION),
@AuthorizationAction(actionUri = AuthorizationConstants.AUTZ_UI_RESOURCES_URL, label = "PageResources.auth.resources.label", description = "PageResources.auth.resources.description") })
public class PageResources extends PageAdminResources {
private static final long serialVersionUID = 1L;
private static final Trace LOGGER = TraceManager.getTrace(PageResources.class);
private static final String DOT_CLASS = PageResources.class.getName() + ".";
private static final String OPERATION_TEST_RESOURCE = DOT_CLASS + "testResource";
private static final String OPERATION_DELETE_RESOURCES = DOT_CLASS + "deleteResources";
private static final String ID_MAIN_FORM = "mainForm";
private static final String ID_TABLE = "table";
private static final String ID_CONNECTOR_TABLE = "connectorTable";
private IModel<Search> searchModel;
private ResourceType singleDelete;
public PageResources() {
this(true);
}
public PageResources(boolean clearSessionPaging) {
this(clearSessionPaging, "");
}
public PageResources(String searchText) {
this(true, searchText);
}
public PageResources(boolean clearSessionPaging, String searchText) {
searchModel = new LoadableModel<Search>(false) {
private static final long serialVersionUID = 1L;
@Override
protected Search load() {
ResourcesStorage storage = getSessionStorage().getResources();
Search dto = storage.getSearch();
if (dto == null) {
dto = SearchFactory.createSearch(ResourceType.class, getPrismContext(),
getModelInteractionService());
}
return dto;
}
};
if (StringUtils.isNotEmpty(searchText)){
initSearch(searchText);
}
initLayout();
}
private void initSearch(String text){
PageStorage storage = getSessionStorage().getPageStorageMap().get(SessionStorage.KEY_RESOURCES);
if (storage == null) {
storage = getSessionStorage().initPageStorage(SessionStorage.KEY_RESOURCES);
}
Search search = SearchFactory.createSearch(UserType.class, getPrismContext(), getModelInteractionService());
if (search.getItems() != null && search.getItems().size() > 0){
SearchItem searchItem = search.getItems().get(0);
searchItem.getValues().add(new SearchValue<>(text));
}
storage.setSearch(search);
getSessionStorage().getPageStorageMap().put(SessionStorage.KEY_RESOURCES, storage);
}
private void initLayout() {
Form mainForm = new Form(ID_MAIN_FORM);
add(mainForm);
Collection<SelectorOptions<GetOperationOptions>> options = SelectorOptions
.createCollection(GetOperationOptions.createNoFetch());
options.add(SelectorOptions.create(ResourceType.F_CONNECTOR_REF,
GetOperationOptions.createRetrieve(RetrieveOption.INCLUDE)));
MainObjectListPanel<ResourceType> resourceListPanel = new MainObjectListPanel<ResourceType>(ID_TABLE,
ResourceType.class, TableId.TABLE_RESOURCES, options, this) {
private static final long serialVersionUID = 1L;
@Override
protected List<InlineMenuItem> createInlineMenu() {
return PageResources.this.createRowMenuItems(false);
}
@Override
protected List<IColumn<SelectableBean<ResourceType>, String>> createColumns() {
return PageResources.this.initResourceColumns();
}
@Override
protected IColumn<SelectableBean<ResourceType>, String> createActionsColumn() {
return PageResources.this.createResourceActionsColumn();
}
@Override
protected void objectDetailsPerformed(AjaxRequestTarget target, ResourceType object) {
PageResources.this.resourceDetailsPerformed(target, object.getOid());
}
@Override
protected void newObjectPerformed(AjaxRequestTarget target) {
navigateToNext(PageResourceWizard.class);
}
};
resourceListPanel.setOutputMarkupId(true);
resourceListPanel.setAdditionalBoxCssClasses(GuiStyleConstants.CLASS_OBJECT_RESOURCE_BOX_CSS_CLASSES);
mainForm.add(resourceListPanel);
}
private List<InlineMenuItem> createRowMenuItems(boolean isHeader) {
List<InlineMenuItem> menuItems = new ArrayList<>();
menuItems.add(new InlineMenuItem(createStringResource("PageResources.inlineMenuItem.test"),
new Model<Boolean>(false), new Model<Boolean>(false), false,
new ColumnMenuAction<SelectableBean<ResourceType>>() {
@Override
public void onClick(AjaxRequestTarget target) {
SelectableBean<ResourceType> rowDto = getRowModel().getObject();
testResourcePerformed(target, rowDto.getValue());
}
}, isHeader ? InlineMenuItem.RESOURCE_INLINE_MENU_ITEM_ID.HEADER_TEST_CONNECTION.getMenuItemId()
: InlineMenuItem.RESOURCE_INLINE_MENU_ITEM_ID.TEST_CONNECTION.getMenuItemId(),
GuiStyleConstants.CLASS_TEST_CONNECTION_MENU_ITEM,
DoubleButtonColumn.BUTTON_COLOR_CLASS.INFO.toString()));
menuItems.add(new InlineMenuItem(createStringResource("PageBase.button.delete"),
new Model<Boolean>(true), new Model<Boolean>(true), false,
new ColumnMenuAction<SelectableBean<ResourceType>>() {
@Override
public void onClick(AjaxRequestTarget target) {
if (getRowModel() == null){
deleteResourcePerformed(target, null);
} else {
SelectableBean<ResourceType> rowDto = getRowModel().getObject();
deleteResourcePerformed(target, rowDto.getValue());
}
}
}, isHeader ? InlineMenuItem.RESOURCE_INLINE_MENU_ITEM_ID.HEADER_DELETE.getMenuItemId()
: InlineMenuItem.RESOURCE_INLINE_MENU_ITEM_ID.DELETE_RESOURCE.getMenuItemId(),
GuiStyleConstants.CLASS_DELETE_MENU_ITEM,
DoubleButtonColumn.BUTTON_COLOR_CLASS.DANGER.toString()));
menuItems.add(new InlineMenuItem(createStringResource("pageResources.inlineMenuItem.deleteSyncToken"),
isHeader ? new Model<Boolean>(false) : new Model<Boolean>(true),
isHeader ? new Model<Boolean>(false) : new Model<Boolean>(true),
false,
new ColumnMenuAction<SelectableBean<ResourceType>>() {
@Override
public void onClick(AjaxRequestTarget target) {
SelectableBean<ResourceType> rowDto = getRowModel().getObject();
deleteResourceSyncTokenPerformed(target, rowDto.getValue());
}
}));
menuItems.add(new InlineMenuItem(createStringResource("pageResources.inlineMenuItem.editResource"),
isHeader ? new Model<Boolean>(false) : new Model<Boolean>(true),
isHeader ? new Model<Boolean>(false) : new Model<Boolean>(true),
false,
new ColumnMenuAction<SelectableBean<ResourceType>>() {
@Override
public void onClick(AjaxRequestTarget target) {
SelectableBean<ResourceType> rowDto = getRowModel().getObject();
editResourcePerformed(rowDto.getValue());
}
}));
menuItems.add(new InlineMenuItem(createStringResource("pageResources.button.editAsXml"),
new Model<Boolean>(false), new Model<Boolean>(false), false,
new ColumnMenuAction<SelectableBean<ResourceType>>() {
@Override
public void onClick(AjaxRequestTarget target) {
SelectableBean<ResourceType> rowDto = getRowModel().getObject();
editAsXmlPerformed(rowDto.getValue());
}
}, InlineMenuItem.RESOURCE_INLINE_MENU_ITEM_ID.EDIT_XML.getMenuItemId(),
GuiStyleConstants.CLASS_EDIT_MENU_ITEM,
DoubleButtonColumn.BUTTON_COLOR_CLASS.INFO.toString()));
return menuItems;
}
private List<IColumn<SelectableBean<ResourceType>, String>> initResourceColumns() {
List<IColumn<SelectableBean<ResourceType>, String>> columns = new ArrayList<>();
columns.add(new PropertyColumn(createStringResource("pageResources.connectorType"),
SelectableBean.F_VALUE + ".connector.connectorType"));
columns.add(new PropertyColumn(createStringResource("pageResources.version"),
SelectableBean.F_VALUE + ".connector.connectorVersion"));
return columns;
}
private IColumn<SelectableBean<ResourceType>, String> createResourceActionsColumn() {
return new InlineMenuButtonColumn<SelectableBean<ResourceType>>(createRowMenuItems(false), 2){
@Override
protected int getHeaderNumberOfButtons() {
return 1;
}
@Override
protected List<InlineMenuItem> getHeaderMenuItems() {
return createRowMenuItems(true);
}
};
}
private void resourceDetailsPerformed(AjaxRequestTarget target, String oid) {
clearSessionStorageForResourcePage();
PageParameters parameters = new PageParameters();
parameters.add(OnePageParameterEncoder.PARAMETER, oid);
navigateToNext(PageResource.class, parameters);
}
private List<ResourceType> isAnyResourceSelected(AjaxRequestTarget target, ResourceType single) {
List<ResourceType> selected = null;
if (single != null) {
selected = new ArrayList<>(1);
selected.add(single);
return selected;
}
selected = getResourceTable().getSelectedObjects();
if (selected.size() < 1) {
warn(createStringResource("pageResources.message.noResourceSelected"));
}
return selected;
}
private void deleteResourcePerformed(AjaxRequestTarget target, ResourceType single) {
List<ResourceType> selected = isAnyResourceSelected(target, single);
singleDelete = single;
if (selected.isEmpty()) {
return;
}
ConfirmationPanel dialog = new ConfirmationPanel(((PageBase)getPage()).getMainPopupBodyId(),
createDeleteConfirmString("pageResources.message.deleteResourceConfirm",
"pageResources.message.deleteResourcesConfirm", true)){
@Override
public void yesPerformed(AjaxRequestTarget target) {
((PageBase)getPage()).hideMainPopup(target);
deleteResourceConfirmedPerformed(target);
}
};
((PageBase)getPage()).showMainPopup(dialog, target);
}
private MainObjectListPanel<ResourceType> getResourceTable() {
return (MainObjectListPanel<ResourceType>) get(createComponentPath(ID_MAIN_FORM, ID_TABLE));
}
private Table getConnectorHostTable() {
return (Table) get(createComponentPath(ID_MAIN_FORM, ID_CONNECTOR_TABLE));
}
/**
* @param oneDeleteKey
* message if deleting one item
* @param moreDeleteKey
* message if deleting more items
* @param resources
* if true selecting resources if false selecting from hosts
*/
private IModel<String> createDeleteConfirmString(final String oneDeleteKey, final String moreDeleteKey,
final boolean resources) {
return new AbstractReadOnlyModel<String>() {
@Override
public String getObject() {
List selected = new ArrayList();
if (singleDelete != null) {
selected.add(singleDelete);
} else {
selected = getResourceTable().getSelectedObjects();
}
switch (selected.size()) {
case 1:
Object first = selected.get(0);
String name = WebComponentUtil.getName(((ResourceType) first));
return createStringResource(oneDeleteKey, name).getString();
default:
return createStringResource(moreDeleteKey, selected.size()).getString();
}
}
};
}
private void deleteResourceConfirmedPerformed(AjaxRequestTarget target) {
List<ResourceType> selected = new ArrayList<>();
if (singleDelete != null) {
selected.add(singleDelete);
} else {
selected = getResourceTable().getSelectedObjects();
}
OperationResult result = new OperationResult(OPERATION_DELETE_RESOURCES);
for (ResourceType resource : selected) {
try {
Task task = createSimpleTask(OPERATION_DELETE_RESOURCES);
ObjectDelta<ResourceType> delta = ObjectDelta.createDeleteDelta(ResourceType.class,
resource.getOid(), getPrismContext());
getModelService().executeChanges(WebComponentUtil.createDeltaCollection(delta), null, task,
result);
} catch (Exception ex) {
result.recordPartialError("Couldn't delete resource.", ex);
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't delete resource", ex);
}
}
result.recomputeStatus();
if (result.isSuccess()) {
result.recordStatus(OperationResultStatus.SUCCESS,
"The resource(s) have been successfully deleted.");
}
getResourceTable().clearCache();
showResult(result);
target.add(getFeedbackPanel(), (Component) getResourceTable());
}
private void testResourcePerformed(AjaxRequestTarget target, ResourceType resourceType) {
OperationResult result = new OperationResult(OPERATION_TEST_RESOURCE);
// SelectableBean<ResourceType> dto = rowModel.getObject();
// ResourceType resourceType = dto.getValue();
if (StringUtils.isEmpty(resourceType.getOid())) {
result.recordFatalError("Resource oid not defined in request");
}
Task task = createSimpleTask(OPERATION_TEST_RESOURCE);
try {
result = getModelService().testResource(resourceType.getOid(), task);
// ResourceController.updateResourceState(resourceType.getState(),
// result);
// todo de-duplicate code (see the same operation in PageResource)
// this provides some additional tests, namely a test for schema
// handling section
getModelService().getObject(ResourceType.class, resourceType.getOid(), null, task, result);
} catch (Exception ex) {
result.recordFatalError("Failed to test resource connection", ex);
}
// a bit of hack: result of TestConnection contains a result of
// getObject as a subresult
// so in case of TestConnection succeeding we recompute the result to
// show any (potential) getObject problems
if (result.isSuccess()) {
result.recomputeStatus();
}
// if (!result.isSuccess()) {
showResult(result);
target.add(getFeedbackPanel());
// }
target.add(getResourceTable());
}
private void deleteResourceSyncTokenPerformed(AjaxRequestTarget target, ResourceType resourceType) {
deleteSyncTokenPerformed(target, resourceType);
}
private void editResourcePerformed(ResourceType resourceType) {
PageParameters parameters = new PageParameters();
parameters.add(OnePageParameterEncoder.PARAMETER, resourceType.getOid());
navigateToNext(new PageResourceWizard(parameters));
}
private void editAsXmlPerformed(ResourceType resourceType) {
PageParameters parameters = new PageParameters();
parameters.add(PageDebugView.PARAM_OBJECT_ID, resourceType.getOid());
parameters.add(PageDebugView.PARAM_OBJECT_TYPE, ResourceType.class.getSimpleName());
navigateToNext(PageDebugView.class, parameters);
}
private void clearSessionStorageForResourcePage() {
((PageBase) getPage()).getSessionStorage().clearResourceContentStorage();
}
}