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

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

    public static enum ExceptionType
    {
        REPORTNOTFINAL {
            @Override
            String asString()
            { return "Operation not allowed: report is not yet finalized."; } },
        REPORTISFINAL {
            @Override
            String asString()
            { return "Operation not allowed: can't change a finalized report."; } },
        DERIVED {
            @Override
            String asString()
            { return ""; } },
        UNKNOWN {
            @Override
            String asString() { return ""; } };

        abstract String asString();
    }    

    private static String UNKNOWNDESCR = "Unknown error";

    private ExceptionType type;
    private String additionalDescription = "";

    public ReportRuntimeException(Throwable ex) {
        super(ex);
        this.type=ExceptionType.DERIVED;
    }

    public ReportRuntimeException(ExceptionType et) {
        super();
        this.setAttributes(et,"");
    }

    public ReportRuntimeException(String description) {
        super(description);
        this.setAttributes(ExceptionType.UNKNOWN,description);
    }

    public ReportRuntimeException() {
        super();
        this.setAttributes(ExceptionType.UNKNOWN, UNKNOWNDESCR);
    }

    public ReportRuntimeException(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() {
        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(((ReportRuntimeException)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;
    }
}

