Search in sources :

Example 1 with ResultListRowTransformer

use of com.evolveum.midpoint.repo.sqlbase.mapping.ResultListRowTransformer in project midpoint by Evolveum.

the class QAuditEventRecordMapping method createRowTransformer.

@Override
public ResultListRowTransformer<AuditEventRecordType, QAuditEventRecord, MAuditEventRecord> createRowTransformer(SqlQueryContext<AuditEventRecordType, QAuditEventRecord, MAuditEventRecord> sqlQueryContext, JdbcSession jdbcSession) {
    return new ResultListRowTransformer<>() {

        @Override
        public void beforeTransformation(List<Tuple> rowTuples, QAuditEventRecord entityPath) {
            if (rowTuples.isEmpty()) {
                return;
            }
            Instant minTimestamp = Instant.MAX;
            Instant maxTimestamp = Instant.MIN;
            Map<Long, MAuditEventRecord> rowMap = new HashMap<>();
            for (Tuple rowTuple : rowTuples) {
                MAuditEventRecord row = Objects.requireNonNull(rowTuple.get(entityPath));
                rowMap.put(row.id, row);
                if (row.timestamp.isBefore(minTimestamp)) {
                    minTimestamp = row.timestamp;
                }
                if (row.timestamp.isAfter(maxTimestamp)) {
                    maxTimestamp = row.timestamp;
                }
            }
            QAuditDelta qd = QAuditDeltaMapping.get().defaultAlias();
            jdbcSession.newQuery().select(qd).from(qd).where(qd.recordId.in(rowMap.keySet()).and(qd.timestamp.between(minTimestamp, maxTimestamp))).fetch().forEach(d -> rowMap.get(d.recordId).addDelta(d));
            QAuditRefValue qr = QAuditRefValueMapping.get().defaultAlias();
            jdbcSession.newQuery().select(qr).from(qr).where(qr.recordId.in(rowMap.keySet()).and(qr.timestamp.between(minTimestamp, maxTimestamp))).fetch().forEach(r -> rowMap.get(r.recordId).addRefValue(r));
        }

        @Override
        public AuditEventRecordType transform(Tuple tuple, QAuditEventRecord entityPath, Collection<SelectorOptions<GetOperationOptions>> options) {
            return toSchemaObjectSafe(tuple, entityPath, options, jdbcSession, false);
        }
    };
}
Also used : ResultListRowTransformer(com.evolveum.midpoint.repo.sqlbase.mapping.ResultListRowTransformer) Instant(java.time.Instant) GetOperationOptions(com.evolveum.midpoint.schema.GetOperationOptions) Tuple(com.querydsl.core.Tuple)

Example 2 with ResultListRowTransformer

use of com.evolveum.midpoint.repo.sqlbase.mapping.ResultListRowTransformer in project midpoint by Evolveum.

the class QOperationExecutionMapping method createRowTransformer.

@Override
public ResultListRowTransformer<OperationExecutionType, QOperationExecution<OR>, MOperationExecution> createRowTransformer(SqlQueryContext<OperationExecutionType, QOperationExecution<OR>, MOperationExecution> sqlQueryContext, JdbcSession jdbcSession) {
    Map<UUID, ObjectType> owners = new HashMap<>();
    return new ResultListRowTransformer<>() {

        @Override
        public void beforeTransformation(List<Tuple> rowTuples, QOperationExecution<OR> entityPath) throws SchemaException {
            Set<UUID> ownerOids = rowTuples.stream().map(row -> Objects.requireNonNull(row.get(entityPath)).ownerOid).collect(Collectors.toSet());
            // TODO do we need get options here as well? Is there a scenario where we load container
            // and define what to load for referenced/owner object?
            QObject<?> o = QObjectMapping.getObjectMapping().defaultAlias();
            List<Tuple> result = jdbcSession.newQuery().select(o.oid, o.fullObject).from(o).where(o.oid.in(ownerOids)).fetch();
            for (Tuple row : result) {
                UUID oid = Objects.requireNonNull(row.get(o.oid));
                ObjectType owner = parseSchemaObject(row.get(o.fullObject), oid.toString(), ObjectType.class);
                owners.put(oid, owner);
            }
        }

        @Override
        public OperationExecutionType transform(Tuple rowTuple, QOperationExecution<OR> entityPath, Collection<SelectorOptions<GetOperationOptions>> options) {
            MOperationExecution row = Objects.requireNonNull(rowTuple.get(entityPath));
            ObjectType object = Objects.requireNonNull(owners.get(row.ownerOid), () -> "Missing owner with OID " + row.ownerOid + " for OperationExecution with ID " + row.cid);
            PrismContainer<OperationExecutionType> opexContainer = object.asPrismObject().findContainer(ObjectType.F_OPERATION_EXECUTION);
            if (opexContainer == null) {
                throw new SystemException("Object " + object + " has no operation execution as expected from " + row);
            }
            PrismContainerValue<OperationExecutionType> pcv = opexContainer.findValue(row.cid);
            if (pcv == null) {
                throw new SystemException("Object " + object + " has no operation execution with ID " + row.cid);
            }
            return pcv.asContainerable();
        }
    };
}
Also used : SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions) TableRelationResolver(com.evolveum.midpoint.repo.sqlbase.mapping.TableRelationResolver) java.util(java.util) SqlQueryContext(com.evolveum.midpoint.repo.sqlbase.SqlQueryContext) QTaskMapping(com.evolveum.midpoint.repo.sqale.qmodel.task.QTaskMapping) ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) PrismContainer(com.evolveum.midpoint.prism.PrismContainer) JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession) QContainerMapping(com.evolveum.midpoint.repo.sqale.qmodel.common.QContainerMapping) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) MiscUtil(com.evolveum.midpoint.util.MiscUtil) SqaleRepoContext(com.evolveum.midpoint.repo.sqale.SqaleRepoContext) QFocusMapping(com.evolveum.midpoint.repo.sqale.qmodel.focus.QFocusMapping) Collectors(java.util.stream.Collectors) PrismContainerValue(com.evolveum.midpoint.prism.PrismContainerValue) OperationExecutionType(com.evolveum.midpoint.xml.ns._public.common.common_3.OperationExecutionType) SystemException(com.evolveum.midpoint.util.exception.SystemException) PrismConstants(com.evolveum.midpoint.prism.PrismConstants) ResultListRowTransformer(com.evolveum.midpoint.repo.sqlbase.mapping.ResultListRowTransformer) GetOperationOptions(com.evolveum.midpoint.schema.GetOperationOptions) NotNull(org.jetbrains.annotations.NotNull) Tuple(com.querydsl.core.Tuple) ResultListRowTransformer(com.evolveum.midpoint.repo.sqlbase.mapping.ResultListRowTransformer) OperationExecutionType(com.evolveum.midpoint.xml.ns._public.common.common_3.OperationExecutionType) ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) GetOperationOptions(com.evolveum.midpoint.schema.GetOperationOptions) SystemException(com.evolveum.midpoint.util.exception.SystemException) Tuple(com.querydsl.core.Tuple)

Aggregations

ResultListRowTransformer (com.evolveum.midpoint.repo.sqlbase.mapping.ResultListRowTransformer)2 GetOperationOptions (com.evolveum.midpoint.schema.GetOperationOptions)2 Tuple (com.querydsl.core.Tuple)2 PrismConstants (com.evolveum.midpoint.prism.PrismConstants)1 PrismContainer (com.evolveum.midpoint.prism.PrismContainer)1 PrismContainerValue (com.evolveum.midpoint.prism.PrismContainerValue)1 SqaleRepoContext (com.evolveum.midpoint.repo.sqale.SqaleRepoContext)1 QContainerMapping (com.evolveum.midpoint.repo.sqale.qmodel.common.QContainerMapping)1 QFocusMapping (com.evolveum.midpoint.repo.sqale.qmodel.focus.QFocusMapping)1 QTaskMapping (com.evolveum.midpoint.repo.sqale.qmodel.task.QTaskMapping)1 JdbcSession (com.evolveum.midpoint.repo.sqlbase.JdbcSession)1 SqlQueryContext (com.evolveum.midpoint.repo.sqlbase.SqlQueryContext)1 TableRelationResolver (com.evolveum.midpoint.repo.sqlbase.mapping.TableRelationResolver)1 SelectorOptions (com.evolveum.midpoint.schema.SelectorOptions)1 MiscUtil (com.evolveum.midpoint.util.MiscUtil)1 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1 SystemException (com.evolveum.midpoint.util.exception.SystemException)1 ObjectType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType)1 OperationExecutionType (com.evolveum.midpoint.xml.ns._public.common.common_3.OperationExecutionType)1 Instant (java.time.Instant)1