//----------------------------BEGIN LICENSE----------------------------
/* 
 * Willow : the Open Source WorkFlow Project
 * Distributable under GNU LGPL license by gun.org
 *
 * Copyright (C) 2004-2010 huihoo.org 
 * Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com> 
 * 
 * ====================================================================
 * Project Homepage : http://www.huihoo.org/willow
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : willow@lists.sourceforge.net 
 */
//----------------------------END  LICENSE-----------------------------
package org.huihoo.workflow;

public class WorkflowException extends Exception implements WorkflowComponent
{
	/**
	 * Contains the original exception that caused this WorkflowException to
	 * be thrown. This field is set if there is additional
	 * information that could be obtained from the original
	 * exception, or if there original exception could not be
	 * mapped to a subclass of WorkflowException.
	 * Can be null. This field is initialized by the constructors.
	 * You should access and manipulate this field
	 * through its get and set methods.
	 * @serial
	 * @see #getRootCause
	 * @see #setRootCause
	 */
	protected Throwable rootException = null;
	
	/**
	 * Constructs a new org.huihoo.workflow exception.
	 */
	public WorkflowException()
	{
		super();
	}
	/**
	 * Constructs a new org.huihoo.workflow exception with the
	 * specified message. The message can be written 
	 * to the server log and/or displayed for the user. 
	 *
	 * @param message    a <code>String</code> 
	 *       specifying the text of 
	 *       the exception message
	 *
	 */
	public WorkflowException(String message)
	{
		super(message);
	}
	/**
	 * Constructs a new org.huihoo.workflow exception when the org.huihoo.workflow 
	 * needs to throw an exception and include a message 
	 * about the "root cause" exception that interfered with its 
	 * normal operation, including a description message.
	 *
	 *
	 * @param message    a <code>String</code> containing 
	 *       the text of the exception message
	 *
	 * @param rootCause    the <code>Throwable</code> exception 
	 *       that interfered with the org.huihoo.workflow's
	 *       normal operation, making this org.huihoo.workflow
	 *       exception necessary
	 *
	 */
	public WorkflowException(String message, Throwable rootCause)
	{
		super(message, rootCause);
	}
	/**
	 * Constructs a new org.huihoo.workflow exception when the org.huihoo.workflow 
	 * needs to throw an exception and include a message
	 * about the "root cause" exception that interfered with its
	 * normal operation.  The exception's message is based on the localized
	 * message of the underlying exception.
	 *
	 *
	 * @param rootCause  the <code>Throwable</code> exception
	 *         that interfered with the org.huihoo.workflow's
	 *       normal operation, making the org.huihoo.workflow exception
	 *       necessary
	 *
	 */
	public WorkflowException(Throwable rootCause)
	{
		super(rootCause);
	}
	
	/**
		* Retrieves the root cause of this WorkflowException, if any.
		* The root cause of a naming exception is used when the service provider
		* wants to indicate to the caller a non-naming related exception
		* but at the same time wants to use the WorkflowException structure
		* to indicate how far the naming operation proceeded.
		*
		* @return The possibly null exception that caused this naming 
		*    exception. If null, it means no root cause has been
		*	   set for this naming exception.
		* @see #setRootCause
		* @see #rootException
		* @see #getCause
		*/
	public Throwable getRootCause()
	{
		return rootException;
	}

	/**
		* Records the root cause of this WorkflowException.
		* If <tt>e</tt> is <tt>this</tt>, this method does not do anything.
		* @param e The possibly null exception that caused the naming 
		* 	 operation to fail. If null, it means this naming
		* 	 exception has no root cause.
		* @see #getRootCause
		* @see #rootException
		* @see #initCause
		*/
	public void setRootCause(Throwable e)
	{
		if (e != this)
		{
			rootException = e;
		}
	}

	public Throwable getCause()
	{
		return getRootCause();
	}

	public Throwable initCause(Throwable cause)
	{
		super.initCause(cause);
		setRootCause(cause);
		return this;
	}

	/**
	 * Generates the string representation of this exception.
	 * The string representation consists of this exception's class name, 
	 * its detailed message, and if it has a root cause, the string
	 * representation of the root cause exception, followed by
	 * the remaining name (if it is not null). 
	 * This string is used for debugging and not meant to be interpreted
	 * programmatically.
	 *
	 * @return The non-null string containing the string representation 
	 * of this exception.
	 */
	public String toString()
	{
		String answer = super.toString();

		if (rootException != null)
		{
			answer += " [Root exception is " + rootException + "]";
		}
		return answer;
	}
}
