Skip to content

Commit

Permalink
MID-6319: fixed retrieval of audit delta in searchObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
virgo47 committed Aug 20, 2020
1 parent 0c4dbe0 commit c2a72ef
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 41 deletions.
Expand Up @@ -550,7 +550,7 @@ public void test300ConcurrentAudits() throws Exception {
OperationResult threadResult = threadTask.getResult();
for (int iteration = 0; iteration < ITERATIONS; iteration++) {
display("Executing iteration " + iteration + " on user " + index);
ObjectDelta delta = prismContext.deltaFor(UserType.class)
ObjectDelta<? extends ObjectType> delta = prismContext.deltaFor(UserType.class)
.item(UserType.F_FULL_NAME).replace(PolyString.fromOrig("User " + index + " iteration " + iteration))
.asObjectDelta(oids.get(index));
executeChangesAssertSuccess(delta, null, threadTask, threadResult);
Expand Down Expand Up @@ -620,11 +620,11 @@ public void test310ConcurrentAuditsRaw() throws Exception {
AuditEventRecord record = new AuditEventRecord(AuditEventType.MODIFY_OBJECT, AuditEventStage.EXECUTION);
record.setEventIdentifier(
iteration + ":" + System.currentTimeMillis() + "-" + (int) (Math.random() * 1_000_000));
ObjectDelta<?> delta = prismContext.deltaFor(UserType.class)
ObjectDelta<? extends ObjectType> delta = prismContext.deltaFor(UserType.class)
.item(UserType.F_FULL_NAME).replace(PolyString.fromOrig("Hi" + iteration))
.item(UserType.F_METADATA, MetadataType.F_MODIFY_TIMESTAMP).replace(XmlTypeConverter.createXMLGregorianCalendar(new Date()))
.asObjectDelta("oid" + index);
record.addDelta(new ObjectDeltaOperation(delta));
record.addDelta(new ObjectDeltaOperation<>(delta));
modelAuditService.audit(record, threadTask, threadResult);
if (failed.get()) {
results.set(index, new IllegalStateException("Some other thread failed"));
Expand Down
Expand Up @@ -526,6 +526,7 @@ public void checkConsistence() {
ObjectDeltaOperation.checkConsistence(deltas);
}

@Deprecated // should go away with the old audit listRecord
public AuditEventRecordType createAuditEventRecordType() {
return createAuditEventRecordType(false);
}
Expand Down
Expand Up @@ -86,9 +86,10 @@ public void initSystem() throws Exception {
record1.setTarget(target, prismContext);
record1.setTargetOwner(targetOwner, prismContext);
record1.addDelta(createDelta(UserType.F_FULL_NAME)); // values are not even necessary
record1.addDelta(createDelta(UserType.F_FAMILY_NAME));
record1.addDelta(createDelta(UserType.F_FAMILY_NAME, PolyString.fromOrig("familyNameVal")));
record1.addDelta(createDelta(ItemPath.create(
ObjectType.F_METADATA, MetadataType.F_REQUEST_TIMESTAMP)));
ObjectType.F_METADATA, MetadataType.F_REQUEST_TIMESTAMP),
MiscUtil.asXMLGregorianCalendar(System.currentTimeMillis())));
// just want to see two values, that's all
record1.addReferenceValue("ref1",
ObjectTypeUtil.createObjectRef(targetOid, ObjectTypes.USER).asReferenceValue());
Expand Down Expand Up @@ -143,7 +144,7 @@ private ObjectDeltaOperation<UserType> createDelta(ItemPath itemPath, Object...
throws SchemaException {
ObjectDeltaOperation<UserType> delta = new ObjectDeltaOperation<>();
delta.setObjectDelta(deltaFor(UserType.class)
.item(itemPath).add(values)
.item(itemPath).replace(values)
.asObjectDelta("any-oid-we-don't-care-here"));
return delta;
}
Expand Down Expand Up @@ -873,7 +874,8 @@ public void test300SearchReturnsMappedToManyAttributes() throws SchemaException
assertThat(prop1.getValue()).containsExactly("val1");
// for other attributes we just use the size check, fetch mechanism is similar
assertThat(record1.getChangedItem()).hasSize(4);
assertThat(record1.getDelta()).hasSize(1);
assertThat(record1.getDelta()).hasSize(3)
.allMatch(d -> d.getObjectDelta() != null);
assertThat(record1.getReference()).hasSize(2);
assertThat(record1.getResourceOid()).hasSize(3);
// we also want to be sure that returned objects have prism definitions
Expand Down
Expand Up @@ -262,7 +262,8 @@ public void processOptions(Collection<SelectorOptions<GetOperationOptions>> opti
public PageOf<S> transformToSchemaType(PageOf<Tuple> result)
throws SchemaException, QueryException {
try {
SqlTransformer<S, Q, R> transformer = mapping().createTransformer(prismContext());
SqlTransformer<S, Q, R> transformer = mapping()
.createTransformer(prismContext(), querydslConfiguration);
return result.map(row -> transformer.toSchemaObjectSafe(row, root()));
} catch (SqlTransformer.SqlTransformationException e) {
Throwable cause = e.getCause();
Expand Down
Expand Up @@ -8,6 +8,7 @@

import com.querydsl.core.Tuple;
import com.querydsl.sql.ColumnMetadata;
import com.querydsl.sql.Configuration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -28,10 +29,13 @@ public abstract class SqlTransformer<S, Q extends FlexibleRelationalPathBase<R>,

protected final PrismContext prismContext;
protected final QueryModelMapping<S, Q, R> mapping;
protected final Configuration querydslConfiguration;

protected SqlTransformer(PrismContext prismContext, QueryModelMapping<S, Q, R> mapping) {
protected SqlTransformer(PrismContext prismContext,
QueryModelMapping<S, Q, R> mapping, Configuration querydslConfiguration) {
this.prismContext = prismContext;
this.mapping = mapping;
this.querydslConfiguration = querydslConfiguration;
}

/**
Expand Down
Expand Up @@ -15,6 +15,7 @@
import com.querydsl.core.types.Path;
import com.querydsl.core.types.Predicate;
import com.querydsl.sql.ColumnMetadata;
import com.querydsl.sql.Configuration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -219,7 +220,8 @@ public synchronized Q defaultAlias() {
/**
* Creates {@link SqlTransformer} of row bean to schema type, override if provided.
*/
public SqlTransformer<S, Q, R> createTransformer(PrismContext prismContext) {
public SqlTransformer<S, Q, R> createTransformer(
PrismContext prismContext, Configuration querydslConfiguration) {
throw new UnsupportedOperationException("Bean transformer not supported for " + queryType);
}

Expand Down
Expand Up @@ -6,43 +6,61 @@
*/
package com.evolveum.midpoint.repo.sql.pure.querymodel.mapping;

import com.querydsl.sql.Configuration;
import com.querydsl.sql.SQLServerTemplates;

import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.polystring.PolyString;
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.beans.MAuditDelta;
import com.evolveum.midpoint.schema.ObjectDeltaOperation;
import com.evolveum.midpoint.repo.sql.util.RUtil;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectDeltaOperationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.OperationResultType;
import com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;

/**
* Simple class with methods for audit event transformation between repo and Prism world.
*/
public class AuditDeltaSqlTransformer
extends SqlTransformer<ObjectDeltaOperationType, QAuditDelta, MAuditDelta> {

public AuditDeltaSqlTransformer(PrismContext prismContext, QAuditDeltaMapping mapping) {
super(prismContext, mapping);
public AuditDeltaSqlTransformer(PrismContext prismContext,
QAuditDeltaMapping mapping, Configuration querydslConfiguration) {
super(prismContext, mapping, querydslConfiguration);
}

public ObjectDeltaOperationType toSchemaObject(MAuditDelta row) throws SchemaException {
ObjectDeltaOperation<ObjectType> delta = new ObjectDeltaOperation<>();

ObjectDeltaOperationType odo = new ObjectDeltaOperationType();
boolean usingSqlServer = querydslConfiguration.getTemplates() instanceof SQLServerTemplates;
if (row.delta != null) {
String serializedDelta =
RUtil.getSerializedFormFromBytes(row.delta, usingSqlServer);

ObjectDeltaType delta = prismContext.parserFor(serializedDelta)
.parseRealValue(ObjectDeltaType.class);
odo.setObjectDelta(delta);
}
if (row.fullResult != null) {
String serializedResult =
RUtil.getSerializedFormFromBytes(row.fullResult, usingSqlServer);

OperationResultType resultType = prismContext.parserFor(serializedResult)
.parseRealValue(OperationResultType.class);
odo.setExecutionResult(resultType);
}
if (row.objectNameOrig != null || row.objectNameNorm != null) {
odo.setObjectName(new PolyStringType(
new PolyString(row.objectNameOrig, row.objectNameNorm)));
}
odo.setResourceOid(row.resourceOid);
if (row.resourceNameOrig != null || row.resourceNameNorm != null) {
odo.setResourceName(new PolyStringType(
new PolyString(row.resourceNameOrig, row.resourceNameNorm)));
}

return odo;
/*
TODO MID-6319
DeltaConversionOptions options = DeltaConversionOptions.createSerializeReferenceNames();
// This can be tricky because it can create human-readable but machine-unprocessable
// data, see MID-6262. But in current context the results of this method are to be
// used only in GUI and reports, so we are safe here.
// THIS ^^ is taken originally from AuditEventRecord#createAuditEventRecordType(boolean)
options.setEscapeInvalidCharacters(true);
DeltaConvertor.toObjectDeltaOperationType(delta, odo, options);
auditRecordType.getDelta().add(odo);
objectDeltaOperation.;
record.delta(objectDeltaOperation);
*/
}
}
Expand Up @@ -13,6 +13,7 @@
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;
Expand All @@ -38,14 +39,14 @@
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;

/**
* Simple class with methods for audit event transformation between repo and Prism world.
* Transformation of audit event records between repo and Prism world.
*/
public class AuditEventRecordSqlTransformer
extends SqlTransformer<AuditEventRecordType, QAuditEventRecord, MAuditEventRecord> {

public AuditEventRecordSqlTransformer(
PrismContext prismContext, QAuditEventRecordMapping mapping) {
super(prismContext, mapping);
public AuditEventRecordSqlTransformer(PrismContext prismContext,
QAuditEventRecordMapping mapping, Configuration querydslConfiguration) {
super(prismContext, mapping, querydslConfiguration);
}

public AuditEventRecordType toSchemaObject(MAuditEventRecord row) throws SchemaException {
Expand Down Expand Up @@ -100,10 +101,10 @@ private void mapDeltas(AuditEventRecordType record, List<MAuditDelta> deltas)
return;
}

SqlTransformer<ObjectDeltaOperationType, QAuditDelta, MAuditDelta> transformer =
QAuditDeltaMapping.INSTANCE.createTransformer(prismContext);
SqlTransformer<ObjectDeltaOperationType, QAuditDelta, MAuditDelta> deltaTransformer =
QAuditDeltaMapping.INSTANCE.createTransformer(prismContext, querydslConfiguration);
for (MAuditDelta delta : deltas) {
record.delta(transformer.toSchemaObject(delta));
record.delta(deltaTransformer.toSchemaObject(delta));
}
}

Expand Down
Expand Up @@ -8,6 +8,8 @@

import static com.evolveum.midpoint.repo.sql.pure.querymodel.QAuditItem.TABLE_NAME;

import com.querydsl.sql.Configuration;

import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.repo.sql.pure.SqlTransformer;
import com.evolveum.midpoint.repo.sql.pure.mapping.QueryModelMapping;
Expand Down Expand Up @@ -37,7 +39,7 @@ protected QAuditDelta newAliasInstance(String alias) {

@Override
public SqlTransformer<ObjectDeltaOperationType, QAuditDelta, MAuditDelta> createTransformer(
PrismContext prismContext) {
return new AuditDeltaSqlTransformer(prismContext, this);
PrismContext prismContext, Configuration querydslConfiguration) {
return new AuditDeltaSqlTransformer(prismContext, this, querydslConfiguration);
}
}
Expand Up @@ -9,6 +9,8 @@
import static com.evolveum.midpoint.repo.sql.pure.querymodel.QAuditEventRecord.TABLE_NAME;
import static com.evolveum.midpoint.xml.ns._public.common.audit_3.AuditEventRecordType.*;

import com.querydsl.sql.Configuration;

import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.repo.sql.data.audit.RAuditEventStage;
import com.evolveum.midpoint.repo.sql.data.audit.RAuditEventType;
Expand Down Expand Up @@ -108,7 +110,8 @@ protected QAuditEventRecord newAliasInstance(String alias) {
}

@Override
public AuditEventRecordSqlTransformer createTransformer(PrismContext prismContext) {
return new AuditEventRecordSqlTransformer(prismContext, this);
public AuditEventRecordSqlTransformer createTransformer(
PrismContext prismContext, Configuration querydslConfiguration) {
return new AuditEventRecordSqlTransformer(prismContext, this, querydslConfiguration);
}
}

0 comments on commit c2a72ef

Please sign in to comment.