-
Notifications
You must be signed in to change notification settings - Fork 188
/
InfoBoxPanel.java
402 lines (359 loc) · 17 KB
/
InfoBoxPanel.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
/*
* Copyright (c) 2010-2019 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.gui.impl.component.box;
import java.util.HashMap;
import javax.xml.namespace.QName;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import com.evolveum.midpoint.gui.api.component.BasePanel;
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.gui.api.util.WebModelServiceUtils;
import com.evolveum.midpoint.model.api.interaction.DashboardWidget;
import com.evolveum.midpoint.model.api.util.DashboardUtils;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.query.ObjectFilter;
import com.evolveum.midpoint.schema.ResourceShadowDiscriminator;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.ObjectQueryUtil;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.QNameUtil;
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.page.admin.reports.PageAuditLogViewer;
import com.evolveum.midpoint.web.page.admin.resources.PageResource;
import com.evolveum.midpoint.web.page.admin.resources.PageResources;
import com.evolveum.midpoint.web.page.admin.roles.PageRole;
import com.evolveum.midpoint.web.page.admin.roles.PageRoles;
import com.evolveum.midpoint.web.page.admin.server.PageTask;
import com.evolveum.midpoint.web.page.admin.server.PageTasks;
import com.evolveum.midpoint.web.page.admin.services.PageService;
import com.evolveum.midpoint.web.page.admin.services.PageServices;
import com.evolveum.midpoint.web.page.admin.users.PageOrgTree;
import com.evolveum.midpoint.web.page.admin.users.PageOrgUnit;
import com.evolveum.midpoint.web.page.admin.users.PageUser;
import com.evolveum.midpoint.web.page.admin.users.PageUsers;
import com.evolveum.midpoint.web.util.OnePageParameterEncoder;
import com.evolveum.midpoint.xml.ns._public.common.audit_3.AuditEventRecordType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import com.evolveum.prism.xml.ns._public.query_3.SearchFilterType;
/**
* @author skublik
*/
public abstract class InfoBoxPanel extends BasePanel<DashboardWidgetType> {
private static final long serialVersionUID = 1L;
private static final Trace LOGGER = TraceManager.getTrace(InfoBoxPanel.class);
private static final String ID_INFO_BOX = "infoBox";
private static final String ID_ICON = "icon";
private static final String ID_MESSAGE = "message";
private static final String ID_NUMBER = "number";
private static HashMap<String, Class<? extends WebPage>> linksRefCollections;
private static HashMap<QName, Class<? extends WebPage>> linksRefObjects;
private LoadableModel<DashboardWidgetDto> dasboardModel;
static {
linksRefCollections = new HashMap<String, Class<? extends WebPage>>() {
private static final long serialVersionUID = 1L;
{
put(ResourceType.COMPLEX_TYPE.getLocalPart(), PageResources.class);
put(AuditEventRecordType.COMPLEX_TYPE.getLocalPart(), PageAuditLogViewer.class);
put(TaskType.COMPLEX_TYPE.getLocalPart(), PageTasks.class);
put(UserType.COMPLEX_TYPE.getLocalPart(), PageUsers.class);
put(RoleType.COMPLEX_TYPE.getLocalPart(), PageRoles.class);
put(OrgType.COMPLEX_TYPE.getLocalPart(), PageOrgTree.class);
put(ServiceType.COMPLEX_TYPE.getLocalPart(), PageServices.class);
}
};
linksRefObjects = new HashMap<QName, Class<? extends WebPage>>() {
private static final long serialVersionUID = 1L;
{
put(TaskType.COMPLEX_TYPE, PageTask.class);
put(UserType.COMPLEX_TYPE, PageUser.class);
put(RoleType.COMPLEX_TYPE, PageRole.class);
put(OrgType.COMPLEX_TYPE, PageOrgUnit.class);
put(ServiceType.COMPLEX_TYPE, PageService.class);
put(ResourceType.COMPLEX_TYPE, PageResource.class);
}
};
}
public InfoBoxPanel(String id, IModel<DashboardWidgetType> model) {
super(id, model);
Validate.notNull(model, "Model must not be null.");
Validate.notNull(model.getObject(), "Model object must not be null.");
add(AttributeModifier.append("class", "dashboard-info-box"));
dasboardModel = new LoadableModel<DashboardWidgetDto>(false) {
@Override
protected DashboardWidgetDto load() {
Task task = getPageBase().createSimpleTask("Get DashboardWidget");
OperationResult result = task.getResult();
try {
DashboardWidget dashboardWidget = getPageBase().getDashboardService().createWidgetData(getModelObject(), task, result);
result.computeStatusIfUnknown();
return new DashboardWidgetDto(dashboardWidget, getPageBase());
} catch (Exception e) {
LOGGER.error("Couldn't get DashboardWidget with widget " + getModelObject().getIdentifier(), e);
result.recordFatalError("Couldn't get widget, reason: " + e.getMessage(), e);
}
result.computeStatusIfUnknown();
getPageBase().showResult(result);
return null;
}
};
}
@Override
protected void onInitialize() {
super.onInitialize();
initLayout();
}
private void initLayout() {
WebMarkupContainer infoBox = new WebMarkupContainer(ID_INFO_BOX);
add(infoBox);
Label number = new Label(ID_NUMBER, new PropertyModel<>(dasboardModel, DashboardWidgetDto.F_NUMBER_LABEL));
infoBox.add(number);
Label message = new Label(ID_MESSAGE, new PropertyModel<>(dasboardModel, DashboardWidgetDto.F_MESSAGE));
infoBox.add(message);
infoBox.add(AttributeModifier.append("style", new PropertyModel<>(dasboardModel, DashboardWidgetDto.F_STYLE_COLOR)));
infoBox.add(AttributeModifier.append("style", new PropertyModel<>(dasboardModel, DashboardWidgetDto.F_STYLE_CSS_STYLE)));
WebMarkupContainer infoBoxIcon = new WebMarkupContainer(ID_ICON);
infoBox.add(infoBoxIcon);
infoBoxIcon.add(AttributeModifier.append("class", new PropertyModel<>(dasboardModel, DashboardWidgetDto.D_ICON_CSS_CLASS)));
customInitLayout(infoBox);
}
protected void customInitLayout(WebMarkupContainer infoBox) {
}
protected static HashMap<String, Class<? extends WebPage>> getLinksRefCollections() {
return linksRefCollections;
}
protected static HashMap<QName, Class<? extends WebPage>> getLinksRefObjects() {
return linksRefObjects;
}
protected void navigateToPage() {
DashboardWidgetType dashboardWidget = getModelObject();
if (dashboardWidget == null) {
return;
}
DashboardWidgetSourceTypeType sourceType = DashboardUtils.getSourceType(dashboardWidget);
if (sourceType == null) {
return;
}
switch (sourceType) {
case OBJECT_COLLECTION:
navigateToObjectCollectionPage(dashboardWidget);
break;
case AUDIT_SEARCH:
navigateToAuditPage(dashboardWidget);
break;
case OBJECT:
navigateToObjectPage();
}
}
private void navigateToObjectCollectionPage(DashboardWidgetType dashboardWidget) {
ObjectCollectionType collection = getObjectCollectionType();
if(collection != null && collection.getType() != null && collection.getType().getLocalPart() != null) {
Class<? extends WebPage> pageType = getLinksRefCollections().get(collection.getType().getLocalPart());
PageParameters parameters = new PageParameters();
if (QNameUtil.match(collection.getType(), ShadowType.COMPLEX_TYPE)) {
pageType = PageResource.class;
String oid = getResourceOid(collection.getFilter());
if (oid != null) {
parameters.add(OnePageParameterEncoder.PARAMETER, oid);
Integer tab = getResourceTab(collection.getFilter());
if (tab != null) {
parameters.add(PageResource.PARAMETER_SELECTED_TAB, tab);
} else {
parameters.add(PageResource.PARAMETER_SELECTED_TAB, 2);
}
}
}
if(pageType == null) {
return;
}
parameters.add(PageBase.PARAMETER_DASHBOARD_TYPE_OID, getDashboardOid());
parameters.add(PageBase.PARAMETER_DASHBOARD_WIDGET_NAME, dashboardWidget.getIdentifier());
getPageBase().navigateToNext(pageType, parameters);
} else {
LOGGER.error("CollectionType from collectionRef is null in widget " + dashboardWidget.getIdentifier());
}
}
private void navigateToAuditPage(DashboardWidgetType dashboardWidget) {
Task task = getPageBase().createSimpleTask("Is audit collection");
CollectionRefSpecificationType collectionRefSpecificationType = getObjectCollectionRef();
if(DashboardUtils.isAuditCollection(collectionRefSpecificationType, getPageBase().getModelService(), task, task.getResult())) {
PageParameters params = new PageParameters();
params.add(PageAuditLogViewer.PARAM_DASHBOARD, getDashboardOid());
params.add(PageAuditLogViewer.PARAM_DASHBOARD_WIDGET, getModelObject().getIdentifier());
getPageBase().navigateToNext(PageAuditLogViewer.class, params);
} else {
LOGGER.error("CollectionType from collectionRef is null in widget " + dashboardWidget.getIdentifier());
}
}
private void navigateToObjectPage() {
ObjectType object = getObjectFromObjectRef();
if(object == null) {
return;
}
QName typeName = WebComponentUtil.classToQName(getPageBase().getPrismContext(), object.getClass());
Class<? extends WebPage> pageType = getLinksRefObjects().get(typeName);
if(pageType == null) {
return;
}
PageParameters parameters = new PageParameters();
parameters.add(OnePageParameterEncoder.PARAMETER, object.getOid());
getPageBase().navigateToNext(pageType, parameters);
}
private Integer getResourceTab(SearchFilterType searchFilterType) {
ResourceShadowDiscriminator discriminator = getResourceShadowDiscriminator(searchFilterType);
if (discriminator == null) {
return null;
}
ShadowKindType shadowKindType = discriminator.getKind();
if (shadowKindType == null) {
return null;
}
switch (shadowKindType) {
case ACCOUNT:
return 2;
case ENTITLEMENT:
return 3;
case GENERIC:
return 4;
}
return null;
}
protected boolean existLinkRef() {
DashboardWidgetType dashboardWidgetType = getModelObject();
if (dashboardWidgetType == null) {
return false;
}
DashboardWidgetSourceTypeType sourceType = DashboardUtils.getSourceType(dashboardWidgetType);
if (sourceType == null) {
return false;
}
switch (sourceType) {
case OBJECT_COLLECTION:
ObjectCollectionType collection = getObjectCollectionType();
if(collection != null && collection.getType() != null && collection.getType().getLocalPart() != null) {
if (QNameUtil.match(collection.getType(), ShadowType.COMPLEX_TYPE)) {
String oid = getResourceOid(collection.getFilter());
return StringUtils.isNotBlank(oid);
}
return getLinksRefCollections().containsKey(collection.getType().getLocalPart());
} else {
return false;
}
case AUDIT_SEARCH:
Task task = getPageBase().createSimpleTask("Is audit collection");
if(DashboardUtils.isAuditCollection(getObjectCollectionRef(), getPageBase().getModelService(), task, task.getResult())) {
return getLinksRefCollections().containsKey(AuditEventRecordType.COMPLEX_TYPE.getLocalPart());
} else {
return false;
}
case OBJECT:
ObjectType object = getObjectFromObjectRef();
if(object == null) {
return false;
}
QName typeName = WebComponentUtil.classToQName(getPageBase().getPrismContext(), object.getClass());
return getLinksRefObjects().containsKey(typeName);
}
return false;
}
private String getResourceOid(SearchFilterType searchFilterType) {
ResourceShadowDiscriminator discriminator = getResourceShadowDiscriminator(searchFilterType);
if (discriminator == null) {
return null;
}
return discriminator.getResourceOid();
}
private ResourceShadowDiscriminator getResourceShadowDiscriminator(SearchFilterType searchFilterType) {
try {
ObjectFilter filter = getPrismContext().getQueryConverter().createObjectFilter(ShadowType.class, searchFilterType);
return ObjectQueryUtil.getCoordinates(filter, getPrismContext());
} catch (SchemaException e) {
LOGGER.error("Cannot convert filter: {}", e.getMessage(), e);
}
return null;
}
private boolean isDataNull(DashboardWidgetType dashboardWidgetType) {
if(dashboardWidgetType.getData() == null) {
LOGGER.error("Data is not found in widget " + dashboardWidgetType.getIdentifier());
return true;
}
return false;
}
private boolean isCollectionOfDataNull(DashboardWidgetType model) {
if(isDataNull(model)) {
return true;
}
if(model.getData().getCollection() == null) {
LOGGER.error("Collection of data is not found in widget " + model.getIdentifier());
return true;
}
return false;
}
private boolean isCollectionRefOfCollectionNull(DashboardWidgetType model) {
if(isDataNull(model)) {
return true;
}
if(isCollectionOfDataNull(model)) {
return true;
}
ObjectReferenceType ref = model.getData().getCollection().getCollectionRef();
if(ref == null) {
LOGGER.error("CollectionRef of collection is not found in widget " + model.getIdentifier());
return true;
}
return false;
}
private ObjectCollectionType getObjectCollectionType() {
CollectionRefSpecificationType collectionRef = getObjectCollectionRef();
if (collectionRef == null) {
return null;
}
ObjectReferenceType ref = collectionRef.getCollectionRef();
Task task = getPageBase().createSimpleTask("Search collection");
PrismObject<ObjectCollectionType> objectCollection = WebModelServiceUtils.loadObject(ref, getPageBase(), task, task.getResult());
if (objectCollection == null) {
return null;
}
return objectCollection.asObjectable();
}
private CollectionRefSpecificationType getObjectCollectionRef() {
DashboardWidgetType model = getModelObject();
if(isCollectionRefOfCollectionNull(model)) {
return null;
}
return model.getData().getCollection();
}
private <O extends ObjectType> O getObjectFromObjectRef() {
DashboardWidgetType model = getModelObject();
if(isDataNull(model)) {
return null;
}
ObjectReferenceType ref = model.getData().getObjectRef();
if(ref == null) {
LOGGER.error("ObjectRef of data is not found in widget " + model.getIdentifier());
return null;
}
Task task = getPageBase().createSimpleTask("Search domain collection");
PrismObject<O> object = WebModelServiceUtils.loadObject(ref, getPageBase(), task, task.getResult());
if(object == null) {
LOGGER.error("Object from ObjectRef " + ref + " is null in widget " + model.getIdentifier());
return null;
}
return object.asObjectable();
}
public abstract String getDashboardOid();
}