Search in sources :

Example 1 with SQLQuery

use of com.querydsl.sql.SQLQuery in project midpoint by Evolveum.

the class SqaleRepositoryService method testOrgClosureConsistency.

@Override
public void testOrgClosureConsistency(boolean repairIfNecessary, OperationResult parentResult) {
    OperationResult operationResult = parentResult.subresult(opNamePrefix + OP_TEST_ORG_CLOSURE_CONSISTENCY).addParam("repairIfNecessary", repairIfNecessary).build();
    try {
        long closureCount, expectedCount;
        try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startReadOnlyTransaction()) {
            QOrgClosure oc = new QOrgClosure();
            closureCount = jdbcSession.newQuery().from(oc).fetchCount();
            // this is CTE used also for m_org_closure materialized view (here with count)
            QOrg o = QOrgMapping.getOrgMapping().defaultAlias();
            QObjectReference<?> ref = QObjectReferenceMapping.getForParentOrg().newAlias("ref");
            QObjectReference<?> par = QObjectReferenceMapping.getForParentOrg().newAlias("par");
            // noinspection unchecked
            expectedCount = jdbcSession.newQuery().withRecursive(oc, oc.ancestorOid, oc.descendantOid).as(new SQLQuery<>().union(// non-recursive term: initial select
            new SQLQuery<>().select(o.oid, o.oid).from(o).where(new SQLQuery<>().select(Expressions.ONE).from(ref).where(ref.targetOid.eq(o.oid).or(ref.ownerOid.eq(o.oid))).exists()), new SQLQuery<>().select(par.targetOid, oc.descendantOid).from(par, oc).where(par.ownerOid.eq(oc.ancestorOid)))).from(oc).fetchCount();
            logger.info("Org closure consistency checked - closure count {}, expected count {}", closureCount, expectedCount);
        }
        operationResult.addReturn("closure-count", closureCount);
        operationResult.addReturn("expected-count", expectedCount);
        if (repairIfNecessary && closureCount != expectedCount) {
            try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
                jdbcSession.executeStatement("CALL m_refresh_org_closure(true)");
                jdbcSession.commit();
            }
            logger.info("Org closure rebuild was requested and executed");
            operationResult.addReturn("rebuild-done", true);
        } else {
            operationResult.addReturn("rebuild-done", false);
        }
        operationResult.recordSuccess();
    } catch (Exception e) {
        recordFatalError(operationResult, e);
    } finally {
        operationResult.computeStatusIfUnknown();
    }
}
Also used : QOrgClosure(com.evolveum.midpoint.repo.sqale.qmodel.org.QOrgClosure) QOrg(com.evolveum.midpoint.repo.sqale.qmodel.org.QOrg) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) SQLQuery(com.querydsl.sql.SQLQuery) SQLException(java.sql.SQLException)

Example 2 with SQLQuery

use of com.querydsl.sql.SQLQuery in project midpoint by Evolveum.

the class OrgFilterProcessor method process.

@Override
public Predicate process(OrgFilter filter) throws QueryException {
    // necessary for lazy refresh of org closure
    context.markContainsOrgFilter();
    FlexibleRelationalPathBase<?> path = context.root();
    if (!(path instanceof QObject)) {
        throw new QueryException("Org filter can only be used for objects," + " not for path " + path + " of type " + path.getClass());
    }
    QObject<?> objectPath = (QObject<?>) path;
    if (filter.isRoot()) {
        QObjectReference<MObject> ref = getNewRefAlias();
        return subQuery(ref).where(ref.ownerOid.eq(objectPath.oid)).notExists();
    }
    if (filter.getOrgRef() == null) {
        throw new QueryException("No organization reference defined in the search query.");
    }
    String oidParam = filter.getOrgRef().getOid();
    if (oidParam == null) {
        throw new QueryException("No oid specified in organization reference " + filter.getOrgRef().debugDump());
    }
    QName relation = filter.getOrgRef().getRelation();
    // null means ANY (not "default") here, so we only search/normalize non-nulls
    Integer relationId = relation != null ? context.repositoryContext().searchCachedRelationId(relation) : null;
    if (filter.getScope() == OrgFilter.Scope.ONE_LEVEL) {
        QObjectReference<MObject> ref = getNewRefAlias();
        SQLQuery<?> subQuery = subQuery(ref).where(ref.ownerOid.eq(objectPath.oid).and(ref.targetOid.eq(UUID.fromString(oidParam))));
        if (relationId != null) {
            subQuery.where(ref.relationId.eq(relationId));
        }
        return subQuery.exists();
    } else if (filter.getScope() == OrgFilter.Scope.SUBTREE) {
        QObjectReference<MObject> ref = getNewRefAlias();
        QOrgClosure oc = getNewClosureAlias();
        SQLQuery<?> subQuery = subQuery(ref).join(oc).on(oc.descendantOid.eq(ref.targetOid)).where(ref.ownerOid.eq(objectPath.oid).and(oc.ancestorOid.eq(UUID.fromString(oidParam))));
        if (relationId != null) {
            subQuery.where(ref.relationId.eq(relationId));
        }
        return subQuery.exists();
    } else if (filter.getScope() == OrgFilter.Scope.ANCESTORS) {
        QOrgClosure oc = getNewClosureAlias();
        return subQuery(oc).where(oc.ancestorOid.eq(objectPath.oid).and(oc.descendantOid.eq(UUID.fromString(oidParam))).and(oc.ancestorOid.ne(oc.descendantOid))).exists();
    } else {
        throw new QueryException("Unknown scope if org filter: " + filter);
    }
}
Also used : QName(javax.xml.namespace.QName) MObject(com.evolveum.midpoint.repo.sqale.qmodel.object.MObject) SQLQuery(com.querydsl.sql.SQLQuery) QObject(com.evolveum.midpoint.repo.sqale.qmodel.object.QObject) QOrgClosure(com.evolveum.midpoint.repo.sqale.qmodel.org.QOrgClosure) QueryException(com.evolveum.midpoint.repo.sqlbase.QueryException) QObjectReference(com.evolveum.midpoint.repo.sqale.qmodel.ref.QObjectReference)

Aggregations

QOrgClosure (com.evolveum.midpoint.repo.sqale.qmodel.org.QOrgClosure)2 SQLQuery (com.querydsl.sql.SQLQuery)2 MObject (com.evolveum.midpoint.repo.sqale.qmodel.object.MObject)1 QObject (com.evolveum.midpoint.repo.sqale.qmodel.object.QObject)1 QOrg (com.evolveum.midpoint.repo.sqale.qmodel.org.QOrg)1 QObjectReference (com.evolveum.midpoint.repo.sqale.qmodel.ref.QObjectReference)1 QueryException (com.evolveum.midpoint.repo.sqlbase.QueryException)1 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)1 SQLException (java.sql.SQLException)1 QName (javax.xml.namespace.QName)1