-
Notifications
You must be signed in to change notification settings - Fork 188
/
AuditEventRecordSqlTransformer.java
263 lines (235 loc) · 11.4 KB
/
AuditEventRecordSqlTransformer.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
/*
* Copyright (C) 2010-2020 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.repo.sql.pure.querymodel.mapping;
import static com.evolveum.midpoint.repo.sql.pure.querymodel.QAuditEventRecord.MESSAGE;
import java.util.List;
import java.util.Map;
import javax.xml.namespace.QName;
import com.querydsl.core.Tuple;
import com.querydsl.sql.Configuration;
import com.evolveum.midpoint.audit.api.AuditEventRecord;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismReferenceValue;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.repo.sql.data.audit.RAuditEventStage;
import com.evolveum.midpoint.repo.sql.data.audit.RAuditEventType;
import com.evolveum.midpoint.repo.sql.data.common.enums.ROperationResultStatus;
import com.evolveum.midpoint.repo.sql.data.common.other.RObjectType;
import com.evolveum.midpoint.repo.sql.pure.SqlTransformer;
import com.evolveum.midpoint.repo.sql.pure.querymodel.QAuditDelta;
import com.evolveum.midpoint.repo.sql.pure.querymodel.QAuditEventRecord;
import com.evolveum.midpoint.repo.sql.pure.querymodel.beans.MAuditDelta;
import com.evolveum.midpoint.repo.sql.pure.querymodel.beans.MAuditEventRecord;
import com.evolveum.midpoint.repo.sql.pure.querymodel.beans.MAuditRefValue;
import com.evolveum.midpoint.util.MiscUtil;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.audit_3.*;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectDeltaOperationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.OperationResultStatusType;
import com.evolveum.prism.xml.ns._public.types_3.ItemPathType;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;
/**
* Transformation of audit event records between repo and Prism world.
*/
public class AuditEventRecordSqlTransformer
extends SqlTransformer<AuditEventRecordType, QAuditEventRecord, MAuditEventRecord> {
public AuditEventRecordSqlTransformer(PrismContext prismContext,
QAuditEventRecordMapping mapping, Configuration querydslConfiguration) {
super(prismContext, mapping, querydslConfiguration);
}
public AuditEventRecordType toSchemaObject(MAuditEventRecord row) throws SchemaException {
AuditEventRecordType record = mapSimpleAttributes(row);
mapDeltas(record, row.deltas);
mapChangedItems(record, row.changedItemPaths);
mapRefValues(record, row.refValues);
mapProperties(record, row.properties);
mapResourceOids(record, row.resourceOids);
return record;
}
private AuditEventRecordType mapSimpleAttributes(MAuditEventRecord row) {
// prismContext in constructor ensures complex type definition
return new AuditEventRecordType(prismContext)
.channel(row.channel)
.eventIdentifier(row.eventIdentifier)
.eventStage(auditEventStageTypeFromRepo(row.eventStage))
.eventType(auditEventTypeTypeFromRepo(row.eventType))
.hostIdentifier(row.hostIdentifier)
.message(row.message)
.nodeIdentifier(row.nodeIdentifier)
.outcome(operationResultStatusTypeFromRepo(row.outcome))
.parameter(row.parameter)
.remoteHostAddress(row.remoteHostAddress)
.requestIdentifier(row.requestIdentifier)
.result(row.result)
.sessionIdentifier(row.sessionIdentifier)
.taskIdentifier(row.taskIdentifier)
.taskOID(row.taskOid)
.timestamp(MiscUtil.asXMLGregorianCalendar(row.timestamp))
.initiatorRef(objectReferenceType(
row.initiatorOid,
repoObjectType(row.initiatorType, RObjectType.FOCUS),
row.initiatorName))
.attorneyRef(objectReferenceType(
row.attorneyOid, RObjectType.FOCUS, row.attorneyName))
.targetRef(objectReferenceType(
row.targetOid,
// targetType must not be null if targetOid is not
repoObjectType(row.targetType),
row.targetName))
.targetOwnerRef(objectReferenceType(
row.targetOwnerOid,
repoObjectType(row.targetOwnerType),
row.targetOwnerName));
}
private void mapDeltas(AuditEventRecordType record, List<MAuditDelta> deltas)
throws SchemaException {
if (deltas == null) {
return;
}
SqlTransformer<ObjectDeltaOperationType, QAuditDelta, MAuditDelta> deltaTransformer =
QAuditDeltaMapping.INSTANCE.createTransformer(prismContext, querydslConfiguration);
for (MAuditDelta delta : deltas) {
record.delta(deltaTransformer.toSchemaObject(delta));
}
}
// the rest of sub-entities do not deserve the dedicated transformer classes (yet)
private void mapChangedItems(AuditEventRecordType record, List<String> changedItemPaths) {
if (changedItemPaths == null) {
return;
}
for (String changedItemPath : changedItemPaths) {
ItemPath itemPath = ItemPath.create(changedItemPath);
record.getChangedItem().add(new ItemPathType(itemPath));
}
}
private void mapRefValues(
AuditEventRecordType record, Map<String, List<MAuditRefValue>> refValues) {
if (refValues == null) {
return;
}
for (Map.Entry<String, List<MAuditRefValue>> entry : refValues.entrySet()) {
AuditEventRecordReferenceType referenceValues =
new AuditEventRecordReferenceType().name(entry.getKey());
for (MAuditRefValue refValue : entry.getValue()) {
AuditEventRecordReferenceValueType value = new AuditEventRecordReferenceValueType()
.oid(refValue.oid)
.type(QName.valueOf(refValue.type));
if (refValue.targetNameOrig != null) {
value.targetName(new PolyStringType(
new PolyString(refValue.targetNameOrig, refValue.targetNameNorm)));
}
referenceValues.value(value);
}
record.reference(referenceValues);
}
}
private void mapProperties(AuditEventRecordType record, Map<String, List<String>> properties) {
if (properties == null) {
return;
}
for (Map.Entry<String, List<String>> entry : properties.entrySet()) {
AuditEventRecordPropertyType propType =
new AuditEventRecordPropertyType().name(entry.getKey());
propType.getValue().addAll(entry.getValue());
record.property(propType);
}
}
private void mapResourceOids(
AuditEventRecordType record, List<String> resourceOids) {
if (resourceOids == null) {
return;
}
record.getResourceOid().addAll(resourceOids);
}
private AuditEventTypeType auditEventTypeTypeFromRepo(Integer ordinal) {
RAuditEventType eventType = MiscUtil.enumFromOrdinal(RAuditEventType.class, ordinal);
return eventType != null
? eventType.getSchemaValue()
: null;
}
private AuditEventStageType auditEventStageTypeFromRepo(Integer ordinal) {
RAuditEventStage stage = MiscUtil.enumFromOrdinal(RAuditEventStage.class, ordinal);
return stage != null
? stage.getSchemaValue()
: null;
}
private OperationResultStatusType operationResultStatusTypeFromRepo(Integer ordinal) {
ROperationResultStatus status =
MiscUtil.enumFromOrdinal(ROperationResultStatus.class, ordinal);
return status != null
? status.getSchemaValue()
: null;
}
/**
* Transforms {@link AuditEventRecord} to {@link MAuditEventRecord} without any subentities.
* <p>
* Design notes: Arguably, this code could be in {@link MAuditEventRecord}.
* Also the
*/
public MAuditEventRecord from(AuditEventRecord record) {
MAuditEventRecord bean = new MAuditEventRecord();
bean.id = record.getRepoId(); // this better be null if we want to insert
bean.eventIdentifier = record.getEventIdentifier();
bean.timestamp = MiscUtil.asInstant(record.getTimestamp());
bean.channel = record.getChannel();
bean.eventStage = MiscUtil.enumOrdinal(RAuditEventStage.from(record.getEventStage()));
bean.eventType = MiscUtil.enumOrdinal(RAuditEventType.from(record.getEventType()));
bean.hostIdentifier = record.getHostIdentifier();
PrismReferenceValue attorney = record.getAttorneyRef();
if (attorney != null) {
bean.attorneyName = attorney.getDescription();
bean.attorneyOid = attorney.getOid();
}
PrismReferenceValue initiator = record.getInitiatorRef();
if (initiator != null) {
bean.initiatorName = initiator.getDescription();
bean.initiatorOid = initiator.getOid();
bean.initiatorType = targetTypeToRepoOrdinal(initiator);
}
bean.message = trim(record.getMessage(), MESSAGE);
bean.nodeIdentifier = record.getNodeIdentifier();
bean.outcome = MiscUtil.enumOrdinal(ROperationResultStatus.from(record.getOutcome()));
bean.parameter = record.getParameter();
bean.remoteHostAddress = record.getRemoteHostAddress();
bean.requestIdentifier = record.getRequestIdentifier();
bean.result = record.getResult();
bean.sessionIdentifier = record.getSessionIdentifier();
PrismReferenceValue target = record.getTargetRef();
if (target != null) {
bean.targetName = target.getDescription();
bean.targetOid = target.getOid();
bean.targetType = targetTypeToRepoOrdinal(target);
}
PrismReferenceValue targetOwner = record.getTargetOwnerRef();
if (targetOwner != null) {
bean.targetOwnerName = targetOwner.getDescription();
bean.targetOwnerOid = targetOwner.getOid();
bean.targetOwnerType = targetTypeToRepoOrdinal(targetOwner);
}
bean.taskIdentifier = record.getTaskIdentifier();
bean.taskOid = record.getTaskOid();
return bean;
}
private Integer targetTypeToRepoOrdinal(PrismReferenceValue targetOwner) {
//noinspection rawtypes
Class objectClass = prismContext.getSchemaRegistry()
.determineClassForType(targetOwner.getTargetType());
//noinspection unchecked
return MiscUtil.enumOrdinal(RObjectType.getByJaxbType(objectClass));
}
@Override
protected void processExtensionColumns(
AuditEventRecordType schemaObject, Tuple tuple, QAuditEventRecord entityPath) {
for (String propertyName : mapping.getExtensionColumns().keySet()) {
Object customColumnValue = tuple.get(entityPath.getPath(propertyName));
schemaObject.getCustomColumnProperty().add(
new AuditEventRecordCustomColumnPropertyType()
.name(propertyName).value((String) customColumnValue));
}
}
}