use of com.evolveum.midpoint.repo.sqale.qmodel.org.QOrg 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();
}
}
Aggregations