use of com.evolveum.midpoint.repo.sqale.qmodel.org.QOrgClosure 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();
}
}
use of com.evolveum.midpoint.repo.sqale.qmodel.org.QOrgClosure in project midpoint by Evolveum.
the class SqaleRepositoryService method isDescendant.
// endregion
@Override
public <O extends ObjectType> boolean isDescendant(PrismObject<O> object, String ancestorOrgOid) {
Validate.notNull(object, "object must not be null");
Validate.notNull(ancestorOrgOid, "ancestorOrgOid must not be null");
logger.trace("Querying if object {} is descendant of {}", object.getOid(), ancestorOrgOid);
List<ObjectReferenceType> objParentOrgRefs = object.asObjectable().getParentOrgRef();
if (objParentOrgRefs == null || objParentOrgRefs.isEmpty()) {
return false;
}
List<UUID> objParentOrgOids = objParentOrgRefs.stream().map(ref -> UUID.fromString(ref.getOid())).collect(Collectors.toList());
long opHandle = registerOperationStart(OP_IS_DESCENDANT, OrgType.class);
try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
jdbcSession.executeStatement("CALL m_refresh_org_closure()");
QOrgClosure oc = new QOrgClosure();
long count = jdbcSession.newQuery().from(oc).where(oc.ancestorOid.eq(UUID.fromString(ancestorOrgOid)).and(oc.descendantOid.in(objParentOrgOids))).fetchCount();
return count != 0L;
} finally {
registerOperationFinish(opHandle);
}
}
use of com.evolveum.midpoint.repo.sqale.qmodel.org.QOrgClosure in project midpoint by Evolveum.
the class OrgHierarchyPerfTest method initObjects.
@BeforeClass
public void initObjects() throws Exception {
OperationResult result = createOperationResult();
refreshOrgClosureForce();
assertThat(count(QOrg.CLASS)).isZero();
assertThat(count(new QOrgClosure())).isZero();
createOrgsFor(null, 5, 6, result);
assertThatOperationResult(result).isSuccess();
}
use of com.evolveum.midpoint.repo.sqale.qmodel.org.QOrgClosure in project midpoint by Evolveum.
the class SqaleRepoSmokeTest method test020TestOrgClosureConsistency.
@Test
public void test020TestOrgClosureConsistency() throws Exception {
OperationResult result = createOperationResult();
given("reset closure");
refreshOrgClosureForce();
long baseCount = count(new QOrgClosure());
and("user belonging to org hierarchy");
OrgType orgRoot = new OrgType(prismContext).name("orgRoot" + getTestNumber());
String rootOid = repositoryService.addObject(orgRoot.asPrismObject(), null, result);
OrgType org = new OrgType(prismContext).name("org" + getTestNumber()).parentOrgRef(rootOid, OrgType.COMPLEX_TYPE);
String orgOid = repositoryService.addObject(org.asPrismObject(), null, result);
UserType user = new UserType(prismContext).name("user" + getTestNumber()).parentOrgRef(orgOid, OrgType.COMPLEX_TYPE);
repositoryService.addObject(user.asPrismObject(), null, result);
when("testOrgClosureConsistency() is called with rebuild flag");
repositoryService.testOrgClosureConsistency(true, result);
expect("operation is successful and contains info about closure");
assertThatOperationResult(result).isSuccess();
OperationResult subresult = result.getLastSubresult();
assertThat(subresult.getReturnSingle("closure-count")).isEqualTo(String.valueOf(baseCount));
assertThat(subresult.getReturnSingle("expected-count")).isEqualTo(String.valueOf(baseCount + 3));
assertThat(subresult.getReturnSingle("rebuild-done")).isEqualTo("true");
and("closure is rebuilt");
// as explained above
assertThat(count(new QOrgClosure())).isEqualTo(baseCount + 3);
}
use of com.evolveum.midpoint.repo.sqale.qmodel.org.QOrgClosure in project midpoint by Evolveum.
the class SqaleRepoSmokeTest method test021OrgClosureIsRefreshedBeforeOrgFilterQuery.
@Test
public void test021OrgClosureIsRefreshedBeforeOrgFilterQuery() throws Exception {
OperationResult result = createOperationResult();
given("reset closure");
refreshOrgClosureForce();
long baseCount = count(new QOrgClosure());
given("user belonging to org hierarchy");
OrgType orgRoot = new OrgType(prismContext).name("orgRoot" + getTestNumber());
String rootOid = repositoryService.addObject(orgRoot.asPrismObject(), null, result);
OrgType org = new OrgType(prismContext).name("org" + getTestNumber()).parentOrgRef(rootOid, OrgType.COMPLEX_TYPE);
String orgOid = repositoryService.addObject(org.asPrismObject(), null, result);
UserType user = new UserType(prismContext).name("user" + getTestNumber()).parentOrgRef(orgOid, OrgType.COMPLEX_TYPE);
String userOid = repositoryService.addObject(user.asPrismObject(), null, result);
// not refreshed yet
assertThat(count(new QOrgClosure())).isEqualTo(baseCount);
when("query with org filter is used");
SearchResultList<PrismObject<UserType>> users = repositoryService.searchObjects(UserType.class, prismContext.queryFor(UserType.class).isChildOf(rootOid).build(), null, result);
expect("operation is successful and returns proper results");
assertThatOperationResult(result).isSuccess();
assertThat(users).hasSize(1).extracting(p -> p.asObjectable().getOid()).containsExactlyInAnyOrder(userOid);
and("closure is rebuilt");
// see previous test
assertThat(count(new QOrgClosure())).isEqualTo(baseCount + 3);
}
Aggregations