-
Notifications
You must be signed in to change notification settings - Fork 188
/
QObjectMapping.java
372 lines (323 loc) · 16.7 KB
/
QObjectMapping.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
/*
* Copyright (C) 2010-2021 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.sqale.qmodel.object;
import static com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentHolderType.*;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.xml.namespace.QName;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.Path;
import org.jetbrains.annotations.NotNull;
import com.evolveum.midpoint.prism.PrismConstants;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.SerializationOptions;
import com.evolveum.midpoint.repo.sqale.SqaleTransformerSupport;
import com.evolveum.midpoint.repo.sqale.SqaleUtils;
import com.evolveum.midpoint.repo.sqale.qmodel.SqaleTableMapping;
import com.evolveum.midpoint.repo.sqale.qmodel.assignment.QAssignmentMapping;
import com.evolveum.midpoint.repo.sqale.qmodel.common.QUri;
import com.evolveum.midpoint.repo.sqale.qmodel.ref.QObjectReferenceMapping;
import com.evolveum.midpoint.repo.sqlbase.JdbcSession;
import com.evolveum.midpoint.repo.sqlbase.SqlTransformerSupport;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.SelectorOptions;
import com.evolveum.midpoint.schema.util.ObjectTypeUtil;
import com.evolveum.midpoint.util.MiscUtil;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
/**
* Mapping between {@link QObject} and {@link ObjectType}.
*
* @param <S> schema type of the object
* @param <Q> type of entity path
* @param <R> row type related to the {@link Q}
*/
public class QObjectMapping<S extends ObjectType, Q extends QObject<R>, R extends MObject>
extends SqaleTableMapping<S, Q, R> {
public static final String DEFAULT_ALIAS_NAME = "o";
public static final QObjectMapping<ObjectType, QObject<MObject>, MObject> INSTANCE =
new QObjectMapping<>(QObject.TABLE_NAME, DEFAULT_ALIAS_NAME,
ObjectType.class, QObject.CLASS);
protected QObjectMapping(
@NotNull String tableName,
@NotNull String defaultAliasName,
@NotNull Class<S> schemaType,
@NotNull Class<Q> queryType) {
super(tableName, defaultAliasName, schemaType, queryType);
addItemMapping(PrismConstants.T_ID, uuidMapper(q -> q.oid));
addItemMapping(F_NAME, polyStringMapper(
q -> q.nameOrig, q -> q.nameNorm));
addItemMapping(F_TENANT_REF, refMapper(
q -> q.tenantRefTargetOid,
q -> q.tenantRefTargetType,
q -> q.tenantRefRelationId));
addItemMapping(F_LIFECYCLE_STATE, stringMapper(q -> q.lifecycleState));
// version/cid_seq is not mapped for queries or deltas, it's managed by repo explicitly
// TODO mapper for policySituations and subtypes
// TODO ext mapping can't be done statically
addNestedMapping(F_METADATA, MetadataType.class)
.addItemMapping(MetadataType.F_CREATOR_REF, refMapper(
q -> q.creatorRefTargetOid,
q -> q.creatorRefTargetType,
q -> q.creatorRefRelationId))
.addItemMapping(MetadataType.F_CREATE_CHANNEL,
uriMapper(q -> q.createChannelId))
.addItemMapping(MetadataType.F_CREATE_TIMESTAMP,
timestampMapper(q -> q.createTimestamp))
.addItemMapping(MetadataType.F_MODIFIER_REF, refMapper(
q -> q.modifierRefTargetOid,
q -> q.modifierRefTargetType,
q -> q.modifierRefRelationId))
.addItemMapping(MetadataType.F_MODIFY_CHANNEL,
uriMapper(q -> q.modifyChannelId))
.addItemMapping(MetadataType.F_MODIFY_TIMESTAMP,
timestampMapper(q -> q.modifyTimestamp))
.addRefMapping(MetadataType.F_CREATE_APPROVER_REF,
objectCreateApproverReferenceMapping())
.addRefMapping(MetadataType.F_MODIFY_APPROVER_REF,
objectModifyApproverReferenceMapping());
addRefMapping(F_PARENT_ORG_REF, objectParentOrgReferenceMapping());
addContainerTableMapping(AssignmentHolderType.F_ASSIGNMENT, assignmentMapping(),
joinOn((o, a) -> o.oid.eq(a.ownerOid)));
addContainerTableMapping(F_OPERATION_EXECUTION, operationExecutionMapping(),
joinOn((o, trg) -> o.oid.eq(trg.ownerOid)));
addContainerTableMapping(F_TRIGGER, triggerMapping(),
joinOn((o, trg) -> o.oid.eq(trg.ownerOid)));
// AssignmentHolderType
addRefMapping(F_ARCHETYPE_REF, archetypeReferenceMapping());
addRefMapping(F_DELEGATED_REF, delegatedReferenceMapping());
addRefMapping(F_ROLE_MEMBERSHIP_REF, roleMembershipReferenceMapping());
}
/** Fixes rigid parametric types of static mapping instance to this instance. */
@NotNull
public QAssignmentMapping<R> assignmentMapping() {
//noinspection unchecked
return (QAssignmentMapping<R>) QAssignmentMapping.INSTANCE;
}
/** Fixes rigid parametric types of static mapping instance to this instance. */
@NotNull
public QOperationExecutionMapping<R> operationExecutionMapping() {
//noinspection unchecked
return (QOperationExecutionMapping<R>) QOperationExecutionMapping.INSTANCE;
}
/** Fixes rigid parametric types of static mapping instance to this instance. */
@NotNull
public QTriggerMapping<R> triggerMapping() {
//noinspection unchecked
return (QTriggerMapping<R>) QTriggerMapping.INSTANCE;
}
/** Fixes rigid parametric types of static mapping instance to this instance. */
public @NotNull QObjectReferenceMapping<Q, R> archetypeReferenceMapping() {
//noinspection unchecked
return (QObjectReferenceMapping<Q, R>) QObjectReferenceMapping.INSTANCE_ARCHETYPE;
}
/** Fixes rigid parametric types of static mapping instance to this instance. */
public @NotNull QObjectReferenceMapping<Q, R> delegatedReferenceMapping() {
//noinspection unchecked
return (QObjectReferenceMapping<Q, R>) QObjectReferenceMapping.INSTANCE_DELEGATED;
}
/** Fixes rigid parametric types of static mapping instance to this instance. */
public @NotNull QObjectReferenceMapping<Q, R> objectCreateApproverReferenceMapping() {
//noinspection unchecked
return (QObjectReferenceMapping<Q, R>)
QObjectReferenceMapping.INSTANCE_OBJECT_CREATE_APPROVER;
}
/** Fixes rigid parametric types of static mapping instance to this instance. */
public @NotNull QObjectReferenceMapping<Q, R> objectModifyApproverReferenceMapping() {
//noinspection unchecked
return (QObjectReferenceMapping<Q, R>)
QObjectReferenceMapping.INSTANCE_OBJECT_MODIFY_APPROVER;
}
/** Fixes rigid parametric types of static mapping instance to this instance. */
public @NotNull QObjectReferenceMapping<Q, R> objectParentOrgReferenceMapping() {
//noinspection unchecked
return (QObjectReferenceMapping<Q, R>) QObjectReferenceMapping.INSTANCE_OBJECT_PARENT_ORG;
}
/** Fixes rigid parametric types of static mapping instance to this instance. */
public @NotNull QObjectReferenceMapping<Q, R> roleMembershipReferenceMapping() {
//noinspection unchecked
return (QObjectReferenceMapping<Q, R>) QObjectReferenceMapping.INSTANCE_ROLE_MEMBERSHIP;
}
@Override
public @NotNull Path<?>[] selectExpressions(
Q entity, Collection<SelectorOptions<GetOperationOptions>> options) {
return new Path[] { entity.oid, entity.fullObject };
}
@Override
protected Q newAliasInstance(String alias) {
//noinspection unchecked
return (Q) new QObject<>(MObject.class, alias);
}
@Override
public R newRowObject() {
//noinspection unchecked
return (R) new MObject();
}
// region transformation
@Override
public S toSchemaObject(Tuple row, Q entityPath,
Collection<SelectorOptions<GetOperationOptions>> options)
throws SchemaException {
byte[] fullObject = Objects.requireNonNull(row.get(entityPath.fullObject));
PrismObject<S> prismObject;
String serializedForm = new String(fullObject, StandardCharsets.UTF_8);
try {
SqlTransformerSupport.ParseResult<S> result =
SqaleTransformerSupport.getInstance().parsePrismObject(serializedForm);
prismObject = result.prismObject;
if (result.parsingContext.hasWarnings()) {
logger.warn("Object {} parsed with {} warnings",
ObjectTypeUtil.toShortString(prismObject),
result.parsingContext.getWarnings().size());
}
} catch (SchemaException | RuntimeException | Error e) {
// This is a serious thing. We have corrupted XML in the repo. This may happen even
// during system init. We want really loud and detailed error here.
logger.error("Couldn't parse object {} {}: {}: {}\n{}",
schemaType().getSimpleName(), row.get(entityPath.oid),
e.getClass().getName(), e.getMessage(), serializedForm, e);
throw e;
}
return prismObject.asObjectable();
}
/**
* Override this to fill additional row attributes after calling this super version.
*
* *This must be called with active JDBC session* so it can create new {@link QUri} rows.
* As this is intended for inserts *DO NOT* set {@link MObject#objectType} to any value,
* it must be NULL otherwise the DB will complain about the value for the generated column.
*
* OID may be null, hence the method does NOT create any sub-entities, see
* {@link #storeRelatedEntities(MObject, ObjectType, JdbcSession)}.
* Try to keep order of fields here, in M-class (MObject for this one) and in SQL the same.
*/
@SuppressWarnings("DuplicatedCode") // see comment for metadata lower
@NotNull
public R toRowObjectWithoutFullObject(S schemaObject, JdbcSession jdbcSession) {
R row = newRowObject();
row.oid = oidToUUid(schemaObject.getOid());
// objectType MUST be left NULL for INSERT, it's determined by PG
setPolyString(schemaObject.getName(), o -> row.nameOrig = o, n -> row.nameNorm = n);
// fullObject is managed outside of this method
setReference(schemaObject.getTenantRef(),
o -> row.tenantRefTargetOid = o,
t -> row.tenantRefTargetType = t,
r -> row.tenantRefRelationId = r);
row.lifecycleState = schemaObject.getLifecycleState();
// containerIdSeq is managed outside of this method
row.version = SqaleUtils.objectVersionAsInt(schemaObject);
// complex DB fields
row.policySituations = processCacheableUris(schemaObject.getPolicySituation());
row.subtypes = arrayFor(schemaObject.getSubtype());
// TODO textInfo (fulltext support)
// repo.getTextInfoItems().addAll(RObjectTextInfo.createItemsSet(jaxb, repo, repositoryContext));
// TODO extensions stored inline (JSON) - that is ext column
// This is duplicate code with AssignmentSqlTransformer.toRowObject, but making interface
// and needed setters (fields are not "interface-able") would create much more code.
MetadataType metadata = schemaObject.getMetadata();
if (metadata != null) {
setReference(metadata.getCreatorRef(),
o -> row.creatorRefTargetOid = o,
t -> row.creatorRefTargetType = t,
r -> row.creatorRefRelationId = r);
row.createChannelId = processCacheableUri(metadata.getCreateChannel());
row.createTimestamp = MiscUtil.asInstant(metadata.getCreateTimestamp());
setReference(metadata.getModifierRef(),
o -> row.modifierRefTargetOid = o,
t -> row.modifierRefTargetType = t,
r -> row.modifierRefRelationId = r);
row.modifyChannelId = processCacheableUri(metadata.getModifyChannel());
row.modifyTimestamp = MiscUtil.asInstant(metadata.getModifyTimestamp());
}
return row;
}
/**
* Stores other entities related to the main object row like containers, references, etc.
* This is not part of {@link #toRowObjectWithoutFullObject} because it requires know OID
* which is not assured before calling that method.
*
* *Always call this super method first in overriding methods.*
*
* @param row master row for the added object("aggregate root")
* @param schemaObject schema objects for which the details are stored
* @param jdbcSession JDBC session used to insert related rows
*/
public void storeRelatedEntities(
@NotNull R row, @NotNull S schemaObject, @NotNull JdbcSession jdbcSession) {
Objects.requireNonNull(row.oid);
// We're after insert, we can set this for the needs of owned entities (assignments).
row.objectType = MObjectType.fromSchemaType(schemaObject.getClass());
MetadataType metadata = schemaObject.getMetadata();
if (metadata != null) {
storeRefs(row, metadata.getCreateApproverRef(),
objectCreateApproverReferenceMapping(), jdbcSession);
storeRefs(row, metadata.getModifyApproverRef(),
objectModifyApproverReferenceMapping(), jdbcSession);
}
List<TriggerType> triggers = schemaObject.getTrigger();
if (!triggers.isEmpty()) {
triggers.forEach(t -> triggerMapping().insert(t, row, jdbcSession));
}
List<OperationExecutionType> operationExecutions = schemaObject.getOperationExecution();
if (!operationExecutions.isEmpty()) {
operationExecutions.forEach(oe ->
operationExecutionMapping().insert(oe, row, jdbcSession));
}
storeRefs(row, schemaObject.getParentOrgRef(),
objectParentOrgReferenceMapping(), jdbcSession);
if (schemaObject instanceof AssignmentHolderType) {
storeAssignmentHolderEntities(row, (AssignmentHolderType) schemaObject, jdbcSession);
}
/* TODO EAV extensions - the relevant code from old repo RObject#copyObjectInformationFromJAXB
if (jaxb.getExtension() != null) {
copyExtensionOrAttributesFromJAXB(jaxb.getExtension().asPrismContainerValue(), repo, repositoryContext, RObjectExtensionType.EXTENSION, generatorResult);
}
*/
}
private void storeAssignmentHolderEntities(
R row, AssignmentHolderType schemaObject, JdbcSession jdbcSession) {
List<AssignmentType> assignments = schemaObject.getAssignment();
if (!assignments.isEmpty()) {
assignments.forEach(assignment ->
assignmentMapping().insert(assignment, row, jdbcSession));
}
storeRefs(row, schemaObject.getArchetypeRef(),
archetypeReferenceMapping(), jdbcSession);
storeRefs(row, schemaObject.getDelegatedRef(),
delegatedReferenceMapping(), jdbcSession);
storeRefs(row, schemaObject.getRoleMembershipRef(),
roleMembershipReferenceMapping(), jdbcSession);
}
/**
* Serializes schema object and sets {@link R#fullObject}.
*/
public void setFullObject(R row, S schemaObject) throws SchemaException {
row.fullObject = createFullObject(schemaObject);
}
public byte[] createFullObject(S schemaObject) throws SchemaException {
if (schemaObject.getOid() == null || schemaObject.getVersion() == null) {
throw new IllegalArgumentException(
"Serialized object must have assigned OID and version: " + schemaObject);
}
return SqaleTransformerSupport.getInstance().createStringSerializer()
.itemsToSkip(fullObjectItemsToSkip())
.options(SerializationOptions
.createSerializeReferenceNamesForNullOids()
.skipIndexOnly(true)
.skipTransient(true))
.serialize(schemaObject.asPrismObject())
.getBytes(StandardCharsets.UTF_8);
}
protected Collection<? extends QName> fullObjectItemsToSkip() {
// TODO extend later, things like FocusType.F_JPEG_PHOTO, see ObjectUpdater#updateFullObject
return Collections.emptyList();
}
// endregion
}