/
InfoBoxPanel.java
402 lines (355 loc) · 17.6 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 com.evolveum.midpoint.web.page.admin.server.PageTasks;
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.markup.html.panel.Panel;
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.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.task.api.Task;
import com.evolveum.midpoint.util.exception.CommunicationException;
import com.evolveum.midpoint.util.exception.ConfigurationException;
import com.evolveum.midpoint.util.exception.ExpressionEvaluationException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SecurityViolationException;
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.reports.dto.AuditSearchDto;
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.PageTaskEdit;
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.AuditEventRecordItemType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.DashboardWidgetSourceTypeType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.DashboardWidgetType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.DisplayType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectCollectionType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.RoleType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ServiceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
/**
* @author skublik
*/
public abstract class InfoBoxPanel extends Panel{
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 final String DEFAULT_BACKGROUND_COLOR = "background-color:#00a65a;";
private static final String DEFAULT_COLOR = "color: #fff !important;";
private static final String DEFAULT_ICON = "fa fa-question";
private static final String NUMBER_MESSAGE_UNKNOWN = "InfoBoxPanel.message.unknown";
private static HashMap<String, Class<? extends WebPage>> linksRefCollections;
private static HashMap<QName, Class<? extends WebPage>> linksRefObjects;
static {
linksRefCollections = new HashMap<String, Class<? extends WebPage>>() {
private static final long serialVersionUID = 1L;
{
put(ResourceType.COMPLEX_TYPE.getLocalPart(), PageResources.class);
put(AuditEventRecordItemType.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, PageTaskEdit.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);
}
};
}
private static PageBase pageBase;
private DisplayType display;
public InfoBoxPanel(String id, IModel<DashboardWidgetType> model, PageBase pageBase) {
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"));
this.pageBase = pageBase;
}
@Override
protected void onInitialize() {
super.onInitialize();
initLayout();
}
private void initLayout() {
IModel<DashboardWidgetType> model = (IModel<DashboardWidgetType>)getDefaultModel();
IModel<DashboardWidget> data = new IModel<DashboardWidget>() {
private static final long serialVersionUID = 1L;
@Override
public DashboardWidget getObject() {
Task task = getPageBase().createSimpleTask("Get DashboardWidget");
try {
DashboardWidget ret = getPageBase().getDashboardService().createWidgetData(model.getObject(), task, task.getResult());
setDisplay(ret.getDisplay());
return ret;
} catch (SchemaException | CommunicationException | ConfigurationException | SecurityViolationException
| ExpressionEvaluationException | ObjectNotFoundException e) {
LOGGER.error("Couldn't get DashboardWidget with widget " + model.getObject().getIdentifier(), e);
}
return null;
}
};
this.display = model.getObject().getDisplay();
WebMarkupContainer infoBox = new WebMarkupContainer(ID_INFO_BOX);
add(infoBox);
Label number = new Label(ID_NUMBER,
data.getObject().getNumberMessage() == null ?
getPageBase().createStringResource(NUMBER_MESSAGE_UNKNOWN) :
getStringModel(data.getObject().getNumberMessage())); //number message have to add before icon because is needed evaluate variation
infoBox.add(number);
IModel<DisplayType> displayModel = new IModel<DisplayType>() {
private static final long serialVersionUID = 1L;
@Override
public DisplayType getObject() {
return display;
}
};
Label message = null;
if(displayModel.getObject() != null && displayModel.getObject().getLabel() != null) {
message = new Label(ID_MESSAGE, new PropertyModel<String>(displayModel, "label"));
} else {
message = new Label(ID_MESSAGE, new PropertyModel<String>(model, "identifier"));
}
infoBox.add(message);
if(displayModel.getObject() != null && StringUtils.isNoneBlank(displayModel.getObject().getColor())) {
String color = displayModel.getObject().getColor();
infoBox.add(AttributeModifier.append("style", getStringModel("background-color:" + color + ";")));
} else {
infoBox.add(AttributeModifier.append("style", getStringModel(DEFAULT_BACKGROUND_COLOR)));
}
if(displayModel.getObject() != null && StringUtils.isNoneBlank(displayModel.getObject().getCssStyle())) {
String style = displayModel.getObject().getCssStyle();
infoBox.add(AttributeModifier.append("style", style));
if(!style.toLowerCase().contains(" color:") && !style.toLowerCase().startsWith("color:")) {
infoBox.add(AttributeModifier.append("style", getStringModel(DEFAULT_COLOR)));
}
} else {
infoBox.add(AttributeModifier.append("style", getStringModel(DEFAULT_COLOR)));
}
WebMarkupContainer infoBoxIcon = new WebMarkupContainer(ID_ICON);
infoBox.add(infoBoxIcon);
if(displayModel.getObject() != null && displayModel.getObject().getIcon() != null
&& StringUtils.isNoneBlank(displayModel.getObject().getIcon().getCssClass())) {
infoBoxIcon.add(AttributeModifier.append("class", new PropertyModel<String>(displayModel, "icon.cssClass")));
} else {
infoBoxIcon.add(AttributeModifier.append("class", getStringModel(DEFAULT_ICON)));
}
customInitLayout(infoBox);
}
public void setDisplay(DisplayType display) {
this.display = display;
}
private DashboardWidgetSourceTypeType getSourceType(IModel<DashboardWidgetType> model) {
if(isSourceTypeOfDataNull(model)) {
return null;
}
return model.getObject().getData().getSourceType();
}
protected void customInitLayout(WebMarkupContainer infoBox) {
}
private IModel<String> getStringModel(String value){
return new IModel<String>() {
private static final long serialVersionUID = 1L;
@Override
public String getObject() {
return value;
}
};
}
protected static HashMap<String, Class<? extends WebPage>> getLinksRefCollections() {
return linksRefCollections;
}
protected static HashMap<QName, Class<? extends WebPage>> getLinksRefObjects() {
return linksRefObjects;
}
protected static PageBase getPageBase() {
return pageBase;
}
protected WebPage getLinkRef() {
IModel<DashboardWidgetType> model = (IModel<DashboardWidgetType>)getDefaultModel();
DashboardWidgetSourceTypeType sourceType = getSourceType(model);
switch (sourceType) {
case OBJECT_COLLECTION:
ObjectCollectionType collection = getObjectCollectionType();
if(collection != null && collection.getType() != null && collection.getType().getLocalPart() != null) {
Class<? extends WebPage> pageType = getLinksRefCollections().get(collection.getType().getLocalPart());
if(pageType == null) {
return null;
}
return getPageBase().createWebPage(pageType, null);
} else {
LOGGER.error("CollectionType from collectionRef is null in widget " + model.getObject().getIdentifier());
}
break;
case AUDIT_SEARCH:
collection = getObjectCollectionType();
if(collection != null && collection.getAuditSearch() != null && collection.getAuditSearch().getRecordQuery() != null) {
Class<? extends WebPage> pageType = getLinksRefCollections().get(AuditEventRecordItemType.COMPLEX_TYPE.getLocalPart());
if(pageType == null) {
return null;
}
AuditSearchDto searchDto = new AuditSearchDto();
searchDto.setCollection(collection);
getPageBase().getSessionStorage().getAuditLog().setSearchDto(searchDto);
return getPageBase().createWebPage(pageType, null);
} else {
LOGGER.error("CollectionType from collectionRef is null in widget " + model.getObject().getIdentifier());
}
break;
case OBJECT:
ObjectType object = getObjectFromObjectRef();
if(object == null) {
return null;
}
QName typeName = WebComponentUtil.classToQName(getPageBase().getPrismContext(), object.getClass());
Class<? extends WebPage> pageType = getLinksRefObjects().get(typeName);
if(pageType == null) {
return null;
}
PageParameters parameters = new PageParameters();
parameters.add(OnePageParameterEncoder.PARAMETER, object.getOid());
return getPageBase().createWebPage(pageType, parameters);
}
return null;
}
protected boolean existLinkRef() {
IModel<DashboardWidgetType> model = (IModel<DashboardWidgetType>)getDefaultModel();
DashboardWidgetSourceTypeType sourceType = getSourceType(model);
switch (sourceType) {
case OBJECT_COLLECTION:
ObjectCollectionType collection = getObjectCollectionType();
if(collection != null && collection.getType() != null && collection.getType().getLocalPart() != null) {
return getLinksRefCollections().containsKey(collection.getType().getLocalPart());
} else {
return false;
}
case AUDIT_SEARCH:
collection = getObjectCollectionType();
if(collection != null && collection.getAuditSearch() != null && collection.getAuditSearch().getRecordQuery() != null) {
return getLinksRefCollections().containsKey(AuditEventRecordItemType.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 boolean isDataNull(IModel<DashboardWidgetType> model) {
if(model.getObject().getData() == null) {
LOGGER.error("Data is not found in widget " + model.getObject().getIdentifier());
return true;
}
return false;
}
private boolean isSourceTypeOfDataNull(IModel<DashboardWidgetType> model) {
if(isDataNull(model)) {
return true;
}
if(model.getObject().getData().getSourceType() == null) {
LOGGER.error("SourceType of data is not found in widget " + model.getObject().getIdentifier());
return true;
}
return false;
}
private boolean isCollectionOfDataNull(IModel<DashboardWidgetType> model) {
if(isDataNull(model)) {
return true;
}
if(model.getObject().getData().getCollection() == null) {
LOGGER.error("Collection of data is not found in widget " + model.getObject().getIdentifier());
return true;
}
return false;
}
private boolean isCollectionRefOfCollectionNull(IModel<DashboardWidgetType> model) {
if(isDataNull(model)) {
return true;
}
if(isCollectionOfDataNull(model)) {
return true;
}
ObjectReferenceType ref = model.getObject().getData().getCollection().getCollectionRef();
if(ref == null) {
LOGGER.error("CollectionRef of collection is not found in widget " + model.getObject().getIdentifier());
return true;
}
return false;
}
private ObjectCollectionType getObjectCollectionType() {
IModel<DashboardWidgetType> model = (IModel<DashboardWidgetType>)getDefaultModel();
if(isCollectionRefOfCollectionNull(model)) {
return null;
}
ObjectReferenceType ref = model.getObject().getData().getCollection().getCollectionRef();
Task task = getPageBase().createSimpleTask("Search collection");
ObjectCollectionType collection = (ObjectCollectionType)WebModelServiceUtils.loadObject(ref,
getPageBase(), task, task.getResult()).getRealValue();
return collection;
}
private ObjectType getObjectFromObjectRef() {
IModel<DashboardWidgetType> model = (IModel<DashboardWidgetType>)getDefaultModel();
if(isDataNull(model)) {
return null;
}
ObjectReferenceType ref = model.getObject().getData().getObjectRef();
if(ref == null) {
LOGGER.error("ObjectRef of data is not found in widget " + model.getObject().getIdentifier());
return null;
}
Task task = getPageBase().createSimpleTask("Search domain collection");
ObjectType object = WebModelServiceUtils.loadObject(ref,
getPageBase(), task, task.getResult()).getRealValue();
if(object == null) {
LOGGER.error("Object from ObjectRef " + ref + " is null in widget " + model.getObject().getIdentifier());
}
return object;
}
}