Search in sources :

Example 1 with SqlTableMetadata

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

Aggregations

SqaleRepositoryConfiguration (com.evolveum.midpoint.repo.sqale.SqaleRepositoryConfiguration)1 JdbcRepositoryConfiguration (com.evolveum.midpoint.repo.sqlbase.JdbcRepositoryConfiguration)1 JdbcSession (com.evolveum.midpoint.repo.sqlbase.JdbcSession)1 SqlTableMetadata (com.evolveum.midpoint.repo.sqlbase.SqlTableMetadata)1 ColumnMetadata (com.querydsl.sql.ColumnMetadata)1 BaseHierarchicalConfiguration (org.apache.commons.configuration2.BaseHierarchicalConfiguration)1 Configuration (org.apache.commons.configuration2.Configuration)1 HierarchicalConfiguration (org.apache.commons.configuration2.HierarchicalConfiguration)1