/*
 * Copyright 2009 Lari Natri <lari.natri@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  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, see <http://www.gnu.org/licenses/>.
 */

package compid.data;

import java.sql.SQLException;

/**
 *
 * @author Lari Natri
 */
public class DAOException extends RuntimeException {

    public static enum ExceptionType
    {
        CANTLOADDRIVER {
            String asString()
            { return "Can't load database driver."; } },        
        COULDNOTINSERTTUPLE {
            String asString()
            { return "Couldn't insert tuple."; } },
        INVALIDPARAMETERS {
            String asString()
            { return "Invalid parameters in function call."; } },
        INVALIDCONNECTION {
            String asString()
            { return "Database connection is invalid."; } },
        DUPLICATEROW {
            String asString()
            { return "Duplicate row(s) found in database."; } },
        TUPLENOTFOUND {
            String asString()
            { return "Tuple not found in database."; } },       
        NOTCONNECTED {
            String asString()
            { return "Not connected to a database."; } },
        ANOTHERINSTANCEHASBOOTED {
            String asString()
            { return "Another instance of DBMS has booted the database."; } },
        DEADLOCK {
            String asString()
            { return "Database deadlock occurred."; } },
        DERIVED {
            String asString()
            { return ""; } },                   
        UNKNOWN {
            String asString() { return ""; } };
        
        abstract String asString();
    }    
    
    private static String UNKNOWNDESCR = "Unknown error";
    
    private ExceptionType type;
    private String additionalDescription = "";
    
    public DAOException(Throwable ex) {
        super(ex);
        this.type=ExceptionType.DERIVED;
        if (ex instanceof SQLException) {
            String state = ((SQLException)ex).getSQLState();
            if (state.equals("40001")) {
                this.type = ExceptionType.DEADLOCK;
            }
        }

    }
    
    public DAOException(ExceptionType et) {
        super();
        this.setAttributes(et,"");
    }
    
    public DAOException(String description) {
        super(description);
        this.setAttributes(ExceptionType.UNKNOWN,description);
    }
    
    public DAOException() {
        super();
        this.setAttributes(ExceptionType.UNKNOWN, UNKNOWNDESCR);
    }
    
    public DAOException(ExceptionType et, String description) {
        super(description);
        this.setAttributes(et, description);
    }

    public ExceptionType getExceptionType() {
        return this.type;
    }
    
    private void setAttributes(ExceptionType eType, String descr) {
        if (eType==null) this.type=ExceptionType.UNKNOWN;
        else this.type = eType;
        if (descr==null) this.additionalDescription = "";
        else this.additionalDescription = descr;
    }
    
    @Override
    public String toString() {
        if (this.type.equals(ExceptionType.DERIVED)) {
            return super.toString() + " " + this.additionalDescription;
        }
        return this.type.asString() + " " + this.additionalDescription;
    }
    
    @Override
    public boolean equals(Object o) {
        if (o==null||o.getClass()!=this.getClass()) return false;
        if (this==o) return true;
        if (this.type.equals(((DAOException)o).type)) return true;
        return false;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.type != null ? this.type.hashCode() : 0);
        return hash;
    }
} 
