Search in sources :

Example 6 with JdbcSession

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

the class SqaleAuditServiceFactory method initCustomColumns.

private void initCustomColumns(@NotNull Configuration configuration, SqaleRepoContext sqlRepoContext) {
    List<HierarchicalConfiguration<ImmutableNode>> subConfigColumns = ((BaseHierarchicalConfiguration) configuration).configurationsAt(CONF_AUDIT_SERVICE_COLUMNS);
    // here we use config from context, it can be main repository configuration
    SqaleRepositoryConfiguration repoConfig = (SqaleRepositoryConfiguration) sqlRepoContext.getJdbcRepositoryConfiguration();
    boolean createMissing = repoConfig.isCreateMissingCustomColumns() || // but we'll consider the flag also on audit configuration, just in case
    configuration.getBoolean(PROPERTY_CREATE_MISSING_CUSTOM_COLUMNS, false);
    SqlTableMetadata tableMetadata = null;
    if (createMissing) {
        try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startReadOnlyTransaction()) {
            tableMetadata = SqlTableMetadata.create(jdbcSession.connection(), QAuditEventRecord.TABLE_NAME);
        }
    }
    for (Configuration subConfigColumn : subConfigColumns) {
        String columnName = getStringFromConfig(subConfigColumn, CONF_AUDIT_SERVICE_COLUMN_NAME);
        String propertyName = getStringFromConfig(subConfigColumn, CONF_AUDIT_SERVICE_EVENT_RECORD_PROPERTY_NAME);
        // No type definition for now, it's all String or String implicit conversion.
        ColumnMetadata columnMetadata = ColumnMetadata.named(columnName).ofType(Types.VARCHAR);
        QAuditEventRecordMapping.get().addExtensionColumn(propertyName, columnMetadata);
        if (tableMetadata != null && tableMetadata.get(columnName) == null) {
            try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
                jdbcSession.addColumn(QAuditEventRecord.TABLE_NAME, columnMetadata);
                jdbcSession.commit();
            }
        }
    }
}
Also used : JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession) ColumnMetadata(com.querydsl.sql.ColumnMetadata) SqaleRepositoryConfiguration(com.evolveum.midpoint.repo.sqale.SqaleRepositoryConfiguration) JdbcRepositoryConfiguration(com.evolveum.midpoint.repo.sqlbase.JdbcRepositoryConfiguration) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) Configuration(org.apache.commons.configuration2.Configuration) BaseHierarchicalConfiguration(org.apache.commons.configuration2.BaseHierarchicalConfiguration) SqaleRepositoryConfiguration(com.evolveum.midpoint.repo.sqale.SqaleRepositoryConfiguration) BaseHierarchicalConfiguration(org.apache.commons.configuration2.BaseHierarchicalConfiguration) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) BaseHierarchicalConfiguration(org.apache.commons.configuration2.BaseHierarchicalConfiguration) SqlTableMetadata(com.evolveum.midpoint.repo.sqlbase.SqlTableMetadata)

Example 7 with JdbcSession

use of com.evolveum.midpoint.repo.sqlbase.JdbcSession 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)

Example 8 with JdbcSession

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

the class SqaleRepoSmokeTest method test900WorkingWithPgArraysJsonbAndBytea.

// region low-level tests
/**
 * This tests our type mapper/converter classes and related column mapping.
 */
@Test
public void test900WorkingWithPgArraysJsonbAndBytea() {
    QUser u = aliasFor(QUser.class);
    MUser user = new MUser();
    String userName = "user" + getTestNumber();
    setName(user, userName);
    user.policySituations = new Integer[] { 1, 2 };
    user.subtypes = new String[] { "subtype1", "subtype2" };
    // more whitespaces/lines
    user.ext = new Jsonb("{\"key\" : \"value\",\n\"number\": 47} ");
    user.photo = new byte[] { 0, 1, 0, 1 };
    try (JdbcSession jdbcSession = startTransaction()) {
        jdbcSession.newInsert(u).populate(user).execute();
        jdbcSession.commit();
    }
    MUser row = selectOne(u, u.nameNorm.eq(userName));
    assertThat(row.policySituations).contains(1, 2);
    assertThat(row.subtypes).contains("subtype1", "subtype2");
    // normalized
    assertThat(row.ext.value).isEqualTo("{\"key\": \"value\", \"number\": 47}");
    // byte[] is used for fullObject, there is no chance to miss a problem with it
    assertThat(row.photo).hasSize(4);
    // setting NULLs
    try (JdbcSession jdbcSession = startTransaction()) {
        jdbcSession.newUpdate(u).setNull(u.policySituations).set(u.subtypes, // this should do the same
        (String[]) null).setNull(u.ext).setNull(u.photo).where(u.oid.eq(row.oid)).execute();
        jdbcSession.commit();
    }
    row = selectOne(u, u.nameNorm.eq(userName));
    assertThat(row.policySituations).isNull();
    assertThat(row.subtypes).isNull();
    assertThat(row.ext).isNull();
    // but we never set fullObject to null, so this is a good test for doing so with byte[]
    assertThat(row.photo).isNull();
}
Also used : JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession) QUser(com.evolveum.midpoint.repo.sqale.qmodel.focus.QUser) Jsonb(com.evolveum.midpoint.repo.sqale.jsonb.Jsonb) MUser(com.evolveum.midpoint.repo.sqale.qmodel.focus.MUser) Test(org.testng.annotations.Test) SqaleRepoBaseTest(com.evolveum.midpoint.repo.sqale.SqaleRepoBaseTest)

Example 9 with JdbcSession

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

the class SqaleRepoBaseTest method clearAudit.

// Called on demand
public void clearAudit() {
    try (JdbcSession jdbcSession = startTransaction()) {
        jdbcSession.executeStatement("TRUNCATE ma_audit_event CASCADE;");
        jdbcSession.commit();
        display("AUDIT tables cleared");
    }
}
Also used : JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession)

Example 10 with JdbcSession

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

the class SqaleRepoBaseTest method clearDatabase.

/**
 * Database cleanup for Sqale tests only.
 * Check TestSqaleRepositoryBeanConfig.clearDatabase(SqaleRepoContext) for integration tests.
 */
private void clearDatabase() {
    try (JdbcSession jdbcSession = startTransaction()) {
        // object delete cascades to sub-rows of the "object aggregate"
        jdbcSession.executeStatement("TRUNCATE m_object CASCADE;");
        // truncate does not run ON DELETE trigger, many refs/container tables are not cleaned
        jdbcSession.executeStatement("TRUNCATE m_object_oid CASCADE;");
        // but after truncating m_object_oid it cleans all the tables
        // audit is cleaned on-demand using clearAudit()
        /*
            Truncates are much faster than this delete probably because it works row by row:
            long count = jdbcSession.newDelete(QObjectMapping.INSTANCE.defaultAlias()).execute();
            display("Deleted " + count + " objects from DB");
            */
        jdbcSession.commit();
        display("OBJECT tables cleared");
    }
    // this is "suite" scope code, but @BeforeSuite can't use injected fields
    if (!cacheTablesCleared) {
        try (JdbcSession jdbcSession = startTransaction()) {
            // URI cache must work even when default relation ID is not 0, so we can wipe it all.
            jdbcSession.executeStatement("TRUNCATE m_uri CASCADE;");
            jdbcSession.executeStatement("TRUNCATE m_ext_item CASCADE;");
            jdbcSession.commit();
        }
        // uses its own transaction
        sqlRepoContext.clearCaches();
        // It would work with URI cache cleared before every class, but that's not
        // how midPoint will work either.
        cacheTablesCleared = true;
        display("URI cache and Extension item catalog tables cleared");
    }
}
Also used : JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession)

Aggregations

JdbcSession (com.evolveum.midpoint.repo.sqlbase.JdbcSession)27 SqaleRepoBaseTest (com.evolveum.midpoint.repo.sqale.SqaleRepoBaseTest)4 MExtItem (com.evolveum.midpoint.repo.sqale.qmodel.ext.MExtItem)4 QExtItem (com.evolveum.midpoint.repo.sqale.qmodel.ext.QExtItem)4 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)4 NotNull (org.jetbrains.annotations.NotNull)4 MUri (com.evolveum.midpoint.repo.sqale.qmodel.common.MUri)3 QUser (com.evolveum.midpoint.repo.sqale.qmodel.focus.QUser)3 Test (org.testng.annotations.Test)3 PrismContainerValue (com.evolveum.midpoint.prism.PrismContainerValue)2 ItemName (com.evolveum.midpoint.prism.path.ItemName)2 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)2 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)2 RepoModifyOptions (com.evolveum.midpoint.repo.api.RepoModifyOptions)2 Jsonb (com.evolveum.midpoint.repo.sqale.jsonb.Jsonb)2 QUri (com.evolveum.midpoint.repo.sqale.qmodel.common.QUri)2 MUser (com.evolveum.midpoint.repo.sqale.qmodel.focus.MUser)2 QUserMapping (com.evolveum.midpoint.repo.sqale.qmodel.focus.QUserMapping)2 Instant (java.time.Instant)2 QName (javax.xml.namespace.QName)2