All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datacleaner.connection.SchemaNavigator Maven / Gradle / Ivy

/**
 * DataCleaner (community edition)
 * Copyright (C) 2014 Neopost - Customer Information Management
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.datacleaner.connection;

import org.apache.metamodel.DataContext;
import org.apache.metamodel.schema.Column;
import org.apache.metamodel.schema.Schema;
import org.apache.metamodel.schema.Table;
import org.apache.metamodel.schema.TableType;

/**
 * A convenient component used for exploring/navigating the schema of a
 * {@link Datastore}. It is preferred to use this component instead of the
 * {@link DataContext} directly, since it is shared amongst connections while a
 * DataContext may be created per connection (depending on the datastore type).
 */
public final class SchemaNavigator {

    private final DataContext dataContext;

    public SchemaNavigator(final DataContext dataContext) {
        this.dataContext = dataContext;
    }

    public void refreshSchemas() {
        dataContext.refreshSchemas();
    }

    public Schema convertToSchema(final String schemaName) {
        return dataContext.getSchemaByName(schemaName);
    }

    public Schema[] getSchemas() {
        return dataContext.getSchemas().toArray(new Schema[0]);
    }

    public Schema getDefaultSchema() {
        return dataContext.getDefaultSchema();
    }

    public Schema getSchemaByName(final String name) {
        return dataContext.getSchemaByName(name);
    }

    public Table convertToTable(final String schemaName, final String tableName) {
        final Schema schema;
        if (schemaName == null) {
            schema = getDefaultSchema();
        } else {
            schema = getSchemaByName(schemaName);
        }

        if (schema == null) {
            throw new IllegalArgumentException(
                    "Schema " + schemaName + " not found. Available schema names are: " + dataContext.getSchemaNames());
        }

        final Table table;
        if (tableName == null) {
            if (schema.getTables().stream().filter(t -> t.getType() == TableType.TABLE).count() == 1) {
                table = schema.getTables().stream().filter(t -> t.getType() == TableType.TABLE).findFirst().get();
            } else {
                throw new IllegalArgumentException(
                        "No table name specified, and multiple options exist. Available table names are: "
                                + schema.getTableNames());
            }
        } else {
            table = schema.getTableByName(tableName);
        }

        if (table == null) {
            throw new IllegalArgumentException(
                    "Table not found. Available table names are: " + schema.getTableNames());
        }

        return table;
    }

    public Column[] convertToColumns(final String schemaName, final String tableName, final String[] columnNames) {
        if (columnNames == null) {
            return null;
        }

        if (columnNames.length == 0) {
            return new Column[0];
        }

        final Table table = convertToTable(schemaName, tableName);

        final Column[] columns = new Column[columnNames.length];
        for (int i = 0; i < columns.length; i++) {
            columns[i] = table.getColumnByName(columnNames[i]);
        }

        return columns;
    }

    public Schema[] convertToSchemas(final String[] schemaNames) {
        final Schema[] result = new Schema[schemaNames.length];
        for (int i = 0; i < result.length; i++) {
            result[i] = convertToSchema(schemaNames[i]);
        }
        return result;
    }

    public Table[] convertToTables(final String[] tableNames) {
        final Table[] result = new Table[tableNames.length];
        for (int i = 0; i < result.length; i++) {
            result[i] = convertToTable(tableNames[i]);
        }
        return result;
    }

    public Table convertToTable(final String tableName) {
        return dataContext.getTableByQualifiedLabel(tableName);
    }

    public Column[] convertToColumns(final String[] columnNames) {
        final Column[] result = new Column[columnNames.length];
        for (int i = 0; i < result.length; i++) {
            result[i] = convertToColumn(columnNames[i]);
        }
        return result;
    }

    public Column convertToColumn(final String columnName) {
        return dataContext.getColumnByQualifiedLabel(columnName);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy