Search in sources :

Example 1 with MAuditDelta

use of com.evolveum.midpoint.repo.sql.audit.beans.MAuditDelta in project midpoint by Evolveum.

the class SqlAuditServiceImpl method insertAuditDeltas.

private Collection<MAuditDelta> insertAuditDeltas(JdbcSession jdbcSession, long recordId, Collection<ObjectDeltaOperation<?>> deltas) {
    // we want to keep only unique deltas, checksum is also part of PK
    Map<String, MAuditDelta> deltasByChecksum = new HashMap<>();
    for (ObjectDeltaOperation<?> deltaOperation : deltas) {
        if (deltaOperation == null) {
            continue;
        }
        MAuditDelta mAuditDelta = convertDelta(deltaOperation, recordId);
        deltasByChecksum.put(mAuditDelta.checksum, mAuditDelta);
    }
    if (!deltasByChecksum.isEmpty()) {
        SQLInsertClause insertBatch = jdbcSession.newInsert(QAuditDeltaMapping.get().defaultAlias());
        for (MAuditDelta value : deltasByChecksum.values()) {
            // NULLs are important to keep the value count consistent during the batch
            insertBatch.populate(value, DefaultMapper.WITH_NULL_BINDINGS).addBatch();
        }
        insertBatch.setBatchToBulk(true);
        insertBatch.execute();
    }
    return deltasByChecksum.values();
}
Also used : MAuditDelta(com.evolveum.midpoint.repo.sql.audit.beans.MAuditDelta) SQLInsertClause(com.querydsl.sql.dml.SQLInsertClause) PolyString(com.evolveum.midpoint.prism.polystring.PolyString)

Example 2 with MAuditDelta

use of com.evolveum.midpoint.repo.sql.audit.beans.MAuditDelta in project midpoint by Evolveum.

the class SqlAuditServiceImpl method convertDelta.

private MAuditDelta convertDelta(ObjectDeltaOperation<?> deltaOperation, long recordId) {
    MAuditDelta mAuditDelta = new MAuditDelta();
    mAuditDelta.recordId = recordId;
    try {
        ObjectDelta<? extends ObjectType> delta = deltaOperation.getObjectDelta();
        if (delta != null) {
            DeltaConversionOptions options = DeltaConversionOptions.createSerializeReferenceNames();
            options.setEscapeInvalidCharacters(isEscapingInvalidCharacters(auditConfiguration));
            String serializedDelta = DeltaConvertor.serializeDelta(delta, options, PrismContext.LANG_XML);
            // serializedDelta is transient, needed for changed items later
            mAuditDelta.serializedDelta = serializedDelta;
            mAuditDelta.delta = RUtil.getBytesFromSerializedForm(serializedDelta, sqlConfiguration().isUseZipAudit());
            mAuditDelta.deltaOid = delta.getOid();
            mAuditDelta.deltaType = MiscUtil.enumOrdinal(RUtil.getRepoEnumValue(delta.getChangeType(), RChangeType.class));
        }
        OperationResult executionResult = deltaOperation.getExecutionResult();
        if (executionResult != null) {
            OperationResultType jaxb = executionResult.createOperationResultType();
            if (jaxb != null) {
                mAuditDelta.status = MiscUtil.enumOrdinal(RUtil.getRepoEnumValue(jaxb.getStatus(), ROperationResultStatus.class));
                // Note that escaping invalid characters and using toString for unsupported types is safe in the
                // context of operation result serialization.
                String full = schemaService.createStringSerializer(PrismContext.LANG_XML).options(SerializationOptions.createEscapeInvalidCharacters().serializeUnsupportedTypesAsString(true)).serializeRealValue(jaxb, SchemaConstantsGenerated.C_OPERATION_RESULT);
                mAuditDelta.fullResult = RUtil.getBytesFromSerializedForm(full, sqlConfiguration().isUseZipAudit());
            }
        }
        mAuditDelta.resourceOid = deltaOperation.getResourceOid();
        if (deltaOperation.getObjectName() != null) {
            mAuditDelta.objectNameOrig = deltaOperation.getObjectName().getOrig();
            mAuditDelta.objectNameNorm = deltaOperation.getObjectName().getNorm();
        }
        if (deltaOperation.getResourceName() != null) {
            mAuditDelta.resourceNameOrig = deltaOperation.getResourceName().getOrig();
            mAuditDelta.resourceNameNorm = deltaOperation.getResourceName().getNorm();
        }
        mAuditDelta.checksum = RUtil.computeChecksum(mAuditDelta.delta, mAuditDelta.fullResult);
    } catch (Exception ex) {
        throw new SystemException("Problem during audit delta conversion", ex);
    }
    return mAuditDelta;
}
Also used : MAuditDelta(com.evolveum.midpoint.repo.sql.audit.beans.MAuditDelta) SystemException(com.evolveum.midpoint.util.exception.SystemException) OperationResultType(com.evolveum.midpoint.xml.ns._public.common.common_3.OperationResultType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) SystemException(com.evolveum.midpoint.util.exception.SystemException)

Example 3 with MAuditDelta

use of com.evolveum.midpoint.repo.sql.audit.beans.MAuditDelta in project midpoint by Evolveum.

the class SqlAuditServiceImpl method convertDelta.

private MAuditDelta convertDelta(ObjectDeltaOperationType deltaOperation, MAuditEventRecord auditRow) {
    MAuditDelta mAuditDelta = new MAuditDelta();
    mAuditDelta.recordId = auditRow.id;
    try {
        ObjectDeltaType delta = deltaOperation.getObjectDelta();
        if (delta != null) {
            DeltaConversionOptions options = DeltaConversionOptions.createSerializeReferenceNames();
            options.setEscapeInvalidCharacters(isEscapingInvalidCharacters(auditConfiguration));
            String serializedDelta = DeltaConvertor.serializeDelta(delta, options, PrismContext.LANG_XML);
            // serializedDelta is transient, needed for changed items later
            mAuditDelta.serializedDelta = serializedDelta;
            mAuditDelta.delta = RUtil.getBytesFromSerializedForm(serializedDelta, sqlConfiguration().isUseZipAudit());
            mAuditDelta.deltaOid = delta.getOid();
            mAuditDelta.deltaType = MiscUtil.enumOrdinal(RUtil.getRepoEnumValue(ChangeType.toChangeType(delta.getChangeType()), RChangeType.class));
            for (ItemDeltaType itemDelta : delta.getItemDelta()) {
                ItemPath path = itemDelta.getPath().getItemPath();
                CanonicalItemPath canonical = schemaService.createCanonicalItemPath(path, delta.getObjectType());
                for (int i = 0; i < canonical.size(); i++) {
                    auditRow.addChangedItem(canonical.allUpToIncluding(i).asString());
                }
            }
        }
        OperationResultType executionResult = deltaOperation.getExecutionResult();
        if (executionResult != null) {
            mAuditDelta.status = MiscUtil.enumOrdinal(RUtil.getRepoEnumValue(executionResult.getStatus(), ROperationResultStatus.class));
            // Note that escaping invalid characters and using toString for unsupported types is safe in the
            // context of operation result serialization.
            String full = schemaService.createStringSerializer(PrismContext.LANG_XML).options(SerializationOptions.createEscapeInvalidCharacters().serializeUnsupportedTypesAsString(true)).serializeRealValue(executionResult, SchemaConstantsGenerated.C_OPERATION_RESULT);
            mAuditDelta.fullResult = RUtil.getBytesFromSerializedForm(full, sqlConfiguration().isUseZipAudit());
        }
        mAuditDelta.resourceOid = deltaOperation.getResourceOid();
        if (deltaOperation.getObjectName() != null) {
            mAuditDelta.objectNameOrig = deltaOperation.getObjectName().getOrig();
            mAuditDelta.objectNameNorm = deltaOperation.getObjectName().getNorm();
        }
        if (deltaOperation.getResourceName() != null) {
            mAuditDelta.resourceNameOrig = deltaOperation.getResourceName().getOrig();
            mAuditDelta.resourceNameNorm = deltaOperation.getResourceName().getNorm();
        }
        mAuditDelta.checksum = RUtil.computeChecksum(mAuditDelta.delta, mAuditDelta.fullResult);
    } catch (Exception ex) {
        throw new SystemException("Problem during audit delta conversion", ex);
    }
    return mAuditDelta;
}
Also used : MAuditDelta(com.evolveum.midpoint.repo.sql.audit.beans.MAuditDelta) ObjectDeltaType(com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType) SystemException(com.evolveum.midpoint.util.exception.SystemException) OperationResultType(com.evolveum.midpoint.xml.ns._public.common.common_3.OperationResultType) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) CanonicalItemPath(com.evolveum.midpoint.prism.path.CanonicalItemPath) ItemDeltaType(com.evolveum.prism.xml.ns._public.types_3.ItemDeltaType) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) SystemException(com.evolveum.midpoint.util.exception.SystemException) CanonicalItemPath(com.evolveum.midpoint.prism.path.CanonicalItemPath) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 4 with MAuditDelta

use of com.evolveum.midpoint.repo.sql.audit.beans.MAuditDelta in project midpoint by Evolveum.

the class SqlAuditServiceImpl method insertChangedItemPaths.

private void insertChangedItemPaths(JdbcSession jdbcSession, long recordId, Collection<MAuditDelta> deltas) {
    Set<String> changedItemPaths = new HashSet<>();
    for (MAuditDelta delta : deltas) {
        try {
            ObjectDeltaType deltaBean = schemaService.parserFor(delta.serializedDelta).parseRealValue(ObjectDeltaType.class);
            for (ItemDeltaType itemDelta : deltaBean.getItemDelta()) {
                ItemPath path = itemDelta.getPath().getItemPath();
                CanonicalItemPath canonical = schemaService.createCanonicalItemPath(path, deltaBean.getObjectType());
                for (int i = 0; i < canonical.size(); i++) {
                    changedItemPaths.add(canonical.allUpToIncluding(i).asString());
                }
            }
        } catch (SchemaException | SystemException e) {
            // See MID-6446 - we want to throw in tests, old ones should be fixed by now
            if (InternalsConfig.isConsistencyChecks()) {
                throw new SystemException("Problem during audit delta parse", e);
            }
            LOGGER.warn("Serialized audit delta for recordId={} cannot be parsed." + " No changed items were created. This may cause problem later, but is not" + " critical for storing the audit record.", recordId, e);
        }
    }
    if (!changedItemPaths.isEmpty()) {
        QAuditItem qAuditItem = QAuditItemMapping.get().defaultAlias();
        SQLInsertClause insertBatch = jdbcSession.newInsert(qAuditItem);
        for (String changedItemPath : changedItemPaths) {
            insertBatch.set(qAuditItem.recordId, recordId).set(qAuditItem.changedItemPath, changedItemPath).addBatch();
        }
        insertBatch.setBatchToBulk(true);
        insertBatch.execute();
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) CanonicalItemPath(com.evolveum.midpoint.prism.path.CanonicalItemPath) ItemDeltaType(com.evolveum.prism.xml.ns._public.types_3.ItemDeltaType) MAuditDelta(com.evolveum.midpoint.repo.sql.audit.beans.MAuditDelta) ObjectDeltaType(com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType) SystemException(com.evolveum.midpoint.util.exception.SystemException) SQLInsertClause(com.querydsl.sql.dml.SQLInsertClause) CanonicalItemPath(com.evolveum.midpoint.prism.path.CanonicalItemPath) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 5 with MAuditDelta

use of com.evolveum.midpoint.repo.sql.audit.beans.MAuditDelta in project midpoint by Evolveum.

the class SqlAuditServiceImpl method insertAuditDeltas.

private void insertAuditDeltas(JdbcSession jdbcSession, MAuditEventRecord auditRow, List<ObjectDeltaOperationType> deltas) {
    // we want to keep only unique deltas, checksum is also part of PK
    Map<String, MAuditDelta> deltasByChecksum = new HashMap<>();
    for (ObjectDeltaOperationType delta : deltas) {
        if (delta == null) {
            continue;
        }
        MAuditDelta mAuditDelta = convertDelta(delta, auditRow);
        deltasByChecksum.put(mAuditDelta.checksum, mAuditDelta);
    }
    if (!deltasByChecksum.isEmpty()) {
        SQLInsertClause insertBatch = jdbcSession.newInsert(QAuditDeltaMapping.get().defaultAlias());
        for (MAuditDelta value : deltasByChecksum.values()) {
            // NULLs are important to keep the value count consistent during the batch
            insertBatch.populate(value, DefaultMapper.WITH_NULL_BINDINGS).addBatch();
        }
        insertBatch.setBatchToBulk(true);
        insertBatch.execute();
    }
}
Also used : MAuditDelta(com.evolveum.midpoint.repo.sql.audit.beans.MAuditDelta) ObjectDeltaOperationType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectDeltaOperationType) SQLInsertClause(com.querydsl.sql.dml.SQLInsertClause) PolyString(com.evolveum.midpoint.prism.polystring.PolyString)

Aggregations

PolyString (com.evolveum.midpoint.prism.polystring.PolyString)5 MAuditDelta (com.evolveum.midpoint.repo.sql.audit.beans.MAuditDelta)5 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)3 SystemException (com.evolveum.midpoint.util.exception.SystemException)3 SQLInsertClause (com.querydsl.sql.dml.SQLInsertClause)3 CanonicalItemPath (com.evolveum.midpoint.prism.path.CanonicalItemPath)2 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)2 OperationResultType (com.evolveum.midpoint.xml.ns._public.common.common_3.OperationResultType)2 ItemDeltaType (com.evolveum.prism.xml.ns._public.types_3.ItemDeltaType)2 ObjectDeltaType (com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType)2 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)1 ObjectDeltaOperationType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectDeltaOperationType)1