Skip to content
Column.java 3.81 KiB
Newer Older
/* 
 * _____________________________________________________________________________
 * 
 * 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 java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
 */
public class Column extends ChildEntity<Table> {

    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 static final long serialVersionUID = 9175956487892235521L;
    private static final Logger LOG = LoggerFactory.getLogger(Column.class);

    private boolean foreignKeySearched;
    private KeyMetadata foreignKey;

    private Table parentTable;

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

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

    public KeyMetadata getForeignKey() {
        if (!foreignKeySearched) { // lazy loading (but the foreignKey value can be null, so we use this boolean)
            foreignKey = tapSchema.searchKeyMetadata(parentTable.getParent().getName(), parentTable.getName(), getName());
        }

        return foreignKey;
    }

    public String getTableCompleteName() {
        return getValue(TABLE_NAME_KEY, String.class);
    }

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

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

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

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 29 * hash + Objects.hashCode(getTableCompleteName());
        hash = 29 * hash + Objects.hashCode(getName());
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Column other = (Column) obj;
        if (!Objects.equals(this.getTableCompleteName(), other.getTableCompleteName())) {
            return false;
        }
        return Objects.equals(this.getName(), other.getName());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Table getParent() {
        return parentTable;
    }
}