Search in sources :

Example 1 with QOrgClosure

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();
    }
}
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 QOrgClosure

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);
    }
}
Also used : Connection(java.sql.Connection) com.evolveum.midpoint.prism.query(com.evolveum.midpoint.prism.query) com.evolveum.midpoint.repo.sqlbase(com.evolveum.midpoint.repo.sqlbase) QOrgClosure(com.evolveum.midpoint.repo.sqale.qmodel.org.QOrgClosure) Autowired(org.springframework.beans.factory.annotation.Autowired) QueryTableMapping(com.evolveum.midpoint.repo.sqlbase.mapping.QueryTableMapping) com.evolveum.midpoint.util.exception(com.evolveum.midpoint.util.exception) InternalsConfig(com.evolveum.midpoint.schema.internals.InternalsConfig) OperationResultStatus(com.evolveum.midpoint.schema.result.OperationResultStatus) S_MatchingRuleEntry(com.evolveum.midpoint.prism.query.builder.S_MatchingRuleEntry) com.evolveum.midpoint.repo.api(com.evolveum.midpoint.repo.api) com.evolveum.midpoint.prism(com.evolveum.midpoint.prism) ObjectQueryUtil(com.evolveum.midpoint.schema.util.ObjectQueryUtil) XmlTypeConverter(com.evolveum.midpoint.prism.xml.XmlTypeConverter) S_ConditionEntry(com.evolveum.midpoint.prism.query.builder.S_ConditionEntry) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) RootUpdateContext(com.evolveum.midpoint.repo.sqale.update.RootUpdateContext) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) Expressions(com.querydsl.core.types.dsl.Expressions) MObject(com.evolveum.midpoint.repo.sqale.qmodel.object.MObject) PropertyDelta(com.evolveum.midpoint.prism.delta.PropertyDelta) QName(javax.xml.namespace.QName) NotNull(org.jetbrains.annotations.NotNull) FocusTypeUtil(com.evolveum.midpoint.schema.util.FocusTypeUtil) Tuple(com.querydsl.core.Tuple) FlexibleRelationalPathBase(com.evolveum.midpoint.repo.sqlbase.querydsl.FlexibleRelationalPathBase) com.evolveum.midpoint.schema(com.evolveum.midpoint.schema) java.util(java.util) com.evolveum.midpoint.xml.ns._public.common.common_3(com.evolveum.midpoint.xml.ns._public.common.common_3) ObjectFilterExpressionEvaluator(com.evolveum.midpoint.repo.api.query.ObjectFilterExpressionEvaluator) AddObjectContext(com.evolveum.midpoint.repo.sqale.update.AddObjectContext) CryptoUtil(com.evolveum.midpoint.common.crypto.CryptoUtil) SchemaConstants(com.evolveum.midpoint.schema.constants.SchemaConstants) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ItemDeltaCollectionsUtil(com.evolveum.midpoint.prism.delta.ItemDeltaCollectionsUtil) Trace(com.evolveum.midpoint.util.logging.Trace) ObjectUtils.defaultIfNull(org.apache.commons.lang3.ObjectUtils.defaultIfNull) Path(com.querydsl.core.types.Path) PrettyPrinter(com.evolveum.midpoint.util.PrettyPrinter) QOrgMapping(com.evolveum.midpoint.repo.sqale.qmodel.org.QOrgMapping) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) QObjectReferenceMapping(com.evolveum.midpoint.repo.sqale.qmodel.ref.QObjectReferenceMapping) Strings(com.google.common.base.Strings) SQLException(java.sql.SQLException) EquivalenceStrategy(com.evolveum.midpoint.prism.equivalence.EquivalenceStrategy) ObjectTypeUtil(com.evolveum.midpoint.schema.util.ObjectTypeUtil) MObjectType(com.evolveum.midpoint.repo.sqale.qmodel.object.MObjectType) SQLQuery(com.querydsl.sql.SQLQuery) QObjectReference(com.evolveum.midpoint.repo.sqale.qmodel.ref.QObjectReference) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) SqaleTableMapping(com.evolveum.midpoint.repo.sqale.mapping.SqaleTableMapping) QObject(com.evolveum.midpoint.repo.sqale.qmodel.object.QObject) ObjectArrays(com.google.common.collect.ObjectArrays) InternalMonitor(com.evolveum.midpoint.schema.internals.InternalMonitor) QOrg(com.evolveum.midpoint.repo.sqale.qmodel.org.QOrg) SqlPerformanceMonitorImpl(com.evolveum.midpoint.repo.sqlbase.perfmon.SqlPerformanceMonitorImpl) ItemPath(com.evolveum.midpoint.prism.path.ItemPath) Consumer(java.util.function.Consumer) Validate(org.apache.commons.lang3.Validate) Driver(java.sql.Driver) SearchFilterType(com.evolveum.prism.xml.ns._public.query_3.SearchFilterType) DriverManager(java.sql.DriverManager) QOrgClosure(com.evolveum.midpoint.repo.sqale.qmodel.org.QOrgClosure)

Example 3 with QOrgClosure

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();
}
Also used : QOrgClosure(com.evolveum.midpoint.repo.sqale.qmodel.org.QOrgClosure) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) BeforeClass(org.testng.annotations.BeforeClass)

Example 4 with QOrgClosure

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);
}
Also used : QOrgClosure(com.evolveum.midpoint.repo.sqale.qmodel.org.QOrgClosure) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Test(org.testng.annotations.Test) SqaleRepoBaseTest(com.evolveum.midpoint.repo.sqale.SqaleRepoBaseTest)

Example 5 with QOrgClosure

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);
}
Also used : QOrgClosure(com.evolveum.midpoint.repo.sqale.qmodel.org.QOrgClosure) SkipException(org.testng.SkipException) FlexibleRelationalPathBase(com.evolveum.midpoint.repo.sqlbase.querydsl.FlexibleRelationalPathBase) com.evolveum.midpoint.schema(com.evolveum.midpoint.schema) DEFAULT_SCHEMA_NAME(com.evolveum.midpoint.repo.sqlbase.querydsl.FlexibleRelationalPathBase.DEFAULT_SCHEMA_NAME) com.evolveum.midpoint.xml.ns._public.common.common_3(com.evolveum.midpoint.xml.ns._public.common.common_3) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Jsonb(com.evolveum.midpoint.repo.sqale.jsonb.Jsonb) QOrgClosure(com.evolveum.midpoint.repo.sqale.qmodel.org.QOrgClosure) JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession) SchemaConstants(com.evolveum.midpoint.schema.constants.SchemaConstants) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) SqaleRepoBaseTest(com.evolveum.midpoint.repo.sqale.SqaleRepoBaseTest) AuditEventStage(com.evolveum.midpoint.audit.api.AuditEventStage) AuditEventRecordType(com.evolveum.midpoint.xml.ns._public.common.audit_3.AuditEventRecordType) SQLException(java.sql.SQLException) SqlRecorder(com.evolveum.midpoint.repo.sqlbase.querydsl.SqlRecorder) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Assert(org.testng.Assert) RepositoryService(com.evolveum.midpoint.repo.api.RepositoryService) AuditEventRecord(com.evolveum.midpoint.audit.api.AuditEventRecord) XmlTypeConverter(com.evolveum.midpoint.prism.xml.XmlTypeConverter) DeleteObjectResult(com.evolveum.midpoint.repo.api.DeleteObjectResult) QUser(com.evolveum.midpoint.repo.sqale.qmodel.focus.QUser) QObject(com.evolveum.midpoint.repo.sqale.qmodel.object.QObject) QContainer(com.evolveum.midpoint.repo.sqale.qmodel.common.QContainer) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) Collection(java.util.Collection) MiscUtil(com.evolveum.midpoint.util.MiscUtil) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException) UUID(java.util.UUID) PrismObject(com.evolveum.midpoint.prism.PrismObject) NullTaskImpl(com.evolveum.midpoint.task.api.test.NullTaskImpl) MUser(com.evolveum.midpoint.repo.sqale.qmodel.focus.MUser) SqlPerformanceMonitorImpl(com.evolveum.midpoint.repo.sqlbase.perfmon.SqlPerformanceMonitorImpl) AuditEventTypeType(com.evolveum.midpoint.xml.ns._public.common.audit_3.AuditEventTypeType) QReference(com.evolveum.midpoint.repo.sqale.qmodel.ref.QReference) AuditEventType(com.evolveum.midpoint.audit.api.AuditEventType) AuditEventStageType(com.evolveum.midpoint.xml.ns._public.common.audit_3.AuditEventStageType) SystemException(com.evolveum.midpoint.util.exception.SystemException) Queue(java.util.Queue) PrismObject(com.evolveum.midpoint.prism.PrismObject) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Test(org.testng.annotations.Test) SqaleRepoBaseTest(com.evolveum.midpoint.repo.sqale.SqaleRepoBaseTest)

Aggregations

QOrgClosure (com.evolveum.midpoint.repo.sqale.qmodel.org.QOrgClosure)8 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)6 SqaleRepoBaseTest (com.evolveum.midpoint.repo.sqale.SqaleRepoBaseTest)3 QObject (com.evolveum.midpoint.repo.sqale.qmodel.object.QObject)3 SQLQuery (com.querydsl.sql.SQLQuery)3 Test (org.testng.annotations.Test)3 XmlTypeConverter (com.evolveum.midpoint.prism.xml.XmlTypeConverter)2 MObject (com.evolveum.midpoint.repo.sqale.qmodel.object.MObject)2 QOrg (com.evolveum.midpoint.repo.sqale.qmodel.org.QOrg)2 QObjectReference (com.evolveum.midpoint.repo.sqale.qmodel.ref.QObjectReference)2 SqlPerformanceMonitorImpl (com.evolveum.midpoint.repo.sqlbase.perfmon.SqlPerformanceMonitorImpl)2 FlexibleRelationalPathBase (com.evolveum.midpoint.repo.sqlbase.querydsl.FlexibleRelationalPathBase)2 com.evolveum.midpoint.schema (com.evolveum.midpoint.schema)2 SchemaConstants (com.evolveum.midpoint.schema.constants.SchemaConstants)2 SQLException (java.sql.SQLException)2 AuditEventRecord (com.evolveum.midpoint.audit.api.AuditEventRecord)1 AuditEventStage (com.evolveum.midpoint.audit.api.AuditEventStage)1 AuditEventType (com.evolveum.midpoint.audit.api.AuditEventType)1 CryptoUtil (com.evolveum.midpoint.common.crypto.CryptoUtil)1 com.evolveum.midpoint.prism (com.evolveum.midpoint.prism)1