/
ReportHTMLCreateTaskHandler.java
624 lines (556 loc) · 25 KB
/
ReportHTMLCreateTaskHandler.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/*
* Copyright (c) 2010-2019 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.report.impl;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;
import com.evolveum.midpoint.repo.common.ObjectResolver;
import com.evolveum.midpoint.repo.common.expression.ExpressionFactory;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.evolveum.midpoint.audit.api.AuditEventRecord;
import com.evolveum.midpoint.audit.api.AuditService;
import com.evolveum.midpoint.common.Clock;
import com.evolveum.midpoint.model.api.ModelInteractionService;
import com.evolveum.midpoint.model.api.ModelService;
import com.evolveum.midpoint.model.api.interaction.DashboardService;
import com.evolveum.midpoint.model.api.interaction.DashboardWidget;
import com.evolveum.midpoint.model.api.util.DashboardUtils;
import com.evolveum.midpoint.prism.Item;
import com.evolveum.midpoint.prism.PrismContainer;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismProperty;
import com.evolveum.midpoint.prism.PrismPropertyValue;
import com.evolveum.midpoint.prism.PrismReference;
import com.evolveum.midpoint.prism.PrismValue;
import com.evolveum.midpoint.prism.Referencable;
import com.evolveum.midpoint.prism.path.ItemName;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.ObjectTypeUtil;
import com.evolveum.midpoint.task.api.RunningTask;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.task.api.TaskManager;
import com.evolveum.midpoint.task.api.TaskRunResult;
import com.evolveum.midpoint.task.api.TaskRunResult.TaskRunResultStatus;
import com.evolveum.midpoint.util.QNameUtil;
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.xml.ns._public.common.audit_3.AuditEventRecordItemType;
import com.evolveum.midpoint.xml.ns._public.common.audit_3.AuditEventRecordType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AbstractRoleType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ConnectorConfigurationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ConnectorType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.DashboardType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.DashboardWidgetPresentationType;
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.ExportType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.FocusType;
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.ReportEngineSelectionType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ReportType;
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.TaskExecutionStatusType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.TaskPartitionDefinitionType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import com.fasterxml.jackson.databind.deser.ValueInstantiator.Gettable;
import j2html.TagCreator;
import j2html.tags.ContainerTag;
/**
* @author skublik
*/
@Component
public class ReportHTMLCreateTaskHandler extends ReportJasperCreateTaskHandler {
public static final String REPORT_HTML_CREATE_TASK_URI = "http://midpoint.evolveum.com/xml/ns/public/report/html/create/handler-3";
private static final Trace LOGGER = TraceManager.getTrace(ReportHTMLCreateTaskHandler.class);
private static final String REPORT_CSS_STYLE_FILE_NAME = "dashboard-report-style.css";
private static final String LABEL_COLUMN = "Label";
private static final String NUMBER_COLUMN = "Number";
private static final String STATUS_COLUMN = "Status";
private static final String TIME_COLUMN = "Time";
private static final String INITIATOR_COLUMN = "Initiator";
private static final String EVENT_STAGE_COLUMN = "Event stage";
private static final String EVENT_TYPE_COLUMN = "Event type";
private static final String TARGET_COLUMN = "Target";
private static final String TARGET_OWNER_COLUMN = "Target owner";
private static final String CHANNEL_COLUMN = "Channel";
private static final String OUTCOME_COLUMN = "Outcome";
private static final String NAME_COLUMN = "Name";
private static final String CONNECTOR_TYPE_COLUMN = "Connector type";
private static final String VERSION_COLUMN = "Version";
private static final String GIVEN_NAME_COLUMN = "Given name";
private static final String FAMILY_NAME_COLUMN = "Family name";
private static final String FULL_NAME_COLUMN = "Full name";
private static final String EMAIL_COLUMN = "Email";
private static final String ACCOUNTS_COLUMN = "Accounts";
private static final String DISPLAY_NAME_COLUMN = "Display name";
private static final String DESCRIPTION_COLUMN = "Description";
private static final String IDENTIFIER_COLUMN = "Identifier";
private static final String CATEGORY_COLUMN = "Category";
private static final String OBJECT_REFERENCE_COLUMN = "Object reference";
private static final String EXECUTION_COLUMN = "Execution";
private static final String EXECUTING_AT_COLUMN = "Executing at";
private static final String PROGRES_COLUMN = "Progress";
private static final String CURRENT_RUN_TIME_COLUMN = "Current run time";
private static final String SCHEDULED_TO_START_AGAIN_COLUMN = "Scheduled to start again";
private static final QName CUSTOM = new QName("custom");
@Autowired private ExpressionFactory expressionFactory;
@Autowired private Clock clock;
@Autowired private ModelInteractionService modelInteractionService;
@Autowired private TaskManager taskManager;
@Autowired private AuditService auditService;
@Autowired private ModelService modelService;
@Autowired private PrismContext prismContext;
@Autowired @Qualifier("modelObjectResolver") private ObjectResolver objectResolver;
@Autowired private DashboardService dashboardService;
private static LinkedHashMap<Class<? extends ObjectType>, LinkedHashMap<String, ItemPath>> columnDef;
private static Set<String> headsOfWidget;
private static Set<String> headsOfAuditEventRecords;
static {
columnDef = new LinkedHashMap<Class<? extends ObjectType>, LinkedHashMap<String, ItemPath>>() {
private static final long serialVersionUID = 1L;
{
put(ResourceType.class, new LinkedHashMap<String, ItemPath>() {
private static final long serialVersionUID = 1L;
{
put(NAME_COLUMN, ResourceType.F_NAME);
put(CONNECTOR_TYPE_COLUMN,
ItemPath.create(ResourceType.F_CONNECTOR_REF, ConnectorType.F_CONNECTOR_TYPE));
put(VERSION_COLUMN,
ItemPath.create(ResourceType.F_CONNECTOR_REF, ConnectorType.F_CONNECTOR_VERSION));
}
});
put(UserType.class, new LinkedHashMap<String, ItemPath>() {
private static final long serialVersionUID = 1L;
{
put(NAME_COLUMN, UserType.F_NAME);
put(GIVEN_NAME_COLUMN, UserType.F_GIVEN_NAME);
put(FAMILY_NAME_COLUMN, UserType.F_FAMILY_NAME);
put(FULL_NAME_COLUMN, UserType.F_FULL_NAME);
put(EMAIL_COLUMN, UserType.F_EMAIL_ADDRESS);
put(ACCOUNTS_COLUMN, ItemPath.create(AbstractRoleType.F_LINK_REF, CUSTOM));
}
});
put(AbstractRoleType.class, new LinkedHashMap<String, ItemPath>() {
private static final long serialVersionUID = 1L;
{
put(NAME_COLUMN, AbstractRoleType.F_NAME);
put(DISPLAY_NAME_COLUMN, AbstractRoleType.F_DISPLAY_NAME);
put(DESCRIPTION_COLUMN, AbstractRoleType.F_DESCRIPTION);
put(IDENTIFIER_COLUMN, AbstractRoleType.F_IDENTIFIER);
put(ACCOUNTS_COLUMN, ItemPath.create(AbstractRoleType.F_LINK_REF, CUSTOM));
}
});
put(TaskType.class, new LinkedHashMap<String, ItemPath>() {
private static final long serialVersionUID = 1L;
{
put(NAME_COLUMN, TaskType.F_NAME);
put(CATEGORY_COLUMN, TaskType.F_CATEGORY);
put(OBJECT_REFERENCE_COLUMN, TaskType.F_OBJECT_REF);
put(EXECUTION_COLUMN, TaskType.F_EXECUTION_STATUS);
put(EXECUTING_AT_COLUMN, TaskType.F_NODE_AS_OBSERVED);
put(PROGRES_COLUMN, TaskType.F_PROGRESS);
put(CURRENT_RUN_TIME_COLUMN, ItemPath.create(CUSTOM));
// put(SCHEDULED_TO_START_AGAIN_COLUMN, ItemPath.create(CUSTOM));
put(STATUS_COLUMN, TaskType.F_RESULT_STATUS);
}
});
}
};
headsOfWidget = new LinkedHashSet<String>() {
{
add(LABEL_COLUMN);
add(NUMBER_COLUMN);
add(STATUS_COLUMN);
}
};
headsOfAuditEventRecords = new LinkedHashSet<String>() {
{
add(TIME_COLUMN);
add(INITIATOR_COLUMN);
add(EVENT_STAGE_COLUMN);
add(EVENT_TYPE_COLUMN);
add(TARGET_COLUMN);
add(TARGET_OWNER_COLUMN);
add(CHANNEL_COLUMN);
add(OUTCOME_COLUMN);
}
};
}
@Override
protected void initialize() {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Registering with taskManager as a handler for " + REPORT_HTML_CREATE_TASK_URI);
}
taskManager.registerHandler(REPORT_HTML_CREATE_TASK_URI, this);
}
@Override
public TaskRunResult run(RunningTask task, TaskPartitionDefinitionType partition) {
OperationResult parentResult = task.getResult();
OperationResult result = parentResult
.createSubresult(ReportHTMLCreateTaskHandler.class.getSimpleName() + ".run");
TaskRunResult runResult = new TaskRunResult();
runResult.setOperationResult(result);
super.recordProgress(task, 0, result);
try {
ReportType parentReport = objectResolver.resolve(task.getObjectRef(), ReportType.class, null,
"resolving report", task, result);
if (parentReport.getReportEngine() == null) {
throw new IllegalArgumentException("Report Object doesn't have ReportEngine attribute");
}
if (parentReport.getReportEngine().equals(ReportEngineSelectionType.JASPER)) {
parentReport.setExport(ExportType.HTML);
return super.run(task, partition);
} else if (parentReport.getReportEngine().equals(ReportEngineSelectionType.DASHBOARD)) {
if (parentReport.getDashboard() != null && parentReport.getDashboard().getDashboardRef() != null) {
ObjectReferenceType ref = parentReport.getDashboard().getDashboardRef();
Class<ObjectType> type = prismContext.getSchemaRegistry().determineClassForType(ref.getType());
Task taskSearchDashboard = taskManager.createTaskInstance("Search dashboard");
DashboardType dashboard = (DashboardType) modelService
.getObject(type, ref.getOid(), null, taskSearchDashboard, taskSearchDashboard.getResult())
.asObjectable();
String style = "";
ClassLoader classLoader = getClass().getClassLoader();
InputStream in = classLoader.getResourceAsStream(REPORT_CSS_STYLE_FILE_NAME);
byte[] data = IOUtils.toByteArray(in);
style = new String(data, Charset.defaultCharset());
String reportFilePath = getDestinationFileName(parentReport);
FileUtils.writeByteArrayToFile(new File(reportFilePath), getBody(dashboard, style, task, result).getBytes());
super.saveReportOutputType(reportFilePath, parentReport, task, result);
LOGGER.trace("create report output type : {}", reportFilePath);
if (parentReport.getPostReportScript() != null) {
super.processPostReportScript(parentReport, reportFilePath, task, result);
}
result.computeStatus();
} else {
LOGGER.error("Dashboard or DashboardRef is null");
throw new IllegalArgumentException("Dashboard or DashboardRef is null");
}
}
} catch (Exception ex) {
LOGGER.error("CreateReport: {}", ex.getMessage(), ex);
result.recordFatalError(ex.getMessage(), ex);
runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
return runResult;
}
// This "run" is finished. But the task goes on ...
runResult.setRunResultStatus(TaskRunResultStatus.FINISHED);
LOGGER.trace("CreateReportTaskHandler.run stopping");
return runResult;
}
private String getBody(DashboardType dashboard, String cssStyle, Task task, OperationResult result) throws SchemaException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException, ObjectNotFoundException {
StringBuilder body = new StringBuilder();
body.append("<div> <style> ").append(cssStyle).append(" </style>");
ContainerTag widgetTable = createTable();
widgetTable.with(createTHead(getHeadsOfWidget()));
ContainerTag widgetTBody = TagCreator.tbody();
List<ContainerTag> tableboxesFromWidgets = new ArrayList<ContainerTag>();
for (DashboardWidgetType widget : dashboard.getWidget()) {
DashboardWidget widgetData = dashboardService.createWidgetData(widget, task, result);
widgetTBody.with(createTBodyRow(widgetData));
ContainerTag tableBox = createTableBoxForWidget(widgetData, task, result);
if (tableBox != null) {
tableboxesFromWidgets.add(tableBox);
}
}
widgetTable.with(widgetTBody);
body.append(createTableBox(widgetTable, "Widgets").render());
appendSpace(body);
tableboxesFromWidgets.forEach(table -> {
body.append(table.render());
appendSpace(body);
});
body.append("</div>");
return body.toString();
}
private void appendSpace(StringBuilder body) {
body.append("<br>");
}
private ContainerTag createTableBoxForWidget(DashboardWidget widgetData, Task task, OperationResult result) throws ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
DashboardWidgetType widget = widgetData.getWidget();
if (widget == null) {
throw new IllegalArgumentException("Widget in DashboardWidget is null");
}
DashboardWidgetSourceTypeType sourceType = DashboardUtils.getSourceType(widget);
DashboardWidgetPresentationType presentation = widget.getPresentation();
switch (sourceType) {
case OBJECT_COLLECTION:
if (!DashboardUtils.isDataFieldsOfPresentationNullOrEmpty(presentation)) {
ObjectCollectionType collection = dashboardService.getObjectCollectionType(widget, task, result);
List<PrismObject<ObjectType>> values = dashboardService.searchObjectFromCollection(collection, true, task, result);
if (values == null || values.isEmpty()) {
return null;
}
Class<ObjectType> type = (Class<ObjectType>) prismContext.getSchemaRegistry()
.getCompileTimeClassForObjectType(collection.getType());
HashMap<String, ItemPath> columns = getColumnsForType(type);
if (columns == null) {
LOGGER.error("Couldn't create table for objects with class {}", type.getName());
}
ContainerTag table = createTable();
table.with(createTHead(columns.keySet()));
ContainerTag tBody = TagCreator.tbody();
values.forEach(value -> {
ContainerTag tr = TagCreator.tr();
columns.keySet().forEach(column -> {
tr.with(TagCreator
.th(TagCreator.div(getRealValueAsString(column, value, columns.get(column)))));
});
tBody.with(tr);
});
table.with(tBody);
return createTableBox(table, widgetData.getLabel());
}
break;
case AUDIT_SEARCH:
if (!DashboardUtils.isDataFieldsOfPresentationNullOrEmpty(presentation)) {
Map<String, Object> parameters = new HashMap<String, Object>();
ObjectCollectionType collection = dashboardService.getObjectCollectionType(widget, task, result);
String query = DashboardUtils
.getQueryForListRecords(DashboardUtils.createQuery(collection, parameters, false, clock));
List<AuditEventRecord> records = auditService.listRecords(query, parameters);
if (records == null || records.isEmpty()) {
return null;
}
List<AuditEventRecordType> auditRecordList = new ArrayList<>();
for (AuditEventRecord record : records) {
auditRecordList.add(record.createAuditEventRecordType());
}
ContainerTag table = createTable();
table.with(createTHead(getHeadsOfAuditEventRecords()));
ContainerTag tBody = TagCreator.tbody();
auditRecordList.forEach(record -> {
ContainerTag tr = TagCreator.tr();
getHeadsOfAuditEventRecords().forEach(column -> {
tr.with(TagCreator.th(TagCreator.div(getColumnForAuditEventRecord(column, record))));
});
tBody.with(tr);
});
table.with(tBody);
return createTableBox(table, widgetData.getLabel());
}
break;
}
return null;
}
private String getColumnForAuditEventRecord(String column, AuditEventRecordType record) {
switch (column) {
case TIME_COLUMN:
SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE, d. MMM yyyy HH:mm:ss", Locale.US);
return dateFormat.format(record.getTimestamp().toGregorianCalendar().getTime());
case INITIATOR_COLUMN:
ObjectReferenceType initiatorRef = record.getInitiatorRef();
return getObjectNameFromRef(initiatorRef);
case EVENT_STAGE_COLUMN:
return record.getEventStage() == null ? "" : record.getEventStage().toString();
case EVENT_TYPE_COLUMN:
return record.getEventType() == null ? "" : record.getEventType().toString();
case TARGET_COLUMN:
ObjectReferenceType targetRef = record.getTargetRef();
return getObjectNameFromRef(targetRef);
case TARGET_OWNER_COLUMN:
ObjectReferenceType targetOwnerRef = record.getTargetOwnerRef();
return getObjectNameFromRef(targetOwnerRef);
case CHANNEL_COLUMN:
return record.getChannel() == null ? "" : QNameUtil.uriToQName(record.getChannel()).getLocalPart();
case OUTCOME_COLUMN:
if (record.getOutcome() == null) {
return "";
}
return record.getOutcome().toString();
}
return "";
}
private String getObjectNameFromRef(Referencable ref) {
if (ref == null) {
return "";
}
if (ref.getTargetName() != null && ref.getTargetName().getOrig() != null) {
return ref.getTargetName().getOrig();
}
PrismObject object = getObjectFromReference(ref);
if (object == null) {
return ref.getOid();
}
if (object.getName() == null || object.getName().getOrig() == null) {
return "";
}
return object.getName().getOrig();
}
private String getRealValueAsString(String nameOfColumn, PrismObject<ObjectType> object, ItemPath itemPath) {
Iterator<QName> iterator = (Iterator<QName>) itemPath.getSegments().iterator();
Item valueObject = object;
while (iterator.hasNext()) {
QName name = iterator.next();
if (QNameUtil.match(name, CUSTOM)) {
return getCustomValueForColumn(valueObject, nameOfColumn);
}
valueObject = (Item) valueObject.find(ItemPath.create(name));
if (valueObject instanceof PrismProperty && iterator.hasNext()) {
throw new IllegalArgumentException("Found object is PrismProperty, but ItemPath isn't empty");
}
if (valueObject instanceof PrismContainer && !iterator.hasNext()) {
throw new IllegalArgumentException("Found object is PrismContainer, but ItemPath is empty");
}
if (valueObject instanceof PrismReference) {
Referencable ref = ((PrismReference) valueObject).getRealValue();
if (!iterator.hasNext()) {
return getObjectNameFromRef(ref);
}
valueObject = getObjectFromReference(ref);
}
if (valueObject == null) {
if(nameOfColumn.equals(ACCOUNTS_COLUMN)) {
return "0";
}
return "";
}
}
return ((PrismProperty) valueObject).getRealValue().toString();
}
private String getCustomValueForColumn(Item valueObject, String nameOfColumn) {
switch (nameOfColumn) {
case ACCOUNTS_COLUMN:
if(!(valueObject instanceof PrismObject)){
return "";
}
return String.valueOf(((PrismObject)valueObject).getRealValues().size());
case CURRENT_RUN_TIME_COLUMN:
if(!(valueObject instanceof PrismObject)
&& !(((PrismObject)valueObject).getRealValue() instanceof TaskType)){
return "";
}
TaskType task = (TaskType)((PrismObject)valueObject).getRealValue();
XMLGregorianCalendar timestapm = task.getCompletionTimestamp();
if(timestapm != null && task.getExecutionStatus().equals(TaskExecutionStatusType.CLOSED)) {
SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE, d. MMM yyyy HH:mm:ss", Locale.US);
return "closed at " + dateFormat.format(task.getCompletionTimestamp().toGregorianCalendar().getTime());
}
return "";
case SCHEDULED_TO_START_AGAIN_COLUMN:
return "";
}
return "";
}
private PrismObject<ObjectType> getObjectFromReference(Referencable ref) {
Task task = taskManager.createTaskInstance("Get object");
Class<ObjectType> type = prismContext.getSchemaRegistry().determineClassForType(ref.getType());
if (ref.asReferenceValue().getObject() != null) {
return ref.asReferenceValue().getObject();
}
PrismObject<ObjectType> object = null;
try {
object = modelService.getObject(type, ref.getOid(), null, task, task.getResult());
} catch (Exception e) {
LOGGER.error("Couldn't get object from objectRef " + ref, e);
}
return object;
}
private ContainerTag createTable() {
return TagCreator.table().withClasses("table", "table-striped", "table-hover", "table-bordered");
}
private ContainerTag createTableBox(ContainerTag table, String nameOfTable) {
ContainerTag div = TagCreator.div().withClasses("box-body", "no-padding").with(TagCreator.h1(nameOfTable))
.with(table);
return TagCreator.div().withClasses("box", "boxed-table").with(div);
}
private ContainerTag createTHead(Set<String> set) {
return TagCreator.thead(TagCreator.tr(TagCreator.each(set,
header -> TagCreator.th(TagCreator.div(TagCreator.span(header).withClass("sortableLabel"))))));
}
private ContainerTag createTBodyRow(DashboardWidget data) {
return TagCreator.tr(TagCreator.each(getHeadsOfWidget(), header -> getContainerTagForWidgetHeader(header, data)
));
}
private ContainerTag getContainerTagForWidgetHeader(String header, DashboardWidget data) {
if (header.equals(LABEL_COLUMN)) {
ContainerTag div = TagCreator.div(data.getLabel());
return TagCreator.th().with(div);
}
if (header.equals(NUMBER_COLUMN)) {
ContainerTag div = TagCreator.div(data.getNumberMessage());
return TagCreator.th().with(div);
}
if (header.equals(STATUS_COLUMN)) {
ContainerTag div = TagCreator.div().withStyle("width: 100%; height: 20px; ");
ContainerTag th = TagCreator.th();
addStatusColor(th, data.getDisplay());
th.with(div);
return th;
}
return TagCreator.th();
}
private void addStatusColor(ContainerTag div, DisplayType display) {
if (display != null && StringUtils.isNoneBlank(display.getColor())) {
div.withStyle("background-color: " + display.getColor() + "; !important;");
}
}
private Set<String> getHeadsOfWidget() {
return headsOfWidget;
}
private static Set<String> getHeadsOfAuditEventRecords() {
return headsOfAuditEventRecords;
}
private static LinkedHashMap<Class<? extends ObjectType>, LinkedHashMap<String, ItemPath>> getColumnDef() {
return columnDef;
}
private static LinkedHashMap<String, ItemPath> getColumnsForType(Class<? extends ObjectType> type ) {
if(type.equals(RoleType.class)
|| type.equals(OrgType.class)
|| type.equals(ServiceType.class)) {
return getColumnDef().get(AbstractRoleType.class);
}
return getColumnDef().get(type);
}
@Override
protected ExportType getExport(ReportType report) {
return ExportType.HTML;
}
}