package it.mice.voila.runtime.exception;

import it.mice.voila.runtime.util.UserMessage;
import it.mice.voila.runtime.util.UserMessages;

/**
 * Created by IntelliJ IDEA.
 * User: zzy9v4
 * Date: 10-ott-2006
 * Time: 9.35.25
 * To change this template use File | Settings | File Templates.
 */
public class BusinessException extends RuntimeException {
    private UserMessages userMessages;

    public BusinessException(UserMessages userMessages) {
        this.userMessages = userMessages;
    }

    public BusinessException(String message, UserMessages userMessages) {
        super(message);
        this.userMessages = userMessages;
    }

    public BusinessException(String message, Throwable cause, UserMessages userMessages) {
        super(message, cause);
        this.userMessages = userMessages;
    }

    public BusinessException(Throwable cause, UserMessage userMessage) {
        super(cause);
        this.userMessages = new UserMessages();
        this.userMessages.add(userMessage);
    }

    public BusinessException(UserMessage userMessage) {
        this.userMessages = new UserMessages();
        this.userMessages.add(userMessage);
    }

    public BusinessException(String message, UserMessage userMessage) {
        super(message);
        this.userMessages = new UserMessages();
        this.userMessages.add(userMessage);
    }

    public BusinessException(String message, Throwable cause, UserMessage userMessage) {
        super(message, cause);
        this.userMessages = new UserMessages();
        this.userMessages.add(userMessage);
    }

    public BusinessException(Throwable cause, UserMessages userMessages) {
        super(cause);
        this.userMessages = userMessages;
    }

    public BusinessException(String message) {
        this.userMessages = new UserMessages();
        this.userMessages.add(new UserMessage(message));
	}

	public UserMessages getUserMessages() {
        return userMessages;
    }

    public void setUserMessages(UserMessages userMessages) {
        this.userMessages = userMessages;
    }

    /* (non-Javadoc)
	 * @see java.lang.Throwable#getMessage()
	 */
	@Override
	public String getMessage() {
		if (super.getMessage() == null) {
			return toString();
		}
		return super.getMessage();
	}

	public String toString() {
        final StringBuffer sb = new StringBuffer();
        sb.append("BusinessException");
        sb.append("{message=").append(super.getMessage());
        if (userMessages != null) {
            sb.append(", ").append(userMessages);
        }
        sb.append('}');
        return sb.toString();
    }
}
