Commit d3484c88 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Big refactoring in order to allow multiple TAP versions (1)

parent ae6327c1
Loading
Loading
Loading
Loading
+35 −11
Original line number Diff line number Diff line
@@ -20,7 +20,10 @@
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package it.inaf.ia2.tsm.api.contract;
package it.inaf.ia2.tsm;

import it.inaf.ia2.tsm.xmlmodel.TableModel;
import java.util.Map;

/**
 * A {@link TapSchemaEntity} that can be a child of an
@@ -28,23 +31,44 @@ package it.inaf.ia2.tsm.api.contract;
 *
 * @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
 */
public interface ChildEntity<T extends EntitiesContainer> extends TapSchemaEntity {
public abstract class ChildEntity<T extends EntitiesContainer> extends TapSchemaEntity {

    private static final long serialVersionUID = -8941059435527998685L;

    private Status status;

    protected ChildEntity() {
    }

    public ChildEntity(TapSchema tapSchema, TableModel tableModel, Map<String, Object> metadata) {
        super(tapSchema, tableModel, metadata);
    }

    /**
     * Each child has a name that is univocal for a given parent, in this way
     * the parent can search a child by name.
     * The persistence status of the {@link ChildEntity}.
     */
    String getName();
    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    @Override
    public void save() {
        setStatus(Status.ADDED_PERSISTED);
        super.save();
    }

    /**
     * The {@link EntitiesContainer} that owns the {@link ChildEntity}.
     * Each child has a name that is univocal for a given parent, in this way
     * the parent can search a child by name.
     */
    T getParent();
    public abstract String getName();

    /**
     * The persistence status of the {@link ChildEntity}.
     * The {@link EntitiesContainer} that owns the {@link ChildEntity}.
     */
    Status getStatus();

    void setStatus(Status status);
    public abstract T getParent();
}
+120 −0
Original line number Diff line number Diff line
@@ -20,254 +20,71 @@
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package it.inaf.ia2.tsm.api;
package it.inaf.ia2.tsm;

import it.inaf.ia2.tsm.api.contract.Column;
import it.inaf.ia2.tsm.api.contract.Key;
import it.inaf.ia2.tsm.api.contract.KeyColumn;
import it.inaf.ia2.tsm.api.contract.Status;
import it.inaf.ia2.tsm.api.contract.Table;
import it.inaf.ia2.tsm.api.contract.TapSchema;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The main implementation of {@link Column}.
 * 
 * @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
 */
public class ColumnImpl extends ChildEntityImpl<Table> implements Column {
public class Column extends ChildEntity<Table> {

    private static final long serialVersionUID = 9175956487892235521L;
    private static final Logger log = LoggerFactory.getLogger(ColumnImpl.class);
    public final static String TABLE_NAME_KEY = "table_name";
    public final static String COLUMN_NAME_KEY = "column_name";
    public final static String DATATYPE_KEY = "datatype";
    public final static String SIZE_KEY = "size";
    public final static String ARRAYSIZE_KEY = "arraysize";
    public final static String INDEXED_KEY = "indexed";
    public final static String PRIMARY_KEY = "primary_key";

    private boolean isPrimaryKey;
    private static final long serialVersionUID = 9175956487892235521L;
    private static final Logger LOG = LoggerFactory.getLogger(Column.class);

    private boolean foreignKeySearched;
    private Key foreignKey;
    private KeyMetadata foreignKey;

    private Table parentTable;

    private ColumnImpl() {
    private Column() {
        // for serialization
        super();
    }

    protected ColumnImpl(DBWrapper dbWrapper, TapSchema tapSchema, Table table, String columnName, boolean indexed, boolean isPrimaryKey, String datatype, Integer size, Integer arraySize) {
        super(dbWrapper, tapSchema);

        this.isPrimaryKey = isPrimaryKey;

    protected Column(TapSchema tapSchema, Table table, String columnName) {
        super(tapSchema, tapSchema.getTableModel(TapSchema.COLUMNS_TABLE), table.getColumnMetadata(columnName));
        parentTable = table;

        addProperty(TABLE_NAME_KEY, new FixedEntityProperty<>(table.getCompleteName()));
        addProperty(COLUMN_NAME_KEY, new FixedEntityProperty<>(columnName));
        addProperty(DATATYPE_KEY, new FixedEntityProperty<>(datatype));
        addProperty(SIZE_KEY, new FixedEntityProperty<>(size));
        addProperty(ARRAYSIZE_KEY, new FixedEntityProperty<>(arraySize));
        addProperty(INDEXED_KEY, new FixedEntityProperty<>(indexed));

        // Updatables
        addProperty(DESCRIPTION_KEY, new EditableProperty<String>());
        addProperty(UTYPE_KEY, new EditableProperty<String>());
        addProperty(UNIT_KEY, new EditableProperty<String>());
        addProperty(UCD_KEY, new EditableProperty<String>());
        addProperty(PRINCIPAL_KEY, new EditableProperty<>(isPrimaryKey, false));
        addProperty(STD_KEY, new EditableProperty<>(false, false));
        addProperty(COLUMN_INDEX_KEY, new EditableProperty<Integer>());
        addProperty(ID_KEY, new EditableProperty<Integer>());
        addProperty(COLUMN_ID_KEY, new EditableProperty<Long>());

        setStatus(Status.LOADED);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Key getForeignKey() {
    public KeyMetadata getForeignKey() {
        if (!foreignKeySearched) { // lazy loading (but the foreignKey value can be null, so we use this boolean)

            String tableCompleteName = getParent().getCompleteName();
            String columnName = getValue(COLUMN_NAME_KEY, String.class);

            keysloop: // we want to loop on all schema keys, also hidden ones.
            for (Key key : ((TapSchemaImpl) tapSchema).getAllKeys()) {
                if (key.getFromTableCompleteName().equals(tableCompleteName)) {
                    for (KeyColumn keyColumn : key.getKeyColumns()) {
                        if (keyColumn.getFromColumn().equals(columnName)) {
                            foreignKey = key;
                            break keysloop;
                        }
                    }
                }
            }

            foreignKeySearched = true;
            foreignKey = tapSchema.searchKeyMetadata(parentTable.getParent().getName(), parentTable.getName(), getName());
        }

        return foreignKey;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getTableCompleteName() {
        return getValue(TABLE_NAME_KEY, String.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getName() {
        return getValue(COLUMN_NAME_KEY, String.class);
    }

    @Override
    public boolean getIndexed() {
        return getValue(INDEXED_KEY, Boolean.class);
    }

    /**
     * {@inheritDoc}
     *
     * This information is not stored into the TAP_SCHEMA, so it will be
     * reloaded from the source schema each time.
     */
    @Override
    public boolean isPrimaryKey() {
        return isPrimaryKey;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getDatatype() {
        return getValue(DATATYPE_KEY, String.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer getArraySize() {
        return getValue(ARRAYSIZE_KEY, Integer.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer getSize() {
        return getValue(SIZE_KEY, Integer.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getDescription() {
        return getValue(DESCRIPTION_KEY, String.class);
    }

    @Override
    public void setDescription(String description) {
        setValue(DESCRIPTION_KEY, description);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getUtype() {
        return getValue(UTYPE_KEY, String.class);
    }

    @Override
    public void setUtype(String utype) {
        setValue(UTYPE_KEY, utype);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getUnit() {
        return getValue(UNIT_KEY, String.class);
    }

    @Override
    public void setUnit(String unit) {
        setValue(UNIT_KEY, unit);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getUCD() {
        return getValue(UCD_KEY, String.class);
    }

    @Override
    public void setUCD(String ucd) {
        setValue(UCD_KEY, ucd);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean getPrincipal() {
        return getValue(PRINCIPAL_KEY, Boolean.class);
    }

    @Override
    public void setPrincipal(boolean principal) {
        setValue(PRINCIPAL_KEY, principal);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean getStd() {
        return getValue(STD_KEY, Boolean.class);
    }

    @Override
    public void setStd(boolean std) {
        setValue(STD_KEY, std);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer getColumnIndex() {
        return getValue(COLUMN_INDEX_KEY, Integer.class);
    }

    @Override
    public void setColumnIndex(Integer columnIndex) {
        setValue(COLUMN_INDEX_KEY, columnIndex);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Long getColumnID() {
        return getValue(COLUMN_ID_KEY, Long.class);
    }

    @Override
    public void setColumnID(Long columnID) {
        setValue(COLUMN_ID_KEY, columnID);
        return (boolean) getMetadata(PRIMARY_KEY);
    }

    @Override
@@ -286,14 +103,11 @@ public class ColumnImpl extends ChildEntityImpl<Table> implements Column {
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ColumnImpl other = (ColumnImpl) obj;
        final Column other = (Column) obj;
        if (!Objects.equals(this.getTableCompleteName(), other.getTableCompleteName())) {
            return false;
        }
        if (!Objects.equals(this.getName(), other.getName())) {
            return false;
        }
        return true;
        return Objects.equals(this.getName(), other.getName());
    }

    /**
+3 −3
Original line number Diff line number Diff line
@@ -20,10 +20,10 @@
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package it.inaf.ia2.tsm.api;
package it.inaf.ia2.tsm;

import it.inaf.ia2.tsm.api.contract.DatabaseType;
import it.inaf.ia2.tsm.api.contract.TapSchema;
import it.inaf.ia2.tsm.datalayer.DatabaseType;
import it.inaf.ia2.tsm.datalayer.DBWrapper;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
+707 −0

File added.

Preview size limit exceeded, changes collapsed.

+298 −0
Original line number Diff line number Diff line
/* 
 * _____________________________________________________________________________
 * 
 * INAF - OATS National Institute for Astrophysics - Astronomical Observatory of
 * Trieste INAF - IA2 Italian Center for Astronomical Archives
 * _____________________________________________________________________________
 * 
 * Copyright (C) 2016 Istituto Nazionale di Astrofisica
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License Version 3 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 General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package it.inaf.ia2.tsm;

import it.inaf.ia2.tsm.datalayer.DatabaseType;
import it.inaf.ia2.tsm.datalayer.DBWrapper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Utility class that contains static methods for managing {@link Column}s into
 * the database.
 *
 * @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
 */
public class DaoColumn {
//
//    private static final Logger log = LoggerFactory.getLogger(DaoColumn.class);
//
//    private static boolean equalsOneOf(String string, String... values) {
//        for (String value : values) {
//            if (string.equals(value)) {
//                return true;
//            }
//        }
//        return false;
//    }
//
//    /**
//     * Returns the list of all the columns names given a schema name and a table
//     * name, also if these objects have never been added into the TAP_SCHEMA.
//     * This can be useful to retrieve the source database structure.
//     */
//    public static List<String> getAllColumnsNames(DBWrapper dbWrapper, TapSchema tapSchema, String schemaName, String tableSimpleName) throws SQLException {
//        final List<String> allColumns = new ArrayList<>();
//
//        DataSource dataSource = TSMUtil.getSchemaDataSource(dbWrapper, tapSchema, schemaName);
//        DatabaseType dbType = TSMUtil.getSchemaDatabaseType(dbWrapper, tapSchema, schemaName);
//
//        String query;
//        if (dbType == DatabaseType.MYSQL) {
//            query = String.format("SHOW COLUMNS FROM `%s`.`%s`", schemaName, tableSimpleName);
//        } else if (dbType == DatabaseType.POSTGRES) {
//            query = "SELECT column_name FROM information_schema.columns WHERE table_schema = '" + schemaName + "' AND table_name = '" + tableSimpleName + "'";
//        } else {
//            throw new UnsupportedOperationException("Database type " + dbType + " not supported");
//        }
//
//        log.debug("Executing query {}", query);
//
//        try (Connection connection = dataSource.getConnection();
//                Statement statement = connection.createStatement();
//                ResultSet resultSet = statement.executeQuery(query)) {
//
//            while (resultSet.next()) {
//
//                String columnName;
//                if (dbType == DatabaseType.MYSQL) {
//                    columnName = resultSet.getString("Field");
//                } else if (dbType == DatabaseType.POSTGRES) {
//                    columnName = resultSet.getString("column_name");
//                } else {
//                    throw new UnsupportedOperationException("Database type " + dbType + " not supported");
//                }
//
//                allColumns.add(columnName);
//            }
//        }
//
//        return allColumns;
//    }
//
//    /**
//     * For performance reasons all columns of a {@link Table} are loaded
//     * together using this method. Columns {@link Status} is at first set as
//     * {@code LOADED}.
//     */
//    protected static List<Column> loadAllTableColumns(DBWrapper dbWrapper, TapSchema tapSchema, String schemaName, Table table) throws SQLException {
//        String tableSimpleName = table.getName();
//        final List<Column> allColumns = new ArrayList<>();
//
//        DataSource dataSource = TSMUtil.getSchemaDataSource(dbWrapper, tapSchema, schemaName);
//        DatabaseType dbType = TSMUtil.getSchemaDatabaseType(dbWrapper, tapSchema, schemaName);
//
//        String query;
//        if (dbType == DatabaseType.MYSQL) {
//            query = String.format("SHOW COLUMNS FROM `%s`.`%s`", schemaName, tableSimpleName);
//        } else if (dbType == DatabaseType.POSTGRES) {
//            query = "SELECT c.column_name, c.data_type, r.contype AS column_type, c.character_maximum_length, c.numeric_precision\n" //, c.numeric_precision_radix
//                    + "FROM information_schema.columns c\n"
//                    + "JOIN pg_catalog.pg_tables t ON c.table_schema = t.schemaname AND c.table_name = t.tablename\n"
//                    + "LEFT JOIN pg_catalog.pg_constraint r ON c.ordinal_position = ANY(r.conkey) AND r.conrelid = (t.schemaname || '.' || t.tablename)::regclass::oid\n"
//                    + "WHERE t.schemaname = '" + schemaName + "' AND t.tablename = '" + tableSimpleName + "'";
//        } else {
//            throw new UnsupportedOperationException("Database type " + dbType + " not supported");
//        }
//
//        log.debug("Executing query {}", query);
//
//        try (Connection connection = dataSource.getConnection();
//                Statement statement = connection.createStatement();
//                ResultSet resultSet = statement.executeQuery(query)) {
//
//            while (resultSet.next()) {
//
//                String columnName;
//                if (dbType == DatabaseType.MYSQL) {
//                    columnName = resultSet.getString("Field");
//                } else if (dbType == DatabaseType.POSTGRES) {
//                    columnName = resultSet.getString("column_name");
//                } else {
//                    throw new UnsupportedOperationException("Database type " + dbType + " not supported");
//                }
//
//                boolean indexed = false, primaryKey = false;
//
//                // Key type
//                if (dbType == DatabaseType.MYSQL) {
//                    String key = resultSet.getString("Key");
//                    primaryKey = key.equals("PRI");
//                    indexed = equalsOneOf(key, "PRI", "UNI", "MUL");
//                } else if (dbType == DatabaseType.POSTGRES) {
//                    String columnType = resultSet.getString("column_type");
//                    if (columnType != null) {
//                        primaryKey = "p".equals(columnType);
//                        indexed = equalsOneOf(columnType, "p", "f", "u");
//                    }
//                } else {
//                    throw new UnsupportedOperationException("Database type " + dbType + " not supported");
//                }
//
//                // Datatype and Size
//                int size = 0;
//                String datatype;
//
//                if (dbType == DatabaseType.MYSQL) {
//                    String type = resultSet.getString("Type").toLowerCase();
//
//                    if (type.startsWith("int")) {
//                        datatype = "adql:INTEGER";
//                    } else if (type.startsWith("smallint")) {
//                        datatype = "adql:SMALLINT";
//                    } else if (type.startsWith("bigint")) {
//                        datatype = "adql:BIGINT";
//                    } else if (type.startsWith("float")) {
//                        datatype = "adql:REAL";
//                    } else if (type.startsWith("char")) {
//                        int beginIndex = type.indexOf('(');
//                        int endIndex = type.indexOf(')');
//                        size = Integer.parseInt(type.substring(beginIndex + 1, endIndex));
//                        datatype = "adql:CHAR";
//                    } else if (type.startsWith("varchar")) {
//                        int beginIndex = type.indexOf('(');
//                        int endIndex = type.indexOf(')');
//                        size = Integer.parseInt(type.substring(beginIndex + 1, endIndex));
//                        datatype = "adql:VARCHAR";
//                    } else if (type.contains("timestamp")) {
//                        datatype = "adql:TIMESTAMP";
//                    } else {
//                        datatype = "adql:" + type.toUpperCase();
//                    }
//                } else if (dbType == DatabaseType.POSTGRES) {
//                    String type = resultSet.getString("data_type");
//
//                    if (type.startsWith("int")) {
//                        datatype = "adql:INTEGER";
//                    } else if (type.startsWith("smallint")) {
//                        datatype = "adql:SMALLINT";
//                    } else if (type.startsWith("bigint")) {
//                        datatype = "adql:BIGINT";
//                    } else if (type.startsWith("double") || type.startsWith("real")) {
//                        datatype = "adql:REAL";
//                    } else if (type.startsWith("character varying")) {
//                        datatype = "adql:VARCHAR";
//                        size = resultSet.getInt("character_maximum_length");
//                    } else if (type.startsWith("char")) {
//                        datatype = "adql:CHAR";
//                        size = resultSet.getInt("character_maximum_length");
//                    } else if (type.contains("timestamp")) {
//                        datatype = "adql:TIMESTAMP";
//                    } else {
//                        datatype = "adql:" + type.toUpperCase();
//                    }
//                } else {
//                    throw new UnsupportedOperationException("Database type " + dbType + " not supported");
//                }
//
//                Integer arraySize = null; // TODO (v 1.1)
//
//                Column column = new Column(dbWrapper, tapSchema, table, columnName, indexed, primaryKey, datatype, size, arraySize);
//
//                allColumns.add(column);
//            }
//        }
//
//        return allColumns;
//    }
//
//    /**
//     * Retrieves saved {@code Column}s from the database and add them into the
//     * specified {@code TapSchema}.
//     */
//    protected static void fillSavedColumns(DBWrapper dbWrapper, final TapSchema tapSchema) throws SQLException {
//
//        log.debug("fillSavedColumns");
//
//        SelectQueryBuilder selectQueryBuilder = new SelectQueryBuilder(dbWrapper.getTapSchemaDatabaseType(), tapSchema, TapSchema.COLUMNS_TABLE) {
//
//            @Override
//            protected TapSchemaEntity getEntity(ResultSet rs) throws SQLException {
//                String tableCompleteName = rs.getString("table_name");
//                String columnName = rs.getString("column_name");
//
//                String[] tableNameSplit = tableCompleteName.split(Pattern.quote("."));
//                String schemaName = tableNameSplit[0];
//                String tableSimpleName = tableNameSplit[1];
//
//                Schema schema = tapSchema.getChild(schemaName);
//                if (schema == null) {
//                    return null;
//                }
//                Table table = schema.getChild(tableSimpleName);
//                if (table == null) {
//                    return null;
//                }
//                Column column = table.addChild(columnName);
//                if (column == null) {
//                    return null;
//                }
//                column.setStatus(Status.ADDED_PERSISTED);
//                return column;
//            }
//        };
//
//        selectQueryBuilder.executeQuery(dbWrapper.getTapSchemaConnection());
//    }
//
//    /**
//     * Save a new {@code Column} into the TAP_SCHEMA schema.
//     */
//    protected static void insertNewColumn(DatabaseType dbType, Connection connection, TapSchema tapSchema, Column column) throws SQLException {
//
//        log.debug("insertNewColumn");
//
//        InsertQueryBuilder insertQueryBuilder = new InsertQueryBuilder(dbType, tapSchema, column, TapSchema.COLUMNS_TABLE);
//        insertQueryBuilder.executeQuery(connection);
//    }
//
//    /**
//     * Updates an existing {@code Column}.
//     */
//    protected static void updateColumn(DatabaseType dbType, Connection connection, TapSchema tapSchema, Column column) throws SQLException {
//
//        UpdateQueryBuilder updateQueryBuilder = new UpdateQueryBuilder(dbType, tapSchema, column, TapSchema.COLUMNS_TABLE, "column_name = ? AND table_name = ?");
//
//        String query = updateQueryBuilder.getQuery();
//
//        try (PreparedStatement statement = connection.prepareStatement(query)) {
//
//            log.debug("Executing query {}", query);
//
//            int i = updateQueryBuilder.addStatementValues(statement);
//            statement.setString(i, column.getName());
//            statement.setString(i + 1, column.getTableCompleteName());
//
//            statement.executeUpdate();
//        }
//    }
}
Loading