All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tkit.jee.base.exception.LocalizableException Maven / Gradle / Ivy

There is a newer version: 0.8.0
Show newest version
package org.tkit.jee.base.exception;

import java.util.*;

public abstract class LocalizableException extends Exception {
    private static final long serialVersionUID = 3961367560967824733L;
    public static final String PARAM_REFERENCE = "reference";
    public static final String PARAM_PHASE = "phase";
    public static final String PARAM_SEVERITY = "severity";
    private boolean logStackTrace = true;
    private Enum messageKey;
    private List parameters = new ArrayList();
    private Map namedParameters = new HashMap();

    public LocalizableException(Enum errorCode) {
        super(getMessage(errorCode));
        this.messageKey = errorCode;
    }

    public LocalizableException(Enum errorCode, Throwable cause) {
        super(getMessage(errorCode), cause);
        this.messageKey = errorCode;
    }

    public LocalizableException(Enum errorCode, String message, Throwable cause) {
        super(message, cause);
        this.messageKey = errorCode;
    }

    public LocalizableException(Enum errorCode, Throwable cause, Object... params) {
        super(getMessage(errorCode), cause);
        this.messageKey = errorCode;
        if (params != null) {
            Collections.addAll(this.parameters, params);
        }

    }

    public LocalizableException addParam(String name, Object value) {
        this.namedParameters.put(name, value);
        return this;
    }

    public LocalizableException addParam(Object value) {
        this.parameters.add(value);
        return this;
    }

    public LocalizableException withReference(Object value) {
        this.namedParameters.put("reference", value);
        return this;
    }

    public LocalizableException withPhase(Object value) {
        this.namedParameters.put("phase", value);
        return this;
    }

    public LocalizableException withSeverity(Object value) {
        this.namedParameters.put("severity", value);
        return this;
    }

    public LocalizableException withParams(Object... params) {
        Collections.addAll(this.parameters, params);
        return this;
    }

    public LocalizableException withNamedParams(Map namedParams) {
        this.namedParameters.putAll(namedParams);
        return this;
    }

    public Enum getMessageKey() {
        return this.messageKey;
    }

    public boolean isLogStackTrace() {
        return this.logStackTrace;
    }

    public void setLogStackTrace(boolean logStackTrace) {
        this.logStackTrace = logStackTrace;
    }

    public List getParameters() {
        return this.parameters;
    }

    public void setParameters(List parameters) {
        this.parameters = parameters;
    }

    public final Map getNamedParameters() {
        return this.namedParameters;
    }

    public void setNamedParameters(Map namedParameters) {
        this.namedParameters = namedParameters;
    }

    private static String getMessage(Enum errorCode) {
        return errorCode != null ? errorCode.name() : "ERROR_MESSAGE";
    }
}