/*
 * 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.importers;

/**
 *
 * @author Lari Natri
 */
public class ImportException extends java.lang.RuntimeException {
    
    public static enum ExceptionType
    {
        FILENOTFOUND {
            String asString()
            { return "Input file not found."; } },
        FILENOTSET {
            String asString()
            { return "Input file not set."; } },
        FILENOTREADABLE {
            String asString()
            { return "Input file not readable."; } },
        FASTA_INCORRECTFORMAT {
            String asString()
            { return "Input file is not in correct FASTA format"; } },
        MASC_FILENOTVALID {
            String asString()
            { return "Not a valid Mascot dat file."; } },            
        MASC_NOMSMSDATA {
            String asString()
            { return "Mascot dat file doesn't include MS/MS data."; } },              
        PGON_SHEETSMISSING {
            String asString() {
            return "One or more required Excel sheets missing in "
                  +"input file."; } },
        DERIVED {
            String asString()
            { return ""; } },                   
        UNKNOWN {
            String asString() { return ""; } };
        
        abstract String asString();
    }

    
    private static String UNKNOWNDESCR = "Unknown error";
    
    private ExceptionType type;
    private String additionalDescription = "";
    
    public ImportException(Throwable ex) {
        super(ex);
        this.type=ExceptionType.DERIVED;
    }
    
    public ImportException(ExceptionType et) {
        super();
        this.setAttributes(et,"");
    }
    
    public ImportException(String description) {
        super(description);
        this.setAttributes(ExceptionType.UNKNOWN,description);
    }
    
    public ImportException() {
        super();
        this.setAttributes(ExceptionType.UNKNOWN, UNKNOWNDESCR);
    }
    
    public ImportException(ExceptionType et, String description) {
        super(description);
        this.setAttributes(et, description);
    }
    
    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() {
        return this.type.asString() + " \n\n" + this.additionalDescription;
    }
    
    @Override
    public boolean equals(Object o) {
        if (this==o) return true;
        if (this.type.equals(((ImportException)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;
    }
}
