package com.quantum.view.subset;

import java.sql.SQLException;

import com.quantum.model.Bookmark;
import com.quantum.model.BookmarkCollection;
import com.quantum.model.Column;
import com.quantum.model.Entity;
import com.quantum.model.ForeignKey;
import com.quantum.model.Index;
import com.quantum.model.NotConnectedException;
import com.quantum.model.Schema;

/**
 * @author BC
 */
public class EntitySubset implements Entity {
    
    private String name;
    private String schema;
    private String bookmarkName;
    
    public EntitySubset(String name, String schema, String bookmarkName) {
        this.name = name;
        this.schema = schema;
        this.bookmarkName = bookmarkName;
    }

    public String getName() {
        return this.name;
    }

    public String getSchema() {
        return schema;
    }

    public String getType() {
        return null;
    }

    public Column[] getColumns() {
        // TODO: limit the columns
        Entity relatedEntity = getEntityFromBookmark();
        if (relatedEntity != null) {
        	try {
	            Column[] columns = relatedEntity.getColumns();
	            return columns;
        	} catch (NotConnectedException e) {
        		return new Column[0];
        	} catch (SQLException e) {
        		return new Column[0];
        	}
        } else {
            return null;
        }
    }

    public Index[] getIndexes() {
        return new Index[0];
    }

    public Column getColumn(String columnName) throws NotConnectedException, SQLException {
        Entity relatedEntity = getEntityFromBookmark();
        return relatedEntity == null 
            ? null : relatedEntity.getColumn(columnName);
    }

    public String getQualifiedName() {
        return this.schema + "." + this.name;
    }

    public Boolean exists() {
        return null;
    }

    public Bookmark getBookmark() {
        return BookmarkCollection.getInstance().find(this.bookmarkName);
    }
    
    private Entity getEntityFromBookmark() {
        try {
            return getBookmark().getEntity(
                new Schema(schema), name);
        } catch (SQLException e) {
            return null;
        }
    }
    /**
     * @see com.quantum.model.Entity#getQuotedTableName()
     */
    public String getQuotedTableName() {
        return getBookmark().getAdapter().filterTableName(getQualifiedName());
    }

	/* (non-Javadoc)
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	public int compareTo(Object arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see com.quantum.model.Entity#getExportedKeys()
	 */
	public ForeignKey[] getExportedKeys() throws NotConnectedException, SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.quantum.model.Entity#getImportedKeys()
	 */
	public ForeignKey[] getImportedKeys() throws NotConnectedException, SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.quantum.model.Entity#getReferences()
	 */
	public ForeignKey[] getReferences() throws NotConnectedException, SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.quantum.model.Entity#isSynonym()
	 */
	public boolean isSynonym() {
		return false;
	}
}